Nut/OS  4.10.3
API Reference
uxmltree.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 by egnite GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00021  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * \file pro/uxmltree.c
00036  * \brief Micro XML tree support functions.
00037  *
00038  * \verbatim
00039  * $Id: uxmltree.c 3021 2010-06-22 18:45:33Z thiagocorrea $
00040  * \endverbatim
00041  */
00042 
00043 #include <sys/types.h>
00044 
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <memdebug.h>
00048 
00049 #include <pro/uxml.h>
00050 
00055 
00063 UXML_NODE *UxmlNodeCreate(char *name)
00064 {
00065     UXML_NODE *node;
00066     size_t nlen;
00067 
00068     if ((node = malloc(sizeof(UXML_NODE))) != NULL) {
00069         memset(node, 0, sizeof(UXML_NODE));
00070         nlen = strlen(name) + 1;
00071         if ((node->xmln_name = malloc(nlen)) != NULL) {
00072             memcpy(node->xmln_name, name, nlen);
00073         }
00074     }
00075     return node;
00076 }
00077 
00087 int UxmlNodeAddAttrib(UXML_NODE * node, char *name, char *value)
00088 {
00089     UXML_ATTRIB *attr;
00090     UXML_ATTRIB *ap;
00091 
00092     attr = malloc(sizeof(UXML_ATTRIB));
00093     if (attr) {
00094         attr->xmla_next = NULL;
00095         attr->xmla_name = strdup(name);
00096         if (attr->xmla_name) {
00097             attr->xmla_value = strdup(value);
00098             if (attr->xmla_value) {
00099                 if (node->xmln_attribs == NULL) {
00100                     node->xmln_attribs = attr;
00101                 } else {
00102                     ap = node->xmln_attribs;
00103                     for (;;) {
00104                         if (ap->xmla_next == NULL) {
00105                             ap->xmla_next = attr;
00106                             break;
00107                         }
00108                         ap = ap->xmla_next;
00109                     }
00110                 }
00111                 return 0;
00112             }
00113             free(attr->xmla_name);
00114         }
00115         free(attr);
00116     }
00117     return -1;
00118 }
00119 
00120 static void UxmlNodeDestroy(UXML_NODE * node)
00121 {
00122     UXML_ATTRIB *ap;
00123     UXML_ATTRIB *attr;
00124 
00125     if (node) {
00126         if (node->xmln_name) {
00127             free(node->xmln_name);
00128         }
00129         ap = node->xmln_attribs;
00130         while (ap) {
00131             attr = ap;
00132             ap = ap->xmla_next;
00133             if (attr->xmla_name) {
00134                 free(attr->xmla_name);
00135             }
00136             if (attr->xmla_value) {
00137                 free(attr->xmla_value);
00138             }
00139             free(attr);
00140         }
00141         free(node);
00142     }
00143 }
00144 
00153 UXML_NODE *UxmlTreeAddSibling(UXML_NODE * node, UXML_NODE * sibling)
00154 {
00155     for (;;) {
00156         if (node->xmln_next == NULL) {
00157             node->xmln_next = sibling;
00158             sibling->xmln_parent = node->xmln_parent;
00159             break;
00160         }
00161         node = node->xmln_next;
00162     }
00163     return sibling;
00164 }
00165 
00174 UXML_NODE *UxmlTreeAddChild(UXML_NODE * node, UXML_NODE * child)
00175 {
00176     if (node->xmln_child == NULL) {
00177         node->xmln_child = child;
00178         child->xmln_parent = node;
00179     } else {
00180         UxmlTreeAddSibling(node->xmln_child, child);
00181     }
00182     return child;
00183 }
00184 
00190 void UxmlTreeDestroy(UXML_NODE * node)
00191 {
00192     UXML_NODE *np = node;
00193 
00194     while (np) {
00195         node = np;
00196         np = np->xmln_next;
00197         if (node->xmln_child) {
00198             UxmlTreeDestroy(node->xmln_child);
00199         }
00200         UxmlNodeDestroy(node);
00201     }
00202 }
00203