ObjFW  Check-in [c9c78d86d3]

Overview
Comment:More style improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c9c78d86d3ded42142f14955c25095bcc15612e1c03d21cd282832aaf94e3283
User & Date: js on 2011-04-22 22:46:18
Other Links: manifest | tags
Context
2011-04-23
00:11
More style improvements. check-in: d8c31454b4 user: js tags: trunk
2011-04-22
22:46
More style improvements. check-in: c9c78d86d3 user: js tags: trunk
21:59
OFURL: Do -[copy] in a try block to prevent leaks. check-in: 22ee389a21 user: js tags: trunk
Changes

Modified src/Makefile from [7e73fb6b16] to [7820d562e6].

54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
54
55
56
57
58
59
60

61
62
63
64
65
66
67
68







-
+







	    ObjFW.h		\
	    asprintf.h		\
	    ${ATOMIC_H}		\
	    macros.h		\
	    objfw-defs.h	\
	    ${THREADING_H}

SRCS += ${ASPRINTF_M} 		\
SRCS += ${ASPRINTF_M}		\
	${FOUNDATION_COMPAT_M}	\
	iso_8859_15.m		\
	windows_1252.m		\
	${OBJC_PROPERTIES_M}	\
	${OBJC_SYNC_M}

include ../buildsys.mk

Modified src/OFArray.h from [e10afe8afa] to [d4a064a71c].

142
143
144
145
146
147
148
149

150
151
152
153
154
155
156
142
143
144
145
146
147
148

149
150
151
152
153
154
155
156







-
+








/**
 * Returns the index of the first object that is equivalent to the specified
 * object or OF_INVALID_INDEX if it was not found.
 *
 * \param object The object whose index is returned
 * \return The index of the first object equivalent to the specified object
 * 	   or OF_INVALID_INDEX if it was not found
 *	   or OF_INVALID_INDEX if it was not found
 */
- (size_t)indexOfObject: (id)object;

/**
 * Returns the index of the first object that has the same address as the
 * specified object or OF_INVALID_INDEX if it was not found.
 *

Modified src/OFNumber.m from [eac6181c63] to [a59a183f51].

80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94







-
+







		return (t)value.float_;					\
	case OF_NUMBER_DOUBLE:						\
		return (t)value.double_;				\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
	}
#define CALCULATE(o, n)							\
	switch (type) { 						\
	switch (type) {							\
	case OF_NUMBER_BOOL:						\
		return [OFNumber numberWithBool:			\
		    value.bool_ o [n boolValue]];			\
	case OF_NUMBER_CHAR:						\
		return [OFNumber numberWithChar:			\
		    value.char_ o [n charValue]];			\
	case OF_NUMBER_SHORT:						\
163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
163
164
165
166
167
168
169

170
171
172
173
174
175
176
177







-
+







	case OF_NUMBER_DOUBLE:						\
		return [OFNumber numberWithDouble:			\
		    value.double_ o [n doubleValue]];			\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
	}
#define CALCULATE2(o, n)						\
	switch (type) { 						\
	switch (type) {							\
	case OF_NUMBER_BOOL:						\
		return [OFNumber numberWithBool:			\
		    value.bool_ o [n boolValue]];			\
	case OF_NUMBER_CHAR:						\
		return [OFNumber numberWithChar:			\
		    value.char_ o [n charValue]];			\
	case OF_NUMBER_SHORT:						\

Modified src/OFStreamObserver.m from [94e7d25075] to [5e763c88de].

77
78
79
80
81
82
83
84
85


86
87
88
89
90
91
92
77
78
79
80
81
82
83


84
85
86
87
88
89
90
91
92







-
-
+
+







		queue = [[OFMutableArray alloc] init];
		queueInfo = [[OFMutableArray alloc] init];
#ifdef OF_HAVE_POLL
		FDs = [[OFDataArray alloc] initWithItemSize:
		    sizeof(struct pollfd)];
		FDToStream = [[OFMutableDictionary alloc] init];
#else
		FD_ZERO(&readfds);
		FD_ZERO(&writefds);
		FD_ZERO(&readFDs);
		FD_ZERO(&writeFDs);
#endif

#ifndef _WIN32
		if (pipe(cancelFD))
			@throw [OFInitializationFailedException
			    newWithClass: isa];
#else
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
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







-
-
+
+













-
+







{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	int fileDescriptor = [stream fileDescriptor];

	FD_SET(fileDescriptor, FDSet);
	FD_SET(fileDescriptor, &exceptFDs);

	if (fd >= nfds)
		nfds = fd + 1;
	if (fileDescriptor >= nFDs)
		nFDs = fileDescriptor + 1;

	[pool release];
}

- (void)_removeStream: (OFStream*)stream
	    withFDSet: (fd_set*)FDSet
	   otherFDSet: (fd_set*)otherFDSet
{
	int fileDescriptor = [stream fileDescriptor];

	FD_CLR(fileDescriptor, FDSet);

	if (!FD_ISSET(fileDescriptor, otherFDSet))
		FD_CLR(fileDescriptor, &exceptfds);
		FD_CLR(fileDescriptor, &exceptFDs);
}
#endif

- (void)addStreamToObserveForReading: (OFStream*)stream
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFNumber *qi = [OFNumber numberWithInt: QUEUE_ADD | QUEUE_READ];

Modified src/OFTCPSocket.m from [1ec7691203] to [9d4c499443].

83
84
85
86
87
88
89
90

91
92
93
94
95

96
97

98
99
100
101
102
103
104
83
84
85
86
87
88
89

90
91
92
93
94

95
96

97
98
99
100
101
102
103
104







-
+




-
+

-
+







{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa
							  socket: self];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res, *res0;
	char port_s[7];
	char portCString[7];

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf(port_s, 7, "%" PRIu16, port);
	snprintf(portCString, 7, "%" PRIu16, port);

	if (getaddrinfo([host cString], port_s, &hints, &res0))
	if (getaddrinfo([host cString], portCString, &hints, &res0))
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];

	for (res = res0; res != NULL; res = res->ai_next) {
		if ((sock = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
208
209
210
211
212
213
214
215

216
217
218
219
220

221
222

223
224
225
226
227
228
229
208
209
210
211
212
213
214

215
216
217
218
219

220
221

222
223
224
225
226
227
228
229







-
+




-
+

-
+








	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa
							  socket: self];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res;
	char port_s[7];
	char portCString[7];

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf(port_s, 7, "%" PRIu16, port);
	snprintf(portCString, 7, "%" PRIu16, port);

	if (getaddrinfo([host cString], port_s, &hints, &res))
	if (getaddrinfo([host cString], portCString, &hints, &res))
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];

	if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
343
344
345
346
347
348
349
350

351
352
353


354
355
356


357
358
359

360
361

362
363
364
365
366


367
368
369
370
371
372
373



374
375

376
377
378
379
380
381
382
383
384
385
386
387
388


389
390
391
392
393
394

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
343
344
345
346
347
348
349

350
351


352
353
354


355
356
357
358

359
360

361
362
363
364


365
366
367
368
369
370



371
372
373
374

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

396
397
398
399
400
401
402
403
404
405
406
407
408


409
410
411
412
413
414
415







-
+

-
-
+
+

-
-
+
+


-
+

-
+



-
-
+
+




-
-
-
+
+
+

-
+













+
+





-
+












-
-







						     backLog: 5];

	isListening = YES;
}

- (OFTCPSocket*)accept
{
	OFTCPSocket *newsock;
	OFTCPSocket *newSocket;
	struct sockaddr *addr;
	socklen_t addrlen;
	int s;
	socklen_t addrLen;
	int newSock;

	newsock = [[[isa alloc] init] autorelease];
	addrlen = sizeof(struct sockaddr);
	newSocket = [[[isa alloc] init] autorelease];
	addrLen = sizeof(struct sockaddr);

	@try {
		addr = [newsock allocMemoryWithSize: sizeof(struct sockaddr)];
		addr = [newSocket allocMemoryWithSize: sizeof(struct sockaddr)];
	} @catch (id e) {
		[newsock release];
		[newSocket release];
		@throw e;
	}

	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock release];
	if ((newSock = accept(sock, addr, &addrLen)) == INVALID_SOCKET) {
		[newSocket release];
		@throw [OFAcceptFailedException newWithClass: isa
						      socket: self];
	}

	newsock->sock = s;
	newsock->sockAddr = addr;
	newsock->sockAddrLen = addrlen;
	newSocket->sock = newSock;
	newSocket->sockAddr = addr;
	newSocket->sockAddrLen = addrLen;

	return newsock;
	return newSocket;
}

- (void)setKeepAlivesEnabled: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: isa
							 stream: self];
}

- (OFString*)remoteAddress
{
	char *host;

	if (sockAddr == NULL || sockAddrLen == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	char *host = [self allocMemoryWithSize: NI_MAXHOST];
	host = [self allocMemoryWithSize: NI_MAXHOST];

	@try {
		if (getnameinfo(sockAddr, sockAddrLen, host, NI_MAXHOST, NULL,
		    0, NI_NUMERICHOST))
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: host];
	} @finally {
		[self freeMemory: host];
	}
#else
	char *host;

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		host = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);

Modified src/OFThread.h from [a4a4af59a1] to [035bd5514f].

30
31
32
33
34
35
36
37

38
39
40
41
42
43
44
30
31
32
33
34
35
36

37
38
39
40
41
42
43
44







-
+







	of_tlskey_t key;
/* Work around a bug in gcc 4.4.4 (possibly only on Haiku) */
#if !defined(__GNUC__) || __GNUC__ != 4 || __GNUC_MINOR__ != 4 || \
    __GNUC_PATCHLEVEL__ != 4
@protected
#endif
	void (*destructor)(id);
	of_list_object_t *listobj;
	of_list_object_t *listObject;
	BOOL initialized;
}

/**
 * \return A new autoreleased Thread Local Storage key
 */
+ TLSKey;
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
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







-
+











-
+











-
+




















-
+

-
+




-
-
+
+

-
-
+
+







	of_thread_t thread;
@public
	enum {
		OF_THREAD_NOT_RUNNING,
		OF_THREAD_RUNNING,
		OF_THREAD_WAITING_FOR_JOIN
	} running;
	id retval;
	id returnValue;
}

/**
 * \return A new, autoreleased thread
 */
+ thread;

/**
 * \param obj An object that is passed to the main method as a copy or nil
 * \return A new, autoreleased thread
 */
+ threadWithObject: (id)obj;
+ threadWithObject: (id)object;

/**
 * Sets the Thread Local Storage for the specified key.
 *
 * The specified object is first retained and then the object stored before is
 * released. You can specify nil as object if you want the old object to be
 * released and don't want any new object for the TLS key.
 *
 * \param key The Thread Local Storage key
 * \param obj The object the Thread Local Storage key will be set to
 */
+ (void)setObject: (id)obj
+ (void)setObject: (id)object
	forTLSKey: (OFTLSKey*)key;

/**
 * Returns the object for the specified Thread Local Storage key.
 *
 * The returned object is <i>not</i> retained and autoreleased for performance
 * reasons!
 *
 * \param key The Thread Local Storage key
 */
+ (id)objectForTLSKey: (OFTLSKey*)key;

/**
 * \return The current thread or nil if we are in the main thread
 */
+ (OFThread*)currentThread;

/**
 * Suspends execution of the current thread for the specified time interval.
 *
 * \param sec The number of seconds to sleep
 * \param seconds The number of seconds to sleep
 */
+ (void)sleepForTimeInterval: (int64_t)sec;
+ (void)sleepForTimeInterval: (int64_t)seconds;

/**
 * Suspends execution of the current thread for the specified time interval.
 *
 * \param sec The number of seconds to sleep
 * \param usec The number of microseconds to sleep
 * \param seconds The number of seconds to sleep
 * \param microseconds The number of microseconds to sleep
 */
+ (void)sleepForTimeInterval: (int64_t)sec
		microseconds: (uint32_t)usec;
+ (void)sleepForTimeInterval: (int64_t)seconds
		microseconds: (uint32_t)microseconds;

/**
 * Suspends execution of the current thread until the specified date.
 */
+ (void)sleepUntilDate: (OFDate*)date;

/**
154
155
156
157
158
159
160
161

162
163
164
165
166
167

168
169
170
171
172
173
174
154
155
156
157
158
159
160

161
162
163
164
165
166

167
168
169
170
171
172
173
174







-
+





-
+







+ (void)terminate;

/**
 * Terminates the current thread, letting it return the specified object.
 *
 * \param obj The object which the terminated thread will return
 */
+ (void)terminateWithObject: (id)obj;
+ (void)terminateWithObject: (id)object;

/**
 * \param obj An object that is passed to the main method as a copy or nil
 * \return An initialized OFThread.
 */
- initWithObject: (id)obj;
- initWithObject: (id)object;

/**
 * The main routine of the thread. You need to reimplement this!
 *
 * It can access the object passed to the threadWithObject or initWithObject
 * method using the instance variable named object.
 *
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
230
231
232
233
234
235
236

237
238
239
240
241
242
243
244







-
+








/**
 * \brief A class implementing a condition variable for thread synchronization.
 */
@interface OFCondition: OFMutex
{
	of_condition_t condition;
	BOOL cond_initialized;
	BOOL conditionInitialized;
}

/**
 * \return A new, autoreleased OFCondition
 */
+ condition;

Modified src/OFThread.m from [926b087d59] to [2f72c3fe38].

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
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







-
-
+
+


-
+





-
+

-
+





-
+

-
+

-
+




-
+














-
+








-
+

-
+


-
+


-
+

-
+



-
+









-
+


-
+

-
+



-
+


-
+

-
+


-
+



-
-
+
+

-
+



-
-
+
+

-
+


-
+







-
-
+
+

-
+


-
+




-
+


-
-
+
+

-
+


-
+

















-
+

-
+


-
+


















-
+




-
+







#import "OFOutOfRangeException.h"
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"

#import "threading.h"

static OFList *tlskeys;
static of_tlskey_t thread_self;
static OFList *TLSKeys;
static of_tlskey_t threadSelf;

static id
call_main(id obj)
call_main(OFThread *thread)
{
#if defined(OF_GNU_RUNTIME) || defined(OF_OLD_GNU_RUNTIME)
	objc_thread_add();
#endif

	if (!of_tlskey_set(thread_self, obj))
	if (!of_tlskey_set(threadSelf, thread))
		@throw [OFInitializationFailedException
		    newWithClass: [obj class]];
		    newWithClass: [thread class]];

	/*
	 * Nasty workaround for thread implementations which can't return a
	 * value on join.
	 */
	((OFThread*)obj)->retval = [[obj main] retain];
	thread->returnValue = [[thread main] retain];

	[obj handleTermination];
	[thread handleTermination];

	((OFThread*)obj)->running = OF_THREAD_WAITING_FOR_JOIN;
	thread->running = OF_THREAD_WAITING_FOR_JOIN;

	[OFTLSKey callAllDestructors];
	[OFAutoreleasePool releaseAll];

	[obj release];
	[thread release];

#if defined(OF_GNU_RUNTIME) || defined(OF_OLD_GNU_RUNTIME)
	objc_thread_remove();
#endif

	return 0;
}

@implementation OFThread
+ (void)initialize
{
	if (self != [OFThread class])
		return;

	if (!of_tlskey_new(&thread_self))
	if (!of_tlskey_new(&threadSelf))
		@throw [OFInitializationFailedException newWithClass: self];
}

+ thread
{
	return [[[self alloc] init] autorelease];
}

+ threadWithObject: (id)obj
+ threadWithObject: (id)object
{
	return [[[self alloc] initWithObject: obj] autorelease];
	return [[[self alloc] initWithObject: object] autorelease];
}

+ (void)setObject: (id)obj
+ (void)setObject: (id)object
	forTLSKey: (OFTLSKey*)key
{
	id old = of_tlskey_get(key->key);
	id oldObject = of_tlskey_get(key->key);

	if (!of_tlskey_set(key->key, [obj retain]))
	if (!of_tlskey_set(key->key, [object retain]))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	[old release];
	[oldObject release];
}

+ (id)objectForTLSKey: (OFTLSKey*)key
{
	return [[of_tlskey_get(key->key) retain] autorelease];
}

+ (OFThread*)currentThread
{
	return [[of_tlskey_get(thread_self) retain] autorelease];
	return [[of_tlskey_get(threadSelf) retain] autorelease];
}

+ (void)sleepForTimeInterval: (int64_t)sec
+ (void)sleepForTimeInterval: (int64_t)seconds
{
	if (sec < 0)
	if (seconds < 0)
		@throw [OFOutOfRangeException newWithClass: self];

#ifndef _WIN32
	if (sec > UINT_MAX)
	if (seconds > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	sleep((unsigned int)sec);
	sleep((unsigned int)seconds);
#else
	if (sec * 1000 > UINT_MAX)
	if (seconds * 1000 > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	Sleep((unsigned int)sec * 1000);
	Sleep((unsigned int)seconds * 1000);
#endif
}

+ (void)sleepForTimeInterval: (int64_t)sec
		microseconds: (uint32_t)usec
+ (void)sleepForTimeInterval: (int64_t)seconds
		microseconds: (uint32_t)microseconds
{
	if (sec < 0)
	if (seconds < 0)
		@throw [OFOutOfRangeException newWithClass: self];

#ifndef _WIN32
	sleep((unsigned int)sec);
	usleep(usec);
	sleep((unsigned int)seconds);
	usleep(microseconds);
#else
	if (sec * 1000 + usec / 1000 > UINT_MAX)
	if (seconds * 1000 + microseconds / 1000 > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	Sleep((unsigned int)sec * 1000 + usec / 1000);
	Sleep((unsigned int)seconds * 1000 + microseconds / 1000);
#endif
}

+ (void)sleepUntilDate: (OFDate*)date
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDate *now = [OFDate date];
	int64_t sec;
	uint32_t usec;
	int64_t seconds;
	uint32_t microseconds;

	if ((sec = [date timeIntervalSinceDate: now]) < 0)
	if ((seconds = [date timeIntervalSinceDate: now]) < 0)
		@throw [OFOutOfRangeException newWithClass: self];

	usec = [date microsecondsOfTimeIntervalSinceDate: now];
	microseconds = [date microsecondsOfTimeIntervalSinceDate: now];

	[pool release];

#ifndef _WIN32
	if (sec > UINT_MAX)
	if (seconds > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	sleep((unsigned int)sec);
	usleep(usec);
	sleep((unsigned int)seconds);
	usleep(microseconds);
#else
	if (sec * 1000 + usec / 1000 > UINT_MAX)
	if (seconds * 1000 + microseconds / 1000 > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	Sleep(sec * 1000 + usec / 1000);
	Sleep(seconds * 1000 + microseconds / 1000);
#endif
}

+ (void)yield
{
#ifdef OF_HAVE_SCHED_YIELD
	sched_yield();
#else
	[self sleepForTimeInterval: 0];
#endif
}

+ (void)terminate
{
	[self terminateWithObject: nil];
}

+ (void)terminateWithObject: (id)obj
+ (void)terminateWithObject: (id)object
{
	OFThread *thread = of_tlskey_get(thread_self);
	OFThread *thread = of_tlskey_get(threadSelf);

	if (thread != nil) {
		thread->retval = [obj retain];
		thread->returnValue = [object retain];

		[thread handleTermination];

		thread->running = OF_THREAD_WAITING_FOR_JOIN;
	}

	[OFTLSKey callAllDestructors];
	[OFAutoreleasePool releaseAll];

	[thread release];

#if defined(OF_GNU_RUNTIME) || defined(OF_OLD_GNU_RUNTIME)
	objc_thread_remove();
#endif

	of_thread_exit();
}

- initWithObject: (id)obj
- initWithObject: (id)object_
{
	self = [super init];

	@try {
		object = [obj retain];
		object = [object_ retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
258
259
260
261
262
263
264
265

266
267
268
269
270
271
272
258
259
260
261
262
263
264

265
266
267
268
269
270
271
272







-
+







{
	if (running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException newWithClass: isa
							    thread: self];

	if (running == OF_THREAD_WAITING_FOR_JOIN) {
		of_thread_detach(thread);
		[retval release];
		[returnValue release];
	}

	[self retain];

	if (!of_thread_new(&thread, call_main, self)) {
		[self release];
		@throw [OFThreadStartFailedException newWithClass: isa
280
281
282
283
284
285
286
287

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312
313
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354


355
356
357
358
359
360
361
280
281
282
283
284
285
286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

304
305
306
307
308
309
310
311
312
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352


353
354
355
356
357
358
359
360
361







-
+
















-
+









-
+
















-
-
+
+




















-
-
+
+







{
	if (running == OF_THREAD_NOT_RUNNING || !of_thread_join(thread))
		@throw [OFThreadJoinFailedException newWithClass: isa
							  thread: self];

	running = OF_THREAD_NOT_RUNNING;

	return retval;
	return returnValue;
}

- (void)dealloc
{
	if (running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException newWithClass: isa
							    thread: self];

	/*
	 * We should not be running anymore, but call detach in order to free
	 * the resources.
	 */
	if (running == OF_THREAD_WAITING_FOR_JOIN)
		of_thread_detach(thread);

	[object release];
	[retval release];
	[returnValue release];

	[super dealloc];
}
@end

@implementation OFTLSKey
+ (void)initialize
{
	if (self == [OFTLSKey class])
		tlskeys = [[OFList alloc] init];
		TLSKeys = [[OFList alloc] init];
}

+ TLSKey
{
	return [[[self alloc] init] autorelease];
}

+ TLSKeyWithDestructor: (void(*)(id))destructor
{
	return [[[self alloc] initWithDestructor: destructor] autorelease];
}

+ (void)callAllDestructors
{
	of_list_object_t *iter;

	@synchronized (tlskeys) {
		for (iter = [tlskeys firstListObject]; iter != NULL;
	@synchronized (TLSKeys) {
		for (iter = [TLSKeys firstListObject]; iter != NULL;
		    iter = iter->next) {
			OFTLSKey *key = (OFTLSKey*)iter->object;

			if (key->destructor != NULL)
				key->destructor(iter->object);
		}
	}
}

- init
{
	self = [super init];

	@try {
		if (!of_tlskey_new(&key))
			@throw [OFInitializationFailedException
			    newWithClass: isa];

		initialized = YES;

		@synchronized (tlskeys) {
			listobj = [tlskeys appendObject: self];
		@synchronized (TLSKeys) {
			listObject = [TLSKeys appendObject: self];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
375
376
377
378
379
380
381
382
383
384



385
386
387
388
389
390
391
375
376
377
378
379
380
381



382
383
384
385
386
387
388
389
390
391







-
-
-
+
+
+







	if (destructor != NULL)
		destructor(self);

	if (initialized)
		of_tlskey_free(key);

	/* In case we called [self release] in init */
	if (listobj != NULL) {
		@synchronized (tlskeys) {
			[tlskeys removeListObject: listobj];
	if (listObject != NULL) {
		@synchronized (TLSKeys) {
			[TLSKeys removeListObject: listObject];
		}
	}

	[super dealloc];
}
@end

452
453
454
455
456
457
458
459

460
461
462
463
464
465
466
452
453
454
455
456
457
458

459
460
461
462
463
464
465
466







-
+








	if (!of_condition_new(&condition)) {
		Class c = isa;
		[self release];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	cond_initialized = YES;
	conditionInitialized = YES;

	return self;
}

- (void)wait
{
	if (!of_condition_wait(&condition, &mutex))
480
481
482
483
484
485
486
487

488
489
490
491
492
493
494
495
480
481
482
483
484
485
486

487
488
489
490
491
492
493
494
495







-
+








	if (!of_condition_broadcast(&condition))
		@throw [OFConditionBroadcastFailedException newWithClass: isa
							       condition: self];
}

- (void)dealloc
{
	if (cond_initialized)
	if (conditionInitialized)
		if (!of_condition_free(&condition))
			@throw [OFConditionStillWaitingException
			    newWithClass: isa
			       condition: self];

	[super dealloc];
}
@end

Modified src/OFURL.h from [dd0072857c] to [8d20bb8741].

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
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







-
+


-
+


-
-
+
+


-
-
+
+




-
+


-
+




-
-
+
+


-
+







@property (copy) OFString *path;
@property (copy) OFString *parameters;
@property (copy) OFString *query;
@property (copy) OFString *fragment;
#endif

/**
 * \param str A string describing a URL
 * \param string A string describing a URL
 * \return A new, autoreleased OFURL
 */
+ URLWithString: (OFString*)str;
+ URLWithString: (OFString*)string;

/**
 * \param str A string describing a URL
 * \param url An URL to which the string is relative
 * \param string A string describing a URL
 * \param URL An URL to which the string is relative
 * \return A new, autoreleased OFURL
 */
+ URLWithString: (OFString*)str
  relativeToURL: (OFURL*)url;
+ URLWithString: (OFString*)string
  relativeToURL: (OFURL*)URL;

/**
 * Initializes an already allocated OFURL.
 *
 * \param str A string describing a URL
 * \param string A string describing a URL
 * \return An initialized OFURL
 */
- initWithString: (OFString*)str;
- initWithString: (OFString*)string;

/**
 * Initializes an already allocated OFURL.
 *
 * \param str A string describing a URL
 * \param url A URL to which the string is relative
 * \param string A string describing a URL
 * \param URL A URL to which the string is relative
 * \return An initialized OFURL
 */
- initWithString: (OFString*)str
- initWithString: (OFString*)string
   relativeToURL: (OFURL*)url;

/**
 * \return The scheme part of the URL
 */
- (OFString*)scheme;

Modified src/OFURL.m from [0a4862ec8c] to [e94e617a72].

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
304
305
306

307
308

309
310

311
312
313

314
315

316
317

318
319

320
321

322
323

324
325

326
327

328
329

330
331

332
333
334
335
336
337
338
339

340
341
342
343
344
345
346
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
304
305
306

307
308

309
310

311
312
313

314
315

316
317

318
319

320
321

322
323

324
325

326
327

328
329

330
331

332
333
334
335
336
337
338
339

340
341
342
343
344
345
346
347







-
-
-
-
-
+
+
+
+
+













-
-
+
+



-
-
+
+






-
+




















-
+

-
+


-
-
+
+

-
-
+
+


-
+

-
+






-
+


-
+

-
+

-
+

-
+

-
+

-
-
+
+

-
+



-
+




-
+





-
+




-
+




-
+

-
+


-
+

-
+




-
+


-
+

-
+



-
+



-
+









-
-
+
+






-
+






-
+






-
+
+






-
+





-
-
+
+

-
+

-
-
+
+






-
-
-
-
-
+
+
+
+
+

-
+


-
+

-
+

-
+




-
+




-
+





-
-
+
+






-
+

-
-
+
+


-
-
+
+









-
+



















-
+

-
+

-
+


-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+







-
+







#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"

#import "macros.h"

#define ADD_STR_HASH(str)			\
	h = [str hash];				\
	OF_HASH_ADD(hash, h >> 24);		\
	OF_HASH_ADD(hash, (h >> 16) & 0xFF);	\
	OF_HASH_ADD(hash, (h >> 8) & 0xFF);	\
	OF_HASH_ADD(hash, h & 0xFF);
	tmp = [str hash];			\
	OF_HASH_ADD(hash, tmp >> 24);		\
	OF_HASH_ADD(hash, (tmp >> 16) & 0xFF);	\
	OF_HASH_ADD(hash, (tmp >> 8) & 0xFF);	\
	OF_HASH_ADD(hash, tmp & 0xFF);

static OF_INLINE OFString*
resolve_relative_path(OFString *path)
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableArray *array;
	OFString *ret;
	BOOL done = NO;

	array = [[[path componentsSeparatedByString: @"/"] mutableCopy]
	    autorelease];

	while (!done) {
		id *array_c = [array cArray];
		size_t i, array_len = [array count];
		id *cArray = [array cArray];
		size_t i, length = [array count];

		done = YES;

		for (i = 0; i < array_len; i++) {
			if ([array_c[i] isEqual: @"."]) {
		for (i = 0; i < length; i++) {
			if ([cArray[i] isEqual: @"."]) {
				[array removeObjectAtIndex: i];
				done = NO;

				break;
			}

			if ([array_c[i] isEqual: @".."]) {
			if ([cArray[i] isEqual: @".."]) {
				[array removeObjectAtIndex: i];

				if (i > 0)
					[array removeObjectAtIndex: i - 1];

				done = NO;

				break;
			}
		}
	}

	ret = [[array componentsJoinedByString: @"/"] retain];

	[pool release];

	return [ret autorelease];
}

@implementation OFURL
+ URLWithString: (OFString*)str
+ URLWithString: (OFString*)string
{
	return [[[self alloc] initWithString: str] autorelease];
	return [[[self alloc] initWithString: string] autorelease];
}

+ URLWithString: (OFString*)str
  relativeToURL: (OFURL*)url
+ URLWithString: (OFString*)string
  relativeToURL: (OFURL*)URL
{
	return [[[self alloc] initWithString: str
			       relativeToURL: url] autorelease];
	return [[[self alloc] initWithString: string
			       relativeToURL: URL] autorelease];
}

- initWithString: (OFString*)str
- initWithString: (OFString*)string
{
	char *str_c, *str_c2 = NULL;
	char *cString, *cString2 = NULL;

	self = [super init];

	@try {
		char *tmp, *tmp2;

		if ((str_c2 = strdup([str cString])) == NULL)
		if ((cString2 = strdup([string cString])) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: [str cStringLength]];
			    requestedSize: [string cStringLength]];

		str_c = str_c2;
		cString = cString2;

		if (!strncmp(str_c, "file://", 7)) {
		if (!strncmp(cString, "file://", 7)) {
			scheme = @"file";
			path = [[OFString alloc] initWithCString: str_c + 7];
			path = [[OFString alloc] initWithCString: cString + 7];
			return self;
		} else if (!strncmp(str_c, "http://", 7)) {
		} else if (!strncmp(cString, "http://", 7)) {
			scheme = @"http";
			str_c += 7;
		} else if (!strncmp(str_c, "https://", 8)) {
			cString += 7;
		} else if (!strncmp(cString, "https://", 8)) {
			scheme = @"https";
			str_c += 8;
			cString += 8;
		} else
			@throw [OFInvalidFormatException newWithClass: isa];

		if ((tmp = strchr(str_c, '/')) != NULL) {
		if ((tmp = strchr(cString, '/')) != NULL) {
			*tmp = '\0';
			tmp++;
		}

		if ((tmp2 = strchr(str_c, '@')) != NULL) {
		if ((tmp2 = strchr(cString, '@')) != NULL) {
			char *tmp3;

			*tmp2 = '\0';
			tmp2++;

			if ((tmp3 = strchr(str_c, ':')) != NULL) {
			if ((tmp3 = strchr(cString, ':')) != NULL) {
				*tmp3 = '\0';
				tmp3++;

				user = [[OFString alloc]
				    initWithCString: str_c];
				    initWithCString: cString];
				password = [[OFString alloc]
				    initWithCString: tmp3];
			} else
				user = [[OFString alloc]
				    initWithCString: str_c];
				    initWithCString: cString];

			str_c = tmp2;
			cString = tmp2;
		}

		if ((tmp2 = strchr(str_c, ':')) != NULL) {
		if ((tmp2 = strchr(cString, ':')) != NULL) {
			OFAutoreleasePool *pool;
			OFString *port_str;
			OFString *portString;

			*tmp2 = '\0';
			tmp2++;

			host = [[OFString alloc] initWithCString: str_c];
			host = [[OFString alloc] initWithCString: cString];

			pool = [[OFAutoreleasePool alloc] init];
			port_str = [[OFString alloc] initWithCString: tmp2];
			portString = [[OFString alloc] initWithCString: tmp2];

			if ([port_str decimalValue] > 65535)
			if ([portString decimalValue] > 65535)
				@throw [OFInvalidFormatException
				    newWithClass: isa];

			port = [port_str decimalValue];
			port = [portString decimalValue];

			[pool release];
		} else {
			host = [[OFString alloc] initWithCString: str_c];
			host = [[OFString alloc] initWithCString: cString];

			if ([scheme isEqual: @"http"])
				port = 80;
			else if ([scheme isEqual: @"https"])
				port = 443;
			else
				assert(0);
		}

		if ((str_c = tmp) != NULL) {
			if ((tmp = strchr(str_c, '#')) != NULL) {
		if ((cString = tmp) != NULL) {
			if ((tmp = strchr(cString, '#')) != NULL) {
				*tmp = '\0';

				fragment = [[OFString alloc]
				    initWithCString: tmp + 1];
			}

			if ((tmp = strchr(str_c, '?')) != NULL) {
			if ((tmp = strchr(cString, '?')) != NULL) {
				*tmp = '\0';

				query = [[OFString alloc]
				    initWithCString: tmp + 1];
			}

			if ((tmp = strchr(str_c, ';')) != NULL) {
			if ((tmp = strchr(cString, ';')) != NULL) {
				*tmp = '\0';

				parameters = [[OFString alloc]
				    initWithCString: tmp + 1];
			}

			path = [[OFString alloc] initWithFormat: @"/%s", str_c];
			path = [[OFString alloc] initWithFormat: @"/%s",
								 cString];
		} else
			path = @"";
	} @catch (id e) {
		[self release];
		@throw e;
	} @finally {
		free(str_c2);
		free(cString2);
	}

	return self;
}

- initWithString: (OFString*)str
   relativeToURL: (OFURL*)url
- initWithString: (OFString*)string
   relativeToURL: (OFURL*)URL
{
	char *str_c, *str_c2 = NULL;
	char *cString, *cString2 = NULL;

	if ([str containsString: @"://"])
		return [self initWithString: str];
	if ([string containsString: @"://"])
		return [self initWithString: string];

	self = [super init];

	@try {
		char *tmp;

		scheme = [url->scheme copy];
		host = [url->host copy];
		port = url->port;
		user = [url->user copy];
		password = [url->password copy];
		scheme = [URL->scheme copy];
		host = [URL->host copy];
		port = URL->port;
		user = [URL->user copy];
		password = [URL->password copy];

		if ((str_c2 = strdup([str cString])) == NULL)
		if ((cString2 = strdup([string cString])) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: [str cStringLength]];
			    requestedSize: [string cStringLength]];

		str_c = str_c2;
		cString = cString2;

		if ((tmp = strchr(str_c, '#')) != NULL) {
		if ((tmp = strchr(cString, '#')) != NULL) {
			*tmp = '\0';
			fragment = [[OFString alloc] initWithCString: tmp + 1];
		}

		if ((tmp = strchr(str_c, '?')) != NULL) {
		if ((tmp = strchr(cString, '?')) != NULL) {
			*tmp = '\0';
			query = [[OFString alloc] initWithCString: tmp + 1];
		}

		if ((tmp = strchr(str_c, ';')) != NULL) {
		if ((tmp = strchr(cString, ';')) != NULL) {
			*tmp = '\0';
			parameters = [[OFString alloc]
			    initWithCString: tmp + 1];
		}

		if (*str_c == '/')
			path = [[OFString alloc] initWithCString: str_c];
		if (*cString == '/')
			path = [[OFString alloc] initWithCString: cString];
		else {
			OFAutoreleasePool *pool;
			OFString *s;

			pool = [[OFAutoreleasePool alloc] init];

			if ([url->path hasSuffix: @"/"])
			if ([URL->path hasSuffix: @"/"])
				s = [OFString stringWithFormat: @"%@%s",
								url->path,
								str_c];
								URL->path,
								cString];
			else
				s = [OFString stringWithFormat: @"%@/../%s",
								url->path,
								str_c];
								URL->path,
								cString];

			path = [resolve_relative_path(s) copy];

			[pool release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	} @finally {
		free(str_c2);
		free(cString2);
	}

	return self;
}

- (void)dealloc
{
	[scheme release];
	[host release];
	[user release];
	[password release];
	[path release];
	[parameters release];
	[query release];
	[fragment release];

	[super dealloc];
}

- (BOOL)isEqual: (id)obj
- (BOOL)isEqual: (id)object
{
	OFURL *url;
	OFURL *otherURL;

	if (![obj isKindOfClass: [OFURL class]])
	if (![object isKindOfClass: [OFURL class]])
		return NO;

	url = obj;
	otherURL = (OFURL*)object;

	if (![url->scheme isEqual: scheme])
	if (![otherURL->scheme isEqual: scheme])
		return NO;
	if (![url->host isEqual: host])
	if (![otherURL->host isEqual: host])
		return NO;
	if (url->port != port)
	if (otherURL->port != port)
		return NO;
	if (![url->user isEqual: user])
	if (![otherURL->user isEqual: user])
		return NO;
	if (![url->password isEqual: password])
	if (![otherURL->password isEqual: password])
		return NO;
	if (![url->path isEqual: path])
	if (![otherURL->path isEqual: path])
		return NO;
	if (![url->parameters isEqual: parameters])
	if (![otherURL->parameters isEqual: parameters])
		return NO;
	if (![url->query isEqual: query])
	if (![otherURL->query isEqual: query])
		return NO;
	if (![url->fragment isEqual: fragment])
	if (![otherURL->fragment isEqual: fragment])
		return NO;

	return YES;
}

- (uint32_t)hash
{
	uint32_t hash, h;
	uint32_t hash, tmp;

	OF_HASH_INIT(hash);

	ADD_STR_HASH(scheme);
	ADD_STR_HASH(host);

	OF_HASH_ADD(hash, (port >> 8) & 0xFF);
356
357
358
359
360
361
362
363

364
365
366
367
368
369
370
371
372
373
374









375
376

377
378
379
380

381
382
383
384
385
386
387
388
389
390
391
392
393
394

395
396
397
398
399
400
401
402
403
357
358
359
360
361
362
363

364
365
366









367
368
369
370
371
372
373
374
375
376

377
378
379
380

381
382
383
384
385
386
387
388
389
390
391
392
393
394

395


396
397
398
399
400
401
402







-
+


-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+

-
+



-
+













-
+
-
-







	OF_HASH_FINALIZE(hash);

	return hash;
}

- copy
{
	OFURL *new = [[OFURL alloc] init];
	OFURL *copy = [[OFURL alloc] init];

	@try {
		new->scheme = [scheme copy];
		new->host = [host copy];
		new->port = port;
		new->user = [user copy];
		new->password = [password copy];
		new->path = [path copy];
		new->parameters = [parameters copy];
		new->query = [query copy];
		new->fragment = [fragment copy];
		copy->scheme = [scheme copy];
		copy->host = [host copy];
		copy->port = port;
		copy->user = [user copy];
		copy->password = [password copy];
		copy->path = [path copy];
		copy->parameters = [parameters copy];
		copy->query = [query copy];
		copy->fragment = [fragment copy];
	} @catch (id e) {
		[new release];
		[copy release];
		@throw e;
	}

	return new;
	return copy;
}

- (OFString*)scheme
{
	OF_GETTER(scheme, YES)
}

- (void)setScheme: (OFString*)scheme_
{
	if (![scheme_ isEqual: @"http"] && ![scheme_ isEqual: @"https"])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	OFString *old = scheme;
	OF_SETTER(scheme, scheme_, YES, YES)
	scheme = [scheme_ copy];
	[old release];
}

- (OFString*)host
{
	OF_GETTER(host, YES)
}

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

454
455
456
457
458
459
460
461
462
438
439
440
441
442
443
444


445
446
447
448
449

450


451
452
453
454
455
456
457







-
-





-
+
-
-







- (OFString*)path
{
	OF_GETTER(path, YES)
}

- (void)setPath: (OFString*)path_
{
	OFString *old;

	if (([scheme isEqual: @"http"] || [scheme isEqual: @"https"]) &&
	    ![path_ hasPrefix: @"/"])
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	old = path;
	OF_SETTER(path, path_, YES, YES)
	path = [path_ copy];
	[old release];
}

- (OFString*)parameters
{
	OF_GETTER(parameters, YES)
}

483
484
485
486
487
488
489
490
491


492
493
494
495
496


497
498
499
500

501
502

503
504

505
506
507
508
509
510
511

512
513

514
515
516

517
518
519

520
521
522

523
524
525
526
527
528
529
530


531
532
478
479
480
481
482
483
484


485
486
487
488
489


490
491
492
493
494

495
496

497
498

499
500
501
502
503
504
505

506
507

508
509
510

511
512
513

514
515
516

517
518
519
520
521
522
523


524
525
526
527







-
-
+
+



-
-
+
+



-
+

-
+

-
+






-
+

-
+


-
+


-
+


-
+






-
-
+
+


- (void)setFragment: (OFString*)fragment_
{
	OF_SETTER(fragment, fragment_, YES, YES)
}

- (OFString*)description
{
	OFMutableString *desc = [OFMutableString stringWithFormat: @"%@://",
								   scheme];
	OFMutableString *ret = [OFMutableString stringWithFormat: @"%@://",
								  scheme];
	BOOL needPort = YES;

	if ([scheme isEqual: @"file"]) {
		[desc appendString: path];
		return desc;
		[ret appendString: path];
		return ret;
	}

	if (user != nil && password != nil)
		[desc appendFormat: @"%@:%@@", user, password];
		[ret appendFormat: @"%@:%@@", user, password];
	else if (user != nil)
		[desc appendFormat: @"%@@", user];
		[ret appendFormat: @"%@@", user];

	[desc appendString: host];
	[ret appendString: host];

	if (([scheme isEqual: @"http"] && port == 80) ||
	    ([scheme isEqual: @"https"] && port == 443))
		needPort = NO;

	if (needPort)
		[desc appendFormat: @":%d", port];
		[ret appendFormat: @":%d", port];

	[desc appendString: path];
	[ret appendString: path];

	if (parameters != nil)
		[desc appendFormat: @";%@", parameters];
		[ret appendFormat: @";%@", parameters];

	if (query != nil)
		[desc appendFormat: @"?%@", query];
		[ret appendFormat: @"?%@", query];

	if (fragment != nil)
		[desc appendFormat: @"#%@", fragment];
		[ret appendFormat: @"#%@", fragment];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	desc->isa = [OFString class];
	return desc;
	ret->isa = [OFString class];
	return ret;
}
@end

Modified src/OFXMLElement.h from [11b7a22a69] to [6e4cad8b3b].

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
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







-
+



















-
+




-
+













-
+





-
+




-
+



-
+




-
+


-
+












-
+


-
+















-
+




-
+



















-
+





-
+





-
+


-
+





-
+


-
+













-
+


-
+







	OFString *name;
	OFString *ns;
	OFString *defaultNamespace;
	OFMutableArray *attributes;
	OFMutableDictionary *namespaces;
	OFMutableArray *children;
	OFString *characters;
	OFString *cdata;
	OFString *CDATA;
	OFMutableString *comment;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, copy) OFString *name;
@property (readonly, copy, getter=namespace) OFString *ns;
@property (copy) OFString *defaultNamespace;
@property (readonly, copy) OFArray *attributes;
@property (readonly, copy) OFArray *children;
#endif

/**
 * \param name The name for the element
 * \return A new autoreleased OFXMLElement with the specified element name
 */
+ elementWithName: (OFString*)name;

/**
 * \param name The name for the element
 * \param stringval The value for the element
 * \param stringValue The value for the element
 * \return A new autoreleased OFXMLElement with the specified element name and
 *	   value
 */
+ elementWithName: (OFString*)name
      stringValue: (OFString*)stringval;
      stringValue: (OFString*)stringValue;

/**
 * \param name The name for the element
 * \param ns The namespace for the element
 * \return A new autoreleased OFXMLElement with the specified element name and
 *	   namespace
 */
+ elementWithName: (OFString*)name
	namespace: (OFString*)ns;

/**
 * \param name The name for the element
 * \param ns The namespace for the element
 * \param stringval The value for the element
 * \param stringValue The value for the element
 * \return A new autoreleased OFXMLElement with the specified element name,
 *	   namespace and value
 */
+ elementWithName: (OFString*)name
	namespace: (OFString*)ns
      stringValue: (OFString*)stringval;
      stringValue: (OFString*)stringValue;

/**
 * Creates a new element, only consisting of the specified characters.
 *
 * \param chars The characters the element represents
 * \param characters The characters the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified
 *	   characters
 */
+ elementWithCharacters: (OFString*)chars;
+ elementWithCharacters: (OFString*)characters;

/**
 * Creates a new element, only consisting of the specified CDATA.
 *
 * \param cdata The CDATA the element represents
 * \param CDATA The CDATA the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified CDATA
 */
+ elementWithCDATA: (OFString*)cdata;
+ elementWithCDATA: (OFString*)CDATA;

/**
 * Creates a new element, only consisting of the specified comment.
 *
 * \param comment The comment the element represents
 * \return A new autoreleased OFXMLElement consisting of the specified comment
 */
+ elementWithComment: (OFString*)comment;

/**
 * Parses the string and returns an OFXMLElement for it.
 *
 * \param str The string to parse
 * \param string The string to parse
 * \return A new autoreleased OFXMLElement with the contents of the string
 */
+ elementWithXMLString: (OFString*)str;
+ elementWithXMLString: (OFString*)string;

/**
 * Initializes an already allocated OFXMLElement with the specified element
 * name.
 *
 * \param name The name for the element
 * \return An initialized OFXMLElement with the specified element name
 */
- initWithName: (OFString*)name;

/**
 * Initializes an already allocated OFXMLElement with the specified element
 * name and value.
 *
 * \param name The name for the element
 * \param stringval The value for the element
 * \param stringValue The value for the element
 * \return An initialized OFXMLElement with the specified element name and
 *	   value
 */
- initWithName: (OFString*)name
   stringValue: (OFString*)stringval;
   stringValue: (OFString*)stringValue;

/**
 * Initializes an already allocated OFXMLElement with the specified element
 * name and namespace.
 *
 * \param name The name for the element
 * \param ns The namespace for the element
 * \return An initialized OFXMLElement with the specified element name and
 *	   namespace
 */
- initWithName: (OFString*)name
     namespace: (OFString*)ns;

/**
 * Initializes an already allocated OFXMLElement with the specified element
 * name, namespace and value.
 *
 * \param name The name for the element
 * \param ns The namespace for the element
 * \param stringval The value for the element
 * \param stringValue The value for the element
 * \return An initialized OFXMLElement with the specified element name,
 *	   namespace and value
 */
- initWithName: (OFString*)name
     namespace: (OFString*)ns
   stringValue: (OFString*)stringval;
   stringValue: (OFString*)stringValue;

/**
 * Initializes an already allocated OFXMLElement so that it only consists of the
 * specified characters.
 *
 * \param chars The characters the element represents
 * \param characters The characters the element represents
 * \return An initialized OFXMLElement consisting of the specified characters
 */
- initWithCharacters: (OFString*)chars;
- initWithCharacters: (OFString*)characters;

/**
 * Initializes an already allocated OFXMLElement so that it only consists of the
 * specified CDATA.
 *
 * \param cdata The CDATA the element represents
 * \param CDATA The CDATA the element represents
 * \return An initialized OFXMLElement consisting of the specified CDATA
 */
- initWithCDATA: (OFString*)cdata;
- initWithCDATA: (OFString*)CDATA;

/**
 * Initializes an already allocated OFXMLElement so that it only consists of the
 * specified comment.
 *
 * \param comment The comment the element represents
 * \return An initialized OFXMLElement consisting of the specified comment
 */
- initWithComment: (OFString*)comment;

/**
 * Parses the string and initializes an already allocated OFXMLElement with it.
 *
 * \param str The string to parse
 * \param string The string to parse
 * \return An initialized OFXMLElement with the contents of the string
 */
- initWithXMLString: (OFString*)str;
- initWithXMLString: (OFString*)string;

/**
 * \return The name of the element
 */
- (OFString*)name;

/**
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
304
305
306


307
308
309


310
311
312
313
314
315
316
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
304


305
306
307


308
309
310
311
312
313
314
315
316







-
+

-
+


















-
+

-
+








-
+


-
+









-
+



-
+


-
+


-
+


-
-
+
+


-
-
+
+




-
+

-
+




-
-
+
+

-
-
+
+







 * \return An array with all children of the element
 */
- (OFArray*)children;

/**
 * Removes all children and sets the string value to the specified string.
 *
 * \param value The new string value for the element
 * \param stringValue The new string value for the element
 */
- (void)setStringValue: (OFString*)value;
- (void)setStringValue: (OFString*)stringValue;

/**
 * \return A string with the string value of all children concatenated
 */
- (OFString*)stringValue;

/**
 * \return A new autoreleased OFString representing the OFXMLElement as an
 * XML string
 */
- (OFString*)XMLString;

/**
 * Adds the specified attribute.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param attr The attribute to add
 * \param attribute The attribute to add
 */
- (void)addAttribute: (OFXMLAttribute*)attr;
- (void)addAttribute: (OFXMLAttribute*)attribute;

/**
 * Adds the specified attribute with the specified value.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param name The name of the attribute
 * \param value The value of the attribute
 * \param stringValue The value of the attribute
 */
- (void)addAttributeWithName: (OFString*)name
		 stringValue: (OFString*)value;
		 stringValue: (OFString*)stringValue;

/**
 * Adds the specified attribute with the specified namespace and value.
 *
 * If an attribute with the same name and namespace already exists, it is not
 * added.
 *
 * \param name The name of the attribute
 * \param ns The namespace of the attribute
 * \param value The value of the attribute
 * \param stringValue The value of the attribute
 */
- (void)addAttributeWithName: (OFString*)name
		   namespace: (OFString*)ns
		 stringValue: (OFString*)value;
		 stringValue: (OFString*)stringValue;

/**
 * \param attrname The name of the attribute
 * \param attributeName The name of the attribute
 * \return The attribute with the specified name
 */
- (OFXMLAttribute*)attributeForName: (OFString*)attrname;
- (OFXMLAttribute*)attributeForName: (OFString*)attributeName;

/**
 * \param attrname The name of the attribute
 * \param attrns The namespace of the attribute
 * \param attributeName The name of the attribute
 * \param attributeNS The namespace of the attribute
 * \return The attribute with the specified name and namespace
 */
- (OFXMLAttribute*)attributeForName: (OFString*)attrname
			  namespace: (OFString*)attrns;
- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
			  namespace: (OFString*)attributeNS;

/**
 * Removes the attribute with the specified name.
 *
 * \param attrname The name of the attribute
 * \param attribteName The name of the attribute
 */
- (void)removeAttributeForName: (OFString*)attrname;
- (void)removeAttributeForName: (OFString*)attributeName;

/**
 * Removes the attribute with the specified name and namespace.
 *
 * \param attrname The name of the attribute
 * \param attrns The namespace of the attribute
 * \param attributeName The name of the attribute
 * \param attributeNS The namespace of the attribute
 */
- (void)removeAttributeForName: (OFString*)attrname
		     namespace: (OFString*)attrns;
- (void)removeAttributeForName: (OFString*)attributeName
		     namespace: (OFString*)attributeNS;

/**
 * Sets a prefix for a namespace.
 *
 * \param prefix The prefix for the namespace
 * \param ns The namespace for which the prefix is set
 */
337
338
339
340
341
342
343
344

345
346
347

348
349
350

351
352
353

354
355
356
357


358
359
360
361


362
363
364
365


366
367
368
369


370
337
338
339
340
341
342
343

344
345
346

347
348
349

350
351
352

353
354
355


356
357
358
359


360
361
362
363


364
365
366
367


368
369
370







-
+


-
+


-
+


-
+


-
-
+
+


-
-
+
+


-
-
+
+


-
-
+
+

 * Adds a child to the OFXMLElement.
 *
 * \param child Another OFXMLElement which is added as a child
 */
- (void)addChild: (OFXMLElement*)child;

/**
 * \param elemname The name of the element
 * \param elementName The name of the element
 * \return The first child element with the specified name
 */
- (OFXMLElement*)elementForName: (OFString*)elemname;
- (OFXMLElement*)elementForName: (OFString*)elementName;

/**
 * \param elemname The name of the elements
 * \param elementName The name of the elements
 * \return The child elements with the specified name
 */
- (OFArray*)elementsForName: (OFString*)elemname;
- (OFArray*)elementsForName: (OFString*)elementName;

/**
 * \param elemname The name of the element
 * \param elemns The namespace of the element
 * \param elementName The name of the element
 * \param elementNS The namespace of the element
 * \return The first child element with the specified name and namespace
 */
- (OFXMLElement*)elementForName: (OFString*)elemname
		      namespace: (OFString*)elemns;
- (OFXMLElement*)elementForName: (OFString*)elementName
		      namespace: (OFString*)elementNS;

/**
 * \param elemname The name of the elements
 * \param elemns The namespace of the elements
 * \param elementName The name of the elements
 * \param elementNS The namespace of the elements
 * \return The child elements with the specified name and namespace
 */
- (OFArray*)elementsForName: (OFString*)elemname
		  namespace: (OFString*)elemns;
- (OFArray*)elementsForName: (OFString*)elementName
		  namespace: (OFString*)elementNS;
@end

Modified src/OFXMLElement.m from [1d2471d4ea] to [bc99bd017c].

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
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







-
+

-
+











-
+

-
+



-
+


-
+











-
+



-
+


-
+

-
+


-
+

-
+







-
+

-
+


















-
+



-
+












-
+







-
+




-
+















-
+




-
+








-
+




-
+







@public
	OFXMLElement *element;
}
@end

@implementation OFXMLElement_OFXMLElementBuilderDelegate
- (void)elementBuilder: (OFXMLElementBuilder*)builder
       didBuildElement: (OFXMLElement*)elem
       didBuildElement: (OFXMLElement*)element_
{
	element = [elem retain];
	element = [element_ retain];
}

- (void)dealloc
{
	[element release];

	[super dealloc];
}
@end

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

+ elementWithName: (OFString*)name
      stringValue: (OFString*)stringval
      stringValue: (OFString*)stringValue
{
	return [[[self alloc] initWithName: name
			       stringValue: stringval] autorelease];
			       stringValue: stringValue] autorelease];
}

+ elementWithName: (OFString*)name
	namespace: (OFString*)ns
{
	return [[[self alloc] initWithName: name
				 namespace: ns] autorelease];
}

+ elementWithName: (OFString*)name
	namespace: (OFString*)ns
      stringValue: (OFString*)stringval
      stringValue: (OFString*)stringValue
{
	return [[[self alloc] initWithName: name
				 namespace: ns
			       stringValue: stringval] autorelease];
			       stringValue: stringValue] autorelease];
}

+ elementWithCharacters: (OFString*)chars
+ elementWithCharacters: (OFString*)characters
{
	return [[[self alloc] initWithCharacters: chars] autorelease];
	return [[[self alloc] initWithCharacters: characters] autorelease];
}

+ elementWithCDATA: (OFString*)cdata
+ elementWithCDATA: (OFString*)CDATA
{
	return [[[self alloc] initWithCDATA: cdata] autorelease];
	return [[[self alloc] initWithCDATA: CDATA] autorelease];
}

+ elementWithComment: (OFString*)comment
{
	return [[[self alloc] initWithComment: comment] autorelease];
}

+ elementWithXMLString: (OFString*)str
+ elementWithXMLString: (OFString*)string
{
	return [[[self alloc] initWithXMLString: str] autorelease];
	return [[[self alloc] initWithXMLString: string] autorelease];
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithName: (OFString*)name_
{
	return [self initWithName: name_
			namespace: nil
		      stringValue: nil];
}

- initWithName: (OFString*)name_
   stringValue: (OFString*)stringval
   stringValue: (OFString*)stringValue
{
	return [self initWithName: name_
			namespace: nil
		      stringValue: stringval];
		      stringValue: stringValue];
}

- initWithName: (OFString*)name_
     namespace: (OFString*)ns_
{
	return [self initWithName: name_
			namespace: ns_
		      stringValue: nil];
}

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

	@try {
		name = [name_ copy];
		ns = [ns_ copy];

		if (stringval != nil) {
		if (stringValue != nil) {
			OFAutoreleasePool *pool;

			pool = [[OFAutoreleasePool alloc] init];
			[self addChild:
			    [OFXMLElement elementWithCharacters: stringval]];
			    [OFXMLElement elementWithCharacters: stringValue]];
			[pool release];
		}

		namespaces = [[OFMutableDictionary alloc]
		    initWithKeysAndObjects:
		    @"http://www.w3.org/XML/1998/namespace", @"xml",
		    @"http://www.w3.org/2000/xmlns/", @"xmlns", nil];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCharacters: (OFString*)chars
- initWithCharacters: (OFString*)characters_
{
	self = [super init];

	@try {
		characters = [chars copy];
		characters = [characters_ copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCDATA: (OFString*)cdata_
- initWithCDATA: (OFString*)CDATA_
{
	self = [super init];

	@try {
		cdata = [cdata_ copy];
		CDATA = [CDATA_ copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

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







-
+


















-
+







		[self release];
		@throw e;
	}

	return self;
}

- initWithXMLString: (OFString*)str
- initWithXMLString: (OFString*)string
{
	OFAutoreleasePool *pool;
	OFXMLParser *parser;
	OFXMLElementBuilder *builder;
	OFXMLElement_OFXMLElementBuilderDelegate *delegate;

	[self release];

	pool = [[OFAutoreleasePool alloc] init];

	parser = [OFXMLParser parser];
	builder = [OFXMLElementBuilder elementBuilder];
	delegate = [[[OFXMLElement_OFXMLElementBuilderDelegate alloc] init]
	    autorelease];

	[parser setDelegate: builder];
	[builder setDelegate: delegate];

	[parser parseString: str];
	[parser parseString: string];

	if (![parser finishedParsing])
		@throw [OFMalformedXMLException newWithClass: isa
						      parser: parser];

	self = [delegate->element retain];

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

304
305
306
307
308
309
310
311
312






313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334




335
336
337


338
339
340
341
342
343


344
345

346
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

364
365
366
367
368
369
370



371
372

373
374
375
376
377




378
379

380
381

382
383

384
385
386
387
388


389
390
391

392
393

394
395
396


397
398
399


400
401

402
403
404
405
406

407
408

409
410
411

412
413
414
415

416
417
418


419
420
421
422


423
424

425
426
427
428

429
430

431
432

433
434

435
436
437
438
439


440
441
442
443
444
445




446
447
448
449



450
451
452

453
454
455



456
457
458
459
460


461
462

463
464
465
466
467
468
469
470
471






472
473
474
475
476
477
478






479
480

481
482
483
484
485
486
487
488


489

490
491
492

493
494
495
496


497
498
499


500
501

502
503
504


505
506

507
508
509
510
511


512
513
514


515
516

517
518
519


520
521

522
523
524
525

526
527
528

529
530

531
532
533

534
535
536


537
538
539
540
541
542


543
544

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

560
561
562
563
564
565
566
567
568
569
570



571
572
573
574

575
576
577
578

579
580
581
582
583

584
585
586
587
588
589
590
591
592
593
594

595
596
597
598

599


600
601







602
603
604
605


606
607
608


609
610
611
612
613
614
615
616
617
618
619










620
621
622
623
624
625

626


627
628
629
630
631
632













633
634
635


636
637

638
639
640
641
642

643
644
645




646
647
648
649
650




651
652
653
654
655
656
657
658
659
660
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
304
305
306






307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330




331
332
333
334
335


336
337
338
339
340
341


342
343
344

345


346



347










348

349
350
351
352
353



354
355
356
357

358
359




360
361
362
363
364

365
366

367
368

369
370
371
372


373
374
375
376

377
378

379
380


381
382
383


384
385
386

387
388
389
390


391
392

393
394
395

396
397
398
399

400
401


402
403

404


405
406
407

408
409
410
411

412
413

414
415

416
417

418
419
420
421


422
423
424
425




426
427
428
429
430



431
432
433
434
435

436
437


438
439
440
441
442
443


444
445
446

447
448
449
450






451
452
453
454
455
456
457






458
459
460
461
462
463
464

465
466
467
468
469
470
471


472
473
474
475
476
477

478

479


480
481



482
483
484

485
486


487
488
489

490
491
492
493


494
495
496


497
498
499

500
501


502
503
504

505
506
507
508

509
510
511

512
513

514
515
516

517
518


519
520
521
522
523
524


525
526
527

528
529
530
531
532
533
534
535
536
537
538
539
540
541
542

543
544
545
546
547
548
549
550
551



552
553
554
555
556
557

558
559
560
561

562
563
564
565
566

567
568
569
570
571
572
573
574
575
576
577

578
579
580
581

582
583
584
585


586
587
588
589
590
591
592
593
594


595
596
597


598
599
600










601
602
603
604
605
606
607
608
609
610

611
612
613
614

615
616
617
618






619
620
621
622
623
624
625
626
627
628
629
630
631
632


633
634
635

636





637



638
639
640
641





642
643
644
645

646

647
648
649
650
651
652
653







-
+




-
+








-
+






-
+



-
-
-
-
-
-
+
+
+
+
+
+


















-
-
-
-
+
+
+
+

-
-
+
+




-
-
+
+

-
+
-
-
+
-
-
-

-
-
-
-
-
-
-
-
-
-

-
+




-
-
-
+
+
+

-
+

-
-
-
-
+
+
+
+

-
+

-
+

-
+



-
-
+
+


-
+

-
+

-
-
+
+

-
-
+
+

-
+



-
-
+

-
+


-
+



-
+

-
-
+
+
-

-
-
+
+

-
+



-
+

-
+

-
+

-
+



-
-
+
+


-
-
-
-
+
+
+
+

-
-
-
+
+
+


-
+

-
-
+
+
+



-
-
+
+

-
+



-
-
-
-
-
-
+
+
+
+
+
+

-
-
-
-
-
-
+
+
+
+
+
+

-
+






-
-
+
+

+


-
+
-

-
-
+
+
-
-
-
+
+

-
+

-
-
+
+

-
+



-
-
+
+

-
-
+
+

-
+

-
-
+
+

-
+



-
+


-
+

-
+


-
+

-
-
+
+




-
-
+
+

-
+














-
+








-
-
-
+
+
+



-
+



-
+




-
+










-
+



-
+

+
+
-
-
+
+
+
+
+
+
+


-
-
+
+

-
-
+
+

-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-




-
+

+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+

-
-
+
+

-
+
-
-
-
-
-
+
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
-

-







}

- (OFArray*)children
{
	return [[children copy] autorelease];
}

- (void)setStringValue: (OFString*)value
- (void)setStringValue: (OFString*)stringValue
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	[self setChildren: [OFArray arrayWithObject:
	    [OFXMLElement elementWithCharacters: value]]];
	    [OFXMLElement elementWithCharacters: stringValue]]];

	[pool release];
}

- (OFString*)stringValue
{
	OFAutoreleasePool *pool;
	OFMutableString *ret;
	OFXMLElement **children_c;
	OFXMLElement **cArray;
	size_t i, count = [children count];

	if (count == 0)
		return @"";

	ret = [OFMutableString string];
	children_c = [children cArray];
	cArray = [children cArray];
	pool = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count; i++) {
		if (children_c[i]->characters != nil)
			[ret appendString: children_c[i]->characters];
		else if (children_c[i]->cdata != nil)
			[ret appendString: children_c[i]->cdata];
		else if (children_c[i]->comment == nil) {
			[ret appendString: [children_c[i] stringValue]];
		if (cArray[i]->characters != nil)
			[ret appendString: cArray[i]->characters];
		else if (cArray[i]->CDATA != nil)
			[ret appendString: cArray[i]->CDATA];
		else if (cArray[i]->comment == nil) {
			[ret appendString: [cArray[i] stringValue]];
			[pool releaseObjects];
		}
	}

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	ret->isa = [OFString class];
	return ret;
}

- (OFString*)_XMLStringWithParent: (OFXMLElement*)parent
{
	OFAutoreleasePool *pool, *pool2;
	char *str_c;
	size_t len, i, j, attrs_count;
	OFString *prefix, *parent_prefix;
	OFXMLAttribute **attrs_carray;
	char *cString;
	size_t length, i, j, attributesCount;
	OFString *prefix, *parentPrefix;
	OFXMLAttribute **attributesCArray;
	OFString *ret, *tmp;
	OFMutableDictionary *all_namespaces;
	OFString *def_ns;
	OFMutableDictionary *allNamespaces;
	OFString *defaultNS;

	if (characters != nil)
		return [characters stringByXMLEscaping];

	if (cdata != nil)
		return [OFString stringWithFormat: @"<![CDATA[%@]]>", cdata];
	if (CDATA != nil)
		return [OFString stringWithFormat: @"<![CDATA[%@]]>", CDATA];

	if (comment != nil) {
	if (comment != nil)
		OFMutableString *str;

		return [OFString stringWithFormat: @"<!--%@-->", comment];
		str = [OFMutableString stringWithString: @"<!--"];
		[str appendString: comment];
		[str appendString: @"-->"];

		/*
		 * Class swizzle the string to be immutable. We declared the
		 * return type to be OFString*, so it can't be modified anyway.
		 * But not swizzling it would create a real copy each time
		 * -[copy] is called.
		 */
		str->isa = [OFString class];
		return str;
	}

	pool = [[OFAutoreleasePool alloc] init];
	def_ns = (defaultNamespace != nil
	defaultNS = (defaultNamespace != nil
	    ? defaultNamespace
	    : (parent != nil ? parent->defaultNamespace : (OFString*)nil));

	if (parent != nil && parent->namespaces != nil) {
		OFEnumerator *key_enum = [namespaces keyEnumerator];
		OFEnumerator *obj_enum = [namespaces objectEnumerator];
		id key, obj;
		OFEnumerator *keyEnumerator = [namespaces keyEnumerator];
		OFEnumerator *objectEnumerator = [namespaces objectEnumerator];
		id key, object;

		all_namespaces = [[parent->namespaces mutableCopy] autorelease];
		allNamespaces = [[parent->namespaces mutableCopy] autorelease];

		while ((key = [key_enum nextObject]) != nil &&
		    (obj = [obj_enum nextObject]) != nil)
			[all_namespaces setObject: obj
					   forKey: key];
		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil)
			[allNamespaces setObject: object
					  forKey: key];
	} else
		all_namespaces = namespaces;
		allNamespaces = namespaces;

	prefix = [all_namespaces objectForKey:
	prefix = [allNamespaces objectForKey:
	    (ns != nil ? ns : (OFString*)@"")];
	parent_prefix = [all_namespaces objectForKey:
	parentPrefix = [allNamespaces objectForKey:
	    (parent != nil && parent->ns != nil ? parent->ns : (OFString*)@"")];

	i = 0;
	len = [name cStringLength] + 3;
	str_c = [self allocMemoryWithSize: len];
	length = [name cStringLength] + 3;
	cString = [self allocMemoryWithSize: length];

	/* Start of tag */
	str_c[i++] = '<';
	cString[i++] = '<';

	if (prefix != nil && ![ns isEqual: def_ns] &&
	if (prefix != nil && ![ns isEqual: defaultNS] &&
	    (![ns isEqual: (parent != nil ? parent->ns : (OFString*)nil)] ||
	    parent_prefix != nil)) {
		len += [prefix cStringLength] + 1;
	    parentPrefix != nil)) {
		length += [prefix cStringLength] + 1;
		@try {
			str_c = [self resizeMemory: str_c
					    toSize: len];
			cString = [self resizeMemory: cString
					      toSize: length];
		} @catch (id e) {
			[self freeMemory: str_c];
			[self freeMemory: cString];
			@throw e;
		}

		memcpy(str_c + i, [prefix cString],
		    [prefix cStringLength]);
		memcpy(cString + i, [prefix cString], [prefix cStringLength]);
		i += [prefix cStringLength];
		str_c[i++] = ':';
		cString[i++] = ':';
	}

	memcpy(str_c + i, [name cString], [name cStringLength]);
	memcpy(cString + i, [name cString], [name cStringLength]);
	i += [name cStringLength];

	/* xmlns if necessary */
	if (ns != nil && prefix == nil && ![ns isEqual: def_ns] &&
	if (ns != nil && prefix == nil && ![ns isEqual: defaultNS] &&
	     (![ns isEqual: (parent != nil ? parent->ns : (OFString*)nil)] ||
	     parent_prefix != nil)) {
		len += [ns cStringLength] + 9;
	     parentPrefix != nil)) {
		length += [ns cStringLength] + 9;

		@try {
			str_c = [self resizeMemory: str_c
					    toSize: len];
			cString = [self resizeMemory: cString
					      toSize: length];
		} @catch (id e) {
			[self freeMemory: str_c];
			[self freeMemory: cString];
			@throw e;
		}

		memcpy(str_c + i, " xmlns='", 8);
		memcpy(cString + i, " xmlns='", 8);
		i += 8;
		memcpy(str_c + i, [ns cString], [ns cStringLength]);
		memcpy(cString + i, [ns cString], [ns cStringLength]);
		i += [ns cStringLength];
		str_c[i++] = '\'';
		cString[i++] = '\'';

		def_ns = ns;
		defaultNS = ns;
	}

	/* Attributes */
	attrs_carray = [attributes cArray];
	attrs_count = [attributes count];
	attributesCArray = [attributes cArray];
	attributesCount = [attributes count];

	pool2 = [[OFAutoreleasePool alloc] init];
	for (j = 0; j < attrs_count; j++) {
		OFString *attr_name = [attrs_carray[j] name];
		OFString *attr_prefix = nil;
		tmp = [[attrs_carray[j] stringValue] stringByXMLEscaping];
	for (j = 0; j < attributesCount; j++) {
		OFString *attributeName = [attributesCArray[j] name];
		OFString *attributePrefix = nil;
		tmp = [[attributesCArray[j] stringValue] stringByXMLEscaping];

		if ([attrs_carray[j] namespace] != nil &&
		    (attr_prefix = [all_namespaces objectForKey:
		    [attrs_carray[j] namespace]]) == nil)
		if ([attributesCArray[j] namespace] != nil &&
		    (attributePrefix = [allNamespaces objectForKey:
		    [attributesCArray[j] namespace]]) == nil)
			@throw [OFUnboundNamespaceException
			    newWithClass: isa
			       namespace: [attrs_carray[j] namespace]];
			       namespace: [attributesCArray[j] namespace]];

		len += [attr_name cStringLength] +
		    (attr_prefix != nil ? [attr_prefix cStringLength] + 1 : 0) +
		length += [attributeName cStringLength] +
		    (attributePrefix != nil ?
		    [attributePrefix cStringLength] + 1 : 0) +
		    [tmp cStringLength] + 4;

		@try {
			str_c = [self resizeMemory: str_c
					    toSize: len];
			cString = [self resizeMemory: cString
					      toSize: length];
		} @catch (id e) {
			[self freeMemory: str_c];
			[self freeMemory: cString];
			@throw e;
		}

		str_c[i++] = ' ';
		if (attr_prefix != nil) {
			memcpy(str_c + i, [attr_prefix cString],
			    [attr_prefix cStringLength]);
			i += [attr_prefix cStringLength];
			str_c[i++] = ':';
		cString[i++] = ' ';
		if (attributePrefix != nil) {
			memcpy(cString + i, [attributePrefix cString],
			    [attributePrefix cStringLength]);
			i += [attributePrefix cStringLength];
			cString[i++] = ':';
		}
		memcpy(str_c + i, [attr_name cString],
				[attr_name cStringLength]);
		i += [attr_name cStringLength];
		str_c[i++] = '=';
		str_c[i++] = '\'';
		memcpy(str_c + i, [tmp cString], [tmp cStringLength]);
		memcpy(cString + i, [attributeName cString],
		    [attributeName cStringLength]);
		i += [attributeName cStringLength];
		cString[i++] = '=';
		cString[i++] = '\'';
		memcpy(cString + i, [tmp cString], [tmp cStringLength]);
		i += [tmp cStringLength];
		str_c[i++] = '\'';
		cString[i++] = '\'';

		[pool2 releaseObjects];
	}

	/* Childen */
	if (children != nil) {
		OFXMLElement **children_carray = [children cArray];
		size_t children_count = [children count];
		OFXMLElement **childrenCArray = [children cArray];
		size_t childrenCount = [children count];
		IMP append;
		SEL appendSel = @selector(appendCStringWithoutUTF8Checking:);

		tmp = [OFMutableString string];
		append = [tmp methodForSelector:
		append = [tmp methodForSelector: appendSel];
		    @selector(appendCStringWithoutUTF8Checking:)];

		for (j = 0; j < children_count; j++)
			append(tmp, @selector(
		for (j = 0; j < childrenCount; j++)
			append(tmp, appendSel,
			    appendCStringWithoutUTF8Checking:),
			    [[children_carray[j]
			    _XMLStringWithParent: self] cString]);
			    [[childrenCArray[j] _XMLStringWithParent: self]
			    cString]);

		len += [tmp cStringLength] + [name cStringLength] + 2;
		length += [tmp cStringLength] + [name cStringLength] + 2;
		@try {
			str_c = [self resizeMemory: str_c
					    toSize: len];
			cString = [self resizeMemory: cString
					      toSize: length];
		} @catch (id e) {
			[self freeMemory: str_c];
			[self freeMemory: cString];
			@throw e;
		}

		str_c[i++] = '>';
		memcpy(str_c + i, [tmp cString], [tmp cStringLength]);
		cString[i++] = '>';
		memcpy(cString + i, [tmp cString], [tmp cStringLength]);
		i += [tmp cStringLength];
		str_c[i++] = '<';
		str_c[i++] = '/';
		cString[i++] = '<';
		cString[i++] = '/';
		if (prefix != nil) {
			len += [prefix cStringLength] + 1;
			length += [prefix cStringLength] + 1;
			@try {
				str_c = [self resizeMemory: str_c
						    toSize: len];
				cString = [self resizeMemory: cString
						      toSize: length];
			} @catch (id e) {
				[self freeMemory: str_c];
				[self freeMemory: cString];
				@throw e;
			}

			memcpy(str_c + i, [prefix cString],
			memcpy(cString + i, [prefix cString],
			    [prefix cStringLength]);
			i += [prefix cStringLength];
			str_c[i++] = ':';
			cString[i++] = ':';
		}
		memcpy(str_c + i, [name cString], [name cStringLength]);
		memcpy(cString + i, [name cString], [name cStringLength]);
		i += [name cStringLength];
	} else
		str_c[i++] = '/';
		cString[i++] = '/';

	str_c[i++] = '>';
	assert(i == len);
	cString[i++] = '>';
	assert(i == length);

	[pool release];

	@try {
		ret = [OFString stringWithCString: str_c
					   length: len];
		ret = [OFString stringWithCString: cString
					   length: length];
	} @finally {
		[self freeMemory: str_c];
		[self freeMemory: cString];
	}
	return ret;
}

- (OFString*)XMLString
{
	return [self _XMLStringWithParent: nil];
}

- (OFString*)description
{
	return [self XMLString];
}

- (void)addAttribute: (OFXMLAttribute*)attr
- (void)addAttribute: (OFXMLAttribute*)attribute
{
	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if (attributes == nil)
		attributes = [[OFMutableArray alloc] init];

	if ([self attributeForName: attr->name
			 namespace: attr->ns] == nil)
		[attributes addObject: attr];
	if ([self attributeForName: attribute->name
			 namespace: attribute->ns] == nil)
		[attributes addObject: attribute];
}

- (void)addAttributeWithName: (OFString*)name_
		 stringValue: (OFString*)value
		 stringValue: (OFString*)stringValue
{
	[self addAttributeWithName: name_
			 namespace: nil
		       stringValue: value];
		       stringValue: stringValue];
}

- (void)addAttributeWithName: (OFString*)name_
		   namespace: (OFString*)ns_
		 stringValue: (OFString*)value
		 stringValue: (OFString*)stringValue
{
	OFAutoreleasePool *pool;

	if (name == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	pool = [[OFAutoreleasePool alloc] init];
	[self addAttribute: [OFXMLAttribute attributeWithName: name_
						    namespace: ns_
						  stringValue: value]];
						  stringValue: stringValue]];
	[pool release];
}

- (OFXMLAttribute*)attributeForName: (OFString*)attrname
- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
{
	OFXMLAttribute **cArray = [attributes cArray];
	size_t i, count = [attributes count];
	return [self attributeForName: attrname
			    namespace: nil];

	for (i = 0; i < count; i++)
		if (cArray[i]->ns == nil &&
		    [cArray[i]->name isEqual: attributeName])
			return cArray[i];

	return nil;
}

- (OFXMLAttribute*)attributeForName: (OFString*)attrname
			  namespace: (OFString*)attrns
- (OFXMLAttribute*)attributeForName: (OFString*)attributeName
			  namespace: (OFString*)attributeNS
{
	OFXMLAttribute **attrs_c = [attributes cArray];
	size_t i, attrs_count = [attributes count];
	OFXMLAttribute **cArray;
	size_t i, count;

	if (attrns != nil) {
		for (i = 0; i < attrs_count; i++)
			if ([attrs_c[i]->ns isEqual: attrns] &&
			    [attrs_c[i]->name isEqual: attrname])
				return attrs_c[i];
	} else {
		for (i = 0; i < attrs_count; i++)
			if (attrs_c[i]->ns == nil &&
			    [attrs_c[i]->name isEqual: attrname])
				return attrs_c[i];
	if (attributeNS == nil)
		return [self attributeForName: attributeName];

	cArray = [attributes cArray];
	count = [attributes count];

	for (i = 0; i < count; i++)
		if ([cArray[i]->ns isEqual: attributeNS] &&
		    [cArray[i]->name isEqual: attributeName])
			return cArray[i];
	}

	return nil;
}

- (void)removeAttributeForName: (OFString*)attrname
- (void)removeAttributeForName: (OFString*)attributeName
{
	OFXMLAttribute **cArray = [attributes cArray];
	size_t i, count = [attributes count];
	[self removeAttributeForName: attrname
			   namespace: nil];
}

- (void)removeAttributeForName: (OFString*)attrname
		     namespace: (OFString*)attrns

	for (i = 0; i < count; i++) {
		if (cArray[i]->ns == nil &&
		    [cArray[i]->name isEqual: attributeName]) {
			[attributes removeObjectAtIndex: i];

			return;
		}
	}
}

- (void)removeAttributeForName: (OFString*)attributeName
		     namespace: (OFString*)attributeNS
{
	OFXMLAttribute **attrs_c = [attributes cArray];
	size_t i, attrs_count = [attributes count];
	OFXMLAttribute **cArray;
	size_t i, count;

	if (attrns != nil) {
	if (attributeNS == nil)
		for (i = 0; i < attrs_count; i++) {
			if ([attrs_c[i]->ns isEqual: attrns] &&
			    [attrs_c[i]->name isEqual: attrname]) {
				[attributes removeObjectAtIndex: i];

		return [self removeAttributeForName: attributeName];
				return;
			}
		}

	cArray = [attributes cArray];
	count = [attributes count];

	} else {
		for (i = 0; i < attrs_count; i++) {
			if (attrs_c[i]->ns == nil &&
			    [attrs_c[i]->name isEqual: attrname]) {
				[attributes removeObjectAtIndex: i];
	for (i = 0; i < count; i++) {
		if ([cArray[i]->ns isEqual: attributeNS] &&
		    [cArray[i]->name isEqual: attributeName]) {
			[attributes removeObjectAtIndex: i];

				return;
			}
		}
	}
}

- (void)setPrefix: (OFString*)prefix
     forNamespace: (OFString*)ns_
{
706
707
708
709
710
711
712
713

714
715

716
717
718
719


720
721
722


723
724
725

726
727

728
729
730







731



732
733


734
735
736
737



738
739
740
741
742
743







744
745
746
747
748




749
750
751
752
753
754
755
756
757
758
759
760
761
762

763
764
765
766
767
699
700
701
702
703
704
705

706
707

708

709
710

711
712
713


714
715
716
717

718

719
720



721
722
723
724
725
726
727
728
729
730
731


732
733
734



735
736
737
738





739
740
741
742
743
744
745





746
747
748
749

750
751
752
753
754
755
756
757
758
759
760
761

762
763
764
765
766
767







-
+

-
+
-


-
+
+

-
-
+
+


-
+
-

+
-
-
-
+
+
+
+
+
+
+

+
+
+
-
-
+
+

-
-
-
+
+
+

-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-












-
+






	if (children == nil)
		children = [[OFMutableArray alloc] init];

	[children addObject: child];
}

- (OFXMLElement*)elementForName: (OFString*)elemname
- (OFXMLElement*)elementForName: (OFString*)elementName
{
	return [self elementForName: elemname
	return [[self elementsForName: elementName] firstObject];
			  namespace: nil];
}

- (OFArray*)elementsForName: (OFString*)elemname
- (OFXMLElement*)elementForName: (OFString*)elementName
		      namespace: (OFString*)elementNS
{
	return [self elementsForName: elemname
			   namespace: nil];
	return [[self elementsForName: elementName
			    namespace: elementNS] firstObject];
}

- (OFXMLElement*)elementForName: (OFString*)elemname
- (OFArray*)elementsForName: (OFString*)elementName
		      namespace: (OFString*)elemns
{
	OFMutableArray *ret = [OFMutableArray array];
	return [[self elementsForName: elemname
			    namespace: elemns] firstObject];
}
	OFXMLElement **cArray = [children cArray];
	size_t i, count = [children count];

	for (i = 0; i < count; i++)
		if (cArray[i]->ns == nil &&
		    [cArray[i]->name isEqual: elementName])
			[ret addObject: cArray[i]];

	return ret;
}

- (OFArray*)elementsForName: (OFString*)elemname
		  namespace: (OFString*)elemns
- (OFArray*)elementsForName: (OFString*)elementName
		  namespace: (OFString*)elementNS
{
	OFMutableArray *ret = [OFMutableArray array];
	OFXMLElement **children_c = [children cArray];
	size_t i, children_count = [children count];
	OFMutableArray *ret;
	OFXMLElement **cArray;
	size_t i, count;

	if (elemns != nil) {
		for (i = 0; i < children_count; i++)
			if ([children_c[i]->ns isEqual: elemns] &&
			    [children_c[i]->name isEqual: elemname])
				[ret addObject: children_c[i]];
	if (elementNS == nil)
		return [self elementsForName: elementName];

	ret = [OFMutableArray array];
	cArray = [children cArray];
	count = [children count];

	} else {
		for (i = 0; i < children_count; i++)
			if (children_c[i]->ns == nil &&
			    [children_c[i]->name isEqual: elemname])
				[ret addObject: children_c[i]];
	for (i = 0; i < count; i++)
		if ([cArray[i]->ns isEqual: elementNS] &&
		    [cArray[i]->name isEqual: elementName])
			[ret addObject: cArray[i]];
	}

	return ret;
}

- (void)dealloc
{
	[name release];
	[ns release];
	[attributes release];
	[namespaces release];
	[children release];
	[characters release];
	[cdata release];
	[CDATA release];
	[comment release];

	[super dealloc];
}
@end