UniSet
1.4.0
|
00001 /* This file is part of the UniSet project 00002 * Copyright (c) 2002-2010 Free Software Foundation, Inc. 00003 * Copyright (c) 2002, 2009, 2010 Vitaly Lipatov 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 // -------------------------------------------------------------------------- 00026 // -------------------------------------------------------------------------- 00027 00028 // Класс для работы с данными в XML, выборки и перекодирования 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 //unideb << "current\n"; 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 // Создать новый XML-документ 00164 void newDoc(const std::string& root_node, std::string xml_ver="1.0"); 00165 00166 // Получить свойство name указанного узла node 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 // Установить свойство name указанного узла node 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 // используя ->parent 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 // Преобразование текстовой строки из XML в строку нашего внутреннего представления 00206 static std::string xml2local(const std::string text); 00207 00208 // Преобразование текстовой строки из нашего внутреннего представления в строку для XML 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