ezxml.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* ezxml.h
  2. *
  3. * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifndef _EZXML_H
  25. #define _EZXML_H
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. //#include <fcntl.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #define EZXML_BUFSIZE 1024 // size of internal memory buffers
  34. #define EZXML_NAMEM 0x80 // name is malloced
  35. #define EZXML_TXTM 0x40 // txt is malloced
  36. #define EZXML_DUP 0x20 // attribute name and value are strduped
  37. typedef struct ezxml *ezxml_t;
  38. struct ezxml {
  39. char *name; // tag name
  40. char **attr; // tag attributes { name, value, name, value, ... NULL }
  41. char *txt; // tag character content, empty string if none
  42. size_t off; // tag offset from start of parent tag character content
  43. ezxml_t next; // next tag with same name in this section at this depth
  44. ezxml_t sibling; // next tag with different name in same section and depth
  45. ezxml_t ordered; // next tag, same section and depth, in original order
  46. ezxml_t child; // head of sub tag list, NULL if none
  47. ezxml_t parent; // parent tag, NULL if current tag is root tag
  48. short flags; // additional information
  49. };
  50. // Given a string of xml data and its length, parses it and creates an ezxml
  51. // structure. For efficiency, modifies the data by adding null terminators
  52. // and decoding ampersand sequences. If you don't want this, copy the data and
  53. // pass in the copy. Returns NULL on failure.
  54. ezxml_t ezxml_parse_str(char *s, size_t len);
  55. // A wrapper for ezxml_parse_str() that accepts a file descriptor. First
  56. // attempts to mem map the file. Failing that, reads the file into memory.
  57. // Returns NULL on failure.
  58. ezxml_t ezxml_parse_fd(int fd);
  59. // a wrapper for ezxml_parse_fd() that accepts a file name
  60. ezxml_t ezxml_parse_file(const char *file);
  61. // Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
  62. // stream into memory and then parses it. For xml files, use ezxml_parse_file()
  63. // or ezxml_parse_fd()
  64. ezxml_t ezxml_parse_fp(FILE *fp);
  65. // returns the first child tag (one level deeper) with the given name or NULL
  66. // if not found
  67. ezxml_t ezxml_child(ezxml_t xml, const char *name);
  68. // returns the next tag of the same name in the same section and depth or NULL
  69. // if not found
  70. #define ezxml_next(xml) ((xml) ? xml->next : NULL)
  71. // Returns the Nth tag with the same name in the same section at the same depth
  72. // or NULL if not found. An index of 0 returns the tag given.
  73. ezxml_t ezxml_idx(ezxml_t xml, int idx);
  74. // returns the name of the given tag
  75. #define ezxml_name(xml) ((xml) ? xml->name : NULL)
  76. // returns the given tag's character content or empty string if none
  77. #define ezxml_txt(xml) ((xml) ? xml->txt : "")
  78. // returns the value of the requested tag attribute, or NULL if not found
  79. const char *ezxml_attr(ezxml_t xml, const char *attr);
  80. // Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
  81. // variable length list of tag names and indexes. The argument list must be
  82. // terminated by either an index of -1 or an empty string tag name. Example:
  83. // title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
  84. // This retrieves the title of the 3rd book on the 1st shelf of library.
  85. // Returns NULL if not found.
  86. ezxml_t ezxml_get(ezxml_t xml, ...);
  87. // Converts an ezxml structure back to xml. Returns a string of xml data that
  88. // must be freed.
  89. char *ezxml_toxml(ezxml_t xml);
  90. // returns a NULL terminated array of processing instructions for the given
  91. // target
  92. const char **ezxml_pi(ezxml_t xml, const char *target);
  93. // frees the memory allocated for an ezxml structure
  94. void ezxml_free(ezxml_t xml);
  95. // returns parser error message or empty string if none
  96. const char *ezxml_error(ezxml_t xml);
  97. // returns a new empty ezxml structure with the given root tag name
  98. ezxml_t ezxml_new(const char *name);
  99. // wrapper for ezxml_new() that strdup()s name
  100. #define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
  101. // Adds a child tag. off is the offset of the child tag relative to the start
  102. // of the parent tag's character content. Returns the child tag.
  103. ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off);
  104. // wrapper for ezxml_add_child() that strdup()s name
  105. #define ezxml_add_child_d(xml, name, off) \
  106. ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
  107. // sets the character content for the given tag and returns the tag
  108. ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
  109. // wrapper for ezxml_set_txt() that strdup()s txt
  110. #define ezxml_set_txt_d(xml, txt) \
  111. ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
  112. // Sets the given tag attribute or adds a new attribute if not found. A value
  113. // of NULL will remove the specified attribute. Returns the tag given.
  114. ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
  115. // Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
  116. #define ezxml_set_attr_d(xml, name, value) \
  117. ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
  118. // sets a flag for the given tag and returns the tag
  119. ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
  120. // removes a tag along with its subtags without freeing its memory
  121. ezxml_t ezxml_cut(ezxml_t xml);
  122. // inserts an existing tag into an ezxml structure
  123. ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off);
  124. // Moves an existing tag to become a subtag of dest at the given offset from
  125. // the start of dest's character content. Returns the moved tag.
  126. #define ezxml_move(xml, dest, off) ezxml_insert(ezxml_cut(xml), dest, off)
  127. // removes a tag along with all its subtags
  128. #define ezxml_remove(xml) ezxml_free(ezxml_cut(xml))
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif // _EZXML_H