Xml.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /******************************************************************************
  2. Use 'TextData' to handle automatic text data saving and loading.
  3. Use 'XmlData' to handle automatic xml data saving and loading.
  4. /******************************************************************************/
  5. // TEXT
  6. /******************************************************************************/
  7. struct TextParam
  8. {
  9. Str name , // parameter name
  10. value; // parameter value
  11. // set / get
  12. TextParam& setName(C Str &name=S) {T.name=name; return T;}
  13. TextParam& setValue(C Str &value=S) {T.value= value ; return T;} void getValue(Str &value)C {value=asText ();}
  14. TextParam& setValue(C Str8 &value ) {T.value= value ; return T;} void getValue(Str8 &value)C {value=asText ();}
  15. TextParam& setValue(CChar *value ) {T.value= value ; return T;}
  16. TextParam& setValue(CChar8 *value ) {T.value= value ; return T;}
  17. TextParam& setValue( Int value ) {T.value= value ; return T;} void getValue(Int &value)C {value=asInt ();}
  18. TextParam& setValue( UInt value ) {T.value= value ; return T;} void getValue(UInt &value)C {value=asUInt ();}
  19. TextParam& setValue( Long value ) {T.value= value ; return T;} void getValue(Long &value)C {value=asLong ();}
  20. TextParam& setValue( ULong value ) {T.value= value ; return T;} void getValue(ULong &value)C {value=asULong();}
  21. TextParam& setValue( Flt value ) {T.value=TextReal(value , -6); return T;} void getValue(Flt &value)C {value=asFlt ();}
  22. TextParam& setValue( Dbl value ) {T.value=TextReal(value , -9); return T;} void getValue(Dbl &value)C {value=asDbl ();}
  23. TextParam& setValue(C Vec2 &value ) {T.value= value.asText(-6); return T;} void getValue(Vec2 &value)C {value=asVec2 ();}
  24. TextParam& setValue(C VecD2 &value ) {T.value= value.asText(-9); return T;} void getValue(VecD2 &value)C {value=asVecD2();}
  25. TextParam& setValue(C VecI2 &value ) {T.value= value ; return T;} void getValue(VecI2 &value)C {value=asVecI2();}
  26. TextParam& setValue(C Vec &value ) {T.value= value.asText(-6); return T;} void getValue(Vec &value)C {value=asVec ();}
  27. TextParam& setValue(C VecD &value ) {T.value= value.asText(-9); return T;} void getValue(VecD &value)C {value=asVecD ();}
  28. TextParam& setValue(C VecI &value ) {T.value= value ; return T;} void getValue(VecI &value)C {value=asVecI ();}
  29. TextParam& setValue(C Vec4 &value ) {T.value= value.asText(-6); return T;} void getValue(Vec4 &value)C {value=asVec4 ();}
  30. TextParam& setValue(C VecD4 &value ) {T.value= value.asText(-9); return T;} void getValue(VecD4 &value)C {value=asVecD4();}
  31. TextParam& setValue(C VecI4 &value ) {T.value= value ; return T;} void getValue(VecI4 &value)C {value=asVecI4();}
  32. Bool getValue(UID &value)C {return value.fromText(asText());}
  33. TextParam& setValueHex( Flt value ); // set value to 'value' in hex text format
  34. TextParam& setValueHex( Dbl value ); // set value to 'value' in hex text format
  35. TextParam& setValueHex(C UID &value ) {T.value=value.asHex(); return T;} // set value to 'value' in hex text format
  36. TextParam& setValueFN (C UID &value ) {EncodeFileName(T.value, value ); return T;} // set value to 'value' encoded using 'EncodeFileName' function
  37. T1(TYPE) TextParam& setValueRaw(C TYPE &value ) {return setValueRaw(&value, SIZE(value)); } // set value to 'value' encoded using 'EncodeRaw' function
  38. TextParam& setValueRaw(CPtr data, Int size) {EncodeRaw (value, data, size); return T;} // set value to 'data' of 'size' encoded using 'EncodeRaw' function
  39. T1(TYPE) Bool getValueRaw(TYPE &value )C {return getValueRaw(&value, SIZE(value));} // get value and store it in 'value' decoded using 'DecodeRaw' function, returns false on fail (if 'TextParam.value' length does not match size of 'value')
  40. Bool getValueRaw(Ptr data, Int size)C {return DecodeRaw( value, data, size );} // get value and store it in 'data' of 'size' decoded using 'DecodeRaw' function, returns false on fail (if 'TextParam.value' length does not match 'size' of 'data' )
  41. TextParam& set(C Str &name, C Str &value=S) {return setName(name).setValue(value);}
  42. TextParam& set(C Str &name, C Str8 &value ) {return setName(name).setValue(value);}
  43. TextParam& set(C Str &name, CChar *value ) {return setName(name).setValue(value);}
  44. TextParam& set(C Str &name, CChar8 *value ) {return setName(name).setValue(value);}
  45. TextParam& set(C Str &name, Int value ) {return setName(name).setValue(value);}
  46. TextParam& set(C Str &name, UInt value ) {return setName(name).setValue(value);}
  47. TextParam& set(C Str &name, Long value ) {return setName(name).setValue(value);}
  48. TextParam& set(C Str &name, ULong value ) {return setName(name).setValue(value);}
  49. TextParam& set(C Str &name, Flt value ) {return setName(name).setValue(value);}
  50. TextParam& set(C Str &name, Dbl value ) {return setName(name).setValue(value);}
  51. TextParam& set(C Str &name, C Vec2 &value ) {return setName(name).setValue(value);}
  52. TextParam& set(C Str &name, C VecD2 &value ) {return setName(name).setValue(value);}
  53. TextParam& set(C Str &name, C VecI2 &value ) {return setName(name).setValue(value);}
  54. TextParam& set(C Str &name, C Vec &value ) {return setName(name).setValue(value);}
  55. TextParam& set(C Str &name, C VecD &value ) {return setName(name).setValue(value);}
  56. TextParam& set(C Str &name, C VecI &value ) {return setName(name).setValue(value);}
  57. TextParam& set(C Str &name, C Vec4 &value ) {return setName(name).setValue(value);}
  58. TextParam& set(C Str &name, C VecD4 &value ) {return setName(name).setValue(value);}
  59. TextParam& set(C Str &name, C VecI4 &value ) {return setName(name).setValue(value);}
  60. TextParam& setHex(C Str &name, Flt value ) {return setName(name).setValueHex(value );}
  61. TextParam& setHex(C Str &name, Dbl value ) {return setName(name).setValueHex(value );}
  62. TextParam& setHex(C Str &name, C UID &value ) {return setName(name).setValueHex(value );}
  63. TextParam& setFN (C Str &name, C UID &value ) {return setName(name).setValueFN (value );}
  64. T1(TYPE) TextParam& setRaw(C Str &name, C TYPE &value ) {return setName(name).setValueRaw(value );}
  65. TextParam& setRaw(C Str &name, CPtr data, Int size) {return setName(name).setValueRaw(data, size);}
  66. C Str& asText ()C {return value ;}
  67. Bool asBool ()C {return TextBool (value);}
  68. Bool asBool1()C {return TextBool1(value);}
  69. Int asInt ()C {return TextInt (value);}
  70. UInt asUInt ()C {return TextUInt (value);}
  71. Long asLong ()C {return TextLong (value);}
  72. ULong asULong()C {return TextULong(value);}
  73. Flt asFlt ()C {return TextFlt (value);}
  74. Dbl asDbl ()C {return TextDbl (value);}
  75. Vec2 asVec2 ()C {return TextVec2 (value);}
  76. VecD2 asVecD2()C {return TextVecD2(value);}
  77. VecI2 asVecI2()C {return TextVecI2(value);}
  78. Vec asVec ()C {return TextVec (value);}
  79. VecD asVecD ()C {return TextVecD (value);}
  80. VecI asVecI ()C {return TextVecI (value);}
  81. Vec4 asVec4 ()C {return TextVec4 (value);}
  82. VecD4 asVecD4()C {return TextVecD4(value);}
  83. VecI4 asVecI4()C {return TextVecI4(value);}
  84. Color asColor()C {return TextColor(value);}
  85. UID asUID ()C {return TextUID (value);}
  86. TextParam& clear() {name.clear(); value.clear(); return T;}
  87. TextParam& del () {name.del (); value.del (); return T;}
  88. TextParam() {}
  89. explicit TextParam(C Str &name, C Str &value=S) {set(name, value);}
  90. };
  91. /******************************************************************************/
  92. STRUCT(TextNode , TextParam)
  93. //{
  94. Memc<TextNode> nodes;
  95. // get / set
  96. TextNode* findNode(C Str &name, Int i=0) ; // find i-th node which name is equal to 'name', null on fail (if not found)
  97. C TextNode* findNode(C Str &name, Int i=0)C; // find i-th node which name is equal to 'name', null on fail (if not found)
  98. TextNode& getNode(C Str &name ) ; // get node which name is equal to 'name', New on fail (if not found)
  99. TextNode& setName(C Str &name=S) {super::setName(name); return T;}
  100. // manage
  101. TextNode& clear() {super::clear(); nodes.clear(); return T;} // clear
  102. TextNode& del () {super::del (); nodes.del (); return T;} // delete manually
  103. #if EE_PRIVATE
  104. Bool save (FileText &f, Bool just_values)C;
  105. Char load (FileText &f, Bool just_values, Char first_char);
  106. Char loadJSON(FileText &f, Bool just_values, Char first_char);
  107. Char loadYAML(FileText &f, Bool just_values, Char first_char, const Int node_spaces, Int &cur_spaces);
  108. #endif
  109. TextNode() {}
  110. TextNode(C XmlNode &xml); // create from 'XmlNode'
  111. };
  112. /******************************************************************************/
  113. struct TextData
  114. {
  115. Memc<TextNode> nodes;
  116. // get
  117. TextNode* findNode(C Str &name, Int i=0) ; // find i-th node which name is equal to 'name', null on fail (if not found)
  118. C TextNode* findNode(C Str &name, Int i=0)C; // find i-th node which name is equal to 'name', null on fail (if not found)
  119. TextNode& getNode(C Str &name ) ; // get node which name is equal to 'name', New on fail (if not found)
  120. // manage
  121. TextData& clear() {nodes.clear(); return T;} // clear
  122. TextData& del () {nodes.del (); return T;} // delete manually
  123. // io
  124. Bool save(C Str &name, ENCODING encoding=UTF_8, INDENT indent=INDENT_TABS, const_mem_addr Cipher *cipher=null)C; // save to file, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  125. Bool save(FileText &f )C; // save to file, false on fail, 'f' file must be already opened for writing
  126. Bool load(C Str &name, const_mem_addr Cipher *cipher=null); // load from file, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  127. Bool load(C UID &id , const_mem_addr Cipher *cipher=null); // load from file, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  128. Bool load(FileText &f ); // load from file, false on fail, 'f' file must be already opened for reading
  129. Bool loadJSON(C Str &name, const_mem_addr Cipher *cipher=null); // load from file in JSON format, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  130. Bool loadJSON(FileText &f ); // load from file in JSON format, false on fail, 'f' file must be already opened for reading
  131. Bool loadYAML(C Str &name, const_mem_addr Cipher *cipher=null); // load from file in YAML format, false on fail, 'cipher' must point to object in constant memory address (only pointer is stored through which the object can be later accessed)
  132. Bool loadYAML(FileText &f ); // load from file in YAML format, false on fail, 'f' file must be already opened for reading
  133. TextData() {}
  134. TextData(C XmlData &xml); // create from 'XmlData'
  135. };
  136. /******************************************************************************/
  137. // XML
  138. /******************************************************************************/
  139. typedef TextParam XmlParam; // Xml Node Parameter
  140. /******************************************************************************/
  141. struct XmlNode // Xml Node
  142. {
  143. Str name ; // node name
  144. Memc<XmlParam> params; // node parameters
  145. Memc<Str > data ; // node data
  146. Memc<XmlNode > nodes ; // node children
  147. XmlNode& setName(C Str &name) {T.name=name; return T;}
  148. // get
  149. XmlParam* findParam(C Str &name, Int i=0) ; // find i-th parameter which name is equal to 'name', null on fail (if not found)
  150. C XmlParam* findParam(C Str &name, Int i=0)C; // find i-th parameter which name is equal to 'name', null on fail (if not found)
  151. XmlParam& getParam(C Str &name ) ; // get parameter which name is equal to 'name', New on fail (if not found)
  152. XmlNode * findNode (C Str &name, Int i=0) ; // find i-th node which name is equal to 'name', null on fail (if not found)
  153. XmlNode & getNode (C Str &name ) ; // get node which name is equal to 'name', New on fail (if not found)
  154. // manage
  155. XmlNode& clear() {name.clear(); params.clear(); data.clear(); nodes.clear(); return T;} // clear
  156. XmlNode& del () {name.del (); params.del (); data.del (); nodes.del (); return T;} // delete manually
  157. #if EE_PRIVATE
  158. Bool save(FileText &f, Bool params_in_separate_lines)C;
  159. Bool load(FileText &f, Char first_char);
  160. #endif
  161. XmlNode() {}
  162. XmlNode(C TextNode &text); // create from 'TextNode'
  163. };
  164. /******************************************************************************/
  165. struct XmlData // Xml Data
  166. {
  167. Memc<XmlNode> nodes;
  168. // get
  169. XmlNode* findNode(C Str &name, Int i=0); // find i-th node which name is equal to 'name', null on fail (if not found)
  170. XmlNode& getNode(C Str &name ); // get node which name is equal to 'name', New on fail (if not found)
  171. // manage
  172. XmlData& clear() {nodes.clear(); return T;} // clear
  173. XmlData& del () {nodes.del (); return T;} // delete manually
  174. // io
  175. Bool save(C Str &name, Bool params_in_separate_lines=false, ENCODING encoding=UTF_8)C; // save to file, false on fail
  176. Bool save(FileText &f , Bool params_in_separate_lines=false )C; // save to file, false on fail, 'f' file must be already opened for writing
  177. Bool load(C Str &name); // load from file, false on fail
  178. Bool load(C UID &id ); // load from file, false on fail
  179. Bool load(FileText &f ); // load from file, false on fail, 'f' file must be already opened for reading
  180. XmlData() {}
  181. XmlData(C TextData &text); // create from 'TextData'
  182. };
  183. /******************************************************************************/
  184. TextNode* FindNode ( MemPtr<TextNode> nodes, C Str &name, Int i=0); // find i-th node which name is equal to 'name', null on fail (if not found)
  185. C TextNode* FindNodeC(C MemPtr<TextNode> &nodes, C Str &name, Int i=0); // find i-th node which name is equal to 'name', null on fail (if not found)
  186. TextNode& GetNode ( MemPtr<TextNode> nodes, C Str &name ); // get node which name is equal to 'name', New on fail (if not found)
  187. XmlNode* FindNode(MemPtr<XmlNode> nodes, C Str &name, Int i=0); // find i-th node which name is equal to 'name', null on fail (if not found)
  188. XmlNode& GetNode(MemPtr<XmlNode> nodes, C Str &name ); // get node which name is equal to 'name', New on fail (if not found)
  189. /******************************************************************************/