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