2
0

PolyObject.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyObject.h"
  20. #include "tinyxml.h"
  21. #include <sstream>
  22. using namespace Polycode;
  23. void ObjectEntry::Clear() {
  24. for(int i=0; i < children.size(); i++) {
  25. children[i]->Clear();
  26. delete children[i];
  27. }
  28. children.clear();
  29. }
  30. String ObjectEntry::getTypedName() const {
  31. const String _name = name.size() > 0 ? name : String("nil");
  32. if (type == ObjectEntry::ARRAY_ENTRY)
  33. return String("polyarray:") + _name;
  34. if (type == ObjectEntry::STRING_ENTRY && stringVal.size() == 0)
  35. return String("polystring:") + _name;
  36. // TODO: In interest of consistency, make sure that STRING_ENTRYs stay STRING_ENTRYs (etc) if they're ambiguous (i.e. contain numbers)
  37. return _name;
  38. }
  39. void ObjectEntry::setTypedName(const String &str) {
  40. size_t firstColon = str.find(":");
  41. // Note: This will split up a:b:c as having type "a" and name "b:c". Is this appropriate?
  42. if (firstColon == -1) {
  43. name = str;
  44. } else { // There was a namespace
  45. name = str.substr(firstColon+1);
  46. String sty = str.substr(0,firstColon);
  47. if (sty == "polyfloat")
  48. type = ObjectEntry::FLOAT_ENTRY;
  49. else if (sty == "polyint")
  50. type = ObjectEntry::INT_ENTRY;
  51. else if (sty == "polybool")
  52. type = ObjectEntry::BOOL_ENTRY;
  53. else if (sty == "polyarray")
  54. type = ObjectEntry::ARRAY_ENTRY;
  55. else if (sty == "polystring")
  56. type = ObjectEntry::STRING_ENTRY;
  57. else if (sty == "polycontainer")
  58. type = ObjectEntry::CONTAINER_ENTRY;
  59. }
  60. if (name == "nil")
  61. name.contents.clear();
  62. }
  63. Object::Object() {
  64. }
  65. Object::~Object() {
  66. }
  67. void Object::saveToXML(const String& fileName) {
  68. TiXmlDocument doc;
  69. TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
  70. doc.LinkEndChild( decl );
  71. TiXmlElement * rootElement = createElementFromObjectEntry(&root);
  72. doc.LinkEndChild(rootElement);
  73. doc.SaveFile(fileName.c_str());
  74. }
  75. TiXmlElement *Object::createElementFromObjectEntry(ObjectEntry *entry) {
  76. TiXmlElement *newElement = new TiXmlElement(entry->getTypedName().c_str());
  77. switch(entry->type) {
  78. case ObjectEntry::BOOL_ENTRY: {
  79. newElement->LinkEndChild(new TiXmlText( entry->boolVal ? "true" : "false" ));
  80. } break;
  81. case ObjectEntry::FLOAT_ENTRY: case ObjectEntry::INT_ENTRY: {
  82. std::ostringstream o;
  83. if (entry->type == ObjectEntry::FLOAT_ENTRY)
  84. o << entry->NumberVal;
  85. else
  86. o << entry->intVal;
  87. newElement->LinkEndChild(new TiXmlText( o.str().c_str() ));
  88. } break;
  89. case ObjectEntry::STRING_ENTRY: {
  90. newElement->LinkEndChild(new TiXmlText( entry->stringVal.c_str() ));
  91. } break;
  92. default: { // Some sort of container.
  93. for(int i=0; i < entry->children.size(); i++) {
  94. ObjectEntry *childEntry = entry->children[i];
  95. bool needLinkChild = entry->type == ObjectEntry::ARRAY_ENTRY;
  96. // printf("Parsing %s (type: %d)\n", childEntry->name.c_str(), childEntry->type);
  97. if (!needLinkChild) {
  98. const String &childTypedName = childEntry->getTypedName();
  99. switch(childEntry->type) {
  100. case ObjectEntry::BOOL_ENTRY:
  101. if(childEntry->boolVal)
  102. newElement->SetAttribute(childTypedName.c_str(), "true");
  103. else
  104. newElement->SetAttribute(childTypedName.c_str(), "false");
  105. break;
  106. case ObjectEntry::FLOAT_ENTRY: {
  107. std::ostringstream o; // Avoid NumberToString, it truncates
  108. o << childEntry->NumberVal;
  109. newElement->SetAttribute(childTypedName.c_str(), o.str().c_str());
  110. } break;
  111. case ObjectEntry::INT_ENTRY:
  112. newElement->SetAttribute(childTypedName.c_str(), childEntry->intVal);
  113. break;
  114. case ObjectEntry::STRING_ENTRY:
  115. {
  116. TiXmlElement *childElement = new TiXmlElement(childTypedName.c_str());
  117. childElement->LinkEndChild( new TiXmlText(childEntry->stringVal.c_str()));
  118. newElement->LinkEndChild(childElement);
  119. } break;
  120. default:
  121. needLinkChild = true;
  122. break;
  123. }
  124. }
  125. if (needLinkChild) {
  126. TiXmlElement *childElement = createElementFromObjectEntry(childEntry);
  127. newElement->LinkEndChild(childElement);
  128. }
  129. }
  130. } break;
  131. }
  132. return newElement;
  133. }
  134. bool Object::loadFromXMLString(const String &xmlString) {
  135. TiXmlDocument doc;
  136. doc.Parse((const char*)xmlString.c_str(), 0, TIXML_ENCODING_UTF8);
  137. if(doc.Error()) {
  138. Logger::log("Error loading xml string: %s\n", doc.ErrorDesc());
  139. return false;
  140. }
  141. TiXmlElement *rootElement = doc.RootElement();
  142. createFromXMLElement(rootElement, &root);
  143. return true;
  144. }
  145. bool Object::loadFromXML(const String& fileName) {
  146. TiXmlDocument doc(fileName.c_str());
  147. doc.LoadFile();
  148. if(doc.Error()) {
  149. Logger::log("Error loading xml file: %s\n", doc.ErrorDesc());
  150. return false;
  151. }
  152. TiXmlElement *rootElement = doc.RootElement();
  153. createFromXMLElement(rootElement, &root);
  154. return true;
  155. }
  156. void Object::createFromXMLElement(TiXmlElement *element, ObjectEntry *entry) {
  157. entry->type = ObjectEntry::CONTAINER_ENTRY;
  158. int ival;
  159. double dval;
  160. // run through the attributes
  161. TiXmlAttribute* pAttrib=element->FirstAttribute();
  162. int i=0;
  163. while (pAttrib)
  164. {
  165. ObjectEntry *newEntry = new ObjectEntry();
  166. newEntry->type = ObjectEntry::STRING_ENTRY;
  167. newEntry->stringVal = pAttrib->Value();
  168. if (newEntry->stringVal.find(".") == -1 && pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS) {
  169. newEntry->intVal = ival;
  170. newEntry->NumberVal = (Number)ival;
  171. newEntry->type = ObjectEntry::INT_ENTRY;
  172. } else if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) {
  173. newEntry->NumberVal = dval;
  174. newEntry->intVal = dval;
  175. newEntry->type = ObjectEntry::FLOAT_ENTRY;
  176. }
  177. if(newEntry->stringVal == "true") {
  178. newEntry->boolVal = true;
  179. newEntry->intVal = 1;
  180. newEntry->NumberVal = 1;
  181. newEntry->type = ObjectEntry::BOOL_ENTRY;
  182. }
  183. if(newEntry->stringVal == "false") {
  184. newEntry->boolVal = false;
  185. newEntry->intVal = 0;
  186. newEntry->NumberVal = 0;
  187. newEntry->type = ObjectEntry::BOOL_ENTRY;
  188. }
  189. newEntry->setTypedName(pAttrib->Name()); // Set name last because we might override type
  190. entry->children.push_back(newEntry);
  191. i++;
  192. pAttrib=pAttrib->Next();
  193. }
  194. // check if has a value
  195. if(element->GetText()) {
  196. entry->stringVal = element->GetText();
  197. entry->type = ObjectEntry::STRING_ENTRY;
  198. const char *rawVal = entry->stringVal.c_str();
  199. char *endResult = NULL; const char *success = rawVal + entry->stringVal.size();
  200. entry->intVal = strtol(rawVal, &endResult, 10);
  201. if (endResult == success) { // If integer part exhausts string
  202. entry->type = ObjectEntry::INT_ENTRY;
  203. entry->NumberVal = entry->intVal;
  204. entry->boolVal = entry->intVal;
  205. } else {
  206. entry->NumberVal = strtof(rawVal, &endResult);
  207. entry->intVal = entry->NumberVal;
  208. entry->boolVal = entry->NumberVal;
  209. if (endResult == success) {
  210. entry->type = ObjectEntry::FLOAT_ENTRY;
  211. }
  212. }
  213. if(entry->stringVal == "true") {
  214. entry->boolVal = true;
  215. entry->type = ObjectEntry::BOOL_ENTRY;
  216. }
  217. if(entry->stringVal == "false") {
  218. entry->boolVal = false;
  219. entry->type = ObjectEntry::BOOL_ENTRY;
  220. }
  221. } else {
  222. // then through the children
  223. TiXmlNode* pChild;
  224. String lastName = "";
  225. int count = 0;
  226. for (pChild = element->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
  227. TiXmlElement *pChildElement = pChild->ToElement();
  228. if (!pChildElement) continue; // Skip comment nodes
  229. ObjectEntry *newEntry = new ObjectEntry();
  230. createFromXMLElement(pChildElement, newEntry);
  231. entry->children.push_back(newEntry);
  232. if(newEntry->name == lastName) { // Keys cannot repeat in a CONTAINER
  233. entry->type = ObjectEntry::ARRAY_ENTRY;
  234. }
  235. lastName = newEntry->name;
  236. count++;
  237. }
  238. entry->length = count;
  239. }
  240. entry->setTypedName(element->Value()); // Set name last because we might override type
  241. }