mxml-set.c

Go to the documentation of this file.
00001 /*
00002  * "$Id: mxml-set.c 270 2007-04-23 21:48:03Z mike $"
00003  *
00004  * Node set functions for Mini-XML, a small XML-like file parsing library.
00005  *
00006  * Copyright 2003-2007 by Michael Sweet.
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * Contents:
00019  *
00020  *   mxmlSetCustom()  - Set the data and destructor of a custom data node.
00021  *   mxmlSetCDATA()   - Set the element name of a CDATA node.
00022  *   mxmlSetElement() - Set the name of an element node.
00023  *   mxmlSetInteger() - Set the value of an integer node.
00024  *   mxmlSetOpaque()  - Set the value of an opaque node.
00025  *   mxmlSetReal()    - Set the value of a real number node.
00026  *   mxmlSetText()    - Set the value of a text node.
00027  *   mxmlSetTextf()   - Set the value of a text node to a formatted string.
00028  */
00029 
00030 /*
00031  * Include necessary headers...
00032  */
00033 
00034 #include "config.h"
00035 #include "mxml.h"
00036 
00037 
00038 /*
00039  * 'mxmlSetCustom()' - Set the data and destructor of a custom data node.
00040  *
00041  * The node is not changed if it is not a custom node.
00042  *
00043  * @since Mini-XML 2.1@
00044  */
00045 
00046 int                 /* O - 0 on success, -1 on failure */
00047 mxmlSetCustom(
00048     mxml_node_t              *node, /* I - Node to set */
00049     void                     *data, /* I - New data pointer */
00050     mxml_custom_destroy_cb_t destroy)   /* I - New destructor function */
00051 {
00052  /*
00053   * Range check input...
00054   */
00055 
00056   if (!node || node->type != MXML_CUSTOM)
00057     return (-1);
00058 
00059  /*
00060   * Free any old element value and set the new value...
00061   */
00062 
00063   if (node->value.custom.data && node->value.custom.destroy)
00064     (*(node->value.custom.destroy))(node->value.custom.data);
00065 
00066   node->value.custom.data    = data;
00067   node->value.custom.destroy = destroy;
00068 
00069   return (0);
00070 }
00071 
00072 
00073 /*
00074  * 'mxmlSetCDATA()' - Set the element name of a CDATA node.
00075  *
00076  * The node is not changed if it is not a CDATA element node.
00077  *
00078  * @since Mini-XML 2.3@
00079  */
00080 
00081 int                 /* O - 0 on success, -1 on failure */
00082 mxmlSetCDATA(mxml_node_t *node,     /* I - Node to set */
00083              const char  *data)     /* I - New data string */
00084 {
00085  /*
00086   * Range check input...
00087   */
00088 
00089   if (!node || node->type != MXML_ELEMENT || !data ||
00090       strncmp(node->value.element.name, "![CDATA[", 8))
00091     return (-1);
00092 
00093  /*
00094   * Free any old element value and set the new value...
00095   */
00096 
00097   if (node->value.element.name)
00098     free(node->value.element.name);
00099 
00100   node->value.element.name = _mxml_strdupf("![CDATA[%s]]", data);
00101 
00102   return (0);
00103 }
00104 
00105 
00106 /*
00107  * 'mxmlSetElement()' - Set the name of an element node.
00108  *
00109  * The node is not changed if it is not an element node.
00110  */
00111 
00112 int                 /* O - 0 on success, -1 on failure */
00113 mxmlSetElement(mxml_node_t *node,   /* I - Node to set */
00114                const char  *name)   /* I - New name string */
00115 {
00116  /*
00117   * Range check input...
00118   */
00119 
00120   if (!node || node->type != MXML_ELEMENT || !name)
00121     return (-1);
00122 
00123  /*
00124   * Free any old element value and set the new value...
00125   */
00126 
00127   if (node->value.element.name)
00128     free(node->value.element.name);
00129 
00130   node->value.element.name = strdup(name);
00131 
00132   return (0);
00133 }
00134 
00135 
00136 /*
00137  * 'mxmlSetInteger()' - Set the value of an integer node.
00138  *
00139  * The node is not changed if it is not an integer node.
00140  */
00141 
00142 int                 /* O - 0 on success, -1 on failure */
00143 mxmlSetInteger(mxml_node_t *node,   /* I - Node to set */
00144                int         integer) /* I - Integer value */
00145 {
00146  /*
00147   * Range check input...
00148   */
00149 
00150   if (!node || node->type != MXML_INTEGER)
00151     return (-1);
00152 
00153  /*
00154   * Set the new value and return...
00155   */
00156 
00157   node->value.integer = integer;
00158 
00159   return (0);
00160 }
00161 
00162 
00163 /*
00164  * 'mxmlSetOpaque()' - Set the value of an opaque node.
00165  *
00166  * The node is not changed if it is not an opaque node.
00167  */
00168 
00169 int                 /* O - 0 on success, -1 on failure */
00170 mxmlSetOpaque(mxml_node_t *node,    /* I - Node to set */
00171               const char  *opaque)  /* I - Opaque string */
00172 {
00173  /*
00174   * Range check input...
00175   */
00176 
00177   if (!node || node->type != MXML_OPAQUE || !opaque)
00178     return (-1);
00179 
00180  /*
00181   * Free any old opaque value and set the new value...
00182   */
00183 
00184   if (node->value.opaque)
00185     free(node->value.opaque);
00186 
00187   node->value.opaque = strdup(opaque);
00188 
00189   return (0);
00190 }
00191 
00192 
00193 /*
00194  * 'mxmlSetReal()' - Set the value of a real number node.
00195  *
00196  * The node is not changed if it is not a real number node.
00197  */
00198 
00199 int                 /* O - 0 on success, -1 on failure */
00200 mxmlSetReal(mxml_node_t *node,      /* I - Node to set */
00201             double      real)       /* I - Real number value */
00202 {
00203  /*
00204   * Range check input...
00205   */
00206 
00207   if (!node || node->type != MXML_REAL)
00208     return (-1);
00209 
00210  /*
00211   * Set the new value and return...
00212   */
00213 
00214   node->value.real = real;
00215 
00216   return (0);
00217 }
00218 
00219 
00220 /*
00221  * 'mxmlSetText()' - Set the value of a text node.
00222  *
00223  * The node is not changed if it is not a text node.
00224  */
00225 
00226 int                 /* O - 0 on success, -1 on failure */
00227 mxmlSetText(mxml_node_t *node,      /* I - Node to set */
00228             int         whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
00229         const char  *string)    /* I - String */
00230 {
00231  /*
00232   * Range check input...
00233   */
00234 
00235   if (!node || node->type != MXML_TEXT || !string)
00236     return (-1);
00237 
00238  /*
00239   * Free any old string value and set the new value...
00240   */
00241 
00242   if (node->value.text.string)
00243     free(node->value.text.string);
00244 
00245   node->value.text.whitespace = whitespace;
00246   node->value.text.string     = strdup(string);
00247 
00248   return (0);
00249 }
00250 
00251 
00252 /*
00253  * 'mxmlSetTextf()' - Set the value of a text node to a formatted string.
00254  *
00255  * The node is not changed if it is not a text node.
00256  */
00257 
00258 int                 /* O - 0 on success, -1 on failure */
00259 mxmlSetTextf(mxml_node_t *node,     /* I - Node to set */
00260              int         whitespace,    /* I - 1 = leading whitespace, 0 = no whitespace */
00261              const char  *format,   /* I - Printf-style format string */
00262          ...)           /* I - Additional arguments as needed */
00263 {
00264   va_list   ap;         /* Pointer to arguments */
00265 
00266 
00267  /*
00268   * Range check input...
00269   */
00270 
00271   if (!node || node->type != MXML_TEXT || !format)
00272     return (-1);
00273 
00274  /*
00275   * Free any old string value and set the new value...
00276   */
00277 
00278   if (node->value.text.string)
00279     free(node->value.text.string);
00280 
00281   va_start(ap, format);
00282 
00283   node->value.text.whitespace = whitespace;
00284   node->value.text.string     = _mxml_strdupf(format, ap);
00285 
00286   va_end(ap);
00287 
00288   return (0);
00289 }

Generated by  doxygen 1.6.2