serialize_xml.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 <iterator>
  12. #include <limits>
  13. #include <iomanip>
  14. namespace igl
  15. {
  16. namespace xml
  17. {
  18. template <typename T>
  19. IGL_INLINE void serialize_xml(
  20. const T& obj,
  21. const std::string& filename)
  22. {
  23. serialize_xml(obj,"object",filename,false,true);
  24. }
  25. template <typename T>
  26. IGL_INLINE void serialize_xml(
  27. const T& obj,
  28. const std::string& objectName,
  29. const std::string& filename,
  30. bool binary,
  31. bool overwrite)
  32. {
  33. tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
  34. if(overwrite == false)
  35. {
  36. // Check if file exists
  37. tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
  38. if(error != tinyxml2::XML_NO_ERROR)
  39. {
  40. doc->Clear();
  41. }
  42. }
  43. tinyxml2::XMLElement* element = doc->FirstChildElement("serialization");
  44. if(element == NULL)
  45. {
  46. element = doc->NewElement("serialization");
  47. doc->InsertEndChild(element);
  48. }
  49. serialize_xml(obj,objectName,doc,element,binary);
  50. // Save
  51. tinyxml2::XMLError error = doc->SaveFile(filename.c_str());
  52. if(error != tinyxml2::XML_NO_ERROR)
  53. {
  54. doc->PrintError();
  55. }
  56. delete doc;
  57. }
  58. template <typename T>
  59. IGL_INLINE void serialize_xml(
  60. const T& obj,
  61. const std::string& objectName,
  62. tinyxml2::XMLDocument* doc,
  63. tinyxml2::XMLElement* element,
  64. bool binary)
  65. {
  66. static_assert(
  67. serialization_xml::is_serializable<T>::value,
  68. "'igl::xml::serialize_xml': type is not serializable");
  69. std::string name(objectName);
  70. serialization_xml::encodeXMLElementName(name);
  71. tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  72. if(child != NULL)
  73. element->DeleteChild(child);
  74. child = doc->NewElement(name.c_str());
  75. element->InsertEndChild(child);
  76. if(binary)
  77. {
  78. std::vector<char> buffer;
  79. serialize(obj,name,buffer);
  80. std::string data =
  81. serialization_xml::base64_encode(
  82. reinterpret_cast<const unsigned char*>(
  83. buffer.data()),buffer.size());
  84. child->SetAttribute("binary",true);
  85. serialization_xml::serialize(data,doc,element,name);
  86. }
  87. else
  88. {
  89. serialization_xml::serialize(obj,doc,element,name);
  90. }
  91. }
  92. template <typename T>
  93. IGL_INLINE void deserialize_xml(T& obj,const std::string& filename)
  94. {
  95. deserialize_xml(obj,"object",filename);
  96. }
  97. template <typename T>
  98. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename)
  99. {
  100. tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
  101. tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
  102. if(error != tinyxml2::XML_NO_ERROR)
  103. {
  104. std::cerr << "File not found!" << std::endl;
  105. doc->PrintError();
  106. doc = NULL;
  107. }
  108. else
  109. {
  110. tinyxml2::XMLElement* element = doc->FirstChildElement("serialization");
  111. if(element == NULL)
  112. {
  113. std::cerr << "Name of object not found! Initialized with default value." << std::endl;
  114. obj = T();
  115. }
  116. else
  117. {
  118. deserialize_xml(obj,objectName,doc,element);
  119. }
  120. delete doc;
  121. }
  122. }
  123. template <typename T>
  124. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
  125. {
  126. static_assert(serialization::is_serializable<T>::value,"'igl::xml::deserialize_xml': type is not deserializable");
  127. std::string name(objectName);
  128. serialization_xml::encodeXMLElementName(name);
  129. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  130. if(child != NULL)
  131. {
  132. bool isBinary = false;
  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. int r=0;
  506. std::string val;
  507. // for each line
  508. getline(mats,line);
  509. while(getline(mats,line))
  510. {
  511. // get current line
  512. std::stringstream liness(line);
  513. // row
  514. getline(liness,val,',');
  515. int row = atoi(val.c_str());
  516. // col
  517. getline(liness,val,',');
  518. int col = atoi(val.c_str());
  519. // val
  520. getline(liness,val);
  521. T value;
  522. getAttribute(val.c_str(),value);
  523. triplets.push_back(Eigen::Triplet<T,I>(row,col,value));
  524. r++;
  525. }
  526. obj.setFromTriplets(triplets.begin(),triplets.end());
  527. initialized = true;
  528. }
  529. }
  530. }
  531. if(!initialized)
  532. {
  533. obj = Eigen::SparseMatrix<T,P,I>();
  534. }
  535. }
  536. // pointers
  537. template <typename T>
  538. 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)
  539. {
  540. tinyxml2::XMLElement* pointer = getElement(doc,element,name.c_str());
  541. bool isNullPtr = (obj == NULL);
  542. pointer->SetAttribute("isNullPtr",isNullPtr);
  543. if(isNullPtr == false)
  544. serialization_xml::serialize(*obj,doc,element,name);
  545. }
  546. template <typename T>
  547. 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)
  548. {
  549. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  550. if(child != NULL)
  551. {
  552. bool isNullPtr = child->BoolAttribute("isNullPtr");
  553. if(isNullPtr)
  554. {
  555. if(obj != NULL)
  556. {
  557. std::cout << "deserialization: possible memory leak for '" << typeid(obj).name() << "'" << std::endl;
  558. obj = NULL;
  559. }
  560. }
  561. else
  562. {
  563. if(obj != NULL)
  564. std::cout << "deserialization: possible memory leak for '" << typeid(obj).name() << "'" << std::endl;
  565. obj = new typename std::remove_pointer<T>::type();
  566. serialization_xml::deserialize(*obj,doc,element,name);
  567. }
  568. }
  569. }
  570. // helper functions
  571. IGL_INLINE tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  572. {
  573. tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  574. if(child == NULL)
  575. {
  576. child = doc->NewElement(name.c_str());
  577. element->InsertEndChild(child);
  578. }
  579. return child;
  580. }
  581. IGL_INLINE void getAttribute(const char* src,bool& dest)
  582. {
  583. tinyxml2::XMLUtil::ToBool(src,&dest);
  584. }
  585. IGL_INLINE void getAttribute(const char* src,char& dest)
  586. {
  587. dest = (char)atoi(src);
  588. }
  589. IGL_INLINE void getAttribute(const char* src,std::string& dest)
  590. {
  591. dest = src;
  592. }
  593. IGL_INLINE void getAttribute(const char* src,float& dest)
  594. {
  595. tinyxml2::XMLUtil::ToFloat(src,&dest);
  596. }
  597. IGL_INLINE void getAttribute(const char* src,double& dest)
  598. {
  599. tinyxml2::XMLUtil::ToDouble(src,&dest);
  600. }
  601. template<typename T>
  602. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest)
  603. {
  604. unsigned int val;
  605. tinyxml2::XMLUtil::ToUnsigned(src,&val);
  606. dest = (T)val;
  607. }
  608. template<typename T>
  609. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest)
  610. {
  611. int val;
  612. tinyxml2::XMLUtil::ToInt(src,&val);
  613. dest = (T)val;
  614. }
  615. // tinyXML2 related stuff
  616. static const int numForbiddenChars = 8;
  617. static const char forbiddenChars[] ={' ','/','~','#','&','>','<','='};
  618. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace)
  619. {
  620. size_t pos = 0;
  621. while((pos = str.find(search,pos)) != std::string::npos)
  622. {
  623. str.replace(pos,search.length(),replace);
  624. pos += replace.length();
  625. }
  626. }
  627. IGL_INLINE void encodeXMLElementName(std::string& name)
  628. {
  629. // must not start with a digit
  630. if(isdigit(*name.begin()))
  631. {
  632. name = ":::" + name;
  633. }
  634. std::stringstream stream;
  635. for(int i=0;i<numForbiddenChars;i++)
  636. {
  637. std::string search;
  638. search = forbiddenChars[i];
  639. std::stringstream replaces;
  640. replaces << ":" << (int)forbiddenChars[i];
  641. std::string replace = replaces.str();
  642. replaceSubString(name,search,replace);
  643. }
  644. }
  645. IGL_INLINE void decodeXMLElementName(std::string& name)
  646. {
  647. if(name.find("::",0) == 0)
  648. name.replace(0,3,"");
  649. for(auto chr : forbiddenChars)
  650. {
  651. std::stringstream searchs;
  652. searchs << ":" << (int)chr;
  653. std::string search = searchs.str();
  654. std::string replace;
  655. replace = chr;
  656. replaceSubString(name,search,replace);
  657. }
  658. }
  659. /* Copyright(C) 2004-2008 Ren� Nyffenegger
  660. This source code is provided 'as-is',without any express or implied
  661. warranty.In no event will the author be held liable for any damages
  662. arising from the use of this software.
  663. Permission is granted to anyone to use this software for any purpose,
  664. including commercial applications,and to alter it and redistribute it
  665. freely,subject to the following restrictions:
  666. 1. The origin of this source code must not be misrepresented; you must not
  667. claim that you wrote the original source code.If you use this source code
  668. in a product,an acknowledgment in the product documentation would be
  669. appreciated but is not required.
  670. 2. Altered source versions must be plainly marked as such,and must not be
  671. misrepresented as being the original source code.
  672. 3. This notice may not be removed or altered from any source distribution.
  673. Ren� Nyffenegger rene.nyffenegger@adp-gmbh.ch
  674. */
  675. static const std::string base64_chars =
  676. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  677. "abcdefghijklmnopqrstuvwxyz"
  678. "0123456789+/";
  679. static inline bool is_base64(unsigned char c) {
  680. return (isalnum(c) || (c == '+') || (c == '/'));
  681. }
  682. std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len)
  683. {
  684. std::string ret;
  685. int i = 0;
  686. int j = 0;
  687. unsigned char char_array_3[3];
  688. unsigned char char_array_4[4];
  689. while(in_len--) {
  690. char_array_3[i++] = *(bytes_to_encode++);
  691. if(i == 3) {
  692. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  693. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  694. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  695. char_array_4[3] = char_array_3[2] & 0x3f;
  696. for(i = 0; (i <4) ; i++)
  697. ret += base64_chars[char_array_4[i]];
  698. i = 0;
  699. }
  700. }
  701. if(i)
  702. {
  703. for(j = i; j < 3; j++)
  704. char_array_3[j] = '\0';
  705. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  706. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  707. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  708. char_array_4[3] = char_array_3[2] & 0x3f;
  709. for(j = 0; (j < i + 1); j++)
  710. ret += base64_chars[char_array_4[j]];
  711. while((i++ < 3))
  712. ret += '=';
  713. }
  714. return ret;
  715. }
  716. std::string base64_decode(std::string const& encoded_string)
  717. {
  718. int in_len = encoded_string.size();
  719. int i = 0;
  720. int j = 0;
  721. int in_ = 0;
  722. unsigned char char_array_4[4],char_array_3[3];
  723. std::string ret;
  724. // construct fast lookup table
  725. // added by Christian Sch�ller (schuellc@inf.ethz.ch)
  726. int charLookup[200];
  727. for(int i=0;i<(int)(base64_chars.length());i++)
  728. charLookup[(int)base64_chars[i]] = i;
  729. while(in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  730. char_array_4[i++] = encoded_string[in_]; in_++;
  731. if(i ==4) {
  732. for(i = 0; i <4; i++)
  733. char_array_4[i] = charLookup[char_array_4[i]]; // new fast lookup
  734. //char_array_4[i] = base64_chars.find(char_array_4[i]); // original version
  735. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  736. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  737. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  738. for(i = 0; (i < 3); i++)
  739. ret += char_array_3[i];
  740. i = 0;
  741. }
  742. }
  743. if(i) {
  744. for(j = i; j <4; j++)
  745. char_array_4[j] = 0;
  746. for(j = 0; j <4; j++)
  747. char_array_4[j] = base64_chars.find(char_array_4[j]);
  748. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  749. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  750. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  751. for(j = 0; (j < i - 1);
  752. j++) ret += char_array_3[j];
  753. }
  754. return ret;
  755. }
  756. }
  757. }
  758. }
  759. #ifdef IGL_STATIC_LIBRARY
  760. // Explicit template specialization
  761. 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);
  762. 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&);
  763. #endif