ObjFW  Check-in [bd923d90c7]

Overview
Comment:Add -[tryReadTillDelimiter:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bd923d90c7a8cc9753257033af47c4c849ca548143e44fcd2f70df119af9f826
User & Date: js on 2011-09-15 10:57:42
Other Links: manifest | tags
Context
2011-09-15
11:01
Specify the encoding for HTTP POST request data. check-in: 7f93f420b5 user: js tags: trunk
10:57
Add -[tryReadTillDelimiter:]. check-in: bd923d90c7 user: js tags: trunk
2011-09-14
23:28
Add -[tryReadLine] to OFStream. check-in: 6e8023786a user: js tags: trunk
Changes

Modified src/OFStream.h from [32b37edb3b] to [cfff676663].

467
468
469
470
471
472
473
























474
475
476
477
478
479
480
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







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







 * \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: (of_string_encoding_t)encoding;

/**
 * \brief Tries to reads until the specified string or \\0 is found or the end
 *	  of stream (see readTillDelimiter:) and returns nil if not enough data
 *	  has been received yet.
 *
 * \param delimiter The delimiter
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.
 */
- (OFString*)tryReadTillDelimiter: (OFString*)delimiter;

/**
 * \brief Tries to read until the specified string or \\0 is found or the end
 *	  of stream occurs (see readTIllDelimiterWithEncoding:) and returns nil
 *	  if not enough data has been received yet.
 *
 * \param delimiter The delimiter
 * \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*)tryReadTillDelimiter: (OFString*)delimiter
		     withEncoding: (of_string_encoding_t)encoding;

/**
 * \brief Returns a boolen whether writes are buffered.
 *
 * \return A boolean whether writes are buffered
 */
- (BOOL)buffersWrites;

Modified src/OFStream.m from [df855ee15f] to [1971d4e07a].

559
560
561
562
563
564
565
566

567
568
569
570
571
572
573
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 */
	/* 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
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
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 until we get the delimiter or \0 */
	/* Read and see if we get a delimiter or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

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

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

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

				return ret;
			}
			return ret;
		}

			bufferLength = [self _readNBytes: of_pagesize
					      intoBuffer: buffer];
		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;
		/* 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;
			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 1;

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

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

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

						ret = [OFString
						    stringWithCString: rcs
							     encoding: encoding
							       length: rl];
					} @finally {
						[self freeMemory: retCString];
					}
					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);
				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;

					return ret;
				}
			}
				return ret;
			}
		}

			/* Neither the delimiter nor \0 was found */
			cache = [self resizeMemory: cache
					    toSize: cacheLength + bufferLength];
		/* 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);
		/*
		 * 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;
		cacheLength += bufferLength;
		}
	} @finally {
		[self freeMemory: buffer];
	}

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

- (BOOL)buffersWrites
{
	return buffersWrites;
}