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