UniXML.h
См. документацию.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00026
00027
00028
00029
00030 #ifndef UniXML_H_
00031 #define UniXML_H_
00032
00033 #include <assert.h>
00034 #include <string>
00035 #include <cstddef>
00036
00037 #include <libxml/parser.h>
00038 #include <libxml/tree.h>
00039
00040 class UniXML_iterator:
00041 public std::iterator<std::bidirectional_iterator_tag, xmlNode, ptrdiff_t,xmlNode*, xmlNode&>
00042 {
00043 public:
00044 UniXML_iterator(xmlNode* node) :
00045 curNode(node)
00046 {}
00047 UniXML_iterator() {}
00048
00049 std::string getProp(const std::string name) const;
00050 std::string getPropUtf8(const std::string name) const;
00051 int getIntProp(const std::string name) const;
00053 int getPIntProp(const std::string name, int def) const;
00054 void setProp(const std::string name, const std::string text);
00055
00056 bool findName(const std::string node, const std::string searchname);
00057 bool find(const std::string searchnode);
00058
00060 bool goNext();
00061
00063 bool goThrowNext();
00064
00066 bool goPrev();
00067
00068 bool canPrev();
00069 bool canNext();
00070
00071
00072 UniXML_iterator operator ++(int);
00073 UniXML_iterator operator ++();
00074
00075
00076 UniXML_iterator operator --(int);
00077 UniXML_iterator operator --();
00078
00082 bool goParent();
00083
00087 bool goChildren();
00088
00089
00090 xmlNode* getCurrent() const
00091 {
00092 return curNode;
00093 }
00094
00095
00096 const std::string getName() const
00097 {
00098 if( curNode )
00099 return (char*) curNode->name;
00100 else
00101 return "";
00102 }
00103
00104 const std::string getContent() const;
00105
00106 operator xmlNode*()
00107 {
00108
00109 return curNode;
00110 }
00111
00112 inline void goBegin()
00113 {
00114 while(canPrev()){goPrev();}
00115 }
00116
00117 inline void goEnd()
00118 {
00119 while(canNext()){goNext();}
00120 }
00121
00122 protected:
00123 xmlNode* curNode;
00124 };
00125
00126 class UniXML
00127 {
00128 public:
00129
00130 typedef UniXML_iterator iterator;
00131
00132 inline xmlNode* getFirstNode()
00133 {
00134 return xmlDocGetRootElement(doc);
00135 }
00136
00138 inline iterator begin()
00139 {
00140 return iterator(getFirstNode());
00141 }
00142
00143 inline iterator end()
00144 {
00145 return iterator(NULL);
00146 }
00147
00148
00149 void open(const std::string filename);
00150
00151 void close();
00152 inline bool isOpen(){ return doc!=0; }
00153 UniXML(const std::string filename);
00154
00155 UniXML();
00156
00157 ~UniXML();
00158
00159 xmlNode* cur;
00160 xmlDoc* doc;
00161 std::string filename;
00162
00163
00164 void newDoc(const std::string& root_node, std::string xml_ver="1.0");
00165
00166
00167 static std::string getProp(const xmlNode* node, const std::string name);
00168 static std::string getPropUtf8(const xmlNode* node, const std::string name);
00169 static int getIntProp(const xmlNode* node, const std::string name);
00171 static int getPIntProp(const xmlNode* node, const std::string name, int def);
00172
00173
00174 static void setProp(xmlNode* node, const std::string name, const std::string text);
00175
00176
00177 static xmlNode* createChild(xmlNode* node, const std::string title, const std::string text);
00178
00179
00180 static xmlNode* createNext(xmlNode* node, const std::string title, const std::string text);
00181
00182
00183 static void removeNode(xmlNode* node);
00184
00185
00186 static xmlNode* copyNode(xmlNode* node, int recursive=1);
00187
00188
00189
00190 bool save(const std::string filename="", int level = 2);
00191
00192
00193 static xmlNode* nextNode(xmlNode* node);
00194
00195
00196
00197 xmlNode* findNode(xmlNode* node, const std::string searchnode, const std::string name = "") const;
00198 xmlNode* findNodeUtf8(xmlNode* node, const std::string searchnode, const std::string name = "") const;
00199
00200 xmlNode* extFindNode(xmlNode* node, int depth, int width, const std::string searchnode, const std::string name = "", bool top=true );
00201 xmlNode* extFindNodeUtf8(xmlNode* node, int depth, int width, const std::string searchnode, const std::string name = "", bool top=true );
00202
00203
00204 protected:
00205
00206 static std::string xml2local(const std::string text);
00207
00208
00209
00210 static const xmlChar* local2xml(std::string text);
00211 static std::string local2utf8(const std::string text);
00212
00213 static int recur;
00214
00215 };
00216
00217 #endif