mxml-get.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Node get functions for Mini-XML, a small XML file parsing library.
  3. *
  4. * https://www.msweet.org/mxml
  5. *
  6. * Copyright © 2014-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 "mxml-config.h"
  15. #include "mxml-private.h"
  16. /*
  17. * 'mxmlGetCDATA()' - Get the value for a CDATA node.
  18. *
  19. * @code NULL@ is returned if the node is not a CDATA element.
  20. *
  21. * @since Mini-XML 2.7@
  22. */
  23. const char * /* O - CDATA value or @code NULL@ */
  24. mxmlGetCDATA(mxml_node_t *node) /* I - Node to get */
  25. {
  26. /*
  27. * Range check input...
  28. */
  29. if (!node || node->type != MXML_ELEMENT ||
  30. strncmp(node->value.element.name, "![CDATA[", 8))
  31. return (NULL);
  32. /*
  33. * Return the text following the CDATA declaration...
  34. */
  35. return (node->value.element.name + 8);
  36. }
  37. /*
  38. * 'mxmlGetCustom()' - Get the value for a custom node.
  39. *
  40. * @code NULL@ is returned if the node (or its first child) is not a custom
  41. * value node.
  42. *
  43. * @since Mini-XML 2.7@
  44. */
  45. const void * /* O - Custom value or @code NULL@ */
  46. mxmlGetCustom(mxml_node_t *node) /* I - Node to get */
  47. {
  48. /*
  49. * Range check input...
  50. */
  51. if (!node)
  52. return (NULL);
  53. /*
  54. * Return the integer value...
  55. */
  56. if (node->type == MXML_CUSTOM)
  57. return (node->value.custom.data);
  58. else if (node->type == MXML_ELEMENT &&
  59. node->child &&
  60. node->child->type == MXML_CUSTOM)
  61. return (node->child->value.custom.data);
  62. else
  63. return (NULL);
  64. }
  65. /*
  66. * 'mxmlGetElement()' - Get the name for an element node.
  67. *
  68. * @code NULL@ is returned if the node is not an element node.
  69. *
  70. * @since Mini-XML 2.7@
  71. */
  72. const char * /* O - Element name or @code NULL@ */
  73. mxmlGetElement(mxml_node_t *node) /* I - Node to get */
  74. {
  75. /*
  76. * Range check input...
  77. */
  78. if (!node || node->type != MXML_ELEMENT)
  79. return (NULL);
  80. /*
  81. * Return the element name...
  82. */
  83. return (node->value.element.name);
  84. }
  85. /*
  86. * 'mxmlGetFirstChild()' - Get the first child of an element node.
  87. *
  88. * @code NULL@ is returned if the node is not an element node or if the node
  89. * has no children.
  90. *
  91. * @since Mini-XML 2.7@
  92. */
  93. mxml_node_t * /* O - First child or @code NULL@ */
  94. mxmlGetFirstChild(mxml_node_t *node) /* I - Node to get */
  95. {
  96. /*
  97. * Range check input...
  98. */
  99. if (!node || node->type != MXML_ELEMENT)
  100. return (NULL);
  101. /*
  102. * Return the first child node...
  103. */
  104. return (node->child);
  105. }
  106. /*
  107. * 'mxmlGetInteger()' - Get the integer value from the specified node or its
  108. * first child.
  109. *
  110. * 0 is returned if the node (or its first child) is not an integer value node.
  111. *
  112. * @since Mini-XML 2.7@
  113. */
  114. int /* O - Integer value or 0 */
  115. mxmlGetInteger(mxml_node_t *node) /* I - Node to get */
  116. {
  117. /*
  118. * Range check input...
  119. */
  120. if (!node)
  121. return (0);
  122. /*
  123. * Return the integer value...
  124. */
  125. if (node->type == MXML_INTEGER)
  126. return (node->value.integer);
  127. else if (node->type == MXML_ELEMENT &&
  128. node->child &&
  129. node->child->type == MXML_INTEGER)
  130. return (node->child->value.integer);
  131. else
  132. return (0);
  133. }
  134. /*
  135. * 'mxmlGetLastChild()' - Get the last child of an element node.
  136. *
  137. * @code NULL@ is returned if the node is not an element node or if the node
  138. * has no children.
  139. *
  140. * @since Mini-XML 2.7@
  141. */
  142. mxml_node_t * /* O - Last child or @code NULL@ */
  143. mxmlGetLastChild(mxml_node_t *node) /* I - Node to get */
  144. {
  145. /*
  146. * Range check input...
  147. */
  148. if (!node || node->type != MXML_ELEMENT)
  149. return (NULL);
  150. /*
  151. * Return the node type...
  152. */
  153. return (node->last_child);
  154. }
  155. /*
  156. * 'mxmlGetNextSibling()' - Get the next node for the current parent.
  157. *
  158. * @code NULL@ is returned if this is the last child for the current parent.
  159. *
  160. * @since Mini-XML 2.7@
  161. */
  162. mxml_node_t *
  163. mxmlGetNextSibling(mxml_node_t *node) /* I - Node to get */
  164. {
  165. /*
  166. * Range check input...
  167. */
  168. if (!node)
  169. return (NULL);
  170. /*
  171. * Return the node type...
  172. */
  173. return (node->next);
  174. }
  175. /*
  176. * 'mxmlGetOpaque()' - Get an opaque string value for a node or its first child.
  177. *
  178. * @code NULL@ is returned if the node (or its first child) is not an opaque
  179. * value node.
  180. *
  181. * @since Mini-XML 2.7@
  182. */
  183. const char * /* O - Opaque string or @code NULL@ */
  184. mxmlGetOpaque(mxml_node_t *node) /* I - Node to get */
  185. {
  186. /*
  187. * Range check input...
  188. */
  189. if (!node)
  190. return (NULL);
  191. /*
  192. * Return the integer value...
  193. */
  194. if (node->type == MXML_OPAQUE)
  195. return (node->value.opaque);
  196. else if (node->type == MXML_ELEMENT &&
  197. node->child &&
  198. node->child->type == MXML_OPAQUE)
  199. return (node->child->value.opaque);
  200. else
  201. return (NULL);
  202. }
  203. /*
  204. * 'mxmlGetParent()' - Get the parent node.
  205. *
  206. * @code NULL@ is returned for a root node.
  207. *
  208. * @since Mini-XML 2.7@
  209. */
  210. mxml_node_t * /* O - Parent node or @code NULL@ */
  211. mxmlGetParent(mxml_node_t *node) /* I - Node to get */
  212. {
  213. /*
  214. * Range check input...
  215. */
  216. if (!node)
  217. return (NULL);
  218. /*
  219. * Return the node type...
  220. */
  221. return (node->parent);
  222. }
  223. /*
  224. * 'mxmlGetPrevSibling()' - Get the previous node for the current parent.
  225. *
  226. * @code NULL@ is returned if this is the first child for the current parent.
  227. *
  228. * @since Mini-XML 2.7@
  229. */
  230. mxml_node_t * /* O - Previous node or @code NULL@ */
  231. mxmlGetPrevSibling(mxml_node_t *node) /* I - Node to get */
  232. {
  233. /*
  234. * Range check input...
  235. */
  236. if (!node)
  237. return (NULL);
  238. /*
  239. * Return the node type...
  240. */
  241. return (node->prev);
  242. }
  243. /*
  244. * 'mxmlGetReal()' - Get the real value for a node or its first child.
  245. *
  246. * 0.0 is returned if the node (or its first child) is not a real value node.
  247. *
  248. * @since Mini-XML 2.7@
  249. */
  250. double /* O - Real value or 0.0 */
  251. mxmlGetReal(mxml_node_t *node) /* I - Node to get */
  252. {
  253. /*
  254. * Range check input...
  255. */
  256. if (!node)
  257. return (0.0);
  258. /*
  259. * Return the integer value...
  260. */
  261. if (node->type == MXML_REAL)
  262. return (node->value.real);
  263. else if (node->type == MXML_ELEMENT &&
  264. node->child &&
  265. node->child->type == MXML_REAL)
  266. return (node->child->value.real);
  267. else
  268. return (0.0);
  269. }
  270. /*
  271. * 'mxmlGetText()' - Get the text value for a node or its first child.
  272. *
  273. * @code NULL@ is returned if the node (or its first child) is not a text node.
  274. * The "whitespace" argument can be @code NULL@.
  275. *
  276. * Note: Text nodes consist of whitespace-delimited words. You will only get
  277. * single words of text when reading an XML file with @code MXML_TEXT@ nodes.
  278. * If you want the entire string between elements in the XML file, you MUST read
  279. * the XML file with @code MXML_OPAQUE@ nodes and get the resulting strings
  280. * using the @link mxmlGetOpaque@ function instead.
  281. *
  282. * @since Mini-XML 2.7@
  283. */
  284. const char * /* O - Text string or @code NULL@ */
  285. mxmlGetText(mxml_node_t *node, /* I - Node to get */
  286. int *whitespace) /* O - 1 if string is preceded by whitespace, 0 otherwise */
  287. {
  288. /*
  289. * Range check input...
  290. */
  291. if (!node)
  292. {
  293. if (whitespace)
  294. *whitespace = 0;
  295. return (NULL);
  296. }
  297. /*
  298. * Return the integer value...
  299. */
  300. if (node->type == MXML_TEXT)
  301. {
  302. if (whitespace)
  303. *whitespace = node->value.text.whitespace;
  304. return (node->value.text.string);
  305. }
  306. else if (node->type == MXML_ELEMENT &&
  307. node->child &&
  308. node->child->type == MXML_TEXT)
  309. {
  310. if (whitespace)
  311. *whitespace = node->child->value.text.whitespace;
  312. return (node->child->value.text.string);
  313. }
  314. else
  315. {
  316. if (whitespace)
  317. *whitespace = 0;
  318. return (NULL);
  319. }
  320. }
  321. /*
  322. * 'mxmlGetType()' - Get the node type.
  323. *
  324. * @code MXML_IGNORE@ is returned if "node" is @code NULL@.
  325. *
  326. * @since Mini-XML 2.7@
  327. */
  328. mxml_type_t /* O - Type of node */
  329. mxmlGetType(mxml_node_t *node) /* I - Node to get */
  330. {
  331. /*
  332. * Range check input...
  333. */
  334. if (!node)
  335. return (MXML_IGNORE);
  336. /*
  337. * Return the node type...
  338. */
  339. return (node->type);
  340. }
  341. /*
  342. * 'mxmlGetUserData()' - Get the user data pointer for a node.
  343. *
  344. * @since Mini-XML 2.7@
  345. */
  346. void * /* O - User data pointer */
  347. mxmlGetUserData(mxml_node_t *node) /* I - Node to get */
  348. {
  349. /*
  350. * Range check input...
  351. */
  352. if (!node)
  353. return (NULL);
  354. /*
  355. * Return the user data pointer...
  356. */
  357. return (node->user_data);
  358. }