mxml-get.c 8.0 KB

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