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