tc_xml.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef __TC_XML_H_
  2. #define __TC_XML_H_
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <iostream>
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include "util/tc_autoptr.h"
  10. using namespace std;
  11. namespace tars
  12. {
  13. /////////////////////////////////////////////////
  14. // 说明: Xml编解码的公共库
  15. // Author : linfengchen@tencent.com
  16. /////////////////////////////////////////////////
  17. /**
  18. * 编解码抛出的异常
  19. */
  20. struct TC_Xml_Exception : public TC_Exception
  21. {
  22. TC_Xml_Exception(const string &buffer) : TC_Exception(buffer){};
  23. TC_Xml_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
  24. ~TC_Xml_Exception() throw(){};
  25. };
  26. enum eXmlType
  27. {
  28. eXmlTypeString = 1,
  29. eXmlTypeArray,
  30. eXmlTypeObj,
  31. };
  32. /*
  33. * Xml类型的基类。没有任何意义
  34. */
  35. class XmlValue : public tars::TC_HandleBase
  36. {
  37. public:
  38. virtual eXmlType getType()=0;
  39. virtual ~XmlValue(){}
  40. protected:
  41. XmlValue* parent;
  42. };
  43. typedef tars::TC_AutoPtr<XmlValue> XmlValuePtr;
  44. /*
  45. * Xml类型 string类型
  46. */
  47. class XmlValueString : public XmlValue
  48. {
  49. public:
  50. XmlValueString(const string & s, bool _cdata = false):value(s), cdata(_cdata)
  51. {
  52. }
  53. XmlValueString(bool _cdata = false):cdata(_cdata)
  54. {
  55. }
  56. eXmlType getType()
  57. {
  58. return eXmlTypeString;
  59. }
  60. virtual ~XmlValueString(){}
  61. string value;
  62. bool cdata;
  63. };
  64. typedef tars::TC_AutoPtr<XmlValueString> XmlValueStringPtr;
  65. /*
  66. * Xml类型 object类型 例如
  67. */
  68. class XmlValueObj: public XmlValue
  69. {
  70. public:
  71. eXmlType getType()
  72. {
  73. return eXmlTypeObj;
  74. }
  75. virtual ~XmlValueObj(){}
  76. public:
  77. map<string, XmlValuePtr> value;
  78. };
  79. typedef tars::TC_AutoPtr<XmlValueObj> XmlValueObjPtr;
  80. /*
  81. * Xml类型 array类型 例如
  82. */
  83. class XmlValueArray: public XmlValue
  84. {
  85. public:
  86. eXmlType getType()
  87. {
  88. return eXmlTypeArray;
  89. }
  90. void push_back(XmlValuePtr & p)
  91. {
  92. value.push_back(p);
  93. }
  94. virtual ~XmlValueArray(){}
  95. public:
  96. vector<XmlValuePtr> value;
  97. };
  98. typedef tars::TC_AutoPtr<XmlValueArray> XmlValueArrayPtr;
  99. /*
  100. * 分析Xml字符串用到的 读字符的类
  101. */
  102. class BufferXmlReader
  103. {
  104. public:
  105. const char * _buf; ///< 缓冲区
  106. size_t _len; ///< 缓冲区长度
  107. size_t _pos; ///< 当前位置
  108. public:
  109. BufferXmlReader () :_buf(NULL),_len(0), _pos(0) {}
  110. void reset() { _pos = 0;}
  111. void setBuffer(const char * buf, size_t len)
  112. {
  113. _buf = buf;
  114. _len = len;
  115. _pos = 0;
  116. }
  117. void setBuffer(const std::vector<char> &buf)
  118. {
  119. _buf = &buf[0];
  120. _len = buf.size();
  121. _pos = 0;
  122. }
  123. bool hasEnd()
  124. {
  125. return (_pos >= _len || *(_buf + _pos) == 0);
  126. }
  127. bool expect(char ch)
  128. {
  129. if (get() == ch)
  130. {
  131. _pos++;
  132. return true;
  133. }
  134. return false;
  135. }
  136. void skip(size_t i = 1)
  137. {
  138. _pos += i;
  139. }
  140. char read()
  141. {
  142. check(++_pos);
  143. return *(_buf+_pos-1);
  144. }
  145. char get(size_t pos = 0)
  146. {
  147. check(pos + _pos);
  148. return *(_buf+_pos + pos);
  149. }
  150. size_t pos()
  151. {
  152. return _pos;
  153. }
  154. void check(size_t pos)
  155. {
  156. if (pos > _len)
  157. {
  158. char s[64];
  159. snprintf(s, sizeof(s), "buffer[%u] overflow when peekBuf, over %u.", (uint32_t)pos, (uint32_t)_len);
  160. throw TC_Xml_Exception(s);
  161. }
  162. }
  163. };
  164. /*
  165. * 分析Xml的类。都是static
  166. */
  167. class TC_Xml
  168. {
  169. public:
  170. //Xml类型到字符串的转换
  171. static string writeValue(const XmlValuePtr& p, bool bHead = true);
  172. static void writeValue(const XmlValuePtr& p, vector<char>& buf, bool bHead = true);
  173. //Xml字符串到Xml结构的转换
  174. static XmlValuePtr getValue(const string& str);
  175. static XmlValuePtr getValue(const vector<char>& buf);
  176. private:
  177. static XmlValuePtr getNode(BufferXmlReader& reader, const string& nodename = "");
  178. static XmlValueStringPtr getValue(BufferXmlReader& reader);
  179. static XmlValueStringPtr getCdata(BufferXmlReader& reader);
  180. static void insertArray(const string& name, XmlValuePtr& v, XmlValueObjPtr& p);
  181. static bool isEndNode(BufferXmlReader& reader, const string& nodename);
  182. static void ignoreDeclaration(BufferXmlReader& reader);
  183. static bool ignoreComment(BufferXmlReader& reader);
  184. static void writeArray(std::ostream& os, const string& name, const XmlValuePtr& p);
  185. static void writeString(std::ostream& os, const XmlValuePtr& p);
  186. static void writeEChar(std::ostream& os, const string& data);
  187. static void writeObj(std::ostream& os, const XmlValuePtr& p);
  188. //判断一个字符是否符合Xml定义的空白字符
  189. static bool isspace(char c);
  190. };
  191. }
  192. #endif