ObjFW  Check-in [fc0b2500b9]

Overview
Comment:Make OFStream caching invisible to the programmer.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fc0b2500b9bd2aba1fb4a623f6af9e5e358a38a2ed32feea0ae0bdd8d8f1dc8b
User & Date: js on 2009-12-03 18:50:41
Other Links: manifest | tags
Context
2009-12-05
10:45
Move LDFLAGS for rpath in objfw-config to a separate --rpath flag. check-in: d4a60cdef5 user: js tags: trunk
2009-12-03
18:50
Make OFStream caching invisible to the programmer. check-in: fc0b2500b9 user: js tags: trunk
11:51
Add -[componentsJoinedByString:] to OFArray. check-in: fb651428aa user: js tags: trunk
Changes

Modified TODO from [dbceb3bf95] to [83c7dabc44].

1
2
3
4
5
6
7
8
9
10
11
Test if autorelease pool releases everything correctly when thread is ended

Make strings \0-safe
Make OFStream caching transparent to the coder

Cleanup OFXMLElement

Add finalization to OFXMLParser
Add namespaces to OFXMLParser
Add support for different encodings to OFXMLParser




<







1
2
3

4
5
6
7
8
9
10
Test if autorelease pool releases everything correctly when thread is ended

Make strings \0-safe


Cleanup OFXMLElement

Add finalization to OFXMLParser
Add namespaces to OFXMLParser
Add support for different encodings to OFXMLParser

Modified src/OFExceptions.h from [d069b97d96] to [1cdedb1af1].

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

/**
 * An OFException indicating a read or write to the file failed.
 */
@interface OFReadOrWriteFailedException: OFException
{
	size_t req_size;
	size_t req_items;
	BOOL   has_items;
	int    err;
}

/**
 * \param class_ The class of the object which caused the exception
 * \param size The requested size of the data that couldn't be read / written
 * \param items The requested number of items that couldn't be read / written
 * \return A new open file failed exception
 */
+ newWithClass: (Class)class_
	  size: (size_t)size
	 items: (size_t)items;

/**
 * \param class_ The class of the object which caused the exception
 * \param size The requested size of the data that couldn't be read / written
 * \return A new open file failed exception
 */
+ newWithClass: (Class)class_
	  size: (size_t)size;

/**
 * Initializes an already allocated read or write failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param size The requested size of the data that couldn't be read / written
 * \param items The requested number of items that couldn't be read / written
 * \return A new open file failed exception
 */
- initWithClass: (Class)class_
	   size: (size_t)size
	  items: (size_t)items;

/**
 * Initializes an already allocated read or write failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param size The requested size of the data that couldn't be read / written
 * \return A new open file failed exception
 */
- initWithClass: (Class)class_
	   size: (size_t)size;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The requested size of the data that couldn't be read / written
 */
- (size_t)requestedSize;

/**
 * \return The requested number of items that coudln't be read / written
 */
- (size_t)requestedItems;

/**
 * \return Whether NItems was specified
 */
- (BOOL)hasNItems;
@end

/**
 * An OFException indicating a read to the file failed.
 */
@interface OFReadFailedException: OFReadOrWriteFailedException {}
@end







<
<



<
<
<
<
<
<
<
<
<
<








<
<
<
<
<
<
<
<
<
<
<
<



















<
<
<
<
<
<
<
<
<
<







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

/**
 * An OFException indicating a read or write to the file failed.
 */
@interface OFReadOrWriteFailedException: OFException
{
	size_t req_size;


	int    err;
}











/**
 * \param class_ The class of the object which caused the exception
 * \param size The requested size of the data that couldn't be read / written
 * \return A new open file failed exception
 */
+ newWithClass: (Class)class_
	  size: (size_t)size;













/**
 * Initializes an already allocated read or write failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param size The requested size of the data that couldn't be read / written
 * \return A new open file failed exception
 */
- initWithClass: (Class)class_
	   size: (size_t)size;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The requested size of the data that couldn't be read / written
 */
- (size_t)requestedSize;










@end

/**
 * An OFException indicating a read to the file failed.
 */
@interface OFReadFailedException: OFReadOrWriteFailedException {}
@end

Modified src/OFExceptions.m from [58c24c0199] to [528afc5c1f].

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
	return mode;
}
@end

@implementation OFReadOrWriteFailedException
+ newWithClass: (Class)class__
	  size: (size_t)size
	 items: (size_t)items
{
	return [[self alloc] initWithClass: class__
				      size: size
				     items: items];
}

+ newWithClass: (Class)class__
	  size: (size_t)size
{
	return [[self alloc] initWithClass: class__
				      size: size];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithClass: (Class)class__
	   size: (size_t)size
	  items: (size_t)items
{
	self = [super initWithClass: class__];

	req_size = size;
	req_items = items;
	has_items = YES;

	if (class__ == [OFTCPSocket class])
		err = GET_SOCK_ERR;
	else
		err = GET_ERR;

	return self;
}

- initWithClass: (Class)class__
	   size: (size_t)size
{
	self = [super initWithClass: class__];

	req_size = size;
	req_items = 0;
	has_items = NO;

	if (class__ == [OFTCPSocket class])
		err = GET_SOCK_ERR;
	else
		err = GET_ERR;

	return self;
}

- (int)errNo
{
	return err;
}

- (size_t)requestedSize
{
	return req_size;
}

- (size_t)requestedItems
{
	return req_items;
}

- (BOOL)hasNItems
{
	return has_items;
}
@end

@implementation OFReadFailedException
- (OFString*)string
{
	if (string != nil)
		return string;;

	if (has_items)
		string = [[OFString alloc] initWithFormat:
		    @"Failed to read %zu items of size %zu in class %s! "
		    ERRFMT, req_items, req_size, [class_ className], ERRPARAM];
	else
		string = [[OFString alloc] initWithFormat:
		    @"Failed to read %zu bytes in class %s! " ERRFMT, req_size,
		    [class_ className], ERRPARAM];

	return string;
}
@end

@implementation OFWriteFailedException
- (OFString*)string
{
	if (string != nil)
		return string;

	if (has_items)
		string = [[OFString alloc] initWithFormat:
		    @"Failed to write %zu items of size %zu in class %s! "
		    ERRFMT, req_items, req_size, [class_ className], ERRPARAM];
	else
		string = [[OFString alloc] initWithFormat:
		    @"Failed to write %zu bytes in class %s! " ERRFMT, req_size,
		    [class_ className], ERRPARAM];

	return string;
}
@end

@implementation OFChangeFileModeFailedException
+ newWithClass: (Class)class__







<
<
<
<
<
<
<
<
<













<




<
<

<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<

















<
<
<
<
<
<
<
<
<
<








<
|
<
<
<
<
|
|











<
|
<
<
<
<
|
|







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
	return mode;
}
@end

@implementation OFReadOrWriteFailedException
+ newWithClass: (Class)class__
	  size: (size_t)size









{
	return [[self alloc] initWithClass: class__
				      size: size];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithClass: (Class)class__
	   size: (size_t)size

{
	self = [super initWithClass: class__];

	req_size = size;







	/* FIXME: We need something that works for subclasses as well */












	if (class__ == [OFTCPSocket class])
		err = GET_SOCK_ERR;
	else
		err = GET_ERR;

	return self;
}

- (int)errNo
{
	return err;
}

- (size_t)requestedSize
{
	return req_size;
}










@end

@implementation OFReadFailedException
- (OFString*)string
{
	if (string != nil)
		return string;;


	string = [[OFString alloc] initWithFormat:




	    @"Failed to read %zu bytes in class %s! " ERRFMT, req_size,
	    [class_ className], ERRPARAM];

	return string;
}
@end

@implementation OFWriteFailedException
- (OFString*)string
{
	if (string != nil)
		return string;


	string = [[OFString alloc] initWithFormat:




	    @"Failed to write %zu bytes in class %s! " ERRFMT, req_size,
	    [class_ className], ERRPARAM];

	return string;
}
@end

@implementation OFChangeFileModeFailedException
+ newWithClass: (Class)class__

Modified src/OFFile.h from [9d72f8d05d] to [87163d3271].

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
/**
 * Initializes an already allocated OFFile.
 *
 * \param fp A file pointer, returned from for example fopen().
 *	     It is not closed when the OFFile object is deallocated!
 */
- initWithFilePointer: (FILE*)fp;

/**
 * Reads from the file into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size * nitems big!
 * \param nitems nitem The number of items to read
 *	  The buffer MUST be at least size * nitems big!
 * \return The number of bytes read
 */
- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (char*)buf;

/**
 * Writes from a buffer into the file.
 *
 * \param nitems The number of items to write
 * \param size The size of the data that should be written
 * \param buf The buffer from which the data is written to the file
 * \return The number of bytes written
 */
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const char*)buf;
@end

@interface OFFileSingleton: OFFile
@end

/// An OFFile object for stdin.
extern OFFile *of_stdin;
/// An OFFile object for stdout.
extern OFFile *of_stdout;
/// An OFFile object for stderr.
extern OFFile *of_stderr;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<











122
123
124
125
126
127
128


























129
130
131
132
133
134
135
136
137
138
139
/**
 * Initializes an already allocated OFFile.
 *
 * \param fp A file pointer, returned from for example fopen().
 *	     It is not closed when the OFFile object is deallocated!
 */
- initWithFilePointer: (FILE*)fp;


























@end

@interface OFFileSingleton: OFFile
@end

/// An OFFile object for stdin.
extern OFFile *of_stdin;
/// An OFFile object for stdout.
extern OFFile *of_stdout;
/// An OFFile object for stderr.
extern OFFile *of_stderr;

Modified src/OFFile.m from [9c9c1d09c4] to [ea32f092c7].

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
{
	if (close && fp != NULL)
		fclose(fp);

	[super dealloc];
}

- (BOOL)atEndOfStream
{
	if (fp == NULL)
		return YES;

	return (feof(fp) == 0 ? NO : YES);
}

- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (char*)buf
{
	size_t ret;

	if (fp == NULL || feof(fp) ||
	    ((ret = fread(buf, size, nitems, fp)) == 0 &&
	    size != 0 && nitems != 0 && !feof(fp)))
		@throw [OFReadFailedException newWithClass: isa
						      size: size
						     items: nitems];

	return ret;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	return [self readNItems: size
			 ofSize: 1
		     intoBuffer: buf];
}

- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const char*)buf
{
	size_t ret;

	if (fp == NULL || feof(fp) ||
	    ((ret = fwrite(buf, size, nitems, fp)) < nitems &&
	    size != 0 && nitems != 0))
		@throw [OFWriteFailedException newWithClass: isa
						       size: size
						      items: nitems];

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	return [self writeNItems: size
			  ofSize: 1
		      fromBuffer: buf];
}



- (size_t)writeCString: (const char*)str
{
	return [self writeNItems: strlen(str)
			  ofSize: 1
		      fromBuffer: str];
}

- close
{
	fclose(fp);
	fp = NULL;








|







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|

<
<
<
<
<
<
<
<
<


|
<
|
|
|
<







<
|
<
|
|
>
>
|
|
|
<
<







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
{
	if (close && fp != NULL)
		fclose(fp);

	[super dealloc];
}

- (BOOL)atEndOfStreamWithoutCache
{
	if (fp == NULL)
		return YES;

	return (feof(fp) == 0 ? NO : YES);
}

















- (size_t)readNBytesWithoutCache: (size_t)size
		      intoBuffer: (char*)buf
{









	size_t ret;

	if (fp == NULL || feof(fp) || ((ret = fread(buf, 1, size, fp)) == 0 &&

	    size != 0 && !feof(fp)))
		@throw [OFReadFailedException newWithClass: isa
						      size: size];


	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{

	size_t ret;


	if (fp == NULL || feof(fp) ||
	    ((ret = fwrite(buf, 1, size, fp)) < size && size != 0))
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];

	return ret;


}

- close
{
	fclose(fp);
	fp = NULL;

Modified src/OFSocket.m from [d65c17ca33] to [866349c818].

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

	sock = INVALID_SOCKET;
	saddr = NULL;

	return self;
}

- (BOOL)atEndOfStream
{
	return eos;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET || eos)
		@throw [OFNotConnectedException newWithClass: isa];

	if ((ret = recv(sock, buf, size, 0)) < 0)







|




|
|







46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

	sock = INVALID_SOCKET;
	saddr = NULL;

	return self;
}

- (BOOL)atEndOfStreamWithoutCache
{
	return eos;
}

- (size_t)readNBytesWithoutCache: (size_t)size
		      intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET || eos)
		@throw [OFNotConnectedException newWithClass: isa];

	if ((ret = recv(sock, buf, size, 0)) < 0)

Modified src/OFStream.h from [f0bc557550] to [15f483123e].

18
19
20
21
22
23
24






25
26
27
28
29











30




31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


47



48


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@interface OFStream: OFObject
{
	char   *cache;
	size_t cache_len;
}

/**






 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)atEndOfStream;

/**











 * Reads from the stream into a buffer.




 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf;

/**
 * Read until a newline, \\0 or end of stream occurs.
 *
 * If you want to use readNBytes afterwards again, you have to clear the cache
 * before and optionally get the cache before clearing it!
 *
 * You also need to pay attention to the cache if you want to know if there is


 * still data left - atEndOfStream can return NO even if there is still data



 * in the cache!


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

/**
 * Read with the specified encoding until a newline, \\0 or end of stream
 * occurs.
 *
 * If you want to use readNBytes afterwards again, you have to clear the cache
 * before and optionally get the cache before clearing it!
 *
 * \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.







>
>
>
>
>
>





>
>
>
>
>
>
>
>
>
>
>

>
>
>
>










|

|
|

|
>
>
|
>
>
>
|
>
>










<
<
<







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86



87
88
89
90
91
92
93
@interface OFStream: OFObject
{
	char   *cache;
	size_t cache_len;
}

/**
 * Returns a boolean whether the end of the stream has been reached.
 *
 * IMPORTANT: Do *NOT* override this in subclasses! Override
 * atEndOfStreamWithoutCache instead, as otherwise, you *WILL* break caching and
 * thus get broken results!
 *
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)atEndOfStream;

/**
 * Returns a boolean whether the end of the stream has been reached without
 * looking at the cache.
 *
 * IMPORTANT: Do *NOT* use this! Use atEndOfCache instead, as this is *ONLY*
 * for being overriden in subclasses!
 *
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)atEndOfStreamWithoutCache;

/**
 * Reads from the stream into a buffer.
 *
 * IMPORTANT: Do *NOT* override this in subclasses! Override
 * readNBytesWithoutCache:intoBuffer: instead, as otherwise, you *WILL* break
 * caching and thus get broken results!
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf;

/**
 * Reads from the stream into a buffer without looking at the cache.
 *
 * IMPORTANT: Do *NOT* use this! Use readNBytes:intoBuffer: instead, as this is
 * *ONLY* for being overriden in subclasses!
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytesWithoutCache: (size_t)size
		      intoBuffer: (char*)buf;

/**
 * Read 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*)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.
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
 * Writes a string into the stream, without the trailing zero.
 *
 * \param str The string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeString: (OFString*)str;

/**
 * Sets a specified pointer to the cache and returns the length of the cache.
 *
 * \param ptr A pointer to a pointer. It will be set to the cache.
 *	      If it is NULL, only the number of bytes in the cache is returned.
 * \return The number of bytes in the cache.
 */
- (size_t)getCache: (char**)ptr;

/**
 * Clears the cache.
 */
- clearCache;

/**
 * Closes the stream.
 */
- close;
@end







<
<
<
<
<
<
<
<
<
<
<
<
<
<





103
104
105
106
107
108
109














110
111
112
113
114
 * Writes a string into the stream, without the trailing zero.
 *
 * \param str The string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeString: (OFString*)str;















/**
 * Closes the stream.
 */
- close;
@end

Modified src/OFStream.m from [2c5ce7298e] to [dff3658c98].

48
49
50
51
52
53
54








55
56
57
58
59
60
61






























62
63
64
65
66
67
68
#endif

	return self;
}

- (BOOL)atEndOfStream
{








	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{






























	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];







>
>
>
>
>
>
>
>







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







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

	return self;
}

- (BOOL)atEndOfStream
{
	if (cache != NULL)
		return NO;

	return [self atEndOfStreamWithoutCache];
}

- (BOOL)atEndOfStreamWithoutCache
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	if (cache == NULL)
		return [self readNBytesWithoutCache: size
					 intoBuffer: buf];

	if (size >= cache_len) {
		size_t ret = cache_len;
		memcpy(buf, cache, cache_len);

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

		return ret;
	} else {
		char *tmp = [self allocMemoryWithSize: cache_len - size];
		memcpy(tmp, cache + size, cache_len - size);

		memcpy(buf, cache, size);

		[self freeMemory: cache];
		cache = tmp;
		cache_len -= size;

		return size;
	}
}

- (size_t)readNBytesWithoutCache: (size_t)size
		      intoBuffer: (char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
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
		}
	}

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

	for (;;) {
		if ([self atEndOfStream]) {
			[self freeMemory: tmp];

			if (cache == NULL)
				return nil;

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

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

			return ret;
		}

		@try {
			len = [self readNBytes: pagesize - 1
				    intoBuffer: tmp];
		} @catch (OFException *e) {
			[self freeMemory: tmp];
			@throw e;
		}

		/* Look if there's a newline or \0 */
		for (i = 0; i < len; i++) {







|

















|
|







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

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

	for (;;) {
		if ([self atEndOfStreamWithoutCache]) {
			[self freeMemory: tmp];

			if (cache == NULL)
				return nil;

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

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

			return ret;
		}

		@try {
			len = [self readNBytesWithoutCache: pagesize
						intoBuffer: tmp];
		} @catch (OFException *e) {
			[self freeMemory: tmp];
			@throw e;
		}

		/* Look if there's a newline or \0 */
		for (i = 0; i < len; i++) {
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

- (size_t)writeString: (OFString*)str
{
	return [self writeNBytes: [str cStringLength]
		      fromBuffer: [str cString]];
}

- (size_t)getCache: (char**)ptr
{
	if (ptr != NULL)
		*ptr = cache;

	return cache_len;
}

- clearCache
{
	[self freeMemory: cache];

	cache = NULL;
	cache_len = 0;

	return self;
}

- close
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}
@end







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






241
242
243
244
245
246
247


















248
249
250
251
252
253

- (size_t)writeString: (OFString*)str
{
	return [self writeNBytes: [str cStringLength]
		      fromBuffer: [str cString]];
}



















- close
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}
@end