ObjFW  Check-in [66c1d7718d]

Overview
Comment:Add an optional write cache to OFStream.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 66c1d7718d2271b2fb3b49cf510c24a116abfd1bba07c7a8d98cde8a54f53c78
User & Date: js on 2010-04-09 00:55:47
Other Links: manifest | tags
Context
2010-04-09
14:59
Better hiding of the internal OFStream methods. check-in: 2a9e91b67c user: js tags: trunk
00:55
Add an optional write cache to OFStream. check-in: 66c1d7718d user: js tags: trunk
00:21
Add Unicode table generator stuff to .hgignore. check-in: 6de7d549e1 user: js tags: trunk
Changes

Modified src/OFFile.m from [d214a82c1c] to [a377c2db9b].

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
						      size: size];
	if ((ret = read(fd, buf, size)) == 0)
		eos = YES;

	return ret;
}

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

	if (fd == -1 || eos || (ret = write(fd, buf, size)) < size)
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];








|
|







249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
						      size: size];
	if ((ret = read(fd, buf, size)) == 0)
		eos = YES;

	return ret;
}

- (size_t)writeNBytesWithoutCache: (size_t)size
		       fromBuffer: (const char*)buf
{
	size_t ret;

	if (fd == -1 || eos || (ret = write(fd, buf, size)) < size)
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];

Modified src/OFSocket.m from [76195a13fc] to [df33a24aa3].

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

	if (ret == 0)
		eos = YES;

	return ret;
}

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

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

	if ((ret = send(sock, buf, size, 0)) == -1)







|
|







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

	if (ret == 0)
		eos = YES;

	return ret;
}

- (size_t)writeNBytesWithoutCache: (size_t)size
		       fromBuffer: (const char*)buf
{
	ssize_t ret;

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

	if ((ret = send(sock, buf, size, 0)) == -1)

Modified src/OFStream.h from [c88d590591] to [f77e6ede23].

15
16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
@class OFDataArray;

/**
 * \brief A base class for different types of streams.
 */
@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







|
|
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@class OFDataArray;

/**
 * \brief A base class for different types of streams.
 */
@interface OFStream: OFObject
{
	char   *cache, *wcache;
	size_t cache_len, wcache_len;
	BOOL   use_wcache;
}

/**
 * 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
166
167
168
169
170
171
172
173










174




175
176
177
178
179
180
181
182













183
184
185
186
187
188
189
 * \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
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf;














/**
 * Writes an uint8_t into the stream.
 *
 * \param int8 An uint8_t
 */
- (void)writeInt8: (uint8_t)int8;









>
>
>
>
>
>
>
>
>
>

>
>
>
>








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







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
 * \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;

/**
 * Caches all writes until flushWriteCache is called.
 */
- cacheWrites;

/**
 * Writes everything in the write cache to the stream.
 */
- flushWriteCache;

/**
 * Writes from a buffer into the stream.
 *
 * IMPORTANT: Do *NOT* override this in subclasses! Override
 * writeNBytesWithoutCache:fromBuffer: instead, as otherwise, you *WILL* break
 * caching and thus get broken results!
 *
 * \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
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf;

/**
 * Directly writes from a buffer into the stream without caching the data first.
 *
 * IMPORTANT: Do *NOT* use this! Use writeNBytes:fromBuffer: instead, as this is
 * *ONLY* for being overriden in subclasses!
 *
 * \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
 */
- (size_t)writeNBytesWithoutCache: (size_t)size
		       fromBuffer: (const char*)buf;

/**
 * Writes an uint8_t into the stream.
 *
 * \param int8 An uint8_t
 */
- (void)writeInt8: (uint8_t)int8;

Modified src/OFStream.m from [2c3719ee49] to [15f17faee6].

33
34
35
36
37
38
39

40
41
42
43
44
45
46
	self = [super init];

	if (isa == [OFStream class])
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];

	cache = NULL;


	return self;
}

- (BOOL)atEndOfStream
{
	if (cache != NULL)







>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	self = [super init];

	if (isa == [OFStream class])
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];

	cache = NULL;
	wcache = NULL;

	return self;
}

- (BOOL)atEndOfStream
{
	if (cache != NULL)
454
455
456
457
458
459
460
461




















462
463
464
















465
466
467
468
469
470
471
	} @finally {
		[self freeMemory: tmp];
	}

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





















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
















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

- (void)writeInt8: (uint8_t)int8
{
	[self writeNBytes: 1








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



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







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

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

- cacheWrites
{
	use_wcache = YES;

	return self;
}

- flushWriteCache
{
	[self writeNBytesWithoutCache: wcache_len
			   fromBuffer: wcache];

	[self freeMemory: wcache];
	wcache = NULL;
	wcache_len = 0;
	use_wcache = NO;

	return self;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	if (!use_wcache)
		return [self writeNBytesWithoutCache: size
					  fromBuffer: buf];
	else {
		wcache = [self resizeMemory: wcache
				     toSize: wcache_len + size];
		memcpy(wcache + wcache_len, buf, size);
		wcache_len += size;

		return size;
	}
}

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

- (void)writeInt8: (uint8_t)int8
{
	[self writeNBytes: 1
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
{
	return [self writeNBytes: [str cStringLength]
		      fromBuffer: [str cString]];
}

- (size_t)writeLine: (OFString*)str
{
	size_t len = [str cStringLength];
	char *tmp;

	tmp = [self allocMemoryWithSize: len + 2];
	memcpy(tmp, [str cString], len);
	tmp[len] = '\n';
	tmp[len + 1] = '\0';

	@try {
		return [self writeNBytes: len + 1
			      fromBuffer: tmp];
	} @finally {
		[self freeMemory: tmp];
	}

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

- (size_t)writeFormat: (OFString*)fmt, ...
{
	va_list args;
	char *t;
	size_t len;







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







543
544
545
546
547
548
549


550






551



552
553


554
555
556
557
558
559
560
{
	return [self writeNBytes: [str cStringLength]
		      fromBuffer: [str cString]];
}

- (size_t)writeLine: (OFString*)str
{


	size_t ret = [self writeString: str];






	[self writeInt8: '\n'];




	return ret + 1;


}

- (size_t)writeFormat: (OFString*)fmt, ...
{
	va_list args;
	char *t;
	size_t len;