00001 /* 00002 * WBXML Lib, the WBXML Library. 00003 * Copyright (C) 2002-2003 Aymerick Jéhanne 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License (version 2.1) as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * LGPL v2.1: http://www.gnu.org/licenses/lgpl.txt 00019 * 00020 * Author Contact: libwbxml@jehanne.org 00021 * WBXML Lib home: http://libwbxml.jehanne.org 00022 */ 00023 00034 #include <string.h> 00035 00036 #include "wbxml.h" 00037 00038 00040 #define WBXML_ELT_UNKNOWN_NAME ((WB_UTINY *)"unknown") 00041 00042 00043 00044 /*************************************************** 00045 * Public Functions 00046 */ 00047 00048 /* WBXMLTag */ 00049 00050 WBXML_DECLARE(WBXMLTag *) wbxml_tag_create(WBXMLValueType type) 00051 { 00052 WBXMLTag *result = NULL; 00053 00054 if ((result = (WBXMLTag *) wbxml_malloc(sizeof(WBXMLTag))) == NULL) 00055 return NULL; 00056 00057 result->type = type; 00058 result->u.token = NULL; 00059 result->u.literal = NULL; 00060 00061 return result; 00062 } 00063 00064 00065 WBXML_DECLARE(WBXMLTag *) wbxml_tag_create_token(const WBXMLTagEntry *value) 00066 { 00067 WBXMLTag *result = NULL; 00068 00069 if ((result = wbxml_tag_create(WBXML_VALUE_TOKEN)) == NULL) 00070 return NULL; 00071 00072 result->u.token = value; 00073 00074 return result; 00075 } 00076 00077 00078 WBXML_DECLARE(WBXMLTag *) wbxml_tag_create_literal(WB_UTINY *value) 00079 { 00080 WBXMLTag *result = NULL; 00081 00082 if ((result = wbxml_tag_create(WBXML_VALUE_LITERAL)) == NULL) 00083 return NULL; 00084 00085 if (value == NULL) 00086 result->u.literal = NULL; 00087 else { 00088 result->u.literal = wbxml_buffer_create(value, WBXML_STRLEN(value), WBXML_STRLEN(value)); 00089 if (result->u.literal == NULL) { 00090 wbxml_tag_destroy(result); 00091 return NULL; 00092 } 00093 } 00094 00095 return result; 00096 } 00097 00098 00099 WBXML_DECLARE(void) wbxml_tag_destroy(WBXMLTag *tag) 00100 { 00101 if (tag == NULL) 00102 return; 00103 00104 if (tag->type == WBXML_VALUE_LITERAL) 00105 wbxml_buffer_destroy(tag->u.literal); 00106 00107 wbxml_free(tag); 00108 } 00109 00110 00111 WBXML_DECLARE(WBXMLTag *) wbxml_tag_duplicate(WBXMLTag *tag) 00112 { 00113 WBXMLTag *result = NULL; 00114 00115 if (tag == NULL) 00116 return NULL; 00117 00118 if ((result = (WBXMLTag *) wbxml_malloc(sizeof(WBXMLTag))) == NULL) 00119 return NULL; 00120 00121 result->type = tag->type; 00122 00123 switch (result->type) { 00124 case WBXML_VALUE_TOKEN: 00125 result->u.token = tag->u.token; 00126 break; 00127 case WBXML_VALUE_LITERAL: 00128 result->u.literal = wbxml_buffer_duplicate(tag->u.literal); 00129 break; 00130 default: 00131 /* Must Never Happen ! */ 00132 wbxml_free(result); 00133 return NULL; 00134 } 00135 00136 return result; 00137 } 00138 00139 00140 WBXML_DECLARE(const WB_UTINY *) wbxml_tag_get_xml_name(WBXMLTag *tag) 00141 { 00142 if (tag == NULL) 00143 return WBXML_ELT_UNKNOWN_NAME; 00144 00145 switch (tag->type) { 00146 case WBXML_VALUE_TOKEN: 00147 return (const WB_UTINY *) tag->u.token->xmlName; 00148 break; 00149 case WBXML_VALUE_LITERAL: 00150 return (const WB_UTINY *) wbxml_buffer_get_cstr(tag->u.literal); 00151 default: 00152 return WBXML_ELT_UNKNOWN_NAME; 00153 } 00154 } 00155 00156 00157 /* WBXMLAttributeName */ 00158 00159 WBXML_DECLARE(WBXMLAttributeName *) wbxml_attribute_name_create(WBXMLValueType type) 00160 { 00161 WBXMLAttributeName *result = NULL; 00162 00163 if ((result = (WBXMLAttributeName *) wbxml_malloc(sizeof(WBXMLAttributeName))) == NULL) 00164 return NULL; 00165 00166 result->type = type; 00167 result->u.token = NULL; 00168 result->u.literal = NULL; 00169 00170 return result; 00171 } 00172 00173 00174 WBXML_DECLARE(WBXMLAttributeName *) wbxml_attribute_name_create_token(const WBXMLAttrEntry *value) 00175 { 00176 WBXMLAttributeName *result = NULL; 00177 00178 if ((result = wbxml_attribute_name_create(WBXML_VALUE_TOKEN)) == NULL) 00179 return NULL; 00180 00181 result->u.token = value; 00182 00183 return result; 00184 } 00185 00186 00187 WBXML_DECLARE(WBXMLAttributeName *) wbxml_attribute_name_create_literal(WB_UTINY *value) 00188 { 00189 WBXMLAttributeName *result = NULL; 00190 00191 if ((result = wbxml_attribute_name_create(WBXML_VALUE_LITERAL)) == NULL) 00192 return NULL; 00193 00194 if (value == NULL) 00195 result->u.literal = NULL; 00196 else { 00197 result->u.literal = wbxml_buffer_create(value, WBXML_STRLEN(value), WBXML_STRLEN(value)); 00198 if (result->u.literal == NULL) { 00199 wbxml_attribute_name_destroy(result); 00200 return NULL; 00201 } 00202 } 00203 00204 return result; 00205 } 00206 00207 00208 WBXML_DECLARE(void) wbxml_attribute_name_destroy(WBXMLAttributeName *name) 00209 { 00210 if (name == NULL) 00211 return; 00212 00213 if (name->type == WBXML_VALUE_LITERAL) 00214 wbxml_buffer_destroy(name->u.literal); 00215 00216 wbxml_free(name); 00217 } 00218 00219 00220 WBXML_DECLARE(WBXMLAttributeName *) wbxml_attribute_name_duplicate(WBXMLAttributeName *name) 00221 { 00222 WBXMLAttributeName *result = NULL; 00223 00224 if (name == NULL) 00225 return NULL; 00226 00227 if ((result = (WBXMLAttributeName *) wbxml_malloc(sizeof(WBXMLAttributeName))) == NULL) 00228 return NULL; 00229 00230 result->type = name->type; 00231 00232 switch (result->type) { 00233 case WBXML_VALUE_TOKEN: 00234 result->u.token = name->u.token; 00235 break; 00236 case WBXML_VALUE_LITERAL: 00237 result->u.literal = wbxml_buffer_duplicate(name->u.literal); 00238 break; 00239 default: 00240 /* Must Never Happen ! */ 00241 wbxml_free(result); 00242 return NULL; 00243 } 00244 00245 return result; 00246 } 00247 00248 00249 WBXML_DECLARE(const WB_UTINY *) wbxml_attribute_name_get_xml_name(WBXMLAttributeName *name) 00250 { 00251 if (name == NULL) 00252 return WBXML_ELT_UNKNOWN_NAME; 00253 00254 switch (name->type) { 00255 case WBXML_VALUE_TOKEN: 00256 return (const WB_UTINY *) name->u.token->xmlName; 00257 break; 00258 case WBXML_VALUE_LITERAL: 00259 return (const WB_UTINY *) wbxml_buffer_get_cstr(name->u.literal); 00260 default: 00261 return WBXML_ELT_UNKNOWN_NAME; 00262 } 00263 } 00264 00265 00266 /* WBXMLAttribute */ 00267 00268 WBXML_DECLARE(WBXMLAttribute *) wbxml_attribute_create(void) 00269 { 00270 WBXMLAttribute *result = NULL; 00271 00272 if ((result = (WBXMLAttribute *) wbxml_malloc(sizeof(WBXMLAttribute))) == NULL) 00273 return NULL; 00274 00275 result->name = NULL; 00276 result->value = NULL; 00277 00278 return result; 00279 } 00280 00281 00282 WBXML_DECLARE(void) wbxml_attribute_destroy(WBXMLAttribute *attr) 00283 { 00284 if (attr == NULL) 00285 return; 00286 00287 wbxml_attribute_name_destroy(attr->name); 00288 wbxml_buffer_destroy(attr->value); 00289 00290 wbxml_free(attr); 00291 } 00292 00293 00294 WBXML_DECLARE(WBXMLAttribute *) wbxml_attribute_duplicate(WBXMLAttribute *attr) 00295 { 00296 WBXMLAttribute *result = NULL; 00297 00298 if (attr == NULL) 00299 return NULL; 00300 00301 if ((result = (WBXMLAttribute *) wbxml_malloc(sizeof(WBXMLAttribute))) == NULL) 00302 return NULL; 00303 00304 result->name = wbxml_attribute_name_duplicate(attr->name); 00305 result->value = wbxml_buffer_duplicate(attr->value); 00306 00307 return result; 00308 } 00309 00310 00311 WBXML_DECLARE(const WB_UTINY *) wbxml_attribute_get_xml_name(WBXMLAttribute *attr) 00312 { 00313 if (attr == NULL) 00314 return WBXML_ELT_UNKNOWN_NAME; 00315 00316 return wbxml_attribute_name_get_xml_name(attr->name); 00317 } 00318 00319 00320 WBXML_DECLARE(const WB_UTINY *) wbxml_attribute_get_xml_value(WBXMLAttribute *attr) 00321 { 00322 if ((attr == NULL) || (attr->value == NULL)) 00323 return WBXML_UTINY_NULL_STRING; 00324 00325 return wbxml_buffer_get_cstr(attr->value); 00326 }