| 1 | /* | ||
| 2 | * tinyxml.h | ||
| 3 | * | ||
| 4 | * Created by xant on 2/17/06. | ||
| 5 | * | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef __TINYXML_H__ | ||
| 9 | #define __TINYXML_H__ | ||
| 10 | |||
| 11 | #define XmlErr int | ||
| 12 | #define XML_NOERR 0 | ||
| 13 | #define XML_GENERIC_ERR -1 | ||
| 14 | #define XML_BADARGS -2 | ||
| 15 | #define XML_UPDATE_ERR -2 | ||
| 16 | #define XML_OPEN_FILE_ERR -3 | ||
| 17 | #define XML_PARSER_GENERIC_ERR -4 | ||
| 18 | #define XML_MEMORY_ERR -5 | ||
| 19 | #define XML_LINKLIST_ERR -6 | ||
| 20 | #define XML_BAD_CHARS -7 | ||
| 21 | #define XML_MROOT_ERR -8 | ||
| 22 | |||
| 23 | #include "bsd_queue.h" | ||
| 24 | #include <stdio.h> | ||
| 25 | #include <sys/types.h> | ||
| 26 | #include <sys/stat.h> | ||
| 27 | #ifdef WIN32 | ||
| 28 | #include <winsock2.h> // for w32lock/unlock functions | ||
| 29 | #include <io.h> | ||
| 30 | /* strings */ | ||
| 31 | #if !defined snprintf | ||
| 32 | #define snprintf _snprintf | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #if !defined strncasecmp | ||
| 36 | #define strncasecmp _strnicmp | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #if !defined strcasecmp | ||
| 40 | #define strcasecmp _stricmp | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #if !defined strdup | ||
| 44 | #define strdup _strdup | ||
| 45 | #endif | ||
| 46 | |||
| 47 | /* files */ | ||
| 48 | #if !defined stat | ||
| 49 | #define stat _stat | ||
| 50 | #endif | ||
| 51 | |||
| 52 | /* time */ | ||
| 53 | #if !defined sleep | ||
| 54 | #define sleep(_duration) (Sleep(_duration * 1000)) | ||
| 55 | #endif | ||
| 56 | |||
| 57 | #endif // WIN32 | ||
| 58 | |||
| 59 | struct __XmlNode; | ||
| 60 | struct __Txml; | ||
| 61 | |||
| 62 | typedef struct __XmlNamespace { | ||
| 63 | char *name; | ||
| 64 | char *uri; | ||
| 65 | TAILQ_ENTRY(__XmlNamespace) list; | ||
| 66 | } XmlNamespace; | ||
| 67 | |||
| 68 | /** | ||
| 69 | @type XmlNodeAttribute | ||
| 70 | @brief One attribute associated to an element | ||
| 71 | */ | ||
| 72 | typedef struct __XmlNodeAttribute { | ||
| 73 | char *name; ///< the attribute name | ||
| 74 | char *value; ///< the attribute value | ||
| 75 | struct __XmlNode *node; | ||
| 76 | TAILQ_ENTRY(__XmlNodeAttribute) list; | ||
| 77 | } XmlNodeAttribute; | ||
| 78 | |||
| 79 | typedef struct __XmlNamespaceSet { | ||
| 80 | XmlNamespace *ns; | ||
| 81 | TAILQ_ENTRY(__XmlNamespaceSet) next; | ||
| 82 | } XmlNamespaceSet; | ||
| 83 | |||
| 84 | typedef struct __XmlNode { | ||
| 85 | char *path; | ||
| 86 | char *name; | ||
| 87 | struct __XmlNode *parent; | ||
| 88 | char *value; | ||
| 89 | TAILQ_HEAD(,__XmlNode) children; | ||
| 90 | TAILQ_HEAD(,__XmlNodeAttribute) attributes; | ||
| 91 | #define XML_NODETYPE_SIMPLE 0 | ||
| 92 | #define XML_NODETYPE_COMMENT 1 | ||
| 93 | #define XML_NODETYPE_CDATA 2 | ||
| 94 | char type; | ||
| 95 | XmlNamespace *ns; // namespace of this node (if any) | ||
| 96 | XmlNamespace *cns; // new default namespace defined by this node | ||
| 97 | XmlNamespace *hns; // hinerited namespace (if any) | ||
| 98 | // all namespaces valid in this scope ( implicit namespaces ) | ||
| 99 | TAILQ_HEAD(,__XmlNamespaceSet) knownNamespaces; | ||
| 100 | // storage for newly defined namespaces | ||
| 101 | // (needed keep track of allocated XmlNamspace structures for later release) | ||
| 102 | TAILQ_HEAD(,__XmlNamespace) namespaces; | ||
| 103 | TAILQ_ENTRY(__XmlNode) siblings; | ||
| 104 | struct __TXml *context; // set only if rootnode (otherwise it's always NULL) | ||
| 105 | } XmlNode; | ||
| 106 | |||
| 107 | TAILQ_HEAD(nodelistHead, __XmlNode); | ||
| 108 | |||
| 109 | typedef struct __TXml { | ||
| 110 | XmlNode *cNode; | ||
| 111 | TAILQ_HEAD(,__XmlNode) rootElements; | ||
| 112 | char *head; | ||
| 113 | char outputEncoding[64]; /* XXX probably oversized, 24 or 32 should be enough */ | ||
| 114 | char documentEncoding[64]; | ||
| 115 | int useNamespaces; | ||
| 116 | int allowMultipleRootNodes; | ||
| 117 | int ignoreWhiteSpaces; | ||
| 118 | int ignoreBlanks; | ||
| 119 | } TXml; | ||
| 120 | |||
| 121 | /*** | ||
| 122 | @brief access next sibling of a node (if any) | ||
| 123 | @arg pointer to a valid XmlNode structure | ||
| 124 | @return pointer to next sibling if existing, NULL otherwise | ||
| 125 | */ | ||
| 126 | XmlNode *XmlNextSibling(XmlNode *node); | ||
| 127 | |||
| 128 | /*** | ||
| 129 | @brief access previous sibling of a node (if any) | ||
| 130 | @arg pointer to a valid XmlNode structure | ||
| 131 | @return pointer to previous sibling if existing, NULL otherwise | ||
| 132 | */ | ||
| 133 | XmlNode *XmlPrevSibling(XmlNode *node); | ||
| 134 | |||
| 135 | void XmlSetOutputEncoding(TXml *xml, char *encoding); | ||
| 136 | /*** | ||
| 137 | @brief allocates memory for an XmlNode. In case of errors NULL is returned | ||
| 138 | @arg name of the new node | ||
| 139 | @arg value associated to the new node (can be NULL and specified later through XmlSetNodeValue function) | ||
| 140 | @arg parent of the new node if present, NULL if this will be a root node | ||
| 141 | @return the newly created node | ||
| 142 | */ | ||
| 143 | XmlNode *XmlCreateNode(char *name,char *val,XmlNode *parent); | ||
| 144 | /*** | ||
| 145 | @brief associate a value to XmlNode *node. XML_NOERR is returned if no error occurs | ||
| 146 | @arg the node we want to modify | ||
| 147 | @arg the value we want to set for node | ||
| 148 | @return XML_NOERR if success , error code otherwise | ||
| 149 | */ | ||
| 150 | XmlErr XmlSetNodeValue(XmlNode *node,char *val); | ||
| 151 | /*** | ||
| 152 | @brief get value for an XmlNode | ||
| 153 | @arg the XmlNode containing the value we want to access. | ||
| 154 | @return returns value associated to XmlNode *node | ||
| 155 | */ | ||
| 156 | char *XmlGetNodeValue(XmlNode *node); | ||
| 157 | /**** | ||
| 158 | @brief free resources for XmlNode *node and all its subnodes | ||
| 159 | @arg the XmlNode we want to destroy | ||
| 160 | */ | ||
| 161 | void XmlDestroyNode(XmlNode *node); | ||
| 162 | /*** | ||
| 163 | @brief Adds XmlNode *child to the children list of XmlNode *node | ||
| 164 | @arg the parent node | ||
| 165 | @arg the new child | ||
| 166 | @return return XML_NOERR on success, error code otherwise | ||
| 167 | */ | ||
| 168 | XmlErr XmlAddChildNode(XmlNode *parent,XmlNode *child); | ||
| 169 | /*** | ||
| 170 | @brief Makes XmlNode *node a root node in context represented by TXml *xml | ||
| 171 | @arg the xml context pointer | ||
| 172 | @arg the new root node | ||
| 173 | @return XML_NOERR on success, error code otherwise | ||
| 174 | */ | ||
| 175 | XmlErr XmlAddRootNode(TXml *xml,XmlNode *node); | ||
| 176 | /*** | ||
| 177 | @brief add an attribute to XmlNode *node | ||
| 178 | @arg the XmlNode that we want to set attributes to | ||
| 179 | @arg the name of the new attribute | ||
| 180 | @arg the value of the new attribute | ||
| 181 | @return XML_NOERR on success, error code otherwise | ||
| 182 | */ | ||
| 183 | XmlErr XmlAddAttribute(XmlNode *node,char *name,char *val); | ||
| 184 | /*** | ||
| 185 | @brief substitute an existing branch with a new one | ||
| 186 | @arg the xml context pointer | ||
| 187 | @arg the index of the branch we want to substitute | ||
| 188 | @arg the root of the new branch | ||
| 189 | @reurn XML_NOERR on success, error code otherwise | ||
| 190 | */ | ||
| 191 | XmlErr XmlSubstBranch(TXml *xml,unsigned long index, XmlNode *newBranch); | ||
| 192 | /*** | ||
| 193 | @brief Remove a specific node from the xml structure | ||
| 194 | XXX - UNIMPLEMENTED | ||
| 195 | */ | ||
| 196 | XmlErr XmlRemoveNode(TXml *xml,char *path); | ||
| 197 | /*** | ||
| 198 | XXX - UNIMPLEMENTED | ||
| 199 | */ | ||
| 200 | XmlErr XmlRemoveBranch(TXml *xml,unsigned long index); | ||
| 201 | /*** | ||
| 202 | @brief Returns the number of root nodes in the xml context | ||
| 203 | @arg the xml context pointer | ||
| 204 | @return the number of root nodes found in the xml context | ||
| 205 | */ | ||
| 206 | unsigned long XmlCountBranches(TXml *xml); | ||
| 207 | /*** | ||
| 208 | @brief Returns the number of children of the given XmlNode | ||
| 209 | @arg the node we want to query | ||
| 210 | @return the number of children of queried node | ||
| 211 | */ | ||
| 212 | unsigned long XmlCountChildren(XmlNode *node); | ||
| 213 | /*** | ||
| 214 | @brief Returns the number of attributes of the given XmlNode | ||
| 215 | @arg the node we want to query | ||
| 216 | @return the number of attributes that are set for queried node | ||
| 217 | */ | ||
| 218 | unsigned long XmlCountAttributes(XmlNode *node); | ||
| 219 | /*** | ||
| 220 | @brief Returns the XmlNode at specified path | ||
| 221 | @arg the xml context pointer | ||
| 222 | @arg the path that references requested node. | ||
| 223 | This must be of formatted as a slash '/' separated list | ||
| 224 | of node names ( ex. "tag_A/tag_B/tag_C" ) | ||
| 225 | @return the node at specified path | ||
| 226 | */ | ||
| 227 | XmlNode *XmlGetNode(TXml *xml, char *path); | ||
| 228 | /*** | ||
| 229 | @brief get the root node at a specific index | ||
| 230 | @arg the xml context pointer | ||
| 231 | @arg the index of the requested root node | ||
| 232 | @return the root node at requested index | ||
| 233 | |||
| 234 | */ | ||
| 235 | XmlNode *XmlGetBranch(TXml *xml,unsigned long index); | ||
| 236 | /*** | ||
| 237 | @brief get the child at a specific index inside a node | ||
| 238 | @arg the node | ||
| 239 | @arg the index of the child we are interested in | ||
| 240 | @return the selected child node | ||
| 241 | */ | ||
| 242 | XmlNode *XmlGetChildNode(XmlNode *node,unsigned long index); | ||
| 243 | /*** | ||
| 244 | @brief get the first child of an XmlNode whose name is 'name' | ||
| 245 | @arg the parent node | ||
| 246 | @arg the name of the desired child node | ||
| 247 | @return the requested child node | ||
| 248 | */ | ||
| 249 | XmlNode *XmlGetChildNodeByName(XmlNode *node,char *name); | ||
| 250 | |||
| 251 | /*** | ||
| 252 | @brief parse a string buffer containing an xml profile and fills internal structures appropriately | ||
| 253 | @arg the null terminated string buffer containing the xml profile | ||
| 254 | @return true if buffer is parsed successfully , false otherwise) | ||
| 255 | */ | ||
| 256 | XmlErr XmlParseBuffer(TXml *xml,char *buf); | ||
| 257 | |||
| 258 | /*** | ||
| 259 | @brief parse an xml file containing the profile and fills internal structures appropriately | ||
| 260 | @arg a null terminating string representing the path to the xml file | ||
| 261 | @return an XmlErr error status (XML_NOERR if buffer was parsed successfully) | ||
| 262 | */ | ||
| 263 | XmlErr XmlParseFile(TXml *xml,char *path); | ||
| 264 | |||
| 265 | char *XmlDumpBranch(TXml *xml,XmlNode *rNode,unsigned int depth); | ||
| 266 | /*** | ||
| 267 | @brief dump the entire xml configuration tree that reflects the status of internal structures | ||
| 268 | @arg pointer to a valid xml context | ||
| 269 | @arg if not NULL, here will be stored the bytelength of the returned buffer | ||
| 270 | @return a null terminated string containing the xml representation of the configuration tree. | ||
| 271 | The memory allocated for the dump-string must be freed by the user when no more needed | ||
| 272 | */ | ||
| 273 | char *XmlDump(TXml *xml, int *outlen); | ||
| 274 | |||
| 275 | /*** | ||
| 276 | @brief Create a new xml context | ||
| 277 | @return a point to a valid xml context | ||
| 278 | */ | ||
| 279 | |||
| 280 | TXml *XmlCreateContext(); | ||
| 281 | |||
| 282 | |||
| 283 | /*** | ||
| 284 | @brief Resets/cleans an existing context | ||
| 285 | @arg pointer to a valid xml context | ||
| 286 | */ | ||
| 287 | void XmlResetContext(TXml *xml); | ||
| 288 | |||
| 289 | /*** | ||
| 290 | @brief release all resources associated to an xml context | ||
| 291 | @arg pointer to a valid xml context | ||
| 292 | */ | ||
| 293 | void XmlDestroyContext(TXml *xml); | ||
| 294 | |||
| 295 | |||
| 296 | /*** | ||
| 297 | @brief get node attribute at specified index | ||
| 298 | @arg pointer to a valid XmlNode strucutre | ||
| 299 | @arg of the the attribute we want to access (starting by 1) | ||
| 300 | @return a pointer to a valid XmlNodeAttribute structure if found at | ||
| 301 | the specified offset, NULL otherwise | ||
| 302 | */ | ||
| 303 | XmlNodeAttribute *XmlGetAttribute(XmlNode *node,unsigned long index); | ||
| 304 | |||
| 305 | /*** | ||
| 306 | @brief get node attribute with specified name | ||
| 307 | @arg pointer to a valid XmlNode strucutre | ||
| 308 | @arg the name of the desired attribute | ||
| 309 | @return a pointer to a valid XmlNodeAttribute structure if found, NULL otherwise | ||
| 310 | */ | ||
| 311 | XmlNodeAttribute *XmlGetAttributeByName(XmlNode *node, char *name); | ||
| 312 | |||
| 313 | /*** | ||
| 314 | @brief remove attribute at specified index | ||
| 315 | @arg pointer to a valid XmlNode strucutre | ||
| 316 | @arg of the the attribute we want to access (starting by 1) | ||
| 317 | @return XML_NOERR on success, XML_GENERIC_ERR otherwise | ||
| 318 | */ | ||
| 319 | int XmlRemoveAttribute(XmlNode *node, unsigned long index); | ||
| 320 | |||
| 321 | /*** | ||
| 322 | @brief remove all attributes of a node | ||
| 323 | @arg pointer to a valid XmlNode structure | ||
| 324 | */ | ||
| 325 | void XmlClearAttributes(XmlNode *node); | ||
| 326 | |||
| 327 | |||
| 328 | /*** | ||
| 329 | @brief save the configuration stored in the xml file containing the current profile | ||
| 330 | the xml file name is obtained appending '.xml' to the category name . The xml file is stored | ||
| 331 | in the repository directory specified during object construction. | ||
| 332 | @arg pointer to a valid xml context | ||
| 333 | @arg the path where to save the file | ||
| 334 | @return an XmlErr error status (XML_NOERR if buffer was parsed successfully) | ||
| 335 | */ | ||
| 336 | XmlErr XmlSave(TXml *xml,char *path); | ||
| 337 | |||
| 338 | /*** | ||
| 339 | @brief allocate resources for a new namespace | ||
| 340 | @arg the shortname of the new namespace | ||
| 341 | @arg the complete uri of the new namspace | ||
| 342 | @return a valid XmlNamespace pointer on success, NULL otherwise | ||
| 343 | */ | ||
| 344 | XmlNamespace *XmlCreateNamespace(char *nsName, char *nsUri); | ||
| 345 | |||
| 346 | /*** | ||
| 347 | |||
| 348 | */ | ||
| 349 | void XmlDestroyNamespace(XmlNamespace *ns); | ||
| 350 | |||
| 351 | /*** | ||
| 352 | @brief search for a specific namespace defined within the current document | ||
| 353 | @arg pointer to a valid XmlNode structure | ||
| 354 | @arg the shortname of the new namespace | ||
| 355 | */ | ||
| 356 | XmlNamespace *XmlGetNamespaceByName(XmlNode *node, char *nsName); | ||
| 357 | |||
| 358 | /*** | ||
| 359 | @brief search for a specific namespace defined within the current document | ||
| 360 | @arg pointer to a valid XmlNode structure | ||
| 361 | @arg the complete uri of the new namspace | ||
| 362 | @return a valid XmlNamespace pointer if found, NULL otherwise | ||
| 363 | */ | ||
| 364 | XmlNamespace *XmlGetNamespaceByUri(XmlNode *node, char *nsUri); | ||
| 365 | |||
| 366 | /*** | ||
| 367 | @brief create a new namespace and link it to current document/context | ||
| 368 | @arg pointer to a valid XmlNode structure | ||
| 369 | @arg the shortname of the new namespace | ||
| 370 | @arg the complete uri of the new namspace | ||
| 371 | @return a valid XmlNamespace pointer if found, NULL otherwise | ||
| 372 | */ | ||
| 373 | XmlNamespace *XmlAddNamespace(XmlNode *node, char *nsName, char *nsUri); | ||
| 374 | |||
| 375 | /*** | ||
| 376 | @brief get the namespace of a node , if any | ||
| 377 | @arg pointer to a valid XmlNode strucutre | ||
| 378 | @return a pointer to the XmlNamespace of the node if defined or inherited, NULL otherwise | ||
| 379 | */ | ||
| 380 | XmlNamespace *XmlGetNodeNamespace(XmlNode *node); | ||
| 381 | |||
| 382 | /*** | ||
| 383 | @brief set the namespace of a node | ||
| 384 | @arg pointer to a valid XmlNode structure | ||
| 385 | @return XML_NOERR on success, any other xml error code otherwise | ||
| 386 | */ | ||
| 387 | XmlErr XmlSetNodeNamespace(XmlNode *node, XmlNamespace *ns); | ||
| 388 | |||
| 389 | /*** | ||
| 390 | @brief set the default namespace of a node | ||
| 391 | (which will be inherited by all descendant, unless overridden) | ||
| 392 | @arg pointer to a valid XmlNode structure | ||
| 393 | @arg pointer to a valid XmlNamespace structure | ||
| 394 | @return XML_NOERR on success, any other xml error code otherwise | ||
| 395 | */ | ||
| 396 | XmlErr XmlSetNodeCNamespace(XmlNode *node, XmlNamespace *ns); | ||
| 397 | |||
| 398 | /*** | ||
| 399 | @brief set the default namespace for the current node (xml->cNode) | ||
| 400 | @arg pointer to a valid XmlNode structure | ||
| 401 | @arg the namespace uri | ||
| 402 | @return XML_NOERR on success, any other xml error code otherwise | ||
| 403 | */ | ||
| 404 | XmlErr XmlSetCurrentNamespace(TXml *xml, char *nsuri); | ||
| 405 | |||
| 406 | 1 | static inline int XmlHasIconv() | |
| 407 | { | ||
| 408 | #ifdef USE_ICONV | ||
| 409 | 1 | return 1; | |
| 410 | #else | ||
| 411 | return 0; | ||
| 412 | #endif | ||
| 413 | } | ||
| 414 | |||
| 415 | #endif |