ObjFW  Diff

Differences From Artifact [df855ee15f]:

To Artifact [1971d4e07a]:


559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
				cacheLength -= i + 1;

				return ret;
			}
		}
	}

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

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








|







559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
				cacheLength -= i + 1;

				return ret;
			}
		}
	}

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

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

673
674
675
676
677
678
679



















680
681
682
683
684
685
686
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{



















	const char *delimiterUTF8String;
	size_t i, j, delimiterLength, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delimiterUTF8String = [delimiter UTF8String];







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







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
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{
	OFString *ret = nil;

	while ((ret = [self tryReadTillDelimiter: delimiter
				    withEncoding: encoding]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return ret;
}

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

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
		     withEncoding: (of_string_encoding_t)encoding
{
	const char *delimiterUTF8String;
	size_t i, j, delimiterLength, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delimiterUTF8String = [delimiter UTF8String];
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
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
				cacheLength -= i + 1;

				return ret;
			}
		}
	}

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

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

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

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

				return ret;
			}

			bufferLength = [self _readNBytes: of_pagesize
					      intoBuffer: buffer];

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

				if (j == delimiterLength || buffer[i] == '\0') {
					if (buffer[i] == '\0')
						delimiterLength = 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 (i >= delimiterLength)
						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;

					return ret;
				}
			}

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

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

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

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

- (BOOL)buffersWrites
{
	return buffersWrites;
}








|



<
|
|
|

|
|
|

|
|
|

|
|

|
|

|
|
|
|

|
|
|

|
|
|
|

|
<
|
<
|
|
<
|
|
|
<

|
|
|

|
|
|
|
|
|
|

|
|
|
|
|

|
|
|

|
|
|

|
|
|

|
|
|
|
|
|
|

|
<




<
|







736
737
738
739
740
741
742
743
744
745
746

747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779

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
				cacheLength -= i + 1;

				return ret;
			}
		}
	}

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

	@try {

		if ([self _isAtEndOfStream]) {
			if (cache == NULL)
				return nil;

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

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

			return ret;
		}

		bufferLength = [self _readNBytes: of_pagesize
				      intoBuffer: buffer];

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

			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 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 (i >= delimiterLength)
					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;

				return ret;
			}
		}

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

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

		cacheLength += bufferLength;

	} @finally {
		[self freeMemory: buffer];
	}


	return nil;
}

- (BOOL)buffersWrites
{
	return buffersWrites;
}