ObjFW  Check-in [cb70f59a63]

Overview
Comment:Add -[readTillDelimiter:] to OFStream and improve -[readLine].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cb70f59a63ed072d82386953e138a0dd723f3e64c6f0eb752527eef9e6fe98ce
User & Date: js on 2010-02-20 14:44:10
Other Links: manifest | tags
Context
2010-02-24
22:56
Merge to fix wrong parent of last commit. check-in: b190519866 user: js tags: trunk
2010-02-20
14:44
Add -[readTillDelimiter:] to OFStream and improve -[readLine]. check-in: cb70f59a63 user: js tags: trunk
2010-02-16
12:11
Fix a typo. check-in: cdea8f326d user: js tags: trunk
Changes

Modified src/OFStream.h from [8ae41c981b] to [3e6cb38d4d].

81
82
83
84
85
86
87

88
89
90
91
92




















93
94
95
96
97
98
99
 */
- (OFString*)readLine;

/**
 * Read with the specified encoding until a newline, \\0 or end of stream
 * occurs.
 *

 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLineWithEncoding: (enum of_string_encoding)encoding;





















/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */







>





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







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
 */
- (OFString*)readLine;

/**
 * Read with the specified encoding until a newline, \\0 or end of stream
 * occurs.
 *
 * \param encoding The encoding used by the stream
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readLineWithEncoding: (enum of_string_encoding)encoding;

/**
 * Read until the specified string or \\0 is found or the end of stream occurs.
 *
 * \param delimiter The delimiter
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readTillDelimiter: (OFString*)delimiter;

/**
 * Read until the specified string or \\0 is found or the end of stream occurs.
 *
 * \param delimiter The delimiter
 * \param encoding The encoding used by the stream
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (enum of_string_encoding)encoding;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */

Modified src/OFStream.m from [b0b53ca7b3] to [6d96f43f90].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#import "OFStream.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"

#ifdef _WIN32
#include <windows.h>
#endif

static int pagesize = 0;

@implementation OFStream
- init
{







|







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#import "OFStream.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"

#ifdef _WIN32
# include <windows.h>
#endif

static int pagesize = 0;

@implementation OFStream
- init
{
126
127
128
129
130
131
132

133

134
135
136
137
138
139
140

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: ret_len];

				tmp = [self allocMemoryWithSize: cache_len -
								 i - 1];

				memcpy(tmp, cache + i + 1, cache_len - i - 1);


				[self freeMemory: cache];
				cache = tmp;
				cache_len -= i + 1;

				return ret;
			}







>
|
>







126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: ret_len];

				tmp = [self allocMemoryWithSize: cache_len -
								 i - 1];
				if (tmp != NULL)
					memcpy(tmp, cache + i + 1,
					    cache_len - i - 1);

				[self freeMemory: cache];
				cache = tmp;
				cache_len -= i + 1;

				return ret;
			}
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
						    stringWithCString: ret_c
							     encoding: encoding
							       length: ret_len];
					} @finally {
						[self freeMemory: ret_c];
					}

					if (i < len) {
						tmp2 = [self
						    allocMemoryWithSize: len -
									 i - 1];

						memcpy(tmp2, tmp + i + 1,
						    len - i - 1);

						[self freeMemory: cache];
						cache = tmp2;
						cache_len = len - i - 1;
					} else {
						[self freeMemory: cache];
						cache = NULL;
						cache_len = 0;
					}

					return ret;
				}
			}

			/* There was no newline or \0 */
			cache = [self resizeMemory: cache
					    toSize: cache_len + len];















































































































































			/*
			 * It's possible that cache_len + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cache_len, tmp, len);








<
|
|
<
>



|
|
|
<
<
<
<
<









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







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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
						    stringWithCString: ret_c
							     encoding: encoding
							       length: ret_len];
					} @finally {
						[self freeMemory: ret_c];
					}


					tmp2 = [self
					    allocMemoryWithSize: len - i - 1];

					if (tmp2 != NULL)
						memcpy(tmp2, tmp + i + 1,
						    len - i - 1);

					[self freeMemory: cache];
					cache = tmp2;
					cache_len = len - i - 1;






					return ret;
				}
			}

			/* There was no newline or \0 */
			cache = [self resizeMemory: cache
					    toSize: cache_len + len];

			/*
			 * It's possible that cache_len + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cache_len, tmp, len);

			cache_len += len;
		}
	} @finally {
		[self freeMemory: tmp];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (enum of_string_encoding)encoding
{
	const char *delim;
	size_t i, j, delim_len, len, ret_len;
	char *ret_c, *tmp, *tmp2;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delim = [delimiter cString];
	delim_len = [delimiter cStringLength];
	j = 0;

	if (delim_len == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	/* Look if there's something in our cache */
	if (cache != NULL) {
		for (i = 0; i < cache_len; i++) {
			if (cache[i] != delim[j++])
				j = 0;

			if (j == delim_len || cache[i] == '\0') {
				if (cache[i] == '\0')
					delim_len = 1;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: i + 1 -
								   delim_len];

				tmp = [self allocMemoryWithSize: cache_len - i -
								 1];
				if (tmp != NULL)
					memcpy(tmp, cache + i + 1,
					    cache_len - i - 1);

				[self freeMemory: cache];
				cache = tmp;
				cache_len -= i + 1;

				return ret;
			}
		}
	}

	/* Read until we get the delimiter or \0 */
	tmp = [self allocMemoryWithSize: pagesize];

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cache_len];

				[self freeMemory: cache];
				cache = NULL;
				cache_len = 0;

				return ret;
			}

			len = [self readNBytesWithoutCache: pagesize
						intoBuffer: tmp];

			/* Look if there's the delimiter or \0 */
			for (i = 0; i < len; i++) {
				if (tmp[i] != delim[j++])
					j = 0;

				if (j == delim_len || tmp[i] == '\0') {
					if (tmp[i] == '\0')
						delim_len = 1;

					ret_len = cache_len + i + 1 - delim_len;
					ret_c = [self
					    allocMemoryWithSize: ret_len];

					if (cache != NULL &&
					    cache_len <= ret_len)
						memcpy(ret_c, cache, cache_len);
					else if (cache != NULL)
						memcpy(ret_c, cache, ret_len);
					if (i >= delim_len)
						memcpy(ret_c + cache_len, tmp,
					    	    i + 1 - delim_len);

					@try {
						ret = [OFString
						    stringWithCString: ret_c
							     encoding: encoding
							       length: ret_len];
					} @finally {
						[self freeMemory: ret_c];
					}

					tmp2 = [self
					    allocMemoryWithSize: len - i - 1];
					if (tmp2 != NULL)
						memcpy(tmp2, tmp + i + 1,
						    len - i - 1);

					[self freeMemory: cache];
					cache = tmp2;
					cache_len = len - i - 1;

					return ret;
				}
			}

			/* Neither the delimiter nor \0 was found */
			cache = [self resizeMemory: cache
					    toSize: cache_len + len];

			/*
			 * It's possible that cache_len + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cache_len, tmp, len);

Modified src/objc_properties.m from [d54a221003] to [af8db8be34].

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <assert.h>

#import <objc/objc.h>

#import "OFExceptions.h"

#ifdef OF_THREADS
#import "threading.h"

#define NUM_SPINLOCKS 8	/* needs to be a power of 2 */
#define SPINLOCK_HASH(p) ((uintptr_t)p >> 4) & (NUM_SPINLOCKS - 1)
static of_spinlock_t spinlocks[NUM_SPINLOCKS];
#endif

BOOL
objc_properties_init()
{
#ifdef OF_THREADS







|
<
|
|







14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
#include <assert.h>

#import <objc/objc.h>

#import "OFExceptions.h"

#ifdef OF_THREADS
# import "threading.h"

# define NUM_SPINLOCKS 8	/* needs to be a power of 2 */
# define SPINLOCK_HASH(p) ((uintptr_t)p >> 4) & (NUM_SPINLOCKS - 1)
static of_spinlock_t spinlocks[NUM_SPINLOCKS];
#endif

BOOL
objc_properties_init()
{
#ifdef OF_THREADS