Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /* |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 2 | * "$Id: mxml-get.c 451 2014-01-04 21:50:06Z msweet $" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * Node get functions for Mini-XML, a small XML-like file parsing library. |
| 5 | * |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 6 | * Copyright 2014 by Michael R Sweet. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 | * |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 14 | * http://www.msweet.org/projects.php/Mini-XML |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Include necessary headers... |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "mxml.h" |
| 23 | |
| 24 | |
| 25 | /* |
| 26 | * 'mxmlGetCDATA()' - Get the value for a CDATA node. |
| 27 | * |
| 28 | * @code NULL@ is returned if the node is not a CDATA element. |
| 29 | * |
| 30 | * @since Mini-XML 2.7@ |
| 31 | */ |
| 32 | |
| 33 | const char * /* O - CDATA value or NULL */ |
| 34 | mxmlGetCDATA(mxml_node_t *node) /* I - Node to get */ |
| 35 | { |
| 36 | /* |
| 37 | * Range check input... |
| 38 | */ |
| 39 | |
| 40 | if (!node || node->type != MXML_ELEMENT || |
| 41 | strncmp(node->value.element.name, "![CDATA[", 8)) |
| 42 | return (NULL); |
| 43 | |
| 44 | /* |
| 45 | * Return the text following the CDATA declaration... |
| 46 | */ |
| 47 | |
| 48 | return (node->value.element.name + 8); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * 'mxmlGetCustom()' - Get the value for a custom node. |
| 54 | * |
| 55 | * @code NULL@ is returned if the node (or its first child) is not a custom |
| 56 | * value node. |
| 57 | * |
| 58 | * @since Mini-XML 2.7@ |
| 59 | */ |
| 60 | |
| 61 | const void * /* O - Custom value or NULL */ |
| 62 | mxmlGetCustom(mxml_node_t *node) /* I - Node to get */ |
| 63 | { |
| 64 | /* |
| 65 | * Range check input... |
| 66 | */ |
| 67 | |
| 68 | if (!node) |
| 69 | return (NULL); |
| 70 | |
| 71 | /* |
| 72 | * Return the integer value... |
| 73 | */ |
| 74 | |
| 75 | if (node->type == MXML_CUSTOM) |
| 76 | return (node->value.custom.data); |
| 77 | else if (node->type == MXML_ELEMENT && |
| 78 | node->child && |
| 79 | node->child->type == MXML_CUSTOM) |
| 80 | return (node->child->value.custom.data); |
| 81 | else |
| 82 | return (NULL); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * 'mxmlGetElement()' - Get the name for an element node. |
| 88 | * |
| 89 | * @code NULL@ is returned if the node is not an element node. |
| 90 | * |
| 91 | * @since Mini-XML 2.7@ |
| 92 | */ |
| 93 | |
| 94 | const char * /* O - Element name or NULL */ |
| 95 | mxmlGetElement(mxml_node_t *node) /* I - Node to get */ |
| 96 | { |
| 97 | /* |
| 98 | * Range check input... |
| 99 | */ |
| 100 | |
| 101 | if (!node || node->type != MXML_ELEMENT) |
| 102 | return (NULL); |
| 103 | |
| 104 | /* |
| 105 | * Return the element name... |
| 106 | */ |
| 107 | |
| 108 | return (node->value.element.name); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* |
| 113 | * 'mxmlGetFirstChild()' - Get the first child of an element node. |
| 114 | * |
| 115 | * @code NULL@ is returned if the node is not an element node or if the node |
| 116 | * has no children. |
| 117 | * |
| 118 | * @since Mini-XML 2.7@ |
| 119 | */ |
| 120 | |
| 121 | mxml_node_t * /* O - First child or NULL */ |
| 122 | mxmlGetFirstChild(mxml_node_t *node) /* I - Node to get */ |
| 123 | { |
| 124 | /* |
| 125 | * Range check input... |
| 126 | */ |
| 127 | |
| 128 | if (!node || node->type != MXML_ELEMENT) |
| 129 | return (NULL); |
| 130 | |
| 131 | /* |
| 132 | * Return the first child node... |
| 133 | */ |
| 134 | |
| 135 | return (node->child); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * 'mxmlGetInteger()' - Get the integer value from the specified node or its |
| 141 | * first child. |
| 142 | * |
| 143 | * 0 is returned if the node (or its first child) is not an integer value node. |
| 144 | * |
| 145 | * @since Mini-XML 2.7@ |
| 146 | */ |
| 147 | |
| 148 | int /* O - Integer value or 0 */ |
| 149 | mxmlGetInteger(mxml_node_t *node) /* I - Node to get */ |
| 150 | { |
| 151 | /* |
| 152 | * Range check input... |
| 153 | */ |
| 154 | |
| 155 | if (!node) |
| 156 | return (0); |
| 157 | |
| 158 | /* |
| 159 | * Return the integer value... |
| 160 | */ |
| 161 | |
| 162 | if (node->type == MXML_INTEGER) |
| 163 | return (node->value.integer); |
| 164 | else if (node->type == MXML_ELEMENT && |
| 165 | node->child && |
| 166 | node->child->type == MXML_INTEGER) |
| 167 | return (node->child->value.integer); |
| 168 | else |
| 169 | return (0); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* |
| 174 | * 'mxmlGetLastChild()' - Get the last child of an element node. |
| 175 | * |
| 176 | * @code NULL@ is returned if the node is not an element node or if the node |
| 177 | * has no children. |
| 178 | * |
| 179 | * @since Mini-XML 2.7@ |
| 180 | */ |
| 181 | |
| 182 | mxml_node_t * /* O - Last child or NULL */ |
| 183 | mxmlGetLastChild(mxml_node_t *node) /* I - Node to get */ |
| 184 | { |
| 185 | /* |
| 186 | * Range check input... |
| 187 | */ |
| 188 | |
| 189 | if (!node || node->type != MXML_ELEMENT) |
| 190 | return (NULL); |
| 191 | |
| 192 | /* |
| 193 | * Return the node type... |
| 194 | */ |
| 195 | |
| 196 | return (node->last_child); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | * 'mxmlGetNextSibling()' - Get the next node for the current parent. |
| 202 | * |
| 203 | * @code NULL@ is returned if this is the last child for the current parent. |
| 204 | * |
| 205 | * @since Mini-XML 2.7@ |
| 206 | */ |
| 207 | |
| 208 | mxml_node_t * |
| 209 | mxmlGetNextSibling(mxml_node_t *node) /* I - Node to get */ |
| 210 | { |
| 211 | /* |
| 212 | * Range check input... |
| 213 | */ |
| 214 | |
| 215 | if (!node) |
| 216 | return (NULL); |
| 217 | |
| 218 | /* |
| 219 | * Return the node type... |
| 220 | */ |
| 221 | |
| 222 | return (node->next); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* |
| 227 | * 'mxmlGetOpaque()' - Get an opaque string value for a node or its first child. |
| 228 | * |
| 229 | * @code NULL@ is returned if the node (or its first child) is not an opaque |
| 230 | * value node. |
| 231 | * |
| 232 | * @since Mini-XML 2.7@ |
| 233 | */ |
| 234 | |
| 235 | const char * /* O - Opaque string or NULL */ |
| 236 | mxmlGetOpaque(mxml_node_t *node) /* I - Node to get */ |
| 237 | { |
| 238 | /* |
| 239 | * Range check input... |
| 240 | */ |
| 241 | |
| 242 | if (!node) |
| 243 | return (NULL); |
| 244 | |
| 245 | /* |
| 246 | * Return the integer value... |
| 247 | */ |
| 248 | |
| 249 | if (node->type == MXML_OPAQUE) |
| 250 | return (node->value.opaque); |
| 251 | else if (node->type == MXML_ELEMENT && |
| 252 | node->child && |
| 253 | node->child->type == MXML_OPAQUE) |
| 254 | return (node->child->value.opaque); |
| 255 | else |
| 256 | return (NULL); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* |
| 261 | * 'mxmlGetParent()' - Get the parent node. |
| 262 | * |
| 263 | * @code NULL@ is returned for a root node. |
| 264 | * |
| 265 | * @since Mini-XML 2.7@ |
| 266 | */ |
| 267 | |
| 268 | mxml_node_t * /* O - Parent node or NULL */ |
| 269 | mxmlGetParent(mxml_node_t *node) /* I - Node to get */ |
| 270 | { |
| 271 | /* |
| 272 | * Range check input... |
| 273 | */ |
| 274 | |
| 275 | if (!node) |
| 276 | return (NULL); |
| 277 | |
| 278 | /* |
| 279 | * Return the node type... |
| 280 | */ |
| 281 | |
| 282 | return (node->parent); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /* |
| 287 | * 'mxmlGetPrevSibling()' - Get the previous node for the current parent. |
| 288 | * |
| 289 | * @code NULL@ is returned if this is the first child for the current parent. |
| 290 | * |
| 291 | * @since Mini-XML 2.7@ |
| 292 | */ |
| 293 | |
| 294 | mxml_node_t * /* O - Previous node or NULL */ |
| 295 | mxmlGetPrevSibling(mxml_node_t *node) /* I - Node to get */ |
| 296 | { |
| 297 | /* |
| 298 | * Range check input... |
| 299 | */ |
| 300 | |
| 301 | if (!node) |
| 302 | return (NULL); |
| 303 | |
| 304 | /* |
| 305 | * Return the node type... |
| 306 | */ |
| 307 | |
| 308 | return (node->prev); |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /* |
| 313 | * 'mxmlGetReal()' - Get the real value for a node or its first child. |
| 314 | * |
| 315 | * 0.0 is returned if the node (or its first child) is not a real value node. |
| 316 | * |
| 317 | * @since Mini-XML 2.7@ |
| 318 | */ |
| 319 | |
| 320 | double /* O - Real value or 0.0 */ |
| 321 | mxmlGetReal(mxml_node_t *node) /* I - Node to get */ |
| 322 | { |
| 323 | /* |
| 324 | * Range check input... |
| 325 | */ |
| 326 | |
| 327 | if (!node) |
| 328 | return (0.0); |
| 329 | |
| 330 | /* |
| 331 | * Return the integer value... |
| 332 | */ |
| 333 | |
| 334 | if (node->type == MXML_REAL) |
| 335 | return (node->value.real); |
| 336 | else if (node->type == MXML_ELEMENT && |
| 337 | node->child && |
| 338 | node->child->type == MXML_REAL) |
| 339 | return (node->child->value.real); |
| 340 | else |
| 341 | return (0.0); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * 'mxmlGetText()' - Get the text value for a node or its first child. |
| 347 | * |
| 348 | * @code NULL@ is returned if the node (or its first child) is not a text node. |
| 349 | * The "whitespace" argument can be NULL. |
| 350 | * |
| 351 | * @since Mini-XML 2.7@ |
| 352 | */ |
| 353 | |
| 354 | const char * /* O - Text string or NULL */ |
| 355 | mxmlGetText(mxml_node_t *node, /* I - Node to get */ |
| 356 | int *whitespace) /* O - 1 if string is preceded by whitespace, 0 otherwise */ |
| 357 | { |
| 358 | /* |
| 359 | * Range check input... |
| 360 | */ |
| 361 | |
| 362 | if (!node) |
| 363 | { |
| 364 | if (whitespace) |
| 365 | *whitespace = 0; |
| 366 | |
| 367 | return (NULL); |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Return the integer value... |
| 372 | */ |
| 373 | |
| 374 | if (node->type == MXML_TEXT) |
| 375 | { |
| 376 | if (whitespace) |
| 377 | *whitespace = node->value.text.whitespace; |
| 378 | |
| 379 | return (node->value.text.string); |
| 380 | } |
| 381 | else if (node->type == MXML_ELEMENT && |
| 382 | node->child && |
| 383 | node->child->type == MXML_TEXT) |
| 384 | { |
| 385 | if (whitespace) |
| 386 | *whitespace = node->child->value.text.whitespace; |
| 387 | |
| 388 | return (node->child->value.text.string); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | if (whitespace) |
| 393 | *whitespace = 0; |
| 394 | |
| 395 | return (NULL); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /* |
| 401 | * 'mxmlGetType()' - Get the node type. |
| 402 | * |
| 403 | * @code MXML_IGNORE@ is returned if "node" is @code NULL@. |
| 404 | * |
| 405 | * @since Mini-XML 2.7@ |
| 406 | */ |
| 407 | |
| 408 | mxml_type_t /* O - Type of node */ |
| 409 | mxmlGetType(mxml_node_t *node) /* I - Node to get */ |
| 410 | { |
| 411 | /* |
| 412 | * Range check input... |
| 413 | */ |
| 414 | |
| 415 | if (!node) |
| 416 | return (MXML_IGNORE); |
| 417 | |
| 418 | /* |
| 419 | * Return the node type... |
| 420 | */ |
| 421 | |
| 422 | return (node->type); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* |
| 427 | * 'mxmlGetUserData()' - Get the user data pointer for a node. |
| 428 | * |
| 429 | * @since Mini-XML 2.7@ |
| 430 | */ |
| 431 | |
| 432 | void * /* O - User data pointer */ |
| 433 | mxmlGetUserData(mxml_node_t *node) /* I - Node to get */ |
| 434 | { |
| 435 | /* |
| 436 | * Range check input... |
| 437 | */ |
| 438 | |
| 439 | if (!node) |
| 440 | return (NULL); |
| 441 | |
| 442 | /* |
| 443 | * Return the user data pointer... |
| 444 | */ |
| 445 | |
| 446 | return (node->user_data); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 451 | * End of "$Id: mxml-get.c 451 2014-01-04 21:50:06Z msweet $". |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 452 | */ |