File: element.c

package info (click to toggle)
keytouch 2.2.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,212 kB
  • ctags: 1,399
  • sloc: ansic: 9,195; sh: 3,630; makefile: 399
file content (303 lines) | stat: -rw-r--r-- 7,612 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*-------------------------------------------------------------------------------
Name               : element.c
Author             : Marvin Raaijmakers
Description        : Provides functions for XML elements
Date of last change: ?
History            : ?

    Copyright (C) 2005-2006 Marvin Raaijmakers

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    You can contact me at: marvinr(at)users(dot)sf(dot)net
    (replace (at) by @ and (dot) by .)
---------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>

#include <mxml.h>


static void append_element_string (	XmlContent	*element,
					char		*string,
					char		*seperator );
static int element_string_size (	XmlContent	*element,
					int		seperator_size  );



void
XmlAttributeListAdd (	XmlAttributeList	*attribute_list,
			XmlAttribute		*attribute       )
/*
Input:
	attribute	- The attribute to add to attribute_list
Output:
	attribute_list	- The attribute list where attribute was added to.
Returns:
	-
Description:
	This function adds attribute to attribute_list.
*/
{
	if (attribute_list != NULL && attribute != NULL)
	{
		attribute->next = NULL;
		if (attribute_list->head == NULL)
		{
			attribute_list->head = attribute_list->tail = attribute;
		}
		else
		{
			attribute_list->tail->next = attribute;
			attribute_list->tail = attribute;
		}
	}
}


void
XmlSetAttributeValue (	XmlAttributeName	*attribute_name,
			char			*value,
			XmlContent		*element         )
/*
Input:
	
	attribute_name	- The name of attribute to set the value to.
	value		- The value to set to the attribute
Output:
	element		- The element to where the attribute was set to.
Returns:
	-
Description:
	This function sets the value 'value' to the attribute, named attribute_name,
	in element.
*/
{
	XmlAttribute *attribute;
	
	/* Search for the attribute that may already exist in element */
	for (attribute = element->data.element.attributes->head;
	     attribute != NULL && attribute->name != attribute_name;
	     attribute = attribute->next)
		; /* NULL Statement */
	/* If the attribute already exists */
	if (attribute != NULL && attribute->value)
	{
		XmlFree (attribute->value);
	}
	else if (attribute == NULL)
	{
		attribute = XmlMalloc(sizeof(XmlAttribute));
		attribute->name = attribute_name;
		/* Add the attribute to the element */
		XmlAttributeListAdd (element->data.element.attributes, attribute);
	}
	attribute->value = XmlMalloc(strlen(value)+1);
	strcpy (attribute->value, value);
}


void
XmlClearAttributeList (XmlAttributeList *attribute_list)
/*
Input:
	-
Output:
	attribute_list	- The cleared XmlAttributeList.
Returns:
	-
Description:
	This function frees all attributes in attribute_list.
*/
{
	XmlAttribute	*attribute,
			*tmp;
	
	if (attribute_list != NULL)
	{
		attribute = attribute_list->head;
		while (attribute != NULL)
		{
			tmp = attribute;
			attribute = attribute->next;
			if (tmp->value != NULL)
			{
				XmlFree (tmp->value);
			}
			XmlFree (tmp);
		}
	}
	attribute_list->head = attribute_list->tail = NULL;
}


char
*XmlGetAttributeValue (	XmlAttributeName	*attribute_name,
			XmlContent		*content         )
/*
Input:
	content		- The element to get the attribute from.
	attribute_name	- The name of attribute to get the value from.
Output:
	-
Returns:
	The a pointer to a string which is the value of the attribute, named
	attribute_name, of the element content. If no such attribute exists
	NULL is returned.
Description:
	This function returns a pointer to a string which is the value of the
	attribute, named attribute_name, of the element content.
*/
{
	XmlAttribute *attribute;
	
	if (content->type != XmlCType_Element)
	{
		XmlError ("Tried to get an attribute value from content that is not an element.");
	}
	/* Find the matching attribute */
	for (attribute = content->data.element.attributes->head;
	     attribute != NULL && attribute->name != attribute_name;
	     attribute = attribute->next)
		; /* NULL Statement */
	if (attribute == NULL)
	{
		return (NULL);
	}
	return (attribute->value);
}


int
element_string_size (	XmlContent	*element,
			int		seperator_size  )
{
	XmlContent *content;
	int size;
	
	size = 0;	
	for (content = element->data.element.contents->head;
	     content != NULL;
	     content = content->next)
	{
		if (content->type == XmlCType_Element)
		{
			size += element_string_size(content, seperator_size);
		}
		else if (content->type == XmlCType_String)
		{
			size += strlen(content->data.string)+seperator_size;
		}
	}
	return (size);
}


void
append_element_string (	XmlContent	*element,
			char		*string,
			char		*seperator )
{
	XmlContent *content;
	
	for (content = element->data.element.contents->head;
	     content != NULL;
	     content = content->next)
	{
		if (content->type == XmlCType_Element)
		{
			append_element_string (content, string, seperator);
		}
		else if (content->type == XmlCType_String)
		{
			strcat (string, content->data.string);
			strcat (string, seperator);
		}
	}
}


char
*XmlGetElementString (	XmlContent	*content,
			char		*seperator )
/*
Input:
	content		- The element to get the string from.
	seperator	- Strings of different tags will be seperated by this
			  string.
Output:
	-
Returns:
	The a pointer to the string the element 'content' contains.
Description:
	This function returns a pointer to a string the element 'content' contains.
	Strings of different tags will be seperated by seperator. If the string is
	no longer needed it should be free with XmlFree().
*/
{
	int string_size, seperator_size;
	char *string;
	
	if (content == NULL || seperator == NULL)
	{
		return (NULL);
	}
	if (content->type != XmlCType_Element)
	{
		XmlError ("XmlGetElementString(): content is not XmlCType_Element.");
		return (NULL);
	}
	
	seperator_size = strlen(seperator);
	/* element_string_size() returns seperator_size too much */
	string_size = element_string_size(content, seperator_size);
	/* We use the last seperator_size bytes as an anti-overflow buffer */
	string = XmlMalloc(string_size+1);
	string[0] = '\0';
	append_element_string (content, string, seperator);
	/* The anti-overflow buffer is now filled with seperator */
	/* Remove the last seperator from the string */
	string[string_size-seperator_size] = '\0';
	/* Resize the allocated memory */
	string = XmlRealloc(string, string_size+1-seperator_size);
	
	return (string);
}


XmlContent
*XmlCreateElement (	XmlElementType	*type,
			Boolean		is_empty  )
/*
Input:
	type		- The type of the XmlContent to create
	is_empty	- Identifies if the element to create will be an empty element.
Output:
	-
Returns:
	A pointer to a new and initialized XmlContent which is an element.
Description:
	This function returns a pointer to a new and initialized element of type
	'type'.
*/
{
	XmlContent *new_element;
	
	new_element = XmlCreateContent(XmlCType_Element);
	new_element->data.element.type = type;
	new_element->data.element.is_empty = is_empty;
	return (new_element);
}