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 2463 2009-02-13 14:52:05Z haraldkipp $
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     size_t len;
00092 
00093     if ((attr = malloc(sizeof(UXML_ATTRIB))) == NULL) {
00094         return -1;
00095     }
00096     attr->xmla_next = NULL;
00097     len = strlen(name) + 1;
00098     attr->xmla_name = malloc(len);
00099     memcpy(attr->xmla_name, name, len);
00100     len = strlen(value) + 1;
00101     attr->xmla_value = malloc(len);
00102     memcpy(attr->xmla_value, value, len);
00103 
00104     if (node->xmln_attribs == NULL) {
00105         node->xmln_attribs = attr;
00106     } else {
00107         ap = node->xmln_attribs;
00108         for (;;) {
00109             if (ap->xmla_next == NULL) {
00110                 ap->xmla_next = attr;
00111                 break;
00112             }
00113             ap = ap->xmla_next;
00114         }
00115     }
00116     return 0;
00117 }
00118 
00119 static void UxmlNodeDestroy(UXML_NODE * node)
00120 {
00121     UXML_ATTRIB *ap;
00122     UXML_ATTRIB *attr;
00123 
00124     if (node) {
00125         if (node->xmln_name) {
00126             free(node->xmln_name);
00127         }
00128         ap = node->xmln_attribs;
00129         while (ap) {
00130             attr = ap;
00131             ap = ap->xmla_next;
00132             if (attr->xmla_name) {
00133                 free(attr->xmla_name);
00134             }
00135             if (attr->xmla_value) {
00136                 free(attr->xmla_value);
00137             }
00138             free(attr);
00139         }
00140         free(node);
00141     }
00142 }
00143 
00152 UXML_NODE *UxmlTreeAddSibling(UXML_NODE * node, UXML_NODE * sibling)
00153 {
00154     for (;;) {
00155         if (node->xmln_next == NULL) {
00156             node->xmln_next = sibling;
00157             sibling->xmln_parent = node->xmln_parent;
00158             break;
00159         }
00160         node = node->xmln_next;
00161     }
00162     return sibling;
00163 }
00164 
00173 UXML_NODE *UxmlTreeAddChild(UXML_NODE * node, UXML_NODE * child)
00174 {
00175     if (node->xmln_child == NULL) {
00176         node->xmln_child = child;
00177         child->xmln_parent = node;
00178     } else {
00179         UxmlTreeAddSibling(node->xmln_child, child);
00180     }
00181     return child;
00182 }
00183 
00189 void UxmlTreeDestroy(UXML_NODE * node)
00190 {
00191     UXML_NODE *np = node;
00192 
00193     while (np) {
00194         node = np;
00195         np = np->xmln_next;
00196         if (node->xmln_child) {
00197             UxmlTreeDestroy(node->xmln_child);
00198         }
00199         UxmlNodeDestroy(node);
00200     }
00201 }
00202 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/