source: branches/1.3/src/core/scew_extras.c @ 5734

Last change on this file since 5734 was 5734, checked in by ldelgass, 9 years ago

Merge r5725:5726,r5730 from trunk

  • Property svn:eol-style set to native
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-2012  HUBzero Foundation, LLC
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
14#include "scew/scew.h"
15#include "scew/xelement.h"
16#include "scew/xattribute.h"
17#include "scew/xerror.h"
18#include "scew/str.h"
19#include "scew_extras.h"
20#include <assert.h>
21
22scew_element*
23scew_element_parent(scew_element const* element)
24{
25    assert(element != NULL);
26    return element->parent;
27}
28
29scew_element**
30scew_element_list_all(scew_element const* parent, unsigned int* count)
31{
32    unsigned int curr = 0;
33    unsigned int max = 0;
34    scew_element** list = NULL;
35    scew_element* element;
36
37    assert(parent != NULL);
38    assert(count != NULL);
39
40    element = scew_element_next(parent, 0);
41    while (element)
42    {
43        if (curr >= max)
44        {
45            max = (max + 1) * 2;
46            list = (scew_element**) realloc(list,
47                                            sizeof(scew_element*) * max);
48            if (!list)
49            {
50                set_last_error(scew_error_no_memory);
51                return NULL;
52            }
53        }
54        list[curr++] = element;
55
56        element = scew_element_next(parent, element);
57    }
58
59    *count = curr;
60
61    return list;
62}
63
64int
65scew_element_copy_attr(scew_element const* fromElement, scew_element* toElement)
66{
67    scew_attribute* attribute = NULL;
68    XML_Char const* attrVal   = NULL;
69    XML_Char const* attrName  = NULL;
70    int attrCount = 0;
71    int attrAdded = 0;
72
73    if ( (fromElement) && (toElement) && (fromElement != toElement) )
74    {
75        if ((attrCount = scew_attribute_count(fromElement)) > 0) {
76
77            while((attribute=scew_attribute_next(fromElement, attribute)) != NULL)
78            {
79                attrName = scew_attribute_name(attribute),
80                attrVal = scew_attribute_value(attribute);
81                if (scew_element_add_attr_pair(toElement, attrName, attrVal)) {
82                    attrAdded++;
83                }
84            }
85        }
86        else {
87            // there are no attributes, count == 0
88        }
89    }
90
91    return (attrCount - attrAdded);
92}
93
94scew_element*
95scew_element_copy (scew_element* element)
96{
97    XML_Char const* name = NULL;
98    XML_Char const* contents = NULL;
99    scew_element* new_elem = NULL;
100    scew_element* new_child = NULL;
101    scew_element* childNode = NULL;
102
103    name = scew_element_name(element);
104    contents = scew_element_contents(element);
105
106    new_elem = scew_element_create(name);
107    scew_element_set_name(new_elem,name);
108    if (contents) {
109        scew_element_set_contents(new_elem, contents);
110    }
111    scew_element_copy_attr(element,new_elem);
112
113    while ( (childNode = scew_element_next(element,childNode)) ) {
114        new_child = scew_element_copy(childNode);
115        scew_element_add_elem(new_elem, new_child);
116    }
117
118    return new_elem;
119}
120
121XML_Char const*
122scew_element_set_contents_binary(scew_element* element,
123                                 XML_Char const* bytes,
124                                 unsigned int* nbytes)
125{
126    XML_Char* out = NULL;
127
128    assert(element != NULL);
129    assert(nbytes != NULL);
130    if (*nbytes == 0) {
131        return element->contents;
132    }
133    assert(bytes != NULL);
134
135    free(element->contents);
136    out = (XML_Char*) calloc(*nbytes+1, sizeof(XML_Char));
137    element->contents = (XML_Char*) scew_memcpy(out, (XML_Char*)bytes, *nbytes);
138
139    return element->contents;
140}
Note: See TracBrowser for help on using the repository browser.