xmlapi.h 4.5 KB

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