mxml-set.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * "$Id: mxml-set.c 451 2014-01-04 21:50:06Z msweet $"
  3. *
  4. * Node set functions for Mini-XML, a small XML-like file parsing library.
  5. *
  6. * Copyright 2003-2014 by Michael R Sweet.
  7. *
  8. * These coded instructions, statements, and computer programs are the
  9. * property of Michael R Sweet and are protected by Federal copyright
  10. * law. Distribution and use rights are outlined in the file "COPYING"
  11. * which should have been included with this file. If this file is
  12. * missing or damaged, see the license at:
  13. *
  14. * http://www.msweet.org/projects.php/Mini-XML
  15. */
  16. /*
  17. * Include necessary headers...
  18. */
  19. #include "config.h"
  20. #include "mxml.h"
  21. /*
  22. * 'mxmlSetCDATA()' - Set the element name of a CDATA node.
  23. *
  24. * The node is not changed if it (or its first child) is not a CDATA element node.
  25. *
  26. * @since Mini-XML 2.3@
  27. */
  28. int /* O - 0 on success, -1 on failure */
  29. mxmlSetCDATA(mxml_node_t *node, /* I - Node to set */
  30. const char *data) /* I - New data string */
  31. {
  32. /*
  33. * Range check input...
  34. */
  35. if (node && node->type == MXML_ELEMENT &&
  36. strncmp(node->value.element.name, "![CDATA[", 8) &&
  37. node->child && node->child->type == MXML_ELEMENT &&
  38. !strncmp(node->child->value.element.name, "![CDATA[", 8))
  39. node = node->child;
  40. if (!node || node->type != MXML_ELEMENT || !data ||
  41. strncmp(node->value.element.name, "![CDATA[", 8))
  42. return (-1);
  43. /*
  44. * Free any old element value and set the new value...
  45. */
  46. if (node->value.element.name)
  47. free(node->value.element.name);
  48. node->value.element.name = _mxml_strdupf("![CDATA[%s]]", data);
  49. return (0);
  50. }
  51. /*
  52. * 'mxmlSetCustom()' - Set the data and destructor of a custom data node.
  53. *
  54. * The node is not changed if it (or its first child) is not a custom node.
  55. *
  56. * @since Mini-XML 2.1@
  57. */
  58. int /* O - 0 on success, -1 on failure */
  59. mxmlSetCustom(
  60. mxml_node_t *node, /* I - Node to set */
  61. void *data, /* I - New data pointer */
  62. mxml_custom_destroy_cb_t destroy) /* I - New destructor function */
  63. {
  64. /*
  65. * Range check input...
  66. */
  67. if (node && node->type == MXML_ELEMENT &&
  68. node->child && node->child->type == MXML_CUSTOM)
  69. node = node->child;
  70. if (!node || node->type != MXML_CUSTOM)
  71. return (-1);
  72. /*
  73. * Free any old element value and set the new value...
  74. */
  75. if (node->value.custom.data && node->value.custom.destroy)
  76. (*(node->value.custom.destroy))(node->value.custom.data);
  77. node->value.custom.data = data;
  78. node->value.custom.destroy = destroy;
  79. return (0);
  80. }
  81. /*
  82. * 'mxmlSetElement()' - Set the name of an element node.
  83. *
  84. * The node is not changed if it is not an element node.
  85. */
  86. int /* O - 0 on success, -1 on failure */
  87. mxmlSetElement(mxml_node_t *node, /* I - Node to set */
  88. const char *name) /* I - New name string */
  89. {
  90. /*
  91. * Range check input...
  92. */
  93. if (!node || node->type != MXML_ELEMENT || !name)
  94. return (-1);
  95. /*
  96. * Free any old element value and set the new value...
  97. */
  98. if (node->value.element.name)
  99. free(node->value.element.name);
  100. node->value.element.name = strdup(name);
  101. return (0);
  102. }
  103. /*
  104. * 'mxmlSetInteger()' - Set the value of an integer node.
  105. *
  106. * The node is not changed if it (or its first child) is not an integer node.
  107. */
  108. int /* O - 0 on success, -1 on failure */
  109. mxmlSetInteger(mxml_node_t *node, /* I - Node to set */
  110. int integer) /* I - Integer value */
  111. {
  112. /*
  113. * Range check input...
  114. */
  115. if (node && node->type == MXML_ELEMENT &&
  116. node->child && node->child->type == MXML_INTEGER)
  117. node = node->child;
  118. if (!node || node->type != MXML_INTEGER)
  119. return (-1);
  120. /*
  121. * Set the new value and return...
  122. */
  123. node->value.integer = integer;
  124. return (0);
  125. }
  126. /*
  127. * 'mxmlSetOpaque()' - Set the value of an opaque node.
  128. *
  129. * The node is not changed if it (or its first child) is not an opaque node.
  130. */
  131. int /* O - 0 on success, -1 on failure */
  132. mxmlSetOpaque(mxml_node_t *node, /* I - Node to set */
  133. const char *opaque) /* I - Opaque string */
  134. {
  135. /*
  136. * Range check input...
  137. */
  138. if (node && node->type == MXML_ELEMENT &&
  139. node->child && node->child->type == MXML_OPAQUE)
  140. node = node->child;
  141. if (!node || node->type != MXML_OPAQUE || !opaque)
  142. return (-1);
  143. /*
  144. * Free any old opaque value and set the new value...
  145. */
  146. if (node->value.opaque)
  147. free(node->value.opaque);
  148. node->value.opaque = strdup(opaque);
  149. return (0);
  150. }
  151. /*
  152. * 'mxmlSetReal()' - Set the value of a real number node.
  153. *
  154. * The node is not changed if it (or its first child) is not a real number node.
  155. */
  156. int /* O - 0 on success, -1 on failure */
  157. mxmlSetReal(mxml_node_t *node, /* I - Node to set */
  158. double real) /* I - Real number value */
  159. {
  160. /*
  161. * Range check input...
  162. */
  163. if (node && node->type == MXML_ELEMENT &&
  164. node->child && node->child->type == MXML_REAL)
  165. node = node->child;
  166. if (!node || node->type != MXML_REAL)
  167. return (-1);
  168. /*
  169. * Set the new value and return...
  170. */
  171. node->value.real = real;
  172. return (0);
  173. }
  174. /*
  175. * 'mxmlSetText()' - Set the value of a text node.
  176. *
  177. * The node is not changed if it (or its first child) is not a text node.
  178. */
  179. int /* O - 0 on success, -1 on failure */
  180. mxmlSetText(mxml_node_t *node, /* I - Node to set */
  181. int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
  182. const char *string) /* I - String */
  183. {
  184. /*
  185. * Range check input...
  186. */
  187. if (node && node->type == MXML_ELEMENT &&
  188. node->child && node->child->type == MXML_TEXT)
  189. node = node->child;
  190. if (!node || node->type != MXML_TEXT || !string)
  191. return (-1);
  192. /*
  193. * Free any old string value and set the new value...
  194. */
  195. if (node->value.text.string)
  196. free(node->value.text.string);
  197. node->value.text.whitespace = whitespace;
  198. node->value.text.string = strdup(string);
  199. return (0);
  200. }
  201. /*
  202. * 'mxmlSetTextf()' - Set the value of a text node to a formatted string.
  203. *
  204. * The node is not changed if it (or its first child) is not a text node.
  205. */
  206. int /* O - 0 on success, -1 on failure */
  207. mxmlSetTextf(mxml_node_t *node, /* I - Node to set */
  208. int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
  209. const char *format, /* I - Printf-style format string */
  210. ...) /* I - Additional arguments as needed */
  211. {
  212. va_list ap; /* Pointer to arguments */
  213. /*
  214. * Range check input...
  215. */
  216. if (node && node->type == MXML_ELEMENT &&
  217. node->child && node->child->type == MXML_TEXT)
  218. node = node->child;
  219. if (!node || node->type != MXML_TEXT || !format)
  220. return (-1);
  221. /*
  222. * Free any old string value and set the new value...
  223. */
  224. if (node->value.text.string)
  225. free(node->value.text.string);
  226. va_start(ap, format);
  227. node->value.text.whitespace = whitespace;
  228. node->value.text.string = _mxml_strdupf(format, ap);
  229. va_end(ap);
  230. return (0);
  231. }
  232. /*
  233. * 'mxmlSetUserData()' - Set the user data pointer for a node.
  234. *
  235. * @since Mini-XML 2.7@
  236. */
  237. int /* O - 0 on success, -1 on failure */
  238. mxmlSetUserData(mxml_node_t *node, /* I - Node to set */
  239. void *data) /* I - User data pointer */
  240. {
  241. /*
  242. * Range check input...
  243. */
  244. if (!node)
  245. return (-1);
  246. /*
  247. * Set the user data pointer and return...
  248. */
  249. node->user_data = data;
  250. return (0);
  251. }
  252. /*
  253. * End of "$Id: mxml-set.c 451 2014-01-04 21:50:06Z msweet $".
  254. */