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