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-set.c 451 2014-01-04 21:50:06Z msweet $" |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * Node set 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 2003-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 | * 'mxmlSetCDATA()' - Set the element name of a CDATA node. |
| 27 | * |
| 28 | * The node is not changed if it (or its first child) is not a CDATA element node. |
| 29 | * |
| 30 | * @since Mini-XML 2.3@ |
| 31 | */ |
| 32 | |
| 33 | int /* O - 0 on success, -1 on failure */ |
| 34 | mxmlSetCDATA(mxml_node_t *node, /* I - Node to set */ |
| 35 | const char *data) /* I - New data string */ |
| 36 | { |
| 37 | /* |
| 38 | * Range check input... |
| 39 | */ |
| 40 | |
| 41 | if (node && node->type == MXML_ELEMENT && |
| 42 | strncmp(node->value.element.name, "![CDATA[", 8) && |
| 43 | node->child && node->child->type == MXML_ELEMENT && |
| 44 | !strncmp(node->child->value.element.name, "![CDATA[", 8)) |
| 45 | node = node->child; |
| 46 | |
| 47 | if (!node || node->type != MXML_ELEMENT || !data || |
| 48 | strncmp(node->value.element.name, "![CDATA[", 8)) |
| 49 | return (-1); |
| 50 | |
| 51 | /* |
| 52 | * Free any old element value and set the new value... |
| 53 | */ |
| 54 | |
| 55 | if (node->value.element.name) |
| 56 | free(node->value.element.name); |
| 57 | |
| 58 | node->value.element.name = _mxml_strdupf("![CDATA[%s]]", data); |
| 59 | |
| 60 | return (0); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * 'mxmlSetCustom()' - Set the data and destructor of a custom data node. |
| 66 | * |
| 67 | * The node is not changed if it (or its first child) is not a custom node. |
| 68 | * |
| 69 | * @since Mini-XML 2.1@ |
| 70 | */ |
| 71 | |
| 72 | int /* O - 0 on success, -1 on failure */ |
| 73 | mxmlSetCustom( |
| 74 | mxml_node_t *node, /* I - Node to set */ |
| 75 | void *data, /* I - New data pointer */ |
| 76 | mxml_custom_destroy_cb_t destroy) /* I - New destructor function */ |
| 77 | { |
| 78 | /* |
| 79 | * Range check input... |
| 80 | */ |
| 81 | |
| 82 | if (node && node->type == MXML_ELEMENT && |
| 83 | node->child && node->child->type == MXML_CUSTOM) |
| 84 | node = node->child; |
| 85 | |
| 86 | if (!node || node->type != MXML_CUSTOM) |
| 87 | return (-1); |
| 88 | |
| 89 | /* |
| 90 | * Free any old element value and set the new value... |
| 91 | */ |
| 92 | |
| 93 | if (node->value.custom.data && node->value.custom.destroy) |
| 94 | (*(node->value.custom.destroy))(node->value.custom.data); |
| 95 | |
| 96 | node->value.custom.data = data; |
| 97 | node->value.custom.destroy = destroy; |
| 98 | |
| 99 | return (0); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* |
| 104 | * 'mxmlSetElement()' - Set the name of an element node. |
| 105 | * |
| 106 | * The node is not changed if it is not an element node. |
| 107 | */ |
| 108 | |
| 109 | int /* O - 0 on success, -1 on failure */ |
| 110 | mxmlSetElement(mxml_node_t *node, /* I - Node to set */ |
| 111 | const char *name) /* I - New name string */ |
| 112 | { |
| 113 | /* |
| 114 | * Range check input... |
| 115 | */ |
| 116 | |
| 117 | if (!node || node->type != MXML_ELEMENT || !name) |
| 118 | return (-1); |
| 119 | |
| 120 | /* |
| 121 | * Free any old element value and set the new value... |
| 122 | */ |
| 123 | |
| 124 | if (node->value.element.name) |
| 125 | free(node->value.element.name); |
| 126 | |
| 127 | node->value.element.name = strdup(name); |
| 128 | |
| 129 | return (0); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* |
| 134 | * 'mxmlSetInteger()' - Set the value of an integer node. |
| 135 | * |
| 136 | * The node is not changed if it (or its first child) is not an integer node. |
| 137 | */ |
| 138 | |
| 139 | int /* O - 0 on success, -1 on failure */ |
| 140 | mxmlSetInteger(mxml_node_t *node, /* I - Node to set */ |
| 141 | int integer) /* I - Integer value */ |
| 142 | { |
| 143 | /* |
| 144 | * Range check input... |
| 145 | */ |
| 146 | |
| 147 | if (node && node->type == MXML_ELEMENT && |
| 148 | node->child && node->child->type == MXML_INTEGER) |
| 149 | node = node->child; |
| 150 | |
| 151 | if (!node || node->type != MXML_INTEGER) |
| 152 | return (-1); |
| 153 | |
| 154 | /* |
| 155 | * Set the new value and return... |
| 156 | */ |
| 157 | |
| 158 | node->value.integer = integer; |
| 159 | |
| 160 | return (0); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /* |
| 165 | * 'mxmlSetOpaque()' - Set the value of an opaque node. |
| 166 | * |
| 167 | * The node is not changed if it (or its first child) is not an opaque node. |
| 168 | */ |
| 169 | |
| 170 | int /* O - 0 on success, -1 on failure */ |
| 171 | mxmlSetOpaque(mxml_node_t *node, /* I - Node to set */ |
| 172 | const char *opaque) /* I - Opaque string */ |
| 173 | { |
| 174 | /* |
| 175 | * Range check input... |
| 176 | */ |
| 177 | |
| 178 | if (node && node->type == MXML_ELEMENT && |
| 179 | node->child && node->child->type == MXML_OPAQUE) |
| 180 | node = node->child; |
| 181 | |
| 182 | if (!node || node->type != MXML_OPAQUE || !opaque) |
| 183 | return (-1); |
| 184 | |
| 185 | /* |
| 186 | * Free any old opaque value and set the new value... |
| 187 | */ |
| 188 | |
| 189 | if (node->value.opaque) |
| 190 | free(node->value.opaque); |
| 191 | |
| 192 | node->value.opaque = strdup(opaque); |
| 193 | |
| 194 | return (0); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* |
| 199 | * 'mxmlSetReal()' - Set the value of a real number node. |
| 200 | * |
| 201 | * The node is not changed if it (or its first child) is not a real number node. |
| 202 | */ |
| 203 | |
| 204 | int /* O - 0 on success, -1 on failure */ |
| 205 | mxmlSetReal(mxml_node_t *node, /* I - Node to set */ |
| 206 | double real) /* I - Real number value */ |
| 207 | { |
| 208 | /* |
| 209 | * Range check input... |
| 210 | */ |
| 211 | |
| 212 | if (node && node->type == MXML_ELEMENT && |
| 213 | node->child && node->child->type == MXML_REAL) |
| 214 | node = node->child; |
| 215 | |
| 216 | if (!node || node->type != MXML_REAL) |
| 217 | return (-1); |
| 218 | |
| 219 | /* |
| 220 | * Set the new value and return... |
| 221 | */ |
| 222 | |
| 223 | node->value.real = real; |
| 224 | |
| 225 | return (0); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* |
| 230 | * 'mxmlSetText()' - Set the value of a text node. |
| 231 | * |
| 232 | * The node is not changed if it (or its first child) is not a text node. |
| 233 | */ |
| 234 | |
| 235 | int /* O - 0 on success, -1 on failure */ |
| 236 | mxmlSetText(mxml_node_t *node, /* I - Node to set */ |
| 237 | int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */ |
| 238 | const char *string) /* I - String */ |
| 239 | { |
| 240 | /* |
| 241 | * Range check input... |
| 242 | */ |
| 243 | |
| 244 | if (node && node->type == MXML_ELEMENT && |
| 245 | node->child && node->child->type == MXML_TEXT) |
| 246 | node = node->child; |
| 247 | |
| 248 | if (!node || node->type != MXML_TEXT || !string) |
| 249 | return (-1); |
| 250 | |
| 251 | /* |
| 252 | * Free any old string value and set the new value... |
| 253 | */ |
| 254 | |
| 255 | if (node->value.text.string) |
| 256 | free(node->value.text.string); |
| 257 | |
| 258 | node->value.text.whitespace = whitespace; |
| 259 | node->value.text.string = strdup(string); |
| 260 | |
| 261 | return (0); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | * 'mxmlSetTextf()' - Set the value of a text node to a formatted string. |
| 267 | * |
| 268 | * The node is not changed if it (or its first child) is not a text node. |
| 269 | */ |
| 270 | |
| 271 | int /* O - 0 on success, -1 on failure */ |
| 272 | mxmlSetTextf(mxml_node_t *node, /* I - Node to set */ |
| 273 | int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */ |
| 274 | const char *format, /* I - Printf-style format string */ |
| 275 | ...) /* I - Additional arguments as needed */ |
| 276 | { |
| 277 | va_list ap; /* Pointer to arguments */ |
| 278 | |
| 279 | |
| 280 | /* |
| 281 | * Range check input... |
| 282 | */ |
| 283 | |
| 284 | if (node && node->type == MXML_ELEMENT && |
| 285 | node->child && node->child->type == MXML_TEXT) |
| 286 | node = node->child; |
| 287 | |
| 288 | if (!node || node->type != MXML_TEXT || !format) |
| 289 | return (-1); |
| 290 | |
| 291 | /* |
| 292 | * Free any old string value and set the new value... |
| 293 | */ |
| 294 | |
| 295 | if (node->value.text.string) |
| 296 | free(node->value.text.string); |
| 297 | |
| 298 | va_start(ap, format); |
| 299 | |
| 300 | node->value.text.whitespace = whitespace; |
| 301 | node->value.text.string = _mxml_strdupf(format, ap); |
| 302 | |
| 303 | va_end(ap); |
| 304 | |
| 305 | return (0); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* |
| 310 | * 'mxmlSetUserData()' - Set the user data pointer for a node. |
| 311 | * |
| 312 | * @since Mini-XML 2.7@ |
| 313 | */ |
| 314 | |
| 315 | int /* O - 0 on success, -1 on failure */ |
| 316 | mxmlSetUserData(mxml_node_t *node, /* I - Node to set */ |
| 317 | void *data) /* I - User data pointer */ |
| 318 | { |
| 319 | /* |
| 320 | * Range check input... |
| 321 | */ |
| 322 | |
| 323 | if (!node) |
| 324 | return (-1); |
| 325 | |
| 326 | /* |
| 327 | * Set the user data pointer and return... |
| 328 | */ |
| 329 | |
| 330 | node->user_data = data; |
| 331 | return (0); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
Jon Medhurst | 96b5615 | 2014-10-30 18:01:15 +0000 | [diff] [blame] | 336 | * End of "$Id: mxml-set.c 451 2014-01-04 21:50:06Z msweet $". |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 337 | */ |