serialize_xml.cpp 29 KB

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