00001 /* SVN FILE INFO 00002 * $Revision: 531 $ : Last Committed Revision 00003 * $Date: 2010-06-17 10:39:34 -0700 (Thu, 17 Jun 2010) $ : 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 #ifdef _WIN32 00036 #include "winconfig.h" 00037 #else 00038 #include "config.h" 00039 #endif 00040 #include "mxml.h" 00041 00042 00043 /* 00044 * 'mxmlSetCustom()' - Set the data and destructor of a custom data node. 00045 * 00046 * The node is not changed if it is not a custom node. 00047 */ 00048 00049 int /* O - 0 on success, -1 on failure */ 00050 mxmlSetCustom(mxml_node_t *node, /* I - Node to set */ 00051 void *data, /* I - New data pointer */ 00052 void (*destroy)(void *)) 00053 /* I - New destructor function */ 00054 { 00055 /* 00056 * Range check input... 00057 */ 00058 00059 if (!node || node->type != MXML_CUSTOM) 00060 return (-1); 00061 00062 /* 00063 * Free any old element value and set the new value... 00064 */ 00065 00066 if (node->value.custom.data && node->value.custom.destroy) 00067 (*(node->value.custom.destroy))(node->value.custom.data); 00068 00069 node->value.custom.data = data; 00070 node->value.custom.destroy = destroy; 00071 00072 return (0); 00073 } 00074 00075 00076 /* 00077 * 'mxmlSetElement()' - Set the name of an element node. 00078 * 00079 * The node is not changed if it is not an element node. 00080 */ 00081 00082 int /* O - 0 on success, -1 on failure */ 00083 mxmlSetElement(mxml_node_t *node, /* I - Node to set */ 00084 const char *name) /* I - New name string */ 00085 { 00086 /* 00087 * Range check input... 00088 */ 00089 00090 if (!node || node->type != MXML_ELEMENT || !name) 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 = strdup(name); 00101 00102 return (0); 00103 } 00104 00105 00106 /* 00107 * 'mxmlSetInteger()' - Set the value of an integer node. 00108 * 00109 * The node is not changed if it is not an integer node. 00110 */ 00111 00112 int /* O - 0 on success, -1 on failure */ 00113 mxmlSetInteger(mxml_node_t *node, /* I - Node to set */ 00114 int integer) /* I - Integer value */ 00115 { 00116 /* 00117 * Range check input... 00118 */ 00119 00120 if (!node || node->type != MXML_INTEGER) 00121 return (-1); 00122 00123 /* 00124 * Set the new value and return... 00125 */ 00126 00127 node->value.integer = integer; 00128 00129 return (0); 00130 } 00131 00132 00133 /* 00134 * 'mxmlSetOpaque()' - Set the value of an opaque node. 00135 * 00136 * The node is not changed if it is not an opaque node. 00137 */ 00138 00139 int /* O - 0 on success, -1 on failure */ 00140 mxmlSetOpaque(mxml_node_t *node, /* I - Node to set */ 00141 const char *opaque) /* I - Opaque string */ 00142 { 00143 /* 00144 * Range check input... 00145 */ 00146 00147 if (!node || node->type != MXML_OPAQUE || !opaque) 00148 return (-1); 00149 00150 /* 00151 * Free any old opaque value and set the new value... 00152 */ 00153 00154 if (node->value.opaque) 00155 free(node->value.opaque); 00156 00157 node->value.opaque = strdup(opaque); 00158 00159 return (0); 00160 } 00161 00162 00163 /* 00164 * 'mxmlSetReal()' - Set the value of a real number node. 00165 * 00166 * The node is not changed if it is not a real number node. 00167 */ 00168 00169 int /* O - 0 on success, -1 on failure */ 00170 mxmlSetReal(mxml_node_t *node, /* I - Node to set */ 00171 double real) /* I - Real number value */ 00172 { 00173 /* 00174 * Range check input... 00175 */ 00176 00177 if (!node || node->type != MXML_REAL) 00178 return (-1); 00179 00180 /* 00181 * Set the new value and return... 00182 */ 00183 00184 node->value.real = real; 00185 00186 return (0); 00187 } 00188 00189 00190 /* 00191 * 'mxmlSetText()' - Set the value of a text node. 00192 * 00193 * The node is not changed if it is not a text node. 00194 */ 00195 00196 int /* O - 0 on success, -1 on failure */ 00197 mxmlSetText(mxml_node_t *node, /* I - Node to set */ 00198 int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */ 00199 const char *string) /* I - String */ 00200 { 00201 /* 00202 * Range check input... 00203 */ 00204 00205 if (!node || node->type != MXML_TEXT || !string) 00206 return (-1); 00207 00208 /* 00209 * Free any old string value and set the new value... 00210 */ 00211 00212 if (node->value.text.string) 00213 free(node->value.text.string); 00214 00215 node->value.text.whitespace = whitespace; 00216 node->value.text.string = strdup(string); 00217 00218 return (0); 00219 } 00220 00221 00222 /* 00223 * 'mxmlSetTextf()' - Set the value of a text node to a formatted string. 00224 * 00225 * The node is not changed if it is not a text node. 00226 */ 00227 00228 int /* O - 0 on success, -1 on failure */ 00229 mxmlSetTextf(mxml_node_t *node, /* I - Node to set */ 00230 int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */ 00231 const char *format, /* I - Printf-style format string */ 00232 ...) /* I - Additional arguments as needed */ 00233 { 00234 va_list ap; /* Pointer to arguments */ 00235 00236 00237 /* 00238 * Range check input... 00239 */ 00240 00241 if (!node || node->type != MXML_TEXT || !format) 00242 return (-1); 00243 00244 /* 00245 * Free any old string value and set the new value... 00246 */ 00247 00248 if (node->value.text.string) 00249 free(node->value.text.string); 00250 00251 va_start(ap, format); 00252 00253 node->value.text.whitespace = whitespace; 00254 node->value.text.string = mxml_strdupf(format, ap); 00255 00256 va_end(ap); 00257 00258 return (0); 00259 } 00260 00261 00262 /* 00263 * End of "$Id: mxml-set.c,v 1.1 2007/05/23 20:43:28 david_ko Exp $". 00264 */