source: trunk/src/core/scew_extras.c @ 597

Last change on this file since 597 was 583, checked in by dkearney, 18 years ago

updated RpBuffer? object, cleaning up much of the code.
added putData() calls to RpLibrary?, still need a getData call.
added new function to the scew extra functions to directly add a char* buffer to the xml
updated makefiles as needed

File size: 3.7 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Extra helper functions for the scew library
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2004-2005  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13#include "scew/scew.h"
14#include "scew/xelement.h"
15#include "scew/xattribute.h"
16#include "scew/xerror.h"
17#include "scew/str.h"
18
19#include <assert.h>
20
21#include "scew_extras.h"
22
23scew_element*
24scew_element_parent(scew_element const* element)
25{
26    assert(element != NULL);
27    return element->parent;
28}
29
30scew_element**
31scew_element_list_all(scew_element const* parent, unsigned int* count)
32{
33    unsigned int curr = 0;
34    unsigned int max = 0;
35    scew_element** list = NULL;
36    scew_element* element;
37
38    assert(parent != NULL);
39    assert(count != NULL);
40
41    element = scew_element_next(parent, 0);
42    while (element)
43    {
44        if (curr >= max)
45        {
46            max = (max + 1) * 2;
47            list = (scew_element**) realloc(list,
48                                            sizeof(scew_element*) * max);
49            if (!list)
50            {
51                set_last_error(scew_error_no_memory);
52                return NULL;
53            }
54        }
55        list[curr++] = element;
56
57        element = scew_element_next(parent, element);
58    }
59
60    *count = curr;
61
62    return list;
63}
64
65int
66scew_element_copy_attr(scew_element const* fromElement, scew_element* toElement)
67{
68    scew_attribute* attribute = NULL;
69    XML_Char const* attrVal   = NULL;
70    XML_Char const* attrName  = NULL;
71    int attrCount = 0;
72    int attrAdded = 0;
73
74    if ( (fromElement) && (toElement) && (fromElement != toElement) )
75    {
76        if ((attrCount = scew_attribute_count(fromElement)) > 0) {
77
78            while((attribute=scew_attribute_next(fromElement, attribute)) != NULL)
79            {
80                attrName = scew_attribute_name(attribute),
81                attrVal = scew_attribute_value(attribute);
82                if (scew_element_add_attr_pair(toElement, attrName, attrVal)) {
83                    attrAdded++;
84                }
85            }
86        }
87        else {
88            // there are no attributes, count == 0
89        }
90    }
91
92    return (attrCount - attrAdded);
93}
94
95scew_element*
96scew_element_copy (scew_element* element)
97{
98    XML_Char const* name = NULL;
99    XML_Char const* contents = NULL;
100    scew_element* new_elem = NULL;
101    scew_element* new_child = NULL;
102    scew_element* childNode = NULL;
103
104    name = scew_element_name(element);
105    contents = scew_element_contents(element);
106
107    new_elem = scew_element_create(name);
108    scew_element_set_name(new_elem,name);
109    if (contents) {
110        scew_element_set_contents(new_elem, contents);
111    }
112    scew_element_copy_attr(element,new_elem);
113
114    while ( (childNode = scew_element_next(element,childNode)) ) {
115        new_child = scew_element_copy(childNode);
116        scew_element_add_elem(new_elem, new_child);
117    }
118
119    return new_elem;
120}
121
122
123XML_Char const*
124scew_element_set_contents_binary(   scew_element* element,
125                                    XML_Char const* bytes,
126                                    unsigned int* nbytes    )
127{
128    XML_Char* out = NULL;
129
130    assert(element != NULL);
131    assert(bytes != NULL);
132    assert(nbytes != NULL);
133
134    if (*nbytes == 0) {
135        return element->contents;
136    }
137
138    free(element->contents);
139    out = (XML_Char*) calloc(*nbytes+1, sizeof(XML_Char));
140    element->contents = (XML_Char*) scew_memcpy(out, (XML_Char*)bytes, *nbytes);
141
142    return element->contents;
143}
Note: See TracBrowser for help on using the repository browser.