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,v 1.1 2008/02/15 16:46:09 haraldkipp Exp $
00040  * \endverbatim
00041  */
00042 
00043 #include <sys/types.h>
00044 
00045 #include <stdlib.h>
00046 #include <string.h>
00047 
00048 #include <pro/uxml.h>
00049 
00054 
00062 UXML_NODE *UxmlNodeCreate(char *name)
00063 {
00064     UXML_NODE *node;
00065     size_t nlen;
00066 
00067     if ((node = malloc(sizeof(UXML_NODE))) != NULL) {
00068         memset(node, 0, sizeof(UXML_NODE));
00069         nlen = strlen(name) + 1;
00070         if ((node->xmln_name = malloc(nlen)) != NULL) {
00071             memcpy(node->xmln_name, name, nlen);
00072         }
00073     }
00074     return node;
00075 }
00076 
00086 int UxmlNodeAddAttrib(UXML_NODE * node, char *name, char *value)
00087 {
00088     UXML_ATTRIB *attr;
00089     UXML_ATTRIB *ap;
00090     size_t len;
00091 
00092     if ((attr = malloc(sizeof(UXML_ATTRIB))) == NULL) {
00093         return -1;
00094     }
00095     attr->xmla_next = NULL;
00096     len = strlen(name) + 1;
00097     attr->xmla_name = malloc(len);
00098     memcpy(attr->xmla_name, name, len);
00099     len = strlen(value) + 1;
00100     attr->xmla_value = malloc(len);
00101     memcpy(attr->xmla_value, value, len);
00102 
00103     if (node->xmln_attribs == NULL) {
00104         node->xmln_attribs = attr;
00105     } else {
00106         ap = node->xmln_attribs;
00107         for (;;) {
00108             if (ap->xmla_next == NULL) {
00109                 ap->xmla_next = attr;
00110                 break;
00111             }
00112             ap = ap->xmla_next;
00113         }
00114     }
00115     return 0;
00116 }
00117 
00118 static void UxmlNodeDestroy(UXML_NODE * node)
00119 {
00120     UXML_ATTRIB *ap;
00121     UXML_ATTRIB *attr;
00122 
00123     if (node) {
00124         if (node->xmln_name) {
00125             free(node->xmln_name);
00126         }
00127         ap = node->xmln_attribs;
00128         while (ap) {
00129             attr = ap;
00130             ap = ap->xmla_next;
00131             if (attr->xmla_name) {
00132                 free(attr->xmla_name);
00133             }
00134             if (attr->xmla_value) {
00135                 free(attr->xmla_value);
00136             }
00137             free(attr);
00138         }
00139         free(node);
00140     }
00141 }
00142 
00151 UXML_NODE *UxmlTreeAddSibling(UXML_NODE * node, UXML_NODE * sibling)
00152 {
00153     for (;;) {
00154         if (node->xmln_next == NULL) {
00155             node->xmln_next = sibling;
00156             sibling->xmln_parent = node->xmln_parent;
00157             break;
00158         }
00159         node = node->xmln_next;
00160     }
00161     return sibling;
00162 }
00163 
00172 UXML_NODE *UxmlTreeAddChild(UXML_NODE * node, UXML_NODE * child)
00173 {
00174     if (node->xmln_child == NULL) {
00175         node->xmln_child = child;
00176         child->xmln_parent = node;
00177     } else {
00178         UxmlTreeAddSibling(node->xmln_child, child);
00179     }
00180     return child;
00181 }
00182 
00188 void UxmlTreeDestroy(UXML_NODE * node)
00189 {
00190     UXML_NODE *np = node;
00191 
00192     while (np) {
00193         node = np;
00194         np = np->xmln_next;
00195         if (node->xmln_child) {
00196             UxmlTreeDestroy(node->xmln_child);
00197         }
00198         UxmlNodeDestroy(node);
00199     }
00200 }
00201 

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