ObjFW  Diff

Differences From Artifact [3a3eebc79f]:

To Artifact [0bbf55df10]:


64
65
66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
64
65
66
67
68
69
70



71
72
73
74
75
76
77
78







-
-
-
+







			[self release];
			@throw e;
		}
	}

	self = [super init];

	cache = NULL;
	writeBuffer = NULL;
	blocking = YES;
	_blocking = YES;

	return self;
}

- (BOOL)lowlevelIsAtEndOfStream
{
	[self doesNotRecognizeSelector: _cmd];
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
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







-
+








-
+



-
-
-
+
+
+

-
-
-
+
+
+



-
-
+
+

-
+

-
-
-
+
+
+







- copy
{
	return [self retain];
}

- (BOOL)isAtEndOfStream
{
	if (cacheLength > 0)
	if (_cacheLength > 0)
		return NO;

	return [self lowlevelIsAtEndOfStream];
}

- (size_t)readIntoBuffer: (void*)buffer
		  length: (size_t)length
{
	if (cacheLength == 0)
	if (_cacheLength == 0)
		return [self lowlevelReadIntoBuffer: buffer
					     length: length];

	if (length >= cacheLength) {
		size_t ret = cacheLength;
		memcpy(buffer, cache, cacheLength);
	if (length >= _cacheLength) {
		size_t ret = _cacheLength;
		memcpy(buffer, _cache, _cacheLength);

		[self freeMemory: cache];
		cache = NULL;
		cacheLength = 0;
		[self freeMemory: _cache];
		_cache = NULL;
		_cacheLength = 0;

		return ret;
	} else {
		char *tmp = [self allocMemoryWithSize: cacheLength - length];
		memcpy(tmp, cache + length, cacheLength - length);
		char *tmp = [self allocMemoryWithSize: _cacheLength - length];
		memcpy(tmp, _cache + length, _cacheLength - length);

		memcpy(buffer, cache, length);
		memcpy(buffer, _cache, length);

		[self freeMemory: cache];
		cache = tmp;
		cacheLength -= length;
		[self freeMemory: _cache];
		_cache = tmp;
		_cacheLength -= length;

		return length;
	}
}

- (void)readIntoBuffer: (void*)buffer
	   exactLength: (size_t)length
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
661
662
663
664
665



666
667
668
669


670
671
672

673
674
675
676
677
678
679
680
681
682
683
684
685
686
687



688
689

690
691
692
693
694
695
696


697
698
699
700


701
702
703


704
705

706
707
708
709
710

711
712
713
714
715
716
717
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
661
662



663
664
665
666
667


668
669
670
671

672
673
674
675
676
677
678
679
680
681
682
683
684



685
686
687
688

689
690
691
692
693
694


695
696
697
698


699
700
701


702
703
704

705
706
707
708
709

710
711
712
713
714
715
716
717







-
-
-
+
+
+
+


-
+


-
+




-
+

-
-
+
+

-
-
-
+
+
+

-
+











-
-
+
+



-
+

-
+


-
+



-
-
-
+
+
+

-
+










-
+



-
-
-
+
+
+
+


















-
-
-
+
+
+


-
-
+
+


-
+












-
-
-
+
+
+

-
+





-
-
+
+


-
-
+
+

-
-
+
+

-
+




-
+







- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (!waitingForDelimiter && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if OF_UNLIKELY (cache[i] == '\n' || cache[i] == '\0') {
	if (!_waitingForDelimiter && _cache != NULL) {
		for (i = 0; i < _cacheLength; i++) {
			if OF_UNLIKELY (_cache[i] == '\n' ||
			    _cache[i] == '\0') {
				retLength = i;

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

				ret = [OFString stringWithCString: cache
				ret = [OFString stringWithCString: _cache
							 encoding: encoding
							   length: retLength];

				newCache = [self
				    allocMemoryWithSize: cacheLength - i - 1];
				    allocMemoryWithSize: _cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);
					memcpy(newCache, _cache + i + 1,
					    _cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;
				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength -= i + 1;

				waitingForDelimiter = NO;
				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a newline or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (cache == NULL) {
				waitingForDelimiter = NO;
			if (_cache == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			retLength = cacheLength;
			retLength = _cacheLength;

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

			ret = [OFString stringWithCString: cache
			ret = [OFString stringWithCString: _cache
						 encoding: encoding
						   length: retLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;
			[self freeMemory: _cache];
			_cache = NULL;
			_cacheLength = 0;

			waitingForDelimiter = NO;
			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a newline or \0 */
		for (i = 0; i < bufferLength; i++) {
			if OF_UNLIKELY (buffer[i] == '\n' ||
			    buffer[i] == '\0') {
				retLength = cacheLength + i;
				retLength = _cacheLength + i;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (cache != NULL)
					memcpy(retCString, cache, cacheLength);
				memcpy(retCString + cacheLength, buffer, i);
				if (_cache != NULL)
					memcpy(retCString, _cache,
					    _cacheLength);
				memcpy(retCString + _cacheLength, buffer, i);

				if (retLength > 0 &&
				    retCString[retLength - 1] == '\r')
					retLength--;

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @catch (id e) {
					/*
					 * Append data to cache to prevent loss
					 * of data due to wrong encoding.
					 */
					cache = [self
					    resizeMemory: cache
						    size: cacheLength +
					_cache = [self
					    resizeMemory: _cache
						    size: _cacheLength +
							  bufferLength];

					if (cache != NULL)
						memcpy(cache + cacheLength,
					if (_cache != NULL)
						memcpy(_cache + _cacheLength,
						    buffer, bufferLength);

					cacheLength += bufferLength;
					_cacheLength += bufferLength;

					@throw e;
				} @finally {
					[self freeMemory: retCString];
				}

				newCache = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength = bufferLength - i - 1;
				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength = bufferLength - i - 1;

				waitingForDelimiter = NO;
				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* There was no newline or \0 */
		cache = [self resizeMemory: cache
				      size: cacheLength + bufferLength];
		_cache = [self resizeMemory: _cache
				       size: _cacheLength + bufferLength];

		/*
		 * It's possible that cacheLen + len is 0 and thus cache was
		 * set to NULL by resizeMemory:size:.
		 * It's possible that _cacheLength + bufferLength is 0 and thus
		 * _cache was set to NULL by resizeMemory:size:.
		 */
		if (cache != NULL)
			memcpy(cache + cacheLength, buffer, bufferLength);
		if (_cache != NULL)
			memcpy(_cache + _cacheLength, buffer, bufferLength);

		cacheLength += bufferLength;
		_cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	waitingForDelimiter = YES;
	_waitingForDelimiter = YES;
	return nil;
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}
780
781
782
783
784
785
786
787
788
789



790
791
792
793


794
795
796
797

798
799
800
801
802

803
804
805


806
807
808
809



810
811

812
813
814
815
816
817
818
819
820
821
822
823
824


825
826
827
828

829
830

831
832
833
834



835
836

837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

853
854
855
856
857
858
859
860





861
862

863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885



886
887

888
889
890
891
892
893
894


895
896
897
898


899
900

901
902

903
904

905
906
907
908
909

910
911
912
913
914
915
916
780
781
782
783
784
785
786



787
788
789
790
791


792
793
794
795
796

797
798
799
800
801

802
803


804
805
806



807
808
809
810

811
812
813
814
815
816
817
818
819
820
821
822


823
824
825
826
827

828
829

830
831



832
833
834
835

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851

852
853
854
855
856




857
858
859
860
861
862

863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883



884
885
886
887

888
889
890
891
892
893


894
895
896
897


898
899
900

901


902
903

904
905
906
907
908

909
910
911
912
913
914
915
916







-
-
-
+
+
+


-
-
+
+



-
+




-
+

-
-
+
+

-
-
-
+
+
+

-
+











-
-
+
+



-
+

-
+

-
-
-
+
+
+

-
+















-
+




-
-
-
-
+
+
+
+
+

-
+




















-
-
-
+
+
+

-
+





-
-
+
+


-
-
+
+

-
+
-
-
+

-
+




-
+








	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	/* Look if there's something in our cache */
	if (!waitingForDelimiter && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (cache[i] != delimiterCString[j++])
	if (!_waitingForDelimiter && _cache != NULL) {
		for (i = 0; i < _cacheLength; i++) {
			if (_cache[i] != delimiterCString[j++])
				j = 0;

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

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

				newCache = [self allocMemoryWithSize:
				    cacheLength - i - 1];
				    _cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);
					memcpy(newCache, _cache + i + 1,
					    _cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;
				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength -= i + 1;

				waitingForDelimiter = NO;
				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a delimiter or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (cache == NULL) {
				waitingForDelimiter = NO;
			if (_cache == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: cache
			ret = [OFString stringWithCString: _cache
						 encoding: encoding
						   length: cacheLength];
						   length: _cacheLength];

			[self freeMemory: cache];
			cache = NULL;
			cacheLength = 0;
			[self freeMemory: _cache];
			_cache = NULL;
			_cacheLength = 0;

			waitingForDelimiter = NO;
			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a delimiter or \0 */
		for (i = 0; i < bufferLength; i++) {
			if (buffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 1;

				retLength = cacheLength + i + 1 -
				retLength = _cacheLength + i + 1 -
				    delimiterLength;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (cache != NULL && cacheLength <= retLength)
					memcpy(retCString, cache, cacheLength);
				else if (cache != NULL)
					memcpy(retCString, cache, retLength);
				if (_cache != NULL && _cacheLength <= retLength)
					memcpy(retCString, _cache,
					    _cacheLength);
				else if (_cache != NULL)
					memcpy(retCString, _cache, retLength);
				if (i >= delimiterLength)
					memcpy(retCString + cacheLength,
					memcpy(retCString + _cacheLength,
					    buffer, i + 1 - delimiterLength);

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @finally {
					[self freeMemory: retCString];
				}

				newCache = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength = bufferLength - i - 1;
				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength = bufferLength - i - 1;

				waitingForDelimiter = NO;
				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		cache = [self resizeMemory: cache
				      size: cacheLength + bufferLength];
		_cache = [self resizeMemory: _cache
				       size: _cacheLength + bufferLength];

		/*
		 * It's possible that cacheLen + len is 0 and thus cache was
		 * set to NULL by resizeMemory:size:.
		 * It's possible that _cacheLength + bufferLength is 0 and thus
		 * _cache was set to NULL by resizeMemory:size:.
		 */
		if (cache != NULL)
		if (_cache != NULL)
			memcpy(cache + cacheLength, buffer,
			    bufferLength);
			memcpy(_cache + _cacheLength, buffer, bufferLength);

		cacheLength += bufferLength;
		_cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	waitingForDelimiter = YES;
	_waitingForDelimiter = YES;
	return nil;
}


- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
935
936
937
938
939
940
941
942

943
944
945
946
947

948
949
950
951
952

953
954
955
956


957
958
959
960



961
962
963
964
965
966

967
968
969
970
971
972
973




974
975
976
977
978
979
980
935
936
937
938
939
940
941

942
943
944
945
946

947
948
949
950
951

952
953
954


955
956
957



958
959
960
961
962
963
964
965

966
967
968
969




970
971
972
973
974
975
976
977
978
979
980







-
+




-
+




-
+


-
-
+
+

-
-
-
+
+
+





-
+



-
-
-
-
+
+
+
+







{
	return [self tryReadTillDelimiter: delimiter
				 encoding: OF_STRING_ENCODING_UTF_8];
}

- (BOOL)isWriteBufferEnabled
{
	return writeBufferEnabled;
	return _writeBufferEnabled;
}

- (void)setWriteBufferEnabled: (BOOL)enable
{
	writeBufferEnabled = enable;
	_writeBufferEnabled = enable;
}

- (void)flushWriteBuffer
{
	if (writeBuffer == NULL)
	if (_writeBuffer == NULL)
		return;

	[self lowlevelWriteBuffer: writeBuffer
			   length: writeBufferLength];
	[self lowlevelWriteBuffer: _writeBuffer
			   length: _writeBufferLength];

	[self freeMemory: writeBuffer];
	writeBuffer = NULL;
	writeBufferLength = 0;
	[self freeMemory: _writeBuffer];
	_writeBuffer = NULL;
	_writeBufferLength = 0;
}

- (void)writeBuffer: (const void*)buffer
	     length: (size_t)length
{
	if (!writeBufferEnabled)
	if (!_writeBufferEnabled)
		[self lowlevelWriteBuffer: buffer
				   length: length];
	else {
		writeBuffer = [self resizeMemory: writeBuffer
					    size: writeBufferLength + length];
		memcpy(writeBuffer + writeBufferLength, buffer, length);
		writeBufferLength += length;
		_writeBuffer = [self resizeMemory: _writeBuffer
					     size: _writeBufferLength + length];
		memcpy(_writeBuffer + _writeBufferLength, buffer, length);
		_writeBufferLength += length;
	}
}

- (void)writeInt8: (uint8_t)int8
{
	[self writeBuffer: (char*)&int8
		   length: 1];
1452
1453
1454
1455
1456
1457
1458
1459

1460
1461
1462
1463
1464

1465
1466
1467
1468
1469
1470
1471
1452
1453
1454
1455
1456
1457
1458

1459
1460
1461
1462
1463

1464
1465
1466
1467
1468
1469
1470
1471







-
+




-
+







	}

	return length;
}

- (size_t)pendingBytes
{
	return cacheLength;
	return _cacheLength;
}

- (BOOL)isBlocking
{
	return blocking;
	return _blocking;
}

- (void)setBlocking: (BOOL)enable
{
#ifndef _WIN32
	BOOL readImplemented = NO, writeImplemented = NO;

1519
1520
1521
1522
1523
1524
1525


1526
1527
1528
1529
1530
1531
1532
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534







+
+







	} @catch (OFNotImplementedException *e) {
	}

	if (!readImplemented && !writeImplemented)
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	_blocking = enable;
#else
	[self doesNotRecognizeSelector: _cmd];
	abort();
#endif
}

- (int)fileDescriptorForReading
1550
1551
1552
1553
1554
1555
1556
1557

1558
1559
1552
1553
1554
1555
1556
1557
1558

1559
1560
1561







-
+


{
	[self doesNotRecognizeSelector: _cmd];
	abort();
}

- (BOOL)OF_isWaitingForDelimiter
{
	return waitingForDelimiter;
	return _waitingForDelimiter;
}
@end