| 1 | /* |
| 2 | * File loading code for mini-XML, a small XML-like file parsing library. |
| 3 | * |
| 4 | * Copyright 2003 by Michael Sweet. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * Contents: |
| 17 | * |
| 18 | * stp_mxmlLoadFile() - Load a file into an XML node tree. |
| 19 | * stp_mxmlLoadFromFile() - Load a file into an XML node tree. |
| 20 | * stp_mxmlLoadString() - Load a string into an XML node tree. |
| 21 | * stp_mxmlSaveAllocString() - Save an XML node tree to an allocated string. |
| 22 | * stp_mxmlSaveFile() - Save an XML tree to a file. |
| 23 | * stp_mxmlSaveString() - Save an XML node tree to a string. |
| 24 | * mxml_add_char() - Add a character to a buffer, expanding as needed. |
| 25 | * mxml_file_getc() - Get a character from a file. |
| 26 | * mxml_load_data() - Load data into an XML node tree. |
| 27 | * mxml_parse_element() - Parse an element for any attributes... |
| 28 | * mxml_string_getc() - Get a character from a string. |
| 29 | * mxml_write_node() - Save an XML node to a file. |
| 30 | * mxml_write_string() - Write a string, escaping & and < as needed. |
| 31 | * mxml_write_ws() - Do whitespace callback... |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * Include necessary headers... |
| 36 | */ |
| 37 | |
| 38 | #include <gutenprint/util.h> |
| 39 | #include <gutenprint/mxml.h> |
| 40 | #ifdef HAVE_CONFIG_H |
| 41 | #include "config.h" |
| 42 | #endif |
| 43 | #define MXML_BUFSIZE (64) |
| 44 | #define ENTITY_BUFSIZE (64) |
| 45 | |
| 46 | /* |
| 47 | * Local functions... |
| 48 | */ |
| 49 | |
| 50 | static int mxml_add_char(int ch, char **ptr, char **buffer, |
| 51 | int *bufsize); |
| 52 | static int mxml_file_getc(void *p); |
| 53 | static int mxml_file_putc(int ch, void *p); |
| 54 | static stp_mxml_node_t *mxml_load_data(stp_mxml_node_t *top, void *p, |
| 55 | stp_mxml_type_t (*cb)(stp_mxml_node_t *), |
| 56 | int (*getc_cb)(void *)); |
| 57 | static int mxml_parse_element(stp_mxml_node_t *node, void *p, |
| 58 | int (*getc_cb)(void *)); |
| 59 | static int mxml_string_getc(void *p); |
| 60 | static int mxml_string_putc(int ch, void *p); |
| 61 | static int mxml_write_node(stp_mxml_node_t *node, void *p, |
| 62 | int (*cb)(stp_mxml_node_t *, int), |
| 63 | int col, |
| 64 | int (*putc_cb)(int, void *)); |
| 65 | static int mxml_write_string(const char *s, void *p, |
| 66 | int (*putc_cb)(int, void *)); |
| 67 | static int mxml_write_ws(stp_mxml_node_t *node, void *p, |
| 68 | int (*cb)(stp_mxml_node_t *, int), int ws, |
| 69 | int col, int (*putc_cb)(int, void *)); |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * 'stp_mxmlLoadFile()' - Load a file into an XML node tree. |
| 74 | * |
| 75 | * The nodes in the specified file are added to the specified top node. |
| 76 | * If no top node is provided, the XML file MUST be well-formed with a |
| 77 | * single parent node like <?xml> for the entire file. The callback |
| 78 | * function returns the value type that should be used for child nodes. |
| 79 | * If STP_MXML_NO_CALLBACK is specified then all child nodes will be either |
| 80 | * STP_MXML_ELEMENT or STP_MXML_TEXT nodes. |
| 81 | */ |
| 82 | |
| 83 | stp_mxml_node_t * /* O - First node or NULL if the file could not be read. */ |
| 84 | stp_mxmlLoadFile(stp_mxml_node_t *top, /* I - Top node */ |
| 85 | FILE *fp, /* I - File to read from */ |
| 86 | stp_mxml_type_t (*cb)(stp_mxml_node_t *)) |
| 87 | /* I - Callback function or STP_MXML_NO_CALLBACK */ |
| 88 | { |
| 89 | return (mxml_load_data(top, fp, cb, mxml_file_getc)); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * 'stp_mxmlLoadFromFile()' - Load a named file into an XML node tree. |
| 94 | * |
| 95 | * The nodes in the specified file are added to the specified top node. |
| 96 | * If no top node is provided, the XML file MUST be well-formed with a |
| 97 | * single parent node like <?xml> for the entire file. The callback |
| 98 | * function returns the value type that should be used for child nodes. |
| 99 | * If STP_MXML_NO_CALLBACK is specified then all child nodes will be either |
| 100 | * STP_MXML_ELEMENT or STP_MXML_TEXT nodes. |
| 101 | */ |
| 102 | |
| 103 | stp_mxml_node_t * /* O - First node or NULL if the file could not be read. */ |
| 104 | stp_mxmlLoadFromFile(stp_mxml_node_t *top, /* I - Top node */ |
| 105 | const char *file, /* I - File to read from */ |
| 106 | stp_mxml_type_t (*cb)(stp_mxml_node_t *)) |
| 107 | /* I - Callback function or STP_MXML_NO_CALLBACK */ |
| 108 | { |
| 109 | FILE *fp = fopen(file, "r"); |
| 110 | stp_mxml_node_t *doc; |
| 111 | if (! fp) |
| 112 | return NULL; |
| 113 | doc = stp_mxmlLoadFile(top, fp, cb); |
| 114 | fclose(fp); |
| 115 | return doc; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /* |
| 120 | * 'stp_mxmlLoadString()' - Load a string into an XML node tree. |
| 121 | * |
| 122 | * The nodes in the specified string are added to the specified top node. |
| 123 | * If no top node is provided, the XML string MUST be well-formed with a |
| 124 | * single parent node like <?xml> for the entire string. The callback |
| 125 | * function returns the value type that should be used for child nodes. |
| 126 | * If STP_MXML_NO_CALLBACK is specified then all child nodes will be either |
| 127 | * STP_MXML_ELEMENT or STP_MXML_TEXT nodes. |
| 128 | */ |
| 129 | |
| 130 | stp_mxml_node_t * /* O - First node or NULL if the string has errors. */ |
| 131 | stp_mxmlLoadString(stp_mxml_node_t *top, /* I - Top node */ |
| 132 | const char *s, /* I - String to load */ |
| 133 | stp_mxml_type_t (*cb)(stp_mxml_node_t *)) |
| 134 | /* I - Callback function or STP_MXML_NO_CALLBACK */ |
| 135 | { |
| 136 | return (mxml_load_data(top, &s, cb, mxml_string_getc)); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * 'stp_mxmlSaveAllocString()' - Save an XML node tree to an allocated string. |
| 142 | * |
| 143 | * This function returns a pointer to a string containing the textual |
| 144 | * representation of the XML node tree. The string should be freed |
| 145 | * using the free() function when you are done with it. NULL is returned |
| 146 | * if the node would produce an empty string or if the string cannot be |
| 147 | * allocated. |
| 148 | */ |
| 149 | |
| 150 | char * /* O - Allocated string or NULL */ |
| 151 | stp_mxmlSaveAllocString(stp_mxml_node_t *node, /* I - Node to write */ |
| 152 | int (*cb)(stp_mxml_node_t *, int)) |
| 153 | /* I - Whitespace callback or STP_MXML_NO_CALLBACK */ |
| 154 | { |
| 155 | int bytes; /* Required bytes */ |
| 156 | char buffer[8192]; /* Temporary buffer */ |
| 157 | char *s; /* Allocated string */ |
| 158 | |
| 159 | |
| 160 | /* |
| 161 | * Write the node to the temporary buffer... |
| 162 | */ |
| 163 | |
| 164 | bytes = stp_mxmlSaveString(node, buffer, sizeof(buffer), cb); |
| 165 | |
| 166 | if (bytes <= 0) |
| 167 | return (NULL); |
| 168 | |
| 169 | if (bytes < (int)(sizeof(buffer) - 1)) |
| 170 | { |
| 171 | /* |
| 172 | * Node fit inside the buffer, so just duplicate that string and |
| 173 | * return... |
| 174 | */ |
| 175 | |
| 176 | return (stp_strdup(buffer)); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Allocate a buffer of the required size and save the node to the |
| 181 | * new buffer... |
| 182 | */ |
| 183 | |
| 184 | if ((s = malloc(bytes + 1)) == NULL) |
| 185 | return (NULL); |
| 186 | |
| 187 | stp_mxmlSaveString(node, s, bytes + 1, cb); |
| 188 | |
| 189 | /* |
| 190 | * Return the allocated string... |
| 191 | */ |
| 192 | |
| 193 | return (s); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /* |
| 198 | * 'stp_mxmlSaveFile()' - Save an XML tree to a file. |
| 199 | * |
| 200 | * The callback argument specifies a function that returns a whitespace |
| 201 | * character or nul (0) before and after each element. If STP_MXML_NO_CALLBACK |
| 202 | * is specified, whitespace will only be added before STP_MXML_TEXT nodes |
| 203 | * with leading whitespace and before attribute names inside opening |
| 204 | * element tags. |
| 205 | */ |
| 206 | |
| 207 | int /* O - 0 on success, -1 on error. */ |
| 208 | stp_mxmlSaveFile(stp_mxml_node_t *node, /* I - Node to write */ |
| 209 | FILE *fp, /* I - File to write to */ |
| 210 | int (*cb)(stp_mxml_node_t *, int)) |
| 211 | /* I - Whitespace callback or STP_MXML_NO_CALLBACK */ |
| 212 | { |
| 213 | int col; /* Final column */ |
| 214 | |
| 215 | |
| 216 | /* |
| 217 | * Write the node... |
| 218 | */ |
| 219 | |
| 220 | if ((col = mxml_write_node(node, fp, cb, 0, mxml_file_putc)) < 0) |
| 221 | return (-1); |
| 222 | |
| 223 | if (col > 0) |
| 224 | if (putc('\n', fp) < 0) |
| 225 | return (-1); |
| 226 | |
| 227 | /* |
| 228 | * Return 0 (success)... |
| 229 | */ |
| 230 | |
| 231 | return (0); |
| 232 | } |
| 233 | |
| 234 | int /* O - 0 on success, -1 on error. */ |
| 235 | stp_mxmlSaveToFile(stp_mxml_node_t *node, /* I - Node to write */ |
| 236 | const char *file, /* I - File to write to */ |
| 237 | int (*cb)(stp_mxml_node_t *, int)) |
| 238 | /* I - Whitespace callback or STP_MXML_NO_CALLBACK */ |
| 239 | { |
| 240 | FILE *fp = fopen(file, "w"); |
| 241 | int answer; |
| 242 | int status; |
| 243 | if (!fp) |
| 244 | return -1; |
| 245 | answer = stp_mxmlSaveFile(node, fp, cb); |
| 246 | status = fclose(fp); |
| 247 | if (status != 0) |
| 248 | return -1; |
| 249 | else |
| 250 | return answer; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * 'stp_mxmlSaveString()' - Save an XML node tree to a string. |
| 255 | * |
| 256 | * This function returns the total number of bytes that would be |
| 257 | * required for the string but only copies (bufsize - 1) characters |
| 258 | * into the specified buffer. |
| 259 | */ |
| 260 | |
| 261 | int /* O - Size of string */ |
| 262 | stp_mxmlSaveString(stp_mxml_node_t *node, /* I - Node to write */ |
| 263 | char *buffer, /* I - String buffer */ |
| 264 | int bufsize, /* I - Size of string buffer */ |
| 265 | int (*cb)(stp_mxml_node_t *, int)) |
| 266 | /* I - Whitespace callback or STP_MXML_NO_CALLBACK */ |
| 267 | { |
| 268 | int col; /* Final column */ |
| 269 | char *ptr[2]; /* Pointers for putc_cb */ |
| 270 | |
| 271 | |
| 272 | /* |
| 273 | * Write the node... |
| 274 | */ |
| 275 | |
| 276 | ptr[0] = buffer; |
| 277 | ptr[1] = buffer + bufsize; |
| 278 | |
| 279 | if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc)) < 0) |
| 280 | return (-1); |
| 281 | |
| 282 | if (col > 0) |
| 283 | mxml_string_putc('\n', ptr); |
| 284 | |
| 285 | /* |
| 286 | * Nul-terminate the buffer... |
| 287 | */ |
| 288 | |
| 289 | if (ptr[0] >= ptr[1]) |
| 290 | buffer[bufsize - 1] = '\0'; |
| 291 | else |
| 292 | ptr[0][0] = '\0'; |
| 293 | |
| 294 | /* |
| 295 | * Return the number of characters... |
| 296 | */ |
| 297 | |
| 298 | return (ptr[0] - buffer); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /* |
| 303 | * 'mxml_add_char()' - Add a character to a buffer, expanding as needed. |
| 304 | */ |
| 305 | |
| 306 | static int /* O - 0 on success, -1 on error */ |
| 307 | mxml_add_char(int ch, /* I - Character to add */ |
| 308 | char **bufptr, /* IO - Current position in buffer */ |
| 309 | char **buffer, /* IO - Current buffer */ |
| 310 | int *bufsize) /* IO - Current buffer size */ |
| 311 | { |
| 312 | char *newbuffer; /* New buffer value */ |
| 313 | |
| 314 | |
| 315 | if (*bufptr >= (*buffer + *bufsize - 1)) |
| 316 | { |
| 317 | /* |
| 318 | * Increase the size of the buffer... |
| 319 | */ |
| 320 | |
| 321 | if (*bufsize < 1024) |
| 322 | (*bufsize) *= 2; |
| 323 | else |
| 324 | (*bufsize) += 1024; |
| 325 | |
| 326 | if ((newbuffer = realloc(*buffer, *bufsize)) == NULL) |
| 327 | { |
| 328 | free(*buffer); |
| 329 | |
| 330 | fprintf(stderr, "Unable to expand string buffer to %d bytes!\n", |
| 331 | *bufsize); |
| 332 | |
| 333 | return (-1); |
| 334 | } |
| 335 | |
| 336 | *bufptr = newbuffer + (*bufptr - *buffer); |
| 337 | *buffer = newbuffer; |
| 338 | } |
| 339 | |
| 340 | *(*bufptr)++ = ch; |
| 341 | |
| 342 | return (0); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* |
| 347 | * 'mxml_file_getc()' - Get a character from a file. |
| 348 | */ |
| 349 | |
| 350 | static int /* O - Character or EOF */ |
| 351 | mxml_file_getc(void *p) /* I - Pointer to file */ |
| 352 | { |
| 353 | return (getc((FILE *)p)); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* |
| 358 | * 'mxml_file_putc()' - Write a character to a file. |
| 359 | */ |
| 360 | |
| 361 | static int /* O - 0 on success, -1 on failure */ |
| 362 | mxml_file_putc(int ch, /* I - Character to write */ |
| 363 | void *p) /* I - Pointer to file */ |
| 364 | { |
| 365 | return (putc(ch, (FILE *)p)); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | /* |
| 370 | * 'mxml_load_data()' - Load data into an XML node tree. |
| 371 | */ |
| 372 | |
| 373 | static stp_mxml_node_t * /* O - First node or NULL if the file could not be read. */ |
| 374 | mxml_load_data(stp_mxml_node_t *top, /* I - Top node */ |
| 375 | void *p, /* I - Pointer to data */ |
| 376 | stp_mxml_type_t (*cb)(stp_mxml_node_t *), |
| 377 | /* I - Callback function or STP_MXML_NO_CALLBACK */ |
| 378 | int (*getc_cb)(void *)) |
| 379 | /* I - Read function */ |
| 380 | { |
| 381 | stp_mxml_node_t *node, /* Current node */ |
| 382 | *parent; /* Current parent node */ |
| 383 | int ch, /* Character from file */ |
| 384 | whitespace; /* Non-zero if whitespace seen */ |
| 385 | char *buffer, /* String buffer */ |
| 386 | *bufptr; /* Pointer into buffer */ |
| 387 | int bufsize; /* Size of buffer */ |
| 388 | stp_mxml_type_t type; /* Current node type */ |
| 389 | |
| 390 | |
| 391 | /* |
| 392 | * Read elements and other nodes from the file... |
| 393 | */ |
| 394 | |
| 395 | if ((buffer = malloc(MXML_BUFSIZE)) == NULL) |
| 396 | { |
| 397 | fputs("Unable to allocate string buffer!\n", stderr); |
| 398 | return (NULL); |
| 399 | } |
| 400 | |
| 401 | bufsize = MXML_BUFSIZE; |
| 402 | bufptr = buffer; |
| 403 | parent = top; |
| 404 | whitespace = 0; |
| 405 | |
| 406 | if (cb && parent) |
| 407 | type = (*cb)(parent); |
| 408 | else |
| 409 | type = STP_MXML_TEXT; |
| 410 | |
| 411 | while ((ch = (*getc_cb)(p)) != EOF) |
| 412 | { |
| 413 | if ((ch == '<' || (isspace(ch) && type != STP_MXML_OPAQUE)) && bufptr > buffer) |
| 414 | { |
| 415 | /* |
| 416 | * Add a new value node... |
| 417 | */ |
| 418 | |
| 419 | *bufptr = '\0'; |
| 420 | |
| 421 | switch (type) |
| 422 | { |
| 423 | case STP_MXML_INTEGER : |
| 424 | node = stp_mxmlNewInteger(parent, strtol(buffer, &bufptr, 0)); |
| 425 | break; |
| 426 | |
| 427 | case STP_MXML_OPAQUE : |
| 428 | node = stp_mxmlNewOpaque(parent, buffer); |
| 429 | break; |
| 430 | |
| 431 | case STP_MXML_REAL : |
| 432 | node = stp_mxmlNewReal(parent, strtod(buffer, &bufptr)); |
| 433 | break; |
| 434 | |
| 435 | case STP_MXML_DIMENSION : |
| 436 | node = stp_mxmlNewDimension(parent, strtod(buffer, &bufptr)); |
| 437 | break; |
| 438 | |
| 439 | case STP_MXML_TEXT : |
| 440 | node = stp_mxmlNewText(parent, whitespace, buffer); |
| 441 | break; |
| 442 | |
| 443 | default : /* Should never happen... */ |
| 444 | node = NULL; |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | if (*bufptr) |
| 449 | { |
| 450 | /* |
| 451 | * Bad integer/real number value... |
| 452 | */ |
| 453 | |
| 454 | fprintf(stderr, "Bad %s value '%s' in parent <%s>!\n", |
| 455 | type == STP_MXML_INTEGER ? "integer" : "real", buffer, |
| 456 | parent ? parent->value.element.name : "null"); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | bufptr = buffer; |
| 461 | whitespace = isspace(ch) && type == STP_MXML_TEXT; |
| 462 | |
| 463 | if (!node) |
| 464 | { |
| 465 | /* |
| 466 | * Just print error for now... |
| 467 | */ |
| 468 | |
| 469 | fprintf(stderr, "Unable to add value node of type %d to parent <%s>!\n", |
| 470 | type, parent ? parent->value.element.name : "null"); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | else if (isspace(ch) && type == STP_MXML_TEXT) |
| 475 | whitespace = 1; |
| 476 | |
| 477 | /* |
| 478 | * Add lone whitespace node if we have an element and existing |
| 479 | * whitespace... |
| 480 | */ |
| 481 | |
| 482 | if (ch == '<' && whitespace && type == STP_MXML_TEXT) |
| 483 | { |
| 484 | stp_mxmlNewText(parent, whitespace, ""); |
| 485 | whitespace = 0; |
| 486 | } |
| 487 | |
| 488 | if (ch == '<') |
| 489 | { |
| 490 | /* |
| 491 | * Start of open/close tag... |
| 492 | */ |
| 493 | |
| 494 | bufptr = buffer; |
| 495 | |
| 496 | while ((ch = (*getc_cb)(p)) != EOF) |
| 497 | if (isspace(ch) || ch == '>' || (ch == '/' && bufptr > buffer)) |
| 498 | break; |
| 499 | else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize)) |
| 500 | { |
| 501 | return (NULL); |
| 502 | } |
| 503 | else if ((bufptr - buffer) == 3 && !strncmp(buffer, "!--", 3)) |
| 504 | break; |
| 505 | |
| 506 | *bufptr = '\0'; |
| 507 | |
| 508 | if (!strcmp(buffer, "!--")) |
| 509 | { |
| 510 | /* |
| 511 | * Gather rest of comment... |
| 512 | */ |
| 513 | |
| 514 | while ((ch = (*getc_cb)(p)) != EOF) |
| 515 | { |
| 516 | if (ch == '>' && bufptr > (buffer + 4) && |
| 517 | !strncmp(bufptr - 2, "--", 2)) |
| 518 | break; |
| 519 | else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize)) |
| 520 | { |
| 521 | return (NULL); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Error out if we didn't get the whole comment... |
| 527 | */ |
| 528 | |
| 529 | if (ch != '>') |
| 530 | break; |
| 531 | |
| 532 | /* |
| 533 | * Otherwise add this as an element under the current parent... |
| 534 | */ |
| 535 | |
| 536 | *bufptr = '\0'; |
| 537 | |
| 538 | if (!stp_mxmlNewElement(parent, buffer)) |
| 539 | { |
| 540 | /* |
| 541 | * Just print error for now... |
| 542 | */ |
| 543 | |
| 544 | fprintf(stderr, "Unable to add comment node to parent <%s>!\n", |
| 545 | parent ? parent->value.element.name : "null"); |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | else if (buffer[0] == '!') |
| 550 | { |
| 551 | /* |
| 552 | * Gather rest of declaration... |
| 553 | */ |
| 554 | |
| 555 | do |
| 556 | { |
| 557 | if (ch == '>') |
| 558 | break; |
| 559 | else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize)) |
| 560 | { |
| 561 | return (NULL); |
| 562 | } |
| 563 | } |
| 564 | while ((ch = (*getc_cb)(p)) != EOF); |
| 565 | |
| 566 | /* |
| 567 | * Error out if we didn't get the whole declaration... |
| 568 | */ |
| 569 | |
| 570 | if (ch != '>') |
| 571 | break; |
| 572 | |
| 573 | /* |
| 574 | * Otherwise add this as an element under the current parent... |
| 575 | */ |
| 576 | |
| 577 | *bufptr = '\0'; |
| 578 | |
| 579 | node = stp_mxmlNewElement(parent, buffer); |
| 580 | if (!node) |
| 581 | { |
| 582 | /* |
| 583 | * Just print error for now... |
| 584 | */ |
| 585 | |
| 586 | fprintf(stderr, "Unable to add declaration node to parent <%s>!\n", |
| 587 | parent ? parent->value.element.name : "null"); |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Descend into this node, setting the value type as needed... |
| 593 | */ |
| 594 | |
| 595 | parent = node; |
| 596 | |
| 597 | if (cb && parent) |
| 598 | type = (*cb)(parent); |
| 599 | } |
| 600 | else if (buffer[0] == '/') |
| 601 | { |
| 602 | /* |
| 603 | * Handle close tag... |
| 604 | */ |
| 605 | |
| 606 | if (!parent || strcmp(buffer + 1, parent->value.element.name)) |
| 607 | { |
| 608 | /* |
| 609 | * Close tag doesn't match tree; print an error for now... |
| 610 | */ |
| 611 | |
| 612 | fprintf(stderr, "Mismatched close tag <%s> under parent <%s>!\n", |
| 613 | buffer, parent ? parent->value.element.name : "(null)"); |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Keep reading until we see >... |
| 619 | */ |
| 620 | |
| 621 | while (ch != '>' && ch != EOF) |
| 622 | ch = (*getc_cb)(p); |
| 623 | |
| 624 | /* |
| 625 | * Ascend into the parent and set the value type as needed... |
| 626 | */ |
| 627 | |
| 628 | parent = parent->parent; |
| 629 | |
| 630 | if (cb && parent) |
| 631 | type = (*cb)(parent); |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | /* |
| 636 | * Handle open tag... |
| 637 | */ |
| 638 | |
| 639 | node = stp_mxmlNewElement(parent, buffer); |
| 640 | |
| 641 | if (!node) |
| 642 | { |
| 643 | /* |
| 644 | * Just print error for now... |
| 645 | */ |
| 646 | |
| 647 | fprintf(stderr, "Unable to add element node to parent <%s>!\n", |
| 648 | parent ? parent->value.element.name : "null"); |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | if (isspace(ch)) |
| 653 | ch = mxml_parse_element(node, p, getc_cb); |
| 654 | else if (ch == '/') |
| 655 | { |
| 656 | if ((ch = (*getc_cb)(p)) != '>') |
| 657 | { |
| 658 | fprintf(stderr, "Expected > but got '%c' instead for element <%s/>!\n", |
| 659 | ch, buffer); |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | ch = '/'; |
| 664 | } |
| 665 | |
| 666 | if (ch == EOF) |
| 667 | break; |
| 668 | |
| 669 | if (ch != '/') |
| 670 | { |
| 671 | /* |
| 672 | * Descend into this node, setting the value type as needed... |
| 673 | */ |
| 674 | |
| 675 | parent = node; |
| 676 | |
| 677 | if (cb && parent) |
| 678 | type = (*cb)(parent); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | bufptr = buffer; |
| 683 | } |
| 684 | else if (ch == '&') |
| 685 | { |
| 686 | /* |
| 687 | * Add character entity to current buffer... Currently we only |
| 688 | * support <, &, >, , ", &#nnn;, and &#xXXXX;... |
| 689 | */ |
| 690 | |
| 691 | char entity[ENTITY_BUFSIZE], /* Entity string */ |
| 692 | *entptr; /* Pointer into entity */ |
| 693 | |
| 694 | |
| 695 | entity[0] = ch; |
| 696 | entptr = entity + 1; |
| 697 | |
| 698 | while ((ch = (*getc_cb)(p)) != EOF) |
| 699 | if (!isalnum(ch) && ch != '#') |
| 700 | break; |
| 701 | else if (entptr < (entity + sizeof(entity) - 1)) |
| 702 | *entptr++ = ch; |
| 703 | else |
| 704 | { |
| 705 | fprintf(stderr, "Entity name too long under parent <%s>!\n", |
| 706 | parent ? parent->value.element.name : "null"); |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | *entptr = '\0'; |
| 711 | |
| 712 | if (ch != ';') |
| 713 | { |
| 714 | fprintf(stderr, "Entity name \"%s\" not terminated under parent <%s>!\n", |
| 715 | entity, parent ? parent->value.element.name : "null"); |
| 716 | break; |
| 717 | } |
| 718 | |
| 719 | if (entity[1] == '#') |
| 720 | { |
| 721 | if (entity[2] == 'x') |
| 722 | ch = strtol(entity + 3, NULL, 16); |
| 723 | else |
| 724 | ch = strtol(entity + 2, NULL, 10); |
| 725 | } |
| 726 | else if (!strcmp(entity, "&")) |
| 727 | ch = '&'; |
| 728 | else if (!strcmp(entity, ">")) |
| 729 | ch = '>'; |
| 730 | else if (!strcmp(entity, "<")) |
| 731 | ch = '<'; |
| 732 | else if (!strcmp(entity, " ")) |
| 733 | ch = 0xa0; |
| 734 | else if (!strcmp(entity, """)) |
| 735 | ch = '\"'; |
| 736 | else |
| 737 | { |
| 738 | fprintf(stderr, "Entity name \"%s;\" not supported under parent <%s>!\n", |
| 739 | entity, parent ? parent->value.element.name : "null"); |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | if (ch < 128) |
| 744 | { |
| 745 | /* |
| 746 | * Plain ASCII doesn't need special encoding... |
| 747 | */ |
| 748 | |
| 749 | if (mxml_add_char(ch, &bufptr, &buffer, &bufsize)) |
| 750 | { |
| 751 | return (NULL); |
| 752 | } |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | /* |
| 757 | * Use UTF-8 encoding for the Unicode char... |
| 758 | */ |
| 759 | |
| 760 | if (ch < 2048) |
| 761 | { |
| 762 | if (mxml_add_char(0xc0 | (ch >> 6), &bufptr, &buffer, &bufsize)) |
| 763 | { |
| 764 | return (NULL); |
| 765 | } |
| 766 | if (mxml_add_char(0x80 | (ch & 63), &bufptr, &buffer, &bufsize)) |
| 767 | { |
| 768 | return (NULL); |
| 769 | } |
| 770 | } |
| 771 | else if (ch < 65536) |
| 772 | { |
| 773 | if (mxml_add_char(0xe0 | (ch >> 12), &bufptr, &buffer, &bufsize)) |
| 774 | { |
| 775 | return (NULL); |
| 776 | } |
| 777 | if (mxml_add_char(0x80 | ((ch >> 6) & 63), &bufptr, &buffer, &bufsize)) |
| 778 | { |
| 779 | return (NULL); |
| 780 | } |
| 781 | if (mxml_add_char(0x80 | (ch & 63), &bufptr, &buffer, &bufsize)) |
| 782 | { |
| 783 | return (NULL); |
| 784 | } |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | if (mxml_add_char(0xf0 | (ch >> 18), &bufptr, &buffer, &bufsize)) |
| 789 | { |
| 790 | return (NULL); |
| 791 | } |
| 792 | if (mxml_add_char(0x80 | ((ch >> 12) & 63), &bufptr, &buffer, &bufsize)) |
| 793 | { |
| 794 | return (NULL); |
| 795 | } |
| 796 | if (mxml_add_char(0x80 | ((ch >> 6) & 63), &bufptr, &buffer, &bufsize)) |
| 797 | { |
| 798 | return (NULL); |
| 799 | } |
| 800 | if (mxml_add_char(0x80 | (ch & 63), &bufptr, &buffer, &bufsize)) |
| 801 | { |
| 802 | return (NULL); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | else if (type == STP_MXML_OPAQUE || !isspace(ch)) |
| 808 | { |
| 809 | /* |
| 810 | * Add character to current buffer... |
| 811 | */ |
| 812 | |
| 813 | if (mxml_add_char(ch, &bufptr, &buffer, &bufsize)) |
| 814 | { |
| 815 | return (NULL); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Free the string buffer - we don't need it anymore... |
| 822 | */ |
| 823 | |
| 824 | free(buffer); |
| 825 | |
| 826 | /* |
| 827 | * Find the top element and return it... |
| 828 | */ |
| 829 | |
| 830 | if (parent) |
| 831 | { |
| 832 | while (parent->parent != top) |
| 833 | parent = parent->parent; |
| 834 | } |
| 835 | |
| 836 | return (parent); |
| 837 | } |
| 838 | |
| 839 | |
| 840 | /* |
| 841 | * 'mxml_parse_element()' - Parse an element for any attributes... |
| 842 | */ |
| 843 | |
| 844 | static int /* O - Terminating character */ |
| 845 | mxml_parse_element(stp_mxml_node_t *node, /* I - Element node */ |
| 846 | void *p, /* I - Data to read from */ |
| 847 | int (*getc_cb)(void *)) |
| 848 | /* I - Data callback */ |
| 849 | { |
| 850 | int ch, /* Current character in file */ |
| 851 | quote; /* Quoting character */ |
| 852 | char *name, /* Attribute name */ |
| 853 | *value, /* Attribute value */ |
| 854 | *ptr; /* Pointer into name/value */ |
| 855 | int namesize, /* Size of name string */ |
| 856 | valsize; /* Size of value string */ |
| 857 | |
| 858 | |
| 859 | /* |
| 860 | * Initialize the name and value buffers... |
| 861 | */ |
| 862 | |
| 863 | if ((name = malloc(MXML_BUFSIZE)) == NULL) |
| 864 | { |
| 865 | fputs("Unable to allocate memory for name!\n", stderr); |
| 866 | return (EOF); |
| 867 | } |
| 868 | |
| 869 | namesize = MXML_BUFSIZE; |
| 870 | |
| 871 | if ((value = malloc(MXML_BUFSIZE)) == NULL) |
| 872 | { |
| 873 | free(name); |
| 874 | fputs("Unable to allocate memory for value!\n", stderr); |
| 875 | return (EOF); |
| 876 | } |
| 877 | |
| 878 | valsize = MXML_BUFSIZE; |
| 879 | |
| 880 | /* |
| 881 | * Loop until we hit a >, /, ?, or EOF... |
| 882 | */ |
| 883 | |
| 884 | while ((ch = (*getc_cb)(p)) != EOF) |
| 885 | { |
| 886 | #ifdef DEBUG |
| 887 | fprintf(stderr, "parse_element: ch='%c'\n", ch); |
| 888 | #endif /* DEBUG */ |
| 889 | |
| 890 | /* |
| 891 | * Skip leading whitespace... |
| 892 | */ |
| 893 | |
| 894 | if (isspace(ch)) |
| 895 | continue; |
| 896 | |
| 897 | /* |
| 898 | * Stop at /, ?, or >... |
| 899 | */ |
| 900 | |
| 901 | if (ch == '/' || ch == '?') |
| 902 | { |
| 903 | /* |
| 904 | * Grab the > character and print an error if it isn't there... |
| 905 | */ |
| 906 | |
| 907 | quote = (*getc_cb)(p); |
| 908 | |
| 909 | if (quote != '>') |
| 910 | { |
| 911 | fprintf(stderr, "Expected '>' after '%c' for element %s, but got '%c'!\n", |
| 912 | ch, node->value.element.name, quote); |
| 913 | ch = EOF; |
| 914 | } |
| 915 | |
| 916 | break; |
| 917 | } |
| 918 | else if (ch == '>') |
| 919 | break; |
| 920 | |
| 921 | /* |
| 922 | * Read the attribute name... |
| 923 | */ |
| 924 | |
| 925 | name[0] = ch; |
| 926 | ptr = name + 1; |
| 927 | |
| 928 | while ((ch = (*getc_cb)(p)) != EOF) |
| 929 | if (isspace(ch) || ch == '=' || ch == '/' || ch == '>' || ch == '?') |
| 930 | break; |
| 931 | else if (mxml_add_char(ch, &ptr, &name, &namesize)) |
| 932 | { |
| 933 | free(value); |
| 934 | return (EOF); |
| 935 | } |
| 936 | |
| 937 | *ptr = '\0'; |
| 938 | |
| 939 | if (ch == '=') |
| 940 | { |
| 941 | /* |
| 942 | * Read the attribute value... |
| 943 | */ |
| 944 | |
| 945 | if ((ch = (*getc_cb)(p)) == EOF) |
| 946 | { |
| 947 | fprintf(stderr, "Missing value for attribute '%s' in element %s!\n", |
| 948 | name, node->value.element.name); |
| 949 | free(name); |
| 950 | free(value); |
| 951 | return (EOF); |
| 952 | } |
| 953 | |
| 954 | if (ch == '\'' || ch == '\"') |
| 955 | { |
| 956 | /* |
| 957 | * Read quoted value... |
| 958 | */ |
| 959 | |
| 960 | quote = ch; |
| 961 | ptr = value; |
| 962 | |
| 963 | while ((ch = (*getc_cb)(p)) != EOF) |
| 964 | if (ch == quote) |
| 965 | break; |
| 966 | else if (mxml_add_char(ch, &ptr, &value, &valsize)) |
| 967 | { |
| 968 | free(name); |
| 969 | return (EOF); |
| 970 | } |
| 971 | |
| 972 | *ptr = '\0'; |
| 973 | } |
| 974 | else |
| 975 | { |
| 976 | /* |
| 977 | * Read unquoted value... |
| 978 | */ |
| 979 | |
| 980 | value[0] = ch; |
| 981 | ptr = value + 1; |
| 982 | |
| 983 | while ((ch = (*getc_cb)(p)) != EOF) |
| 984 | if (isspace(ch) || ch == '=' || ch == '/' || ch == '>') |
| 985 | break; |
| 986 | else if (mxml_add_char(ch, &ptr, &value, &valsize)) |
| 987 | { |
| 988 | free(name); |
| 989 | return (EOF); |
| 990 | } |
| 991 | |
| 992 | *ptr = '\0'; |
| 993 | } |
| 994 | } |
| 995 | else |
| 996 | value[0] = '\0'; |
| 997 | |
| 998 | /* |
| 999 | * Save last character in case we need it... |
| 1000 | */ |
| 1001 | |
| 1002 | if (ch == '/' || ch == '?') |
| 1003 | { |
| 1004 | /* |
| 1005 | * Grab the > character and print an error if it isn't there... |
| 1006 | */ |
| 1007 | |
| 1008 | quote = (*getc_cb)(p); |
| 1009 | |
| 1010 | if (quote != '>') |
| 1011 | { |
| 1012 | fprintf(stderr, "Expected '>' after '%c' for element %s, but got '%c'!\n", |
| 1013 | ch, node->value.element.name, quote); |
| 1014 | ch = EOF; |
| 1015 | } |
| 1016 | |
| 1017 | break; |
| 1018 | } |
| 1019 | else if (ch == '>') |
| 1020 | break; |
| 1021 | |
| 1022 | /* |
| 1023 | * Set the attribute... |
| 1024 | */ |
| 1025 | |
| 1026 | stp_mxmlElementSetAttr(node, name, value); |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Free the name and value buffers and return... |
| 1031 | */ |
| 1032 | |
| 1033 | free(name); |
| 1034 | free(value); |
| 1035 | |
| 1036 | return (ch); |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | /* |
| 1041 | * 'mxml_string_getc()' - Get a character from a string. |
| 1042 | */ |
| 1043 | |
| 1044 | static int /* O - Character or EOF */ |
| 1045 | mxml_string_getc(void *p) /* I - Pointer to file */ |
| 1046 | { |
| 1047 | int ch; /* Character */ |
| 1048 | const char **s; /* Pointer to string pointer */ |
| 1049 | |
| 1050 | |
| 1051 | s = (const char **)p; |
| 1052 | |
| 1053 | if ((ch = *s[0]) != 0) |
| 1054 | { |
| 1055 | (*s)++; |
| 1056 | return (ch); |
| 1057 | } |
| 1058 | else |
| 1059 | return (EOF); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | /* |
| 1064 | * 'mxml_string_putc()' - Write a character to a string. |
| 1065 | */ |
| 1066 | |
| 1067 | static int /* O - 0 on success, -1 on failure */ |
| 1068 | mxml_string_putc(int ch, /* I - Character to write */ |
| 1069 | void *p) /* I - Pointer to string pointers */ |
| 1070 | { |
| 1071 | char **pp; /* Pointer to string pointers */ |
| 1072 | |
| 1073 | |
| 1074 | pp = (char **)p; |
| 1075 | |
| 1076 | if (pp[0] < pp[1]) |
| 1077 | pp[0][0] = ch; |
| 1078 | |
| 1079 | pp[0] ++; |
| 1080 | |
| 1081 | return (0); |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /* |
| 1086 | * 'mxml_write_node()' - Save an XML node to a file. |
| 1087 | */ |
| 1088 | |
| 1089 | static int /* O - Column or -1 on error */ |
| 1090 | mxml_write_node(stp_mxml_node_t *node, /* I - Node to write */ |
| 1091 | void *p, /* I - File to write to */ |
| 1092 | int (*cb)(stp_mxml_node_t *, int), |
| 1093 | /* I - Whitespace callback */ |
| 1094 | int col, /* I - Current column */ |
| 1095 | int (*putc_cb)(int, void *)) |
| 1096 | { |
| 1097 | int i; /* Looping var */ |
| 1098 | stp_mxml_attr_t *attr; /* Current attribute */ |
| 1099 | char s[255]; /* Temporary string */ |
| 1100 | |
| 1101 | |
| 1102 | while (node != NULL) |
| 1103 | { |
| 1104 | /* |
| 1105 | * Print the node value... |
| 1106 | */ |
| 1107 | |
| 1108 | switch (node->type) |
| 1109 | { |
| 1110 | case STP_MXML_ELEMENT : |
| 1111 | col = mxml_write_ws(node, p, cb, STP_MXML_WS_BEFORE_OPEN, col, putc_cb); |
| 1112 | |
| 1113 | if ((*putc_cb)('<', p) < 0) |
| 1114 | return (-1); |
| 1115 | if (mxml_write_string(node->value.element.name, p, putc_cb) < 0) |
| 1116 | return (-1); |
| 1117 | |
| 1118 | col += strlen(node->value.element.name) + 1; |
| 1119 | |
| 1120 | for (i = node->value.element.num_attrs, attr = node->value.element.attrs; |
| 1121 | i > 0; |
| 1122 | i --, attr ++) |
| 1123 | { |
| 1124 | if ((col + strlen(attr->name) + strlen(attr->value) + 3) > STP_MXML_WRAP) |
| 1125 | { |
| 1126 | if ((*putc_cb)('\n', p) < 0) |
| 1127 | return (-1); |
| 1128 | |
| 1129 | col = 0; |
| 1130 | } |
| 1131 | else |
| 1132 | { |
| 1133 | if ((*putc_cb)(' ', p) < 0) |
| 1134 | return (-1); |
| 1135 | |
| 1136 | col ++; |
| 1137 | } |
| 1138 | |
| 1139 | if (mxml_write_string(attr->name, p, putc_cb) < 0) |
| 1140 | return (-1); |
| 1141 | if ((*putc_cb)('=', p) < 0) |
| 1142 | return (-1); |
| 1143 | if ((*putc_cb)('\"', p) < 0) |
| 1144 | return (-1); |
| 1145 | if (mxml_write_string(attr->value, p, putc_cb) < 0) |
| 1146 | return (-1); |
| 1147 | if ((*putc_cb)('\"', p) < 0) |
| 1148 | return (-1); |
| 1149 | |
| 1150 | col += strlen(attr->name) + strlen(attr->value) + 3; |
| 1151 | } |
| 1152 | |
| 1153 | if (node->child) |
| 1154 | { |
| 1155 | /* |
| 1156 | * The ? and ! elements are special-cases and have no end tags... |
| 1157 | */ |
| 1158 | |
| 1159 | if (node->value.element.name[0] == '?') |
| 1160 | { |
| 1161 | if ((*putc_cb)('?', p) < 0) |
| 1162 | return (-1); |
| 1163 | if ((*putc_cb)('>', p) < 0) |
| 1164 | return (-1); |
| 1165 | if ((*putc_cb)('\n', p) < 0) |
| 1166 | return (-1); |
| 1167 | |
| 1168 | col = 0; |
| 1169 | } |
| 1170 | else if ((*putc_cb)('>', p) < 0) |
| 1171 | return (-1); |
| 1172 | else |
| 1173 | col ++; |
| 1174 | |
| 1175 | col = mxml_write_ws(node, p, cb, STP_MXML_WS_AFTER_OPEN, col, putc_cb); |
| 1176 | |
| 1177 | if ((col = mxml_write_node(node->child, p, cb, col, putc_cb)) < 0) |
| 1178 | return (-1); |
| 1179 | |
| 1180 | if (node->value.element.name[0] != '?' && |
| 1181 | node->value.element.name[0] != '!') |
| 1182 | { |
| 1183 | col = mxml_write_ws(node, p, cb, STP_MXML_WS_BEFORE_CLOSE, col, putc_cb); |
| 1184 | |
| 1185 | if ((*putc_cb)('<', p) < 0) |
| 1186 | return (-1); |
| 1187 | if ((*putc_cb)('/', p) < 0) |
| 1188 | return (-1); |
| 1189 | if (mxml_write_string(node->value.element.name, p, putc_cb) < 0) |
| 1190 | return (-1); |
| 1191 | if ((*putc_cb)('>', p) < 0) |
| 1192 | return (-1); |
| 1193 | |
| 1194 | col += strlen(node->value.element.name) + 3; |
| 1195 | |
| 1196 | col = mxml_write_ws(node, p, cb, STP_MXML_WS_AFTER_CLOSE, col, putc_cb); |
| 1197 | } |
| 1198 | } |
| 1199 | else if (node->value.element.name[0] == '!') |
| 1200 | { |
| 1201 | if ((*putc_cb)('>', p) < 0) |
| 1202 | return (-1); |
| 1203 | else |
| 1204 | col ++; |
| 1205 | |
| 1206 | col = mxml_write_ws(node, p, cb, STP_MXML_WS_AFTER_OPEN, col, putc_cb); |
| 1207 | } |
| 1208 | else |
| 1209 | { |
| 1210 | if ((*putc_cb)('/', p) < 0) |
| 1211 | return (-1); |
| 1212 | if ((*putc_cb)('>', p) < 0) |
| 1213 | return (-1); |
| 1214 | |
| 1215 | col += 2; |
| 1216 | |
| 1217 | col = mxml_write_ws(node, p, cb, STP_MXML_WS_AFTER_OPEN, col, putc_cb); |
| 1218 | } |
| 1219 | break; |
| 1220 | |
| 1221 | case STP_MXML_INTEGER : |
| 1222 | if (node->prev) |
| 1223 | { |
| 1224 | if (col > STP_MXML_WRAP) |
| 1225 | { |
| 1226 | if ((*putc_cb)('\n', p) < 0) |
| 1227 | return (-1); |
| 1228 | |
| 1229 | col = 0; |
| 1230 | } |
| 1231 | else if ((*putc_cb)(' ', p) < 0) |
| 1232 | return (-1); |
| 1233 | else |
| 1234 | col ++; |
| 1235 | } |
| 1236 | |
| 1237 | sprintf(s, "%d", node->value.integer); |
| 1238 | if (mxml_write_string(s, p, putc_cb) < 0) |
| 1239 | return (-1); |
| 1240 | |
| 1241 | col += strlen(s); |
| 1242 | break; |
| 1243 | |
| 1244 | case STP_MXML_OPAQUE : |
| 1245 | if (mxml_write_string(node->value.opaque, p, putc_cb) < 0) |
| 1246 | return (-1); |
| 1247 | |
| 1248 | col += strlen(node->value.opaque); |
| 1249 | break; |
| 1250 | |
| 1251 | case STP_MXML_REAL : |
| 1252 | case STP_MXML_DIMENSION : |
| 1253 | if (node->prev) |
| 1254 | { |
| 1255 | if (col > STP_MXML_WRAP) |
| 1256 | { |
| 1257 | if ((*putc_cb)('\n', p) < 0) |
| 1258 | return (-1); |
| 1259 | |
| 1260 | col = 0; |
| 1261 | } |
| 1262 | else if ((*putc_cb)(' ', p) < 0) |
| 1263 | return (-1); |
| 1264 | else |
| 1265 | col ++; |
| 1266 | } |
| 1267 | |
| 1268 | sprintf(s, "%f", node->value.real); |
| 1269 | if (mxml_write_string(s, p, putc_cb) < 0) |
| 1270 | return (-1); |
| 1271 | |
| 1272 | col += strlen(s); |
| 1273 | break; |
| 1274 | |
| 1275 | case STP_MXML_TEXT : |
| 1276 | if (node->value.text.whitespace && col > 0) |
| 1277 | { |
| 1278 | if (col > STP_MXML_WRAP) |
| 1279 | { |
| 1280 | if ((*putc_cb)('\n', p) < 0) |
| 1281 | return (-1); |
| 1282 | |
| 1283 | col = 0; |
| 1284 | } |
| 1285 | else if ((*putc_cb)(' ', p) < 0) |
| 1286 | return (-1); |
| 1287 | else |
| 1288 | col ++; |
| 1289 | } |
| 1290 | |
| 1291 | if (mxml_write_string(node->value.text.string, p, putc_cb) < 0) |
| 1292 | return (-1); |
| 1293 | |
| 1294 | col += strlen(node->value.text.string); |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * Next node... |
| 1300 | */ |
| 1301 | |
| 1302 | node = node->next; |
| 1303 | } |
| 1304 | |
| 1305 | return (col); |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | /* |
| 1310 | * 'mxml_write_string()' - Write a string, escaping & and < as needed. |
| 1311 | */ |
| 1312 | |
| 1313 | static int /* O - 0 on success, -1 on failure */ |
| 1314 | mxml_write_string(const char *s, /* I - String to write */ |
| 1315 | void *p, /* I - Write pointer */ |
| 1316 | int (*putc_cb)(int, void *)) |
| 1317 | /* I - Write callback */ |
| 1318 | { |
| 1319 | char buf[255], /* Buffer */ |
| 1320 | *bufptr; /* Pointer into buffer */ |
| 1321 | |
| 1322 | |
| 1323 | while (*s) |
| 1324 | { |
| 1325 | if (*s == '&') |
| 1326 | { |
| 1327 | if ((*putc_cb)('&', p) < 0) |
| 1328 | return (-1); |
| 1329 | if ((*putc_cb)('a', p) < 0) |
| 1330 | return (-1); |
| 1331 | if ((*putc_cb)('m', p) < 0) |
| 1332 | return (-1); |
| 1333 | if ((*putc_cb)('p', p) < 0) |
| 1334 | return (-1); |
| 1335 | if ((*putc_cb)(';', p) < 0) |
| 1336 | return (-1); |
| 1337 | } |
| 1338 | else if (*s == '<') |
| 1339 | { |
| 1340 | if ((*putc_cb)('&', p) < 0) |
| 1341 | return (-1); |
| 1342 | if ((*putc_cb)('l', p) < 0) |
| 1343 | return (-1); |
| 1344 | if ((*putc_cb)('t', p) < 0) |
| 1345 | return (-1); |
| 1346 | if ((*putc_cb)(';', p) < 0) |
| 1347 | return (-1); |
| 1348 | } |
| 1349 | else if (*s == '>') |
| 1350 | { |
| 1351 | if ((*putc_cb)('&', p) < 0) |
| 1352 | return (-1); |
| 1353 | if ((*putc_cb)('g', p) < 0) |
| 1354 | return (-1); |
| 1355 | if ((*putc_cb)('t', p) < 0) |
| 1356 | return (-1); |
| 1357 | if ((*putc_cb)(';', p) < 0) |
| 1358 | return (-1); |
| 1359 | } |
| 1360 | else if (*s == '\"') |
| 1361 | { |
| 1362 | if ((*putc_cb)('&', p) < 0) |
| 1363 | return (-1); |
| 1364 | if ((*putc_cb)('q', p) < 0) |
| 1365 | return (-1); |
| 1366 | if ((*putc_cb)('u', p) < 0) |
| 1367 | return (-1); |
| 1368 | if ((*putc_cb)('o', p) < 0) |
| 1369 | return (-1); |
| 1370 | if ((*putc_cb)('t', p) < 0) |
| 1371 | return (-1); |
| 1372 | if ((*putc_cb)(';', p) < 0) |
| 1373 | return (-1); |
| 1374 | } |
| 1375 | else if (*s & 128) |
| 1376 | { |
| 1377 | /* |
| 1378 | * Convert UTF-8 to Unicode constant... |
| 1379 | */ |
| 1380 | |
| 1381 | int ch; /* Unicode character */ |
| 1382 | |
| 1383 | |
| 1384 | ch = *s & 255; |
| 1385 | |
| 1386 | if ((ch & 0xe0) == 0xc0) |
| 1387 | { |
| 1388 | ch = ((ch & 0x1f) << 6) | (s[1] & 0x3f); |
| 1389 | s ++; |
| 1390 | } |
| 1391 | else if ((ch & 0xf0) == 0xe0) |
| 1392 | { |
| 1393 | ch = ((((ch * 0x0f) << 6) | (s[1] & 0x3f)) << 6) | (s[2] & 0x3f); |
| 1394 | s += 2; |
| 1395 | } |
| 1396 | |
| 1397 | if (ch == 0xa0) |
| 1398 | { |
| 1399 | /* |
| 1400 | * Handle non-breaking space as-is... |
| 1401 | */ |
| 1402 | |
| 1403 | if ((*putc_cb)('&', p) < 0) |
| 1404 | return (-1); |
| 1405 | if ((*putc_cb)('n', p) < 0) |
| 1406 | return (-1); |
| 1407 | if ((*putc_cb)('b', p) < 0) |
| 1408 | return (-1); |
| 1409 | if ((*putc_cb)('s', p) < 0) |
| 1410 | return (-1); |
| 1411 | if ((*putc_cb)('p', p) < 0) |
| 1412 | return (-1); |
| 1413 | if ((*putc_cb)(';', p) < 0) |
| 1414 | return (-1); |
| 1415 | } |
| 1416 | else |
| 1417 | { |
| 1418 | sprintf(buf, "&#x%x;", ch); |
| 1419 | |
| 1420 | for (bufptr = buf; *bufptr; bufptr ++) |
| 1421 | if ((*putc_cb)(*bufptr, p) < 0) |
| 1422 | return (-1); |
| 1423 | } |
| 1424 | } |
| 1425 | else if ((*putc_cb)(*s, p) < 0) |
| 1426 | return (-1); |
| 1427 | |
| 1428 | s ++; |
| 1429 | } |
| 1430 | |
| 1431 | return (0); |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | /* |
| 1436 | * 'mxml_write_ws()' - Do whitespace callback... |
| 1437 | */ |
| 1438 | |
| 1439 | static int /* O - New column */ |
| 1440 | mxml_write_ws(stp_mxml_node_t *node, /* I - Current node */ |
| 1441 | void *p, /* I - Write pointer */ |
| 1442 | int (*cb)(stp_mxml_node_t *, int), |
| 1443 | /* I - Callback function */ |
| 1444 | int ws, /* I - Where value */ |
| 1445 | int col, /* I - Current column */ |
| 1446 | int (*putc_cb)(int, void *)) |
| 1447 | /* I - Write callback */ |
| 1448 | { |
| 1449 | int ch; /* Whitespace character */ |
| 1450 | |
| 1451 | |
| 1452 | if (cb && (ch = (*cb)(node, ws)) != 0) |
| 1453 | { |
| 1454 | if ((*putc_cb)(ch, p) < 0) |
| 1455 | return (-1); |
| 1456 | else if (ch == '\n') |
| 1457 | col = 0; |
| 1458 | else if (ch == '\t') |
| 1459 | { |
| 1460 | col += STP_MXML_TAB; |
| 1461 | col = col - (col % STP_MXML_TAB); |
| 1462 | } |
| 1463 | else |
| 1464 | col ++; |
| 1465 | } |
| 1466 | |
| 1467 | return (col); |
| 1468 | } |