Add mxmlSaveAllocString() function.

web
Michael R Sweet 21 years ago
parent 22e5108877
commit cc71dd6baf
  1. 4
      CHANGES
  2. 19
      README
  3. 26
      documentation.html
  4. 26
      index.html
  5. 86
      mxml-file.c
  6. 6
      mxml.h
  7. 12
      mxml.xml

@ -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

@ -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:

@ -49,6 +49,7 @@
<li><a href="#mxmlNewReal"><tt>mxmlNewReal()</tt></a></li>
<li><a href="#mxmlNewText"><tt>mxmlNewText()</tt></a></li>
<li><a href="#mxmlRemove"><tt>mxmlRemove()</tt></a></li>
<li><a href="#mxmlSaveAllocString"><tt>mxmlSaveAllocString()</tt></a></li>
<li><a href="#mxmlSaveFile"><tt>mxmlSaveFile()</tt></a></li>
<li><a href="#mxmlSaveString"><tt>mxmlSaveString()</tt></a></li>
<li><a href="#mxmlWalkNext"><tt>mxmlWalkNext()</tt></a></li>
@ -386,6 +387,31 @@ mxmlRemove(
<h3>Returns</h3>
<p>Nothing.</p>
<hr noshade/>
<h2><a name="mxmlSaveAllocString">mxmlSaveAllocString()</a></h2>
<p>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.</p>
<h3>Syntax</h3>
<pre>
char *
mxmlSaveAllocString(
<a href="#mxml_node_t">mxml_node_t</a> * node,
int (*cb)(mxml_node_t *int));
</pre>
<h3>Arguments</h3>
<p class="table"><table align="center" border="1" width="80%">
<thead><tr><th>Name</th><th>Description</th></tr></thead>
<tbody>
<tr><td><tt>node</tt></td><td>Node to write</td></tr>
<tr><td><tt>(*cb)(mxml_node_t *int)</tt></td><td>Whitespace callback or MXML_NO_CALLBACK</td></tr>
</tbody></table></p>
<h3>Returns</h3>
<p>Allocated string or NULL</p>
<hr noshade/>
<h2><a name="mxmlSaveFile">mxmlSaveFile()</a></h2>
<p>Save an XML tree to a file.

@ -16,7 +16,7 @@ href="../index.html">Back to Home Page</a>&nbsp;]</p>
<h1 class="title" align="center">Mini-XML Home Page</h1>
<p class="title" align="center">Current Release: v1.1.1, July 21, 2003<br/>
<p class="title" align="center">Current Release: v1.1.1, July 22, 2003<br/>
[&nbsp;<a
href="mxml-1.1.1.tar.gz">Download&nbsp;Source&nbsp;(.tar.gz&nbsp;70k)</a>
| <a
@ -155,6 +155,30 @@ mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
fclose(fp);
</pre>
<p>The <a
href="documentation.html#mxmlLoadString"><tt>mxmlLoadString()</tt></a>,
<a
href="documentation.html#mxmlSaveAllocString"><tt>mxmlSaveAllocString()</tt></a>,
and <a
href="documentation.html#mxmlSaveString"><tt>mxmlSaveString()</tt></a>
functions load XML node trees from and save XML node trees to
strings:</p>
<pre>
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);
</pre>
<p>You can find a named element/node using the <a
href="documentation.html#mxmlFindElement"><tt>mxmlFindElement()</tt></a>
function:</p>

@ -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 $".
*/

@ -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 $".
*/

@ -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.</description><argument
name="node" direction="I"><type>mxml_node_t *</type><description>Node to remove</description></argument>
</function>
<function name="mxmlSaveAllocString"><returnvalue><description>Allocated string or NULL</description><type>char
*</type></returnvalue>
<description>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.</description><argument
name="node" direction="I"><type>mxml_node_t *</type><description>Node to write</description></argument>
<argument name="(*cb)(mxml_node_t *int)" direction="I"><type>int</type><description>Whitespace callback or MXML_NO_CALLBACK</description></argument>
</function>
<function name="mxmlSaveFile"><returnvalue><description>0 on success, -1 on error.</description><type>int</type></returnvalue>
<description>Save an XML tree to a file.

Loading…
Cancel
Save