00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef TOOLS_XML_H
00028 #define TOOLS_XML_H
00029
00030 #include "SugoiTools\config.h"
00031
00032 namespace SGE{
00033
00034
00035
00036
00037 #define XML_TAG_VAL_ID "id"
00038 #define XML_TAG_FCT "function"
00039 #define XML_TAG_FCT_MNGR "FunctionMngr"
00040
00041
00042
00043
00044 };
00045
00046 #include "SugoiTools\exceptions.h"
00047 #include "SugoiTools\logger_main.h"
00048 #include "SugoiTools\cl_base_obj.h"
00049
00050 #define EXIT_ON_ERROR true
00051
00052 #if _SG_TLS_XML_STL
00053 #define TIXML_USE_STL //Don't know why it doesn't work with this option...??
00054 #endif
00055
00056
00057 #ifdef TIXML_USE_STL
00058 #include <XML/tinyXML_STL.h>
00059 #else
00060 #include <XML/tinyXML.h>
00061 #endif
00062
00063 #if _SG_TLS_XML
00064 #if NDEBUG //release build
00065 #ifdef TIXML_USE_STL
00066 #pragma comment(lib ,"tinyXML_STL.lib")
00067 #else
00068 #pragma comment(lib ,"tinyXML.lib")
00069 #endif
00070 #else //debug build
00071 #ifdef TIXML_USE_STL
00072 #pragma comment(lib ,"tinyXMLd_STL.lib")
00073 #else
00074 #pragma comment(lib ,"tinyXMLd.lib")
00075 #endif
00076 #endif
00077 #endif//XML
00078
00079
00080 namespace SGE{
00081
00082 #if _SG_TLS_XML
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00104 _SG_TLS_EXPORT
00105 TiXmlElement *
00106 XMLOpenFile(std::string _filname, std::string _FirstElement);
00107
00115 _SG_TLS_EXPORT
00116 TiXmlDocument*
00117 XMLSaveFile(std::string _filname, std::string _FirstElement);
00118
00129 _SG_TLS_EXPORT
00130 TiXmlElement *
00131 XMLGetElement (TiXmlElement* _XML_Src, std::string _ElemName, std::string _ErrorMsg="", bool _ExitOnError = false);
00132
00140 _SG_TLS_EXPORT
00141 TiXmlElement *
00142 XMLAddNewElement (TiXmlElement* _XML_ROOT, std::string _ElemName);
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00179
00180
00181
00182 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, int& _Val);
00183 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, unsigned int& _Val);
00184 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, bool& _Val);
00185 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, double& _Val);
00186 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, float& _Val);
00187 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, std::string& _Val);
00188 _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, long& _Val);
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00254 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const int& _Val);
00255 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const unsigned int& _Val);
00256 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const bool& _Val);
00257 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const double& _Val);
00258 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const float& _Val);
00259 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const std::string& _Val);
00260 _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const long& _Val);
00261
00262
00263
00264 template <class ObjType>
00265 TiXmlElement*
00266 XMLLoadVector(TiXmlElement* _XML_ROOT, std::vector<ObjType*> &_Vector, std::string _TagName, std::string _SubTagName)
00267 {
00268 if (!_XML_ROOT)
00269 return NULL;
00270
00271 TiXmlElement*XML_MAIN = XMLGetElement(_XML_ROOT, _TagName.data());
00272 if (!XML_MAIN)
00273 return NULL;
00274
00275
00276 ObjType * NewObj;
00277
00278 while(XML_MAIN)
00279 {
00280 try
00281 {
00282
00283 NewObj = new ObjType();
00284
00285 if (NewObj->XMLLoad(XML_MAIN, _SubTagName))
00286 _Vector.push_back(NewObj);
00287 else
00288 delete NewObj;
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 XML_MAIN=XML_MAIN->NextSiblingElement();
00308 }
00309
00310 catch(CException &e)
00311 {
00312
00313 {
00314 SG_ERROR_LOG ("Exception catched while reading XMLtag '" << _TagName << "'.\n");
00315 }
00316
00317
00318
00319 std::cerr << "Exception catched : " << e.what() << endl;
00320
00321 if (NewObj)
00322 {
00323
00324 delete NewObj;
00325 }
00326
00327 XML_MAIN=XML_MAIN->NextSiblingElement();
00328 }
00329 }
00330
00331 _SG_DEBUG_XML(std::cout << "Mngr <" << _TagName << "> " << (unsigned int)_Vector.size() << " obj loaded!" << endl);
00332 return XML_MAIN;
00333 }
00334
00335
00336
00337 template <class ObjType>
00338 TiXmlElement*
00339 XMLSaveVector(TiXmlElement* _XML_ROOT, std::vector<ObjType*> &_Vector, std::string _TagName, std::string _SubTagName)
00340 {
00341 TiXmlElement* _XML_VECTOR = new TiXmlElement(_TagName.data() );
00342 TiXmlElement* XML_ELM = NULL;
00343
00344
00345
00346
00347
00348 for (unsigned int i = 0; i<_Vector.size() ;i++ )
00349 {
00350 try
00351 {
00352 XML_ELM = new TiXmlElement( _SubTagName.data() );
00353
00354 _Vector[i]->XMLSave(XML_ELM, _SubTagName);
00355 _XML_VECTOR->InsertEndChild(*XML_ELM);
00356 }
00357
00358 catch(CException &e)
00359 {
00360 SG_ERROR_LOG ("Error while saving XMLtag '<" <<
00361 _TagName <<
00362 "><" <<
00363 _SubTagName <<
00364 ">");
00365 std::cerr << "Exception catched : " << e.what() << endl;
00366
00367 if (XML_ELM)
00368 delete XML_ELM;
00369
00370 continue;
00371 }
00372 }
00373 _XML_ROOT->InsertEndChild(*_XML_VECTOR);
00374 _SG_DEBUG_XML(std::cout << "Mngr <" << _TagName << "> " << (unsigned int)_Vector.size() << " obj saved!" << endl);
00375 return _XML_VECTOR;
00376 }
00377 #endif
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00401 class _SG_TLS_EXPORT CL_XML_OBJ
00402 {
00403 public:
00409 CL_XML_OBJ(const std::string _TagName);
00410
00414 ~CL_XML_OBJ();
00415
00419 const std::string & GetMainTag()const;
00420
00425 void SetMainTag(const std::string & _newTag);
00426
00427 #if _SG_TLS_XML
00428 #if 0
00429
00436 _SG_TLS_INLINE static virtual
00437 TiXmlElement*
00438 XMLLoadFromMngr (TiXmlElement* _XML_Root, std::string _TagName);
00439
00440 _SG_TLS_INLINE static virtual
00441 TiXmlElement*
00442 XMLSaveFromMngr (TiXmlElement* _XML_Root, std::string _TagName);
00443 #endif
00444
00450 _SG_TLS_INLINE virtual
00451 TiXmlElement*
00452 XMLLoad (TiXmlElement* _XML_Root);
00453
00459 _SG_TLS_INLINE virtual
00460 TiXmlElement*
00461 XMLSave (TiXmlElement* _XML_Root);
00462
00471 _SG_TLS_INLINE virtual
00472 TiXmlElement*
00473 XMLLoadFromFile (std::string _fileName);
00474
00483 _SG_TLS_INLINE virtual
00484 TiXmlDocument*
00485 XMLSaveToFile (std::string _fileName);
00486 #endif
00487
00488 private:
00489 std::string XMLTagName;
00490 };
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00511 template <typename P=std::string> class CL_XML_BASE_OBJ
00512 : public CL_XML_OBJ , public CL_BASE_OBJ<P>
00513 {
00514 public:
00515
00521 _SG_TLS_INLINE
00522 CL_XML_BASE_OBJ(P _ID, const std::string _TagName = "obj")
00523 : CL_XML_OBJ(_TagName),
00524 CL_BASE_OBJ<P>(_ID)
00525 {}
00526
00527 _SG_TLS_INLINE
00528 CL_XML_BASE_OBJ(const CL_XML_BASE_OBJ<P> &_copy)
00529 : CL_XML_OBJ(_copy.GetMainTag()),
00530 CL_BASE_OBJ<P>(_copy.GetID())
00531 {
00532
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00547
00548
00549
00550
00551
00552 _SG_TLS_INLINE
00553 CL_XML_BASE_OBJ & operator = (const CL_XML_BASE_OBJ & _obj)
00554 {
00555 return *this;
00556 }
00557
00558 #if _SG_TLS_XML
00559
00566 _SG_TLS_INLINE virtual
00567 TiXmlElement*
00568 XMLLoad (TiXmlElement* _XML_Root)
00569 {
00570 TiXmlElement* XMLElem = CL_XML_OBJ::XMLLoad(_XML_Root);
00571 XMLReadVal(XMLElem, XML_TAG_VAL_ID, ID);
00572 return _XML_Root;
00573 };
00574
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 #endif
00592 };
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00613 class _SG_TLS_EXPORT CL_XML_BASE_OBJ_FILE
00614 : public CL_XML_OBJ , public CL_BASE_OBJ_FILE, virtual public CL_BASE_OBJ<std::string>
00615 {
00616 public:
00617
00623 CL_XML_BASE_OBJ_FILE(const std::string & _ID, const std::string _TagName = "obj")
00624 : CL_XML_OBJ(_TagName),
00625 CL_BASE_OBJ_FILE(_ID),
00626 CL_BASE_OBJ<std::string>(_ID)
00627 {};
00628
00632 ~CL_XML_BASE_OBJ_FILE()
00633 {};
00634
00635 #if _SG_TLS_XML
00636
00643 _SG_TLS_INLINE virtual
00644 TiXmlElement*
00645 XMLLoad (TiXmlElement* _XML_Root)
00646 {
00647 TiXmlElement* XMLElem = CL_XML_OBJ::XMLLoad(_XML_Root);
00648 XMLReadVal(XMLElem, XML_TAG_VAL_ID, ID);
00649 return _XML_Root;
00650 };
00651
00659 _SG_TLS_INLINE virtual
00660 TiXmlElement*
00661 XMLSave (TiXmlElement* _XML_Root)
00662 {
00663 TiXmlElement* XMLElem = new TiXmlElement(GetMainTag().c_str());
00664 XMLWriteVal(XMLElem, XML_TAG_VAL_ID, ID);
00665 return XMLElem;
00666 };
00667 #endif
00668 };
00669
00670 };
00671 #endif