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 | |
---|
22 | scew_element* |
---|
23 | scew_element_parent(scew_element const* element) |
---|
24 | { |
---|
25 | assert(element != NULL); |
---|
26 | return element->parent; |
---|
27 | } |
---|
28 | |
---|
29 | scew_element** |
---|
30 | scew_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 | |
---|
64 | int |
---|
65 | scew_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 | |
---|
94 | scew_element* |
---|
95 | scew_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 | |
---|
121 | |
---|
122 | XML_Char const* |
---|
123 | scew_element_set_contents_binary( scew_element* element, |
---|
124 | XML_Char const* bytes, |
---|
125 | unsigned int* nbytes ) |
---|
126 | { |
---|
127 | XML_Char* out = NULL; |
---|
128 | |
---|
129 | assert(element != NULL); |
---|
130 | assert(nbytes != NULL); |
---|
131 | if (*nbytes == 0) { |
---|
132 | return element->contents; |
---|
133 | } |
---|
134 | assert(bytes != NULL); |
---|
135 | |
---|
136 | free(element->contents); |
---|
137 | out = (XML_Char*) calloc(*nbytes+1, sizeof(XML_Char)); |
---|
138 | element->contents = (XML_Char*) scew_memcpy(out, (XML_Char*)bytes, *nbytes); |
---|
139 | |
---|
140 | return element->contents; |
---|
141 | } |
---|