ObjFW  Diff

Differences From Artifact [f1c60b4b45]:

To Artifact [12bad844a7]:


16
17
18
19
20
21
22


























































23
24
25
26
27
28
29
#include <string.h>

#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

int _OFXMLElement_reference;



























































@implementation OFXMLElement
+ elementWithName: (OFString*)name_
{
	return [[[self alloc] initWithName: name_] autorelease];
}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
#include <string.h>

#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

int _OFXMLElement_reference;

@implementation OFXMLAttribute
+ attributeWithName: (OFString*)name_
	     prefix: (OFString*)prefix_
	  namespace: (OFString*)ns_
	stringValue: (OFString*)value_
{
	return [[[self alloc] initWithName: name_
				    prefix: prefix_
				 namespace: ns_
			       stringValue: value_] autorelease];
}

- initWithName: (OFString*)name_
	prefix: (OFString*)prefix_
     namespace: (OFString*)ns_
   stringValue: (OFString*)value_
{
	self = [super init];

	name = [name_ copy];
	prefix = [prefix_ copy];
	ns = [ns_ copy];
	value = [value_ copy];

	return self;
}

- (void)dealloc
{
	[name release];
	[prefix release];
	[ns release];
	[value release];

	[super dealloc];
}

- (OFString*)name
{
	return [[name copy] autorelease];
}

- (OFString*)prefix
{
	return [[prefix copy] autorelease];
}

- (OFString*)namespace
{
	return [[ns copy] autorelease];
}

- (OFString*)stringValue
{
	return [[value copy] autorelease];
}
@end

@implementation OFXMLElement
+ elementWithName: (OFString*)name_
{
	return [[[self alloc] initWithName: name_] autorelease];
}

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
	return self;
}

- (OFString*)string
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	char *str_c;
	size_t len, i, j;

	OFString *ret, *tmp;

	len = [name length] + 4;
	str_c = [self allocMemoryWithSize: len];

	/* Start of tag */
	*str_c = '<';
	memcpy(str_c + 1, [name cString], [name length]);
	i = [name length] + 1;

	/* Attributes */
	if (attrs != nil) {
		OFIterator *iter = [attrs iterator];

		for (;;) {
			of_iterator_pair_t pair;

			pair = [iter nextKeyObjectPair];

			if (pair.key == nil || pair.object == nil)
				break;

			tmp = [pair.object stringByXMLEscaping];

			len += [pair.key length] + [tmp length] + 4;
			@try {
				str_c = [self resizeMemory: str_c
						    toSize: len];
			} @catch (OFException *e) {
				[self freeMemory: str_c];
				@throw e;
			}

			str_c[i++] = ' ';
			memcpy(str_c + i, [pair.key cString],
			    [pair.key length]);
			i += [pair.key length];
			str_c[i++] = '=';
			str_c[i++] = '\'';
			memcpy(str_c + i, [tmp cString], [tmp length]);
			i += [tmp length];
			str_c[i++] = '\'';

			[pool releaseObjects];
		}
	}

	/* Childen */
	if (stringval != nil || children != nil) {
		if (stringval != nil)
			tmp = [stringval stringByXMLEscaping];
		else if (children != nil) {







|
>











|
|

<
<
|
<
|
<
<
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|

|
<







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
	return self;
}

- (OFString*)string
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	char *str_c;
	size_t len, i, j, attrs_count;
	OFXMLAttribute **attrs_data;
	OFString *ret, *tmp;

	len = [name length] + 4;
	str_c = [self allocMemoryWithSize: len];

	/* Start of tag */
	*str_c = '<';
	memcpy(str_c + 1, [name cString], [name length]);
	i = [name length] + 1;

	/* Attributes */
	attrs_data = [attrs data];
	attrs_count = [attrs count];



	for (j = 0; j < attrs_count; j++) {

		/* FIXME: Add namespace support */


		OFString *attr_name = [attrs_data[j] name];
		tmp = [[attrs_data[j] stringValue] stringByXMLEscaping];

		len += [attr_name length] + [tmp length] + 4;
		@try {
			str_c = [self resizeMemory: str_c
					    toSize: len];
		} @catch (OFException *e) {
			[self freeMemory: str_c];
			@throw e;
		}

		str_c[i++] = ' ';
		memcpy(str_c + i, [attr_name cString],
				[attr_name length]);
		i += [attr_name length];
		str_c[i++] = '=';
		str_c[i++] = '\'';
		memcpy(str_c + i, [tmp cString], [tmp length]);
		i += [tmp length];
		str_c[i++] = '\'';

		[pool releaseObjects];

	}

	/* Childen */
	if (stringval != nil || children != nil) {
		if (stringval != nil)
			tmp = [stringval stringByXMLEscaping];
		else if (children != nil) {
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
	} @finally {
		[self freeMemory: str_c];
	}

	return ret;
}

- addAttributeWithName: (OFString*)name_
	   stringValue: (OFString*)value_
{
	if (attrs == nil)
		attrs = [[OFMutableDictionary alloc] init];


	[attrs setObject: value_

		  forKey: name_];













	return self;
}




- addChild: (OFXMLElement*)child
{
	if (stringval != nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];








|
<


|

>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>







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
	} @finally {
		[self freeMemory: str_c];
	}

	return ret;
}

- addAttribute: (OFXMLAttribute*)attr

{
	if (attrs == nil)
		attrs = [[OFMutableArray alloc] init];

	/* FIXME: Prevent having it twice! */

	[attrs addObject: attr];

	return self;
}

- addAttributeWithName: (OFString*)name_
	   stringValue: (OFString*)value
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						       prefix: nil
						    namespace: nil
						  stringValue: value]];
	[pool release];

	return self;
}

/* TODO: Replace attribute */
/* TODO: Remove attribute */

- addChild: (OFXMLElement*)child
{
	if (stringval != nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];