diff --git a/CHANGES b/CHANGES index b8afdd9..4a52b0e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,4 @@ -README - 07/21/2003 +README - 07/22/2003 ------------------- CHANGES IN Mini-XML 1.1.1 @@ -9,6 +9,8 @@ CHANGES IN Mini-XML 1.1.1 - The private mxml_write_ws() function called putc() instead of using the proper callback which could cause a crash when using mxmlSaveString(). + - Added a mxmlSaveAllocString() convenience function for + saving an XML node tree to an allocated string. CHANGES IN Mini-XML 1.1 diff --git a/README b/README index d209a79..4612651 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -README - 07/21/2003 +README - 07/22/2003 ------------------- @@ -107,6 +107,23 @@ DOCUMENTATION mxmlSaveFile(tree, fp, MXML_NO_CALLBACK); fclose(fp); + The "mxmlLoadString()", "mxmlSaveAllocString()", and + "mxmlSaveString()" functions load XML node trees from and + save XML node trees to strings: + + char buffer[8192]; + char *ptr; + mxml_node_t *tree; + + ... + tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK); + + ... + mxmlSaveString(tree, buffer, sizeof(buffer), MXML_NO_CALLBACK); + + ... + ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + You can find a named element/node using the "mxmlFindElement()" function: diff --git a/documentation.html b/documentation.html index cfb367d..9f30908 100644 --- a/documentation.html +++ b/documentation.html @@ -49,6 +49,7 @@
  • mxmlNewReal()
  • mxmlNewText()
  • mxmlRemove()
  • +
  • mxmlSaveAllocString()
  • mxmlSaveFile()
  • mxmlSaveString()
  • mxmlWalkNext()
  • @@ -386,6 +387,31 @@ mxmlRemove(

    Returns

    Nothing.


    +

    mxmlSaveAllocString()

    +

    Save an XML node tree to an allocated string. + +This function returns a pointer to a string containing the textual +representation of the XML node tree. The string should be freed +using the free() function when you are done with it. NULL is returned +if the node would produce an empty string or if the string cannot be +allocated.

    +

    Syntax

    +
    +char *
    +mxmlSaveAllocString(
    +    mxml_node_t * node,
    +    int (*cb)(mxml_node_t *int));
    +
    +

    Arguments

    +

    + + + + +
    NameDescription
    nodeNode to write
    (*cb)(mxml_node_t *int)Whitespace callback or MXML_NO_CALLBACK

    +

    Returns

    +

    Allocated string or NULL

    +

    mxmlSaveFile()

    Save an XML tree to a file. diff --git a/index.html b/index.html index b00610e..fc2d1cd 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@ href="../index.html">Back to Home Page ]

    Mini-XML Home Page

    -

    Current Release: v1.1.1, July 21, 2003
    +

    Current Release: v1.1.1, July 22, 2003
    Download Source (.tar.gz 70k) | +

    The mxmlLoadString(), +mxmlSaveAllocString(), +and mxmlSaveString() +functions load XML node trees from and save XML node trees to +strings:

    + +
    +char buffer[8192];
    +char *ptr;
    +mxml_node_t *tree;
    +
    +...
    +tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
    +
    +...
    +mxmlSaveString(tree, buffer, sizeof(buffer), MXML_NO_CALLBACK);
    +
    +...
    +ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
    +
    +

    You can find a named element/node using the mxmlFindElement() function:

    diff --git a/mxml-file.c b/mxml-file.c index 520f6f9..42738e3 100644 --- a/mxml-file.c +++ b/mxml-file.c @@ -1,5 +1,5 @@ /* - * "$Id: mxml-file.c,v 1.16 2003/07/21 12:41:47 mike Exp $" + * "$Id: mxml-file.c,v 1.17 2003/07/22 10:29:19 mike Exp $" * * File loading code for mini-XML, a small XML-like file parsing library. * @@ -17,18 +17,19 @@ * * Contents: * - * mxmlLoadFile() - Load a file into an XML node tree. - * mxmlLoadString() - Load a string into an XML node tree. - * mxmlSaveFile() - Save an XML tree to a file. - * mxmlSaveString() - Save an XML node tree to a string. - * mxml_add_char() - Add a character to a buffer, expanding as needed. - * mxml_file_getc() - Get a character from a file. - * mxml_load_data() - Load data into an XML node tree. - * mxml_parse_element() - Parse an element for any attributes... - * mxml_string_getc() - Get a character from a string. - * mxml_write_node() - Save an XML node to a file. - * mxml_write_string() - Write a string, escaping & and < as needed. - * mxml_write_ws() - Do whitespace callback... + * mxmlLoadFile() - Load a file into an XML node tree. + * mxmlLoadString() - Load a string into an XML node tree. + * mxmlSaveAllocString() - Save an XML node tree to an allocated string. + * mxmlSaveFile() - Save an XML tree to a file. + * mxmlSaveString() - Save an XML node tree to a string. + * mxml_add_char() - Add a character to a buffer, expanding as needed. + * mxml_file_getc() - Get a character from a file. + * mxml_load_data() - Load data into an XML node tree. + * mxml_parse_element() - Parse an element for any attributes... + * mxml_string_getc() - Get a character from a string. + * mxml_write_node() - Save an XML node to a file. + * mxml_write_string() - Write a string, escaping & and < as needed. + * mxml_write_ws() - Do whitespace callback... */ /* @@ -107,6 +108,63 @@ mxmlLoadString(mxml_node_t *top, /* I - Top node */ } +/* + * 'mxmlSaveAllocString()' - Save an XML node tree to an allocated string. + * + * This function returns a pointer to a string containing the textual + * representation of the XML node tree. The string should be freed + * using the free() function when you are done with it. NULL is returned + * if the node would produce an empty string or if the string cannot be + * allocated. + */ + +char * /* O - Allocated string or NULL */ +mxmlSaveAllocString(mxml_node_t *node, /* I - Node to write */ + int (*cb)(mxml_node_t *, int)) + /* I - Whitespace callback or MXML_NO_CALLBACK */ +{ + int bytes; /* Required bytes */ + char buffer[8192]; /* Temporary buffer */ + char *s; /* Allocated string */ + + + /* + * Write the node to the temporary buffer... + */ + + bytes = mxmlSaveString(node, buffer, sizeof(buffer), cb); + + if (bytes <= 0) + return (NULL); + + if (bytes < (int)(sizeof(buffer) - 1)) + { + /* + * Node fit inside the buffer, so just duplicate that string and + * return... + */ + + return (strdup(buffer)); + } + + /* + * Allocate a buffer of the required size and save the node to the + * new buffer... + */ + + if ((s = malloc(bytes + 1)) == NULL) + return (NULL); + + mxmlSaveString(node, s, bytes + 1, cb); + + /* + * Return the allocated string... + */ + + return (s); +} + + /* * 'mxmlSaveFile()' - Save an XML tree to a file. * @@ -1374,5 +1432,5 @@ mxml_write_ws(mxml_node_t *node, /* I - Current node */ /* - * End of "$Id: mxml-file.c,v 1.16 2003/07/21 12:41:47 mike Exp $". + * End of "$Id: mxml-file.c,v 1.17 2003/07/22 10:29:19 mike Exp $". */ diff --git a/mxml.h b/mxml.h index 9e8174c..52cdbcf 100644 --- a/mxml.h +++ b/mxml.h @@ -1,5 +1,5 @@ /* - * "$Id: mxml.h,v 1.11 2003/06/19 03:39:23 mike Exp $" + * "$Id: mxml.h,v 1.12 2003/07/22 10:29:19 mike Exp $" * * Header file for mini-XML, a small XML-like file parsing library. * @@ -145,6 +145,8 @@ extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real); extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace, const char *string); extern void mxmlRemove(mxml_node_t *node); +extern char *mxmlSaveAllocString(mxml_node_t *node, + int (*cb)(mxml_node_t *, int)); extern int mxmlSaveFile(mxml_node_t *node, FILE *fp, int (*cb)(mxml_node_t *, int)); extern int mxmlSaveString(mxml_node_t *node, char *buffer, @@ -167,5 +169,5 @@ extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top, /* - * End of "$Id: mxml.h,v 1.11 2003/06/19 03:39:23 mike Exp $". + * End of "$Id: mxml.h,v 1.12 2003/07/22 10:29:19 mike Exp $". */ diff --git a/mxml.xml b/mxml.xml index ba12387..b109e8a 100644 --- a/mxml.xml +++ b/mxml.xml @@ -144,6 +144,18 @@ Does not free memory used by the node - use mxmlDelete() for that. This function does nothing if the node has no parent.mxml_node_t *Node to remove +Allocated string or NULLchar +* +Save an XML node tree to an allocated string. + +This function returns a pointer to a string containing the textual +representation of the XML node tree. The string should be freed +using the free() function when you are done with it. NULL is returned +if the node would produce an empty string or if the string cannot be +allocated.mxml_node_t *Node to write +intWhitespace callback or MXML_NO_CALLBACK + 0 on success, -1 on error.int Save an XML tree to a file.