xmlapi.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * xmlapi.h
  3. *
  4. * private klish file: internal XML API
  5. */
  6. #ifndef clish_xmlapi_included_h
  7. #define clish_xmlapi_included_h
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. /*
  11. * XML document (opaque type)
  12. * The real type is defined by the selected external API
  13. */
  14. typedef struct clish_xmldoc_s clish_xmldoc_t;
  15. /*
  16. * XML node (opaque type)
  17. * The real type is defined by the selected external API
  18. */
  19. typedef struct clish_xmlnode_s clish_xmlnode_t;
  20. /*
  21. * XML attribute (opaque type)
  22. * The real type is defined by the selected external API
  23. */
  24. typedef struct clish_xmlattr_s clish_xmlattr_t;
  25. /*
  26. * read an XML document
  27. */
  28. clish_xmldoc_t *clish_xmldoc_read(const char *filename);
  29. /*
  30. * release a previously opened XML document
  31. */
  32. void clish_xmldoc_release(clish_xmldoc_t *doc);
  33. /*
  34. * check if a doc is valid (i.e. it loaded successfully)
  35. */
  36. int clish_xmldoc_is_valid(clish_xmldoc_t *doc);
  37. /*
  38. * XML implementation error capabilitiess
  39. * The real capabilities is or'ed using the following
  40. * constants
  41. */
  42. typedef enum {
  43. CLISH_XMLERR_NOCAPS = 0,
  44. CLISH_XMLERR_LINE = 0x10,
  45. CLISH_XMLERR_COL = 0x20,
  46. CLISH_XMLERR_DESC = 0x40
  47. } clish_xmlerrcaps_t;
  48. /*
  49. * does this specific implementation define any error?
  50. * -> get the capabilities
  51. */
  52. int clish_xmldoc_error_caps(clish_xmldoc_t *doc);
  53. typedef enum {
  54. CLISH_XMLNODE_DOC,
  55. CLISH_XMLNODE_ELM,
  56. CLISH_XMLNODE_TEXT,
  57. CLISH_XMLNODE_ATTR,
  58. CLISH_XMLNODE_COMMENT,
  59. CLISH_XMLNODE_PI,
  60. CLISH_XMLNODE_DECL,
  61. CLISH_XMLNODE_UNKNOWN,
  62. } clish_xmlnodetype_t;
  63. /*
  64. * get error description, when available
  65. */
  66. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc);
  67. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc);
  68. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc);
  69. /*
  70. * get the node type
  71. */
  72. int clish_xmlnode_get_type(clish_xmlnode_t *node);
  73. /*
  74. * get the document root
  75. */
  76. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc);
  77. /*
  78. * get the next child or NULL. If curchild is NULL,
  79. * then the function returns the first child.
  80. */
  81. clish_xmlnode_t *clish_xmlnode_next_child(
  82. clish_xmlnode_t *parent,
  83. clish_xmlnode_t *curchild);
  84. /*
  85. * get the parent node.
  86. * returns NULL if node is the document root node.
  87. */
  88. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node);
  89. /*
  90. * get the node name.
  91. * neither name not namelen shall be NULL. *namelen is the length of the
  92. * name buffer. If it's too small, we return -E2BIG and set *namelen to
  93. * the minimum length value.
  94. * returns < 0 on error. On error, name shall not be modified.
  95. */
  96. int clish_xmlnode_get_name(
  97. clish_xmlnode_t *node,
  98. char *name,
  99. unsigned int *namelen);
  100. /*
  101. * get the node name
  102. * dynamically allocate the buffer (it must be freed once you don't need it
  103. * anymore) that will contain all the content of the node.
  104. * return NULL on error.
  105. */
  106. static inline char* clish_xmlnode_get_all_name(clish_xmlnode_t *node)
  107. {
  108. char *name = NULL;
  109. unsigned int nlen = 2048;
  110. int result;
  111. do {
  112. name = (char*)realloc(name, nlen);
  113. result = clish_xmlnode_get_name(node, name, &nlen);
  114. } while (result == -E2BIG);
  115. if (result < 0) {
  116. free(name);
  117. return NULL;
  118. }
  119. return name;
  120. }
  121. /*
  122. * get the node content.
  123. * neither content not contentlen shall be NULL. *contentlen is the length
  124. * of the content buffer. If it's too small, we return -E2BIG and set
  125. * *contentlen to the minimum length value (including space for the \0
  126. * character) so that two subsequent calls to this functions are going
  127. * to succeed if the forst one failed because of a too small buffer.
  128. * returns < 0 on error. On error, content shall not be modified.
  129. */
  130. int clish_xmlnode_get_content(
  131. clish_xmlnode_t *node,
  132. char *content,
  133. unsigned int *contentlen);
  134. /*
  135. * get the node content
  136. * dynamically allocate the buffer (it must be freed once you don't need it
  137. * anymore) that will contain all the content of the node.
  138. * return NULL on error.
  139. */
  140. static inline char* clish_xmlnode_get_all_content(clish_xmlnode_t *node)
  141. {
  142. char *content = NULL;
  143. unsigned int clen = 2048;
  144. int result;
  145. do {
  146. content = (char*)realloc(content, clen);
  147. result = clish_xmlnode_get_content(node, content, &clen);
  148. } while (result == -E2BIG);
  149. if (result < 0) {
  150. free(content);
  151. return NULL;
  152. }
  153. return content;
  154. }
  155. /*
  156. * get an attribute by name. May return NULL if the
  157. * attribute is not found
  158. */
  159. clish_xmlattr_t *clish_xmlnode_fetch_attr(
  160. clish_xmlnode_t *node,
  161. const char *attrname);
  162. /*
  163. * get the attribute value.
  164. * neither value not valuelen shall be NULL. *valuelen is the length
  165. * of the value buffer. If it's too small, we return -E2BIG and set
  166. * *valuelen to the minimum length value.
  167. * returns < 0 on error. On error, value shall not be modified.
  168. */
  169. int clish_xmlattr_get_value(
  170. clish_xmlattr_t *attr,
  171. char *value,
  172. unsigned int *valuelen);
  173. /*
  174. * get the attribute value.
  175. * value shall not be NULL, and valuelen shall not be 0.
  176. * On error, value is set to \0 when possible.
  177. */
  178. void clish_xmlattr_get_value_noerr(
  179. clish_xmlattr_t *attr,
  180. char *value,
  181. unsigned int valuelen);
  182. #include <stdio.h> /* need for FILE */
  183. /*
  184. * print an XML node to the out file
  185. */
  186. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out);
  187. #endif /* clish_xmlapi_included_h */