serialize_xml.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Christian Schüller <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "serialize_xml.h"
  9. #include "../STR.h"
  10. #include "../serialize.h"
  11. #include "XMLSerializable.h"
  12. #include <iterator>
  13. #include <limits>
  14. #include <iomanip>
  15. namespace igl
  16. {
  17. namespace xml
  18. {
  19. template <typename T>
  20. IGL_INLINE void serialize_xml(
  21. const T& obj,
  22. const std::string& filename)
  23. {
  24. serialize_xml(obj,"object",filename,false,true);
  25. }
  26. template <typename T>
  27. IGL_INLINE void serialize_xml(
  28. const T& obj,
  29. const std::string& objectName,
  30. const std::string& filename,
  31. bool binary,
  32. bool overwrite)
  33. {
  34. tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
  35. if(overwrite == false)
  36. {
  37. // Check if file exists
  38. tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
  39. if(error != tinyxml2::XML_SUCCESS)
  40. {
  41. doc->Clear();
  42. }
  43. }
  44. tinyxml2::XMLElement* element = doc->FirstChildElement("serialization");
  45. if(element == NULL)
  46. {
  47. element = doc->NewElement("serialization");
  48. doc->InsertEndChild(element);
  49. }
  50. serialize_xml(obj,objectName,doc,element,binary);
  51. // Save
  52. tinyxml2::XMLError error = doc->SaveFile(filename.c_str());
  53. if(error != tinyxml2::XML_SUCCESS)
  54. {
  55. doc->PrintError();
  56. }
  57. delete doc;
  58. }
  59. template <typename T>
  60. IGL_INLINE void serialize_xml(
  61. const T& obj,
  62. const std::string& objectName,
  63. tinyxml2::XMLDocument* doc,
  64. tinyxml2::XMLElement* element,
  65. bool binary)
  66. {
  67. static_assert(
  68. serialization_xml::is_serializable<T>::value,
  69. "'igl::xml::serialize_xml': type is not serializable");
  70. std::string name(objectName);
  71. serialization_xml::encodeXMLElementName(name);
  72. tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  73. if(child != NULL)
  74. element->DeleteChild(child);
  75. child = doc->NewElement(name.c_str());
  76. element->InsertEndChild(child);
  77. if(binary)
  78. {
  79. std::vector<char> buffer;
  80. serialize(obj,name,buffer);
  81. std::string data =
  82. serialization_xml::base64_encode(
  83. reinterpret_cast<const unsigned char*>(
  84. buffer.data()),buffer.size());
  85. child->SetAttribute("binary",true);
  86. serialization_xml::serialize(data,doc,element,name);
  87. }
  88. else
  89. {
  90. serialization_xml::serialize(obj,doc,element,name);
  91. }
  92. }
  93. template <typename T>
  94. IGL_INLINE void deserialize_xml(T& obj,const std::string& filename)
  95. {
  96. deserialize_xml(obj,"object",filename);
  97. }
  98. template <typename T>
  99. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename)
  100. {
  101. tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
  102. tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
  103. if(error != tinyxml2::XML_SUCCESS)
  104. {
  105. std::cerr << "File not found!" << std::endl;
  106. doc->PrintError();
  107. delete doc;
  108. }
  109. else
  110. {
  111. tinyxml2::XMLElement* element = doc->FirstChildElement("serialization");
  112. if(element == NULL)
  113. {
  114. std::cerr << "Name of object not found! Initialized with default value." << std::endl;
  115. obj = T();
  116. }
  117. else
  118. {
  119. deserialize_xml(obj,objectName,doc,element);
  120. }
  121. delete doc;
  122. }
  123. }
  124. template <typename T>
  125. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
  126. {
  127. static_assert(serialization::is_serializable<T>::value,"'igl::xml::deserialize_xml': type is not deserializable");
  128. std::string name(objectName);
  129. serialization_xml::encodeXMLElementName(name);
  130. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  131. if(child != NULL)
  132. {
  133. const tinyxml2::XMLAttribute* attr = child->FindAttribute("binary");
  134. if(attr != NULL)
  135. {
  136. std::string code;
  137. serialization_xml::deserialize(code,doc,element,name);
  138. std::string decoded = serialization_xml::base64_decode(code);
  139. std::vector<char> buffer;
  140. std::copy(decoded.c_str(),decoded.c_str()+decoded.length(),std::back_inserter(buffer));
  141. deserialize(obj,name,buffer);
  142. }
  143. else
  144. {
  145. serialization_xml::deserialize(obj,doc,element,name);
  146. }
  147. }
  148. }
  149. namespace serialization_xml
  150. {
  151. // fundamental types
  152. template <typename T>
  153. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  154. {
  155. tinyxml2::XMLElement* child = getElement(doc,element,name.c_str());
  156. child->SetAttribute("val",obj);
  157. }
  158. template <typename T>
  159. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* /*doc*/,const tinyxml2::XMLElement* element,const std::string& name)
  160. {
  161. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  162. if(child != NULL)
  163. {
  164. getAttribute(child->Attribute("val"),obj);
  165. }
  166. else
  167. {
  168. obj = T();
  169. }
  170. }
  171. // std::string
  172. IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  173. {
  174. tinyxml2::XMLElement* child = getElement(doc,element,name.c_str());
  175. child->SetAttribute("val",obj.c_str());
  176. }
  177. IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* /*doc*/,const tinyxml2::XMLElement* element,const std::string& name)
  178. {
  179. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  180. if(child != NULL)
  181. {
  182. getAttribute(child->Attribute("val"),obj);
  183. }
  184. else
  185. {
  186. obj = std::string("");
  187. }
  188. }
  189. // Serializable
  190. template <typename T>
  191. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  192. {
  193. // Serialize object implementing Serializable interface
  194. const XMLSerializableBase& object = dynamic_cast<const XMLSerializableBase&>(obj);
  195. tinyxml2::XMLElement* child = getElement(doc,element,name.c_str());
  196. object.Serialize(doc,child);
  197. }
  198. template <typename T>
  199. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  200. {
  201. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  202. if(child != NULL)
  203. {
  204. obj.Deserialize(doc,child);
  205. }
  206. else
  207. {
  208. obj = T();
  209. }
  210. }
  211. // STL containers
  212. template <typename T1,typename T2>
  213. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  214. {
  215. tinyxml2::XMLElement* pair = getElement(doc,element,name.c_str());
  216. serialize(obj.first,doc,pair,"first");
  217. serialize(obj.second,doc,pair,"second");
  218. }
  219. template <typename T1,typename T2>
  220. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  221. {
  222. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  223. if(child != NULL)
  224. {
  225. deserialize(obj.first,doc,child,"first");
  226. deserialize(obj.second,doc,child,"second");
  227. }
  228. else
  229. {
  230. obj.first = T1();
  231. obj.second = T2();
  232. }
  233. }
  234. template <typename T1,typename T2>
  235. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  236. {
  237. tinyxml2::XMLElement* vector = getElement(doc,element,name.c_str());
  238. vector->SetAttribute("size",(unsigned int)obj.size());
  239. std::stringstream num;
  240. for(unsigned int i=0;i<obj.size();i++)
  241. {
  242. num.str("");
  243. num << "value" << i;
  244. serialize(obj[i],doc,vector,num.str());
  245. }
  246. }
  247. template <typename T1,typename T2>
  248. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  249. {
  250. obj.clear();
  251. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  252. if(child != NULL)
  253. {
  254. unsigned int size = child->UnsignedAttribute("size");
  255. obj.resize(size);
  256. std::stringstream num;
  257. for(unsigned int i=0;i<size;i++)
  258. {
  259. num.str("");
  260. num << "value" << i;
  261. deserialize(obj[i],doc,child,num.str());
  262. }
  263. }
  264. else
  265. {
  266. obj.clear();
  267. }
  268. }
  269. template <typename T>
  270. IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  271. {
  272. tinyxml2::XMLElement* set = getElement(doc,element,name.c_str());
  273. set->SetAttribute("size",(unsigned int)obj.size());
  274. std::stringstream num;
  275. typename std::set<T>::iterator iter = obj.begin();
  276. for(int i=0;iter!=obj.end();iter++,i++)
  277. {
  278. num.str("");
  279. num << "value" << i;
  280. serialize((T)*iter,doc,set,num.str());
  281. }
  282. }
  283. template <typename T>
  284. IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  285. {
  286. obj.clear();
  287. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  288. if(child != NULL)
  289. {
  290. unsigned int size = child->UnsignedAttribute("size");
  291. std::stringstream num;
  292. typename std::set<T>::iterator iter = obj.begin();
  293. for(int i=0;i<size;i++)
  294. {
  295. num.str("");
  296. num << "value" << i;
  297. T val;
  298. deserialize(val,doc,child,num.str());
  299. obj.insert(val);
  300. }
  301. }
  302. else
  303. {
  304. obj.clear();
  305. }
  306. }
  307. template <typename T1,typename T2>
  308. IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  309. {
  310. tinyxml2::XMLElement* map = getElement(doc,element,name.c_str());
  311. map->SetAttribute("size",(unsigned int)obj.size());
  312. std::stringstream num;
  313. typename std::map<T1,T2>::const_iterator iter = obj.cbegin();
  314. for(int i=0;iter!=obj.end();iter++,i++)
  315. {
  316. num.str("");
  317. num << "value" << i;
  318. serialize((std::pair<T1,T2>)*iter,doc,map,num.str());
  319. }
  320. }
  321. template <typename T1,typename T2>
  322. IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  323. {
  324. obj.clear();
  325. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  326. if(child != NULL)
  327. {
  328. unsigned int size = child->UnsignedAttribute("size");
  329. std::stringstream num;
  330. typename std::map<T1,T2>::iterator iter = obj.begin();
  331. for(int i=0;i<size;i++)
  332. {
  333. num.str("");
  334. num << "value" << i;
  335. std::pair<T1,T2> pair;
  336. deserialize(pair,doc,child,num.str());
  337. obj.insert(pair);
  338. }
  339. }
  340. else
  341. {
  342. obj.clear();
  343. }
  344. }
  345. template<typename T,int R,int C,int P,int MR,int MC>
  346. IGL_INLINE void serialize(
  347. const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  348. const std::string& name,
  349. const std::function<std::string(const T &) >& to_string,
  350. tinyxml2::XMLDocument* doc,
  351. tinyxml2::XMLElement* element)
  352. {
  353. tinyxml2::XMLElement* matrix = getElement(doc,element,name.c_str());
  354. const unsigned int rows = obj.rows();
  355. const unsigned int cols = obj.cols();
  356. matrix->SetAttribute("rows",rows);
  357. matrix->SetAttribute("cols",cols);
  358. std::stringstream ms;
  359. ms << "\n";
  360. for(unsigned int r=0;r<rows;r++)
  361. {
  362. for(unsigned int c=0;c<cols;c++)
  363. {
  364. ms << to_string(obj(r,c)) << ",";
  365. }
  366. ms << "\n";
  367. }
  368. std::string mString = ms.str();
  369. if(mString.size() > 1)
  370. mString[mString.size()-2] = '\0';
  371. matrix->SetAttribute("matrix",mString.c_str());
  372. }
  373. // Eigen types
  374. template<typename T,int R,int C,int P,int MR,int MC>
  375. IGL_INLINE void serialize(
  376. const Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  377. tinyxml2::XMLDocument* doc,
  378. tinyxml2::XMLElement* element,
  379. const std::string& name)
  380. {
  381. const std::function<std::string(const T &) > to_string =
  382. [](const T & v)->std::string
  383. {
  384. return
  385. STR(std::setprecision(std::numeric_limits<double>::digits10+2)<<v);
  386. };
  387. serialize(obj,name,to_string,doc,element);
  388. }
  389. template<typename T,int R,int C,int P,int MR,int MC>
  390. IGL_INLINE void deserialize(
  391. const tinyxml2::XMLDocument* /*doc*/,
  392. const tinyxml2::XMLElement* element,
  393. const std::string& name,
  394. const std::function<void(const std::string &,T &)> & from_string,
  395. Eigen::Matrix<T,R,C,P,MR,MC>& obj)
  396. {
  397. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  398. bool initialized = false;
  399. if(child != NULL)
  400. {
  401. const unsigned int rows = child->UnsignedAttribute("rows");
  402. const unsigned int cols = child->UnsignedAttribute("cols");
  403. if(rows > 0 && cols > 0)
  404. {
  405. obj.resize(rows,cols);
  406. const tinyxml2::XMLAttribute* attribute = child->FindAttribute("matrix");
  407. if(attribute != NULL)
  408. {
  409. std::string matTemp;
  410. getAttribute(attribute->Value(),matTemp);
  411. std::string line,srows,scols;
  412. std::stringstream mats;
  413. mats << matTemp;
  414. int r=0;
  415. std::string val;
  416. // for each line
  417. getline(mats,line);
  418. while(getline(mats,line))
  419. {
  420. // get current line
  421. std::stringstream liness(line);
  422. for(unsigned int c=0;c<cols-1;c++)
  423. {
  424. // split line
  425. getline(liness,val,',');
  426. // push pack the data if any
  427. if(!val.empty())
  428. {
  429. from_string(val,obj.coeffRef(r,c));
  430. }
  431. }
  432. getline(liness,val);
  433. from_string(val,obj.coeffRef(r,cols-1));
  434. r++;
  435. }
  436. initialized = true;
  437. }
  438. }
  439. }
  440. if(!initialized)
  441. {
  442. obj = Eigen::Matrix<T,R,C,P,MR,MC>();
  443. }
  444. }
  445. template<typename T,int R,int C,int P,int MR,int MC>
  446. IGL_INLINE void deserialize(
  447. Eigen::Matrix<T,R,C,P,MR,MC>& obj,
  448. const tinyxml2::XMLDocument* doc,
  449. const tinyxml2::XMLElement* element,
  450. const std::string& name)
  451. {
  452. const std::function<void(const std::string &,T &)> & from_string =
  453. [](const std::string & s,T & v)
  454. {
  455. getAttribute(s.c_str(),v);
  456. };
  457. deserialize(doc,element,name,from_string,obj);
  458. }
  459. template<typename T,int P,typename I>
  460. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  461. {
  462. tinyxml2::XMLElement* matrix = getElement(doc,element,name.c_str());
  463. const unsigned int rows = obj.rows();
  464. const unsigned int cols = obj.cols();
  465. matrix->SetAttribute("rows",rows);
  466. matrix->SetAttribute("cols",cols);
  467. char buffer[200];
  468. std::stringstream ms;
  469. ms << "\n";
  470. for(int k=0;k<obj.outerSize();++k)
  471. {
  472. for(typename Eigen::SparseMatrix<T,P,I>::InnerIterator it(obj,k);it;++it)
  473. {
  474. tinyxml2::XMLUtil::ToStr(it.value(),buffer,200);
  475. ms << it.row() << "," << it.col() << "," << buffer << "\n";
  476. }
  477. }
  478. std::string mString = ms.str();
  479. if(mString.size() > 0)
  480. mString[mString.size()-1] = '\0';
  481. matrix->SetAttribute("matrix",mString.c_str());
  482. }
  483. template<typename T,int P,typename I>
  484. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* /*doc*/,const tinyxml2::XMLElement* element,const std::string& name)
  485. {
  486. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  487. bool initialized = false;
  488. if(child != NULL)
  489. {
  490. const unsigned int rows = child->UnsignedAttribute("rows");
  491. const unsigned int cols = child->UnsignedAttribute("cols");
  492. if(rows > 0 && cols > 0)
  493. {
  494. obj.resize(rows,cols);
  495. obj.setZero();
  496. const tinyxml2::XMLAttribute* attribute = child->FindAttribute("matrix");
  497. if(attribute != NULL)
  498. {
  499. std::string matTemp;
  500. getAttribute(attribute->Value(),matTemp);
  501. std::string line,srows,scols;
  502. std::stringstream mats;
  503. mats << matTemp;
  504. std::vector<Eigen::Triplet<T,I> > triplets;
  505. std::string val;
  506. // for each line
  507. getline(mats,line);
  508. while(getline(mats,line))
  509. {
  510. // get current line
  511. std::stringstream liness(line);
  512. // row
  513. getline(liness,val,',');
  514. int row = atoi(val.c_str());
  515. // col
  516. getline(liness,val,',');
  517. int col = atoi(val.c_str());
  518. // val
  519. getline(liness,val);
  520. T value;
  521. getAttribute(val.c_str(),value);
  522. triplets.push_back(Eigen::Triplet<T,I>(row,col,value));
  523. }
  524. obj.setFromTriplets(triplets.begin(),triplets.end());
  525. initialized = true;
  526. }
  527. }
  528. }
  529. if(!initialized)
  530. {
  531. obj = Eigen::SparseMatrix<T,P,I>();
  532. }
  533. }
  534. // pointers
  535. template <typename T>
  536. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  537. {
  538. tinyxml2::XMLElement* pointer = getElement(doc,element,name.c_str());
  539. bool isNullPtr = (obj == NULL);
  540. pointer->SetAttribute("isNullPtr",isNullPtr);
  541. if(isNullPtr == false)
  542. serialization_xml::serialize(*obj,doc,element,name);
  543. }
  544. template <typename T>
  545. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  546. {
  547. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  548. if(child != NULL)
  549. {
  550. bool isNullPtr = child->BoolAttribute("isNullPtr");
  551. if(isNullPtr)
  552. {
  553. if(obj != NULL)
  554. {
  555. std::cout << "deserialization: possible memory leak for '" << typeid(obj).name() << "'" << std::endl;
  556. obj = NULL;
  557. }
  558. }
  559. else
  560. {
  561. if(obj != NULL)
  562. std::cout << "deserialization: possible memory leak for '" << typeid(obj).name() << "'" << std::endl;
  563. obj = new typename std::remove_pointer<T>::type();
  564. serialization_xml::deserialize(*obj,doc,element,name);
  565. }
  566. }
  567. }
  568. // helper functions
  569. IGL_INLINE tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  570. {
  571. tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  572. if(child == NULL)
  573. {
  574. child = doc->NewElement(name.c_str());
  575. element->InsertEndChild(child);
  576. }
  577. return child;
  578. }
  579. IGL_INLINE void getAttribute(const char* src,bool& dest)
  580. {
  581. tinyxml2::XMLUtil::ToBool(src,&dest);
  582. }
  583. IGL_INLINE void getAttribute(const char* src,char& dest)
  584. {
  585. dest = (char)atoi(src);
  586. }
  587. IGL_INLINE void getAttribute(const char* src,std::string& dest)
  588. {
  589. dest = src;
  590. }
  591. IGL_INLINE void getAttribute(const char* src,float& dest)
  592. {
  593. tinyxml2::XMLUtil::ToFloat(src,&dest);
  594. }
  595. IGL_INLINE void getAttribute(const char* src,double& dest)
  596. {
  597. tinyxml2::XMLUtil::ToDouble(src,&dest);
  598. }
  599. template<typename T>
  600. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest)
  601. {
  602. unsigned int val;
  603. tinyxml2::XMLUtil::ToUnsigned(src,&val);
  604. dest = (T)val;
  605. }
  606. template<typename T>
  607. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest)
  608. {
  609. int val;
  610. tinyxml2::XMLUtil::ToInt(src,&val);
  611. dest = (T)val;
  612. }
  613. // tinyXML2 related stuff
  614. static const int numForbiddenChars = 8;
  615. static const char forbiddenChars[] ={' ','/','~','#','&','>','<','='};
  616. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace)
  617. {
  618. size_t pos = 0;
  619. while((pos = str.find(search,pos)) != std::string::npos)
  620. {
  621. str.replace(pos,search.length(),replace);
  622. pos += replace.length();
  623. }
  624. }
  625. IGL_INLINE void encodeXMLElementName(std::string& name)
  626. {
  627. // must not start with a digit
  628. if(isdigit(*name.begin()))
  629. {
  630. name = ":::" + name;
  631. }
  632. std::stringstream stream;
  633. for(int i=0;i<numForbiddenChars;i++)
  634. {
  635. std::string search;
  636. search = forbiddenChars[i];
  637. std::stringstream replaces;
  638. replaces << ":" << (int)forbiddenChars[i];
  639. std::string replace = replaces.str();
  640. replaceSubString(name,search,replace);
  641. }
  642. }
  643. IGL_INLINE void decodeXMLElementName(std::string& name)
  644. {
  645. if(name.find("::",0) == 0)
  646. name.replace(0,3,"");
  647. for(auto chr : forbiddenChars)
  648. {
  649. std::stringstream searchs;
  650. searchs << ":" << (int)chr;
  651. std::string search = searchs.str();
  652. std::string replace;
  653. replace = chr;
  654. replaceSubString(name,search,replace);
  655. }
  656. }
  657. /* Copyright(C) 2004-2008 Ren� Nyffenegger
  658. This source code is provided 'as-is',without any express or implied
  659. warranty.In no event will the author be held liable for any damages
  660. arising from the use of this software.
  661. Permission is granted to anyone to use this software for any purpose,
  662. including commercial applications,and to alter it and redistribute it
  663. freely,subject to the following restrictions:
  664. 1. The origin of this source code must not be misrepresented; you must not
  665. claim that you wrote the original source code.If you use this source code
  666. in a product,an acknowledgment in the product documentation would be
  667. appreciated but is not required.
  668. 2. Altered source versions must be plainly marked as such,and must not be
  669. misrepresented as being the original source code.
  670. 3. This notice may not be removed or altered from any source distribution.
  671. Ren� Nyffenegger [email protected]
  672. */
  673. static const std::string base64_chars =
  674. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  675. "abcdefghijklmnopqrstuvwxyz"
  676. "0123456789+/";
  677. static inline bool is_base64(unsigned char c) {
  678. return (isalnum(c) || (c == '+') || (c == '/'));
  679. }
  680. std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len)
  681. {
  682. std::string ret;
  683. int i = 0;
  684. int j = 0;
  685. unsigned char char_array_3[3];
  686. unsigned char char_array_4[4];
  687. while(in_len--) {
  688. char_array_3[i++] = *(bytes_to_encode++);
  689. if(i == 3) {
  690. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  691. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  692. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  693. char_array_4[3] = char_array_3[2] & 0x3f;
  694. for(i = 0; (i <4) ; i++)
  695. ret += base64_chars[char_array_4[i]];
  696. i = 0;
  697. }
  698. }
  699. if(i)
  700. {
  701. for(j = i; j < 3; j++)
  702. char_array_3[j] = '\0';
  703. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  704. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  705. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  706. char_array_4[3] = char_array_3[2] & 0x3f;
  707. for(j = 0; (j < i + 1); j++)
  708. ret += base64_chars[char_array_4[j]];
  709. while((i++ < 3))
  710. ret += '=';
  711. }
  712. return ret;
  713. }
  714. std::string base64_decode(std::string const& encoded_string)
  715. {
  716. int in_len = encoded_string.size();
  717. int i = 0;
  718. int j = 0;
  719. int in_ = 0;
  720. unsigned char char_array_4[4],char_array_3[3];
  721. std::string ret;
  722. // construct fast lookup table
  723. // added by Christian Sch�ller ([email protected])
  724. int charLookup[200];
  725. for(int i=0;i<(int)(base64_chars.length());i++)
  726. charLookup[(int)base64_chars[i]] = i;
  727. while(in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  728. char_array_4[i++] = encoded_string[in_]; in_++;
  729. if(i ==4) {
  730. for(i = 0; i <4; i++)
  731. char_array_4[i] = charLookup[char_array_4[i]]; // new fast lookup
  732. //char_array_4[i] = base64_chars.find(char_array_4[i]); // original version
  733. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  734. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  735. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  736. for(i = 0; (i < 3); i++)
  737. ret += char_array_3[i];
  738. i = 0;
  739. }
  740. }
  741. if(i) {
  742. for(j = i; j <4; j++)
  743. char_array_4[j] = 0;
  744. for(j = 0; j <4; j++)
  745. char_array_4[j] = base64_chars.find(char_array_4[j]);
  746. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  747. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  748. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  749. for(j = 0; (j < i - 1);
  750. j++) ret += char_array_3[j];
  751. }
  752. return ret;
  753. }
  754. }
  755. }
  756. }
  757. #ifdef IGL_STATIC_LIBRARY
  758. // Explicit template instantiation
  759. template void igl::xml::serialize_xml<std::vector<float, std::allocator<float> > >(std::vector<float, std::allocator<float> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool);
  760. template void igl::xml::deserialize_xml<std::vector<float, std::allocator<float> > >(std::vector<float, std::allocator<float> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&);
  761. #endif