ObjFW  Diff

Differences From Artifact [15f17faee6]:

To Artifact [62c939ae2f]:


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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+






-
-
-
-
+
-
-
-






-
-
+
+







						      selector: _cmd];

	cache = NULL;
	wcache = NULL;

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

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

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

	return [self atEndOfStreamWithoutCache];
}

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

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

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

		[self freeMemory: cache];
		cache = NULL;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
96
97
98
99
100
101
102







103
104
105
106
107
108
109







-
-
-
-
-
-
-







		cache = tmp;
		cache_len -= size;

		return size;
	}
}

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

- (void)readExactlyNBytes: (size_t)size
	       intoBuffer: (char*)buf
{
	size_t len = 0;

	while (len < size)
		len += [self readNBytes: size - len
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
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







-
+



















-
-
+
+







	}

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

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

				ret_len = cache_len;

				if (ret_len > 0 && cache[ret_len - 1] == '\r')
					ret_len--;

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

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

				return ret;
			}

			len = [self readNBytesWithoutCache: of_pagesize
						intoBuffer: tmp];
			len = [self _readNBytes: of_pagesize
				     intoBuffer: tmp];

			/* Look if there's a newline or \0 */
			for (i = 0; i < len; i++) {
				if (OF_UNLIKELY(tmp[i] == '\n' ||
				    tmp[i] == '\0')) {
					ret_len = cache_len + i;
					ret_c = [self
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
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







-
+














-
-
+
+







	}

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

	@try {
		for (;;) {
			if ([self atEndOfStreamWithoutCache]) {
			if ([self _atEndOfStream]) {
				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: of_pagesize
						intoBuffer: tmp];
			len = [self _readNBytes: of_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') {
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
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







-
-
+
+













-
-
+
+










-
-
-
-
-
-
-







	use_wcache = YES;

	return self;
}

- flushWriteCache
{
	[self writeNBytesWithoutCache: wcache_len
			   fromBuffer: wcache];
	[self _writeNBytes: 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];
		return [self _writeNBytes: 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
	       fromBuffer: (char*)&int8];
}

- (void)writeBigEndianInt16: (uint16_t)int16