mxml-set.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Node set functions for Mini-XML, a small XML file parsing library.
  3. *
  4. * https://www.msweet.org/mxml
  5. *
  6. * Copyright © 2003-2019 by Michael R Sweet.
  7. *
  8. * Licensed under Apache License v2.0. See the file "LICENSE" for more
  9. * information.
  10. */
  11. /*
  12. * Include necessary headers...
  13. */
  14. #include "config.h"
  15. #include "mxml-private.h"
  16. /*
  17. * 'mxmlSetCDATA()' - Set the element name of a CDATA node.
  18. *
  19. * The node is not changed if it (or its first child) is not a CDATA element node.
  20. *
  21. * @since Mini-XML 2.3@
  22. */
  23. int /* O - 0 on success, -1 on failure */
  24. mxmlSetCDATA(mxml_node_t *node, /* I - Node to set */
  25. const char *data) /* I - New data string */
  26. {
  27. char *s; /* String pointer */
  28. /*
  29. * Range check input...
  30. */
  31. if (node && node->type == MXML_ELEMENT &&
  32. strncmp(node->value.element.name, "![CDATA[", 8) &&
  33. node->child && node->child->type == MXML_ELEMENT &&
  34. !strncmp(node->child->value.element.name, "![CDATA[", 8))
  35. node = node->child;
  36. if (!node || node->type != MXML_ELEMENT || !data ||
  37. strncmp(node->value.element.name, "![CDATA[", 8))
  38. return (-1);
  39. if (data == (node->value.element.name + 8))
  40. return (0);
  41. /*
  42. * Allocate the new value, free any old element value, and set the new value...
  43. */
  44. s = _mxml_strdupf("![CDATA[%s", data);
  45. if (node->value.element.name)
  46. free(node->value.element.name);
  47. node->value.element.name = s;
  48. return (0);
  49. }
  50. /*
  51. * 'mxmlSetCustom()' - Set the data and destructor of a custom data node.
  52. *
  53. * The node is not changed if it (or its first child) is not a custom node.
  54. *
  55. * @since Mini-XML 2.1@
  56. */
  57. int /* O - 0 on success, -1 on failure */
  58. mxmlSetCustom(
  59. mxml_node_t *node, /* I - Node to set */
  60. void *data, /* I - New data pointer */
  61. mxml_custom_destroy_cb_t destroy) /* I - New destructor function */
  62. {
  63. /*
  64. * Range check input...
  65. */
  66. if (node && node->type == MXML_ELEMENT &&
  67. node->child && node->child->type == MXML_CUSTOM)
  68. node = node->child;
  69. if (!node || node->type != MXML_CUSTOM)
  70. return (-1);
  71. if (data == node->value.custom.data)
  72. {
  73. node->value.custom.destroy = destroy;
  74. return (0);
  75. }
  76. /*
  77. * Free any old element value and set the new value...
  78. */
  79. if (node->value.custom.data && node->value.custom.destroy)
  80. (*(node->value.custom.destroy))(node->value.custom.data);
  81. node->value.custom.data = data;
  82. node->value.custom.destroy = destroy;
  83. return (0);
  84. }
  85. /*
  86. * 'mxmlSetElement()' - Set the name of an element node.
  87. *
  88. * The node is not changed if it is not an element node.
  89. */
  90. int /* O - 0 on success, -1 on failure */
  91. mxmlSetElement(mxml_node_t *node, /* I - Node to set */
  92. const char *name) /* I - New name string */
  93. {
  94. /*
  95. * Range check input...
  96. */
  97. if (!node || node->type != MXML_ELEMENT || !name)
  98. return (-1);
  99. if (name == node->value.element.name)
  100. return (0);
  101. /*
  102. * Free any old element value and set the new value...
  103. */
  104. if (node->value.element.name)
  105. free(node->value.element.name);
  106. node->value.element.name = strdup(name);
  107. return (0);
  108. }
  109. /*
  110. * 'mxmlSetInteger()' - Set the value of an integer node.
  111. *
  112. * The node is not changed if it (or its first child) is not an integer node.
  113. */
  114. int /* O - 0 on success, -1 on failure */
  115. mxmlSetInteger(mxml_node_t *node, /* I - Node to set */
  116. int integer) /* I - Integer value */
  117. {
  118. /*
  119. * Range check input...
  120. */
  121. if (node && node->type == MXML_ELEMENT &&
  122. node->child && node->child->type == MXML_INTEGER)
  123. node = node->child;
  124. if (!node || node->type != MXML_INTEGER)
  125. return (-1);
  126. /*
  127. * Set the new value and return...
  128. */
  129. node->value.integer = integer;
  130. return (0);
  131. }
  132. /*
  133. * 'mxmlSetOpaque()' - Set the value of an opaque node.
  134. *
  135. * The node is not changed if it (or its first child) is not an opaque node.
  136. */
  137. int /* O - 0 on success, -1 on failure */
  138. mxmlSetOpaque(mxml_node_t *node, /* I - Node to set */
  139. const char *opaque) /* I - Opaque string */
  140. {
  141. /*
  142. * Range check input...
  143. */
  144. if (node && node->type == MXML_ELEMENT &&
  145. node->child && node->child->type == MXML_OPAQUE)
  146. node = node->child;
  147. if (!node || node->type != MXML_OPAQUE || !opaque)
  148. return (-1);
  149. if (node->value.opaque == opaque)
  150. return (0);
  151. /*
  152. * Free any old opaque value and set the new value...
  153. */
  154. if (node->value.opaque)
  155. free(node->value.opaque);
  156. node->value.opaque = strdup(opaque);
  157. return (0);
  158. }
  159. /*
  160. * 'mxmlSetOpaquef()' - Set the value of an opaque string node to a formatted string.
  161. *
  162. * The node is not changed if it (or its first child) is not an opaque node.
  163. *
  164. * @since Mini-XML 2.11@
  165. */
  166. int /* O - 0 on success, -1 on failure */
  167. mxmlSetOpaquef(mxml_node_t *node, /* I - Node to set */
  168. const char *format, /* I - Printf-style format string */
  169. ...) /* I - Additional arguments as needed */
  170. {
  171. va_list ap; /* Pointer to arguments */
  172. char *s; /* Temporary string */
  173. /*
  174. * Range check input...
  175. */
  176. if (node && node->type == MXML_ELEMENT &&
  177. node->child && node->child->type == MXML_OPAQUE)
  178. node = node->child;
  179. if (!node || node->type != MXML_OPAQUE || !format)
  180. return (-1);
  181. /*
  182. * Format the new string, free any old string value, and set the new value...
  183. */
  184. va_start(ap, format);
  185. s = _mxml_vstrdupf(format, ap);
  186. va_end(ap);
  187. if (node->value.opaque)
  188. free(node->value.opaque);
  189. node->value.opaque = s;
  190. return (0);
  191. }
  192. /*
  193. * 'mxmlSetReal()' - Set the value of a real number node.
  194. *
  195. * The node is not changed if it (or its first child) is not a real number node.
  196. */
  197. int /* O - 0 on success, -1 on failure */
  198. mxmlSetReal(mxml_node_t *node, /* I - Node to set */
  199. double real) /* I - Real number value */
  200. {
  201. /*
  202. * Range check input...
  203. */
  204. if (node && node->type == MXML_ELEMENT &&
  205. node->child && node->child->type == MXML_REAL)
  206. node = node->child;
  207. if (!node || node->type != MXML_REAL)
  208. return (-1);
  209. /*
  210. * Set the new value and return...
  211. */
  212. node->value.real = real;
  213. return (0);
  214. }
  215. /*
  216. * 'mxmlSetText()' - Set the value of a text node.
  217. *
  218. * The node is not changed if it (or its first child) is not a text node.
  219. */
  220. int /* O - 0 on success, -1 on failure */
  221. mxmlSetText(mxml_node_t *node, /* I - Node to set */
  222. int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
  223. const char *string) /* I - String */
  224. {
  225. /*
  226. * Range check input...
  227. */
  228. if (node && node->type == MXML_ELEMENT &&
  229. node->child && node->child->type == MXML_TEXT)
  230. node = node->child;
  231. if (!node || node->type != MXML_TEXT || !string)
  232. return (-1);
  233. if (string == node->value.text.string)
  234. {
  235. node->value.text.whitespace = whitespace;
  236. return (0);
  237. }
  238. /*
  239. * Free any old string value and set the new value...
  240. */
  241. if (node->value.text.string)
  242. free(node->value.text.string);
  243. node->value.text.whitespace = whitespace;
  244. node->value.text.string = strdup(string);
  245. return (0);
  246. }
  247. /*
  248. * 'mxmlSetTextf()' - Set the value of a text node to a formatted string.
  249. *
  250. * The node is not changed if it (or its first child) is not a text node.
  251. */
  252. int /* O - 0 on success, -1 on failure */
  253. mxmlSetTextf(mxml_node_t *node, /* I - Node to set */
  254. int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
  255. const char *format, /* I - Printf-style format string */
  256. ...) /* I - Additional arguments as needed */
  257. {
  258. va_list ap; /* Pointer to arguments */
  259. char *s; /* Temporary string */
  260. /*
  261. * Range check input...
  262. */
  263. if (node && node->type == MXML_ELEMENT &&
  264. node->child && node->child->type == MXML_TEXT)
  265. node = node->child;
  266. if (!node || node->type != MXML_TEXT || !format)
  267. return (-1);
  268. /*
  269. * Free any old string value and set the new value...
  270. */
  271. va_start(ap, format);
  272. s = _mxml_vstrdupf(format, ap);
  273. va_end(ap);
  274. if (node->value.text.string)
  275. free(node->value.text.string);
  276. node->value.text.whitespace = whitespace;
  277. node->value.text.string = s;
  278. return (0);
  279. }
  280. /*
  281. * 'mxmlSetUserData()' - Set the user data pointer for a node.
  282. *
  283. * @since Mini-XML 2.7@
  284. */
  285. int /* O - 0 on success, -1 on failure */
  286. mxmlSetUserData(mxml_node_t *node, /* I - Node to set */
  287. void *data) /* I - User data pointer */
  288. {
  289. /*
  290. * Range check input...
  291. */
  292. if (!node)
  293. return (-1);
  294. /*
  295. * Set the user data pointer and return...
  296. */
  297. node->user_data = data;
  298. return (0);
  299. }