ObjFW  Check-in [6e8023786a]

Overview
Comment:Add -[tryReadLine] to OFStream.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6e8023786a012dcc2bba6513d3722707cfbb3960978c35a2688a1463afd78b83
User & Date: js on 2011-09-14 23:28:32
Other Links: manifest | tags
Context
2011-09-15
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
2011-09-13
19:34
Add +[dateWith(Local)DateString:format:]. check-in: 42cd6eebb4 user: js tags: trunk
Changes

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

425
426
427
428
429
430
431




















432
433
434
435
436
437
438
 *
 * \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*)readLineWithEncoding: (of_string_encoding_t)encoding;





















/**
 * \brief Reads until the specified string or \\0 is found or the end of stream
 *	  occurs.
 *
 * \param delimiter The delimiter
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.







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







425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
 *
 * \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*)readLineWithEncoding: (of_string_encoding_t)encoding;

/**
 * \brief Tries to read a line from the stream (see readLine) and returns nil if
 *	  no complete line has been received yet.
 *
 * \return The line that was read, autoreleased, or nil if the line is not
 *	   complete yet
 */
- (OFString*)tryReadLine;

/**
 * \brief Tries to read a line from the stream with the specified encoding (see
 *	  readLineWithEncoding:) and returns nil if no complete line has been
 *	  received yet.
 *
 * \param encoding The encoding used by the stream
 * \return The line that was read, autoreleased, or nil if the line is not
 *	   complete yet
 */
- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding;

/**
 * \brief Reads until the specified string or \\0 is found or the end of stream
 *	  occurs.
 *
 * \param delimiter The delimiter
 * \return The line that was read, autoreleased, or nil if the end of the
 *	   stream has been reached.

Modified src/OFStream.m from [da83014b0a] to [df855ee15f].

510
511
512
513
514
515
516
















517
518
519
520
521
522
523
- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
















	size_t i, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
		for (i = 0; i < cacheLength; i++) {







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







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
537
538
539
- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
	OFString *line = nil;

	while ((line = [self tryReadLineWithEncoding: encoding]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return line;
}

- (OFString*)tryReadLine
{
	return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

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

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
		}
	}

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

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

				retLength = cacheLength;

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

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

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

				return ret;
			}

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

			/* 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;
					retCString = [self
					    allocMemoryWithSize: retLength];

					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
							  toSize: cacheLength +
								  bufferLength];

						if (cache != NULL)
							memcpy(cache +
							    cacheLength, buffer,
							    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;

					return ret;
				}
			}

			/* There was no newline or \0 */
			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);
}

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







<
|
|
|

|

|
<
|

|
|
|

|
|
|

|
|

|
|

|
|
|
|
|
|
|

|
|
<
|
<

|
|
|

|
|
|

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

|
|
<
|

|

|
|
|
|

|
|
|
|
|

|
|
|

|
|
|

|
|
|

|
|
|
|
|
|
<

|
<




<
|







563
564
565
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
		}
	}

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

	@try {

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

			retLength = cacheLength;

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

				retLength--;

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

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

			return ret;
		}

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

		/* 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;
				retCString = [self
				    allocMemoryWithSize: retLength];

				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
						  toSize: cacheLength +
							  bufferLength];

					if (cache != NULL)
						memcpy(cache + cacheLength,

						    buffer, 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;

				return ret;
			}
		}

		/* There was no newline or \0 */
		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;
}

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