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