ObjFW  Check-in [95e2910172]

Overview
Comment:Add -[OFStream asyncReadString]
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 95e2910172fdbf31cb373bbc5fdde7dffeeafe0a6b4f57b1bcdd2c040e955ba2
User & Date: js on 2024-10-19 11:38:08
Other Links: manifest | tags
Context
2024-10-19
14:07
GitHub Actions: Fix OpenBSD check-in: e389aa247e user: js tags: trunk
11:38
Add -[OFStream asyncReadString] check-in: 95e2910172 user: js tags: trunk
11:09
Add -[OFStream tryReadStringWithEncoding:] check-in: 6e91e313a5 user: js tags: trunk
Changes

Modified src/OFRunLoop+Private.h from [80e1bfb64f] to [525119de79].

56
57
58
59
60
61
62










63
64
65
66
67
68
69
			  buffer: (void *)buffer
		     exactLength: (size_t)length
			    mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
			   block: (nullable OFStreamAsyncReadBlock)block
# endif
			delegate: (nullable id <OFStreamDelegate>)delegate;










+ (void)of_addAsyncReadLineForStream: (OFStream <OFReadyForReadingObserving> *)
					  stream
			    encoding: (OFStringEncoding)encoding
				mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
			       block: (nullable OFStreamAsyncReadLineBlock)block
# endif







>
>
>
>
>
>
>
>
>
>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
			  buffer: (void *)buffer
		     exactLength: (size_t)length
			    mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
			   block: (nullable OFStreamAsyncReadBlock)block
# endif
			delegate: (nullable id <OFStreamDelegate>)delegate;
+ (void)of_addAsyncReadStringForStream: (OFStream <OFReadyForReadingObserving
					    > *)stream
			      encoding: (OFStringEncoding)encoding
				  mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
				 block: (nullable OFStreamAsyncReadStringBlock)
					    block
# endif
			      delegate: (nullable id <OFStreamDelegate>)
					    delegate;
+ (void)of_addAsyncReadLineForStream: (OFStream <OFReadyForReadingObserving> *)
					  stream
			    encoding: (OFStringEncoding)encoding
				mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
			       block: (nullable OFStreamAsyncReadLineBlock)block
# endif

Modified src/OFRunLoop.m from [7691af0c6a] to [11ea957026].

109
110
111
112
113
114
115










116
117
118
119
120
121
122
# ifdef OF_HAVE_BLOCKS
	OFStreamAsyncReadBlock _block;
# endif
	void *_buffer;
	size_t _exactLength, _readLength;
}
@end











@interface OFRunLoopReadLineQueueItem: OFRunLoopQueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
	OFStreamAsyncReadLineBlock _block;
# endif







>
>
>
>
>
>
>
>
>
>







109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# ifdef OF_HAVE_BLOCKS
	OFStreamAsyncReadBlock _block;
# endif
	void *_buffer;
	size_t _exactLength, _readLength;
}
@end

@interface OFRunLoopReadStringQueueItem: OFRunLoopQueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
	OFStreamAsyncReadStringBlock _block;
# endif
	OFStringEncoding _encoding;
}
@end

@interface OFRunLoopReadLineQueueItem: OFRunLoopQueueItem
{
@public
# ifdef OF_HAVE_BLOCKS
	OFStreamAsyncReadLineBlock _block;
# endif
529
530
531
532
533
534
535











































536
537
538
539
540
541
542

		_readLength = 0;
		return true;
# ifdef OF_HAVE_BLOCKS
	}
# endif
}












































# ifdef OF_HAVE_BLOCKS
- (void)dealloc
{
	[_block release];

	[super dealloc];







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







539
540
541
542
543
544
545
546
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

		_readLength = 0;
		return true;
# ifdef OF_HAVE_BLOCKS
	}
# endif
}

# ifdef OF_HAVE_BLOCKS
- (void)dealloc
{
	[_block release];

	[super dealloc];
}
# endif
@end

@implementation OFRunLoopReadStringQueueItem
- (bool)handleObject: (id)object
{
	OFString *string;
	id exception = nil;

	@try {
		string = [object tryReadStringWithEncoding: _encoding];
	} @catch (id e) {
		string = nil;
		exception = e;
	}

	if (string == nil && ![object isAtEndOfStream] && exception == nil)
		return true;

# ifdef OF_HAVE_BLOCKS
	if (_block != NULL)
		return _block(string, exception);
	else {
# endif
		if (![_delegate respondsToSelector:
		    @selector(stream:didReadString:exception:)])
			return false;

		return [_delegate stream: object
			   didReadString: string
			       exception: exception];
# ifdef OF_HAVE_BLOCKS
	}
# endif
}

# ifdef OF_HAVE_BLOCKS
- (void)dealloc
{
	[_block release];

	[super dealloc];
1279
1280
1281
1282
1283
1284
1285




















1286
1287
1288
1289
1290
1291
1292
	queueItem->_delegate = [delegate retain];
# ifdef OF_HAVE_BLOCKS
	queueItem->_block = [block copy];
# endif
	queueItem->_buffer = buffer;
	queueItem->_exactLength = exactLength;





















	QUEUE_ITEM
}

+ (void)of_addAsyncReadLineForStream: (OFStream <OFReadyForReadingObserving> *)
					  stream
			    encoding: (OFStringEncoding)encoding
				mode: (OFRunLoopMode)mode







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







1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
	queueItem->_delegate = [delegate retain];
# ifdef OF_HAVE_BLOCKS
	queueItem->_block = [block copy];
# endif
	queueItem->_buffer = buffer;
	queueItem->_exactLength = exactLength;

	QUEUE_ITEM
}

+ (void)of_addAsyncReadStringForStream: (OFStream <OFReadyForReadingObserving
					    > *)stream
			      encoding: (OFStringEncoding)encoding
				  mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
				 block: (OFStreamAsyncReadStringBlock)block
# endif
			      delegate: (id <OFStreamDelegate>)delegate
{
	NEW_READ(OFRunLoopReadStringQueueItem, stream, mode)

	queueItem->_delegate = [delegate retain];
# ifdef OF_HAVE_BLOCKS
	queueItem->_block = [block copy];
# endif
	queueItem->_encoding = encoding;

	QUEUE_ITEM
}

+ (void)of_addAsyncReadLineForStream: (OFStream <OFReadyForReadingObserving> *)
					  stream
			    encoding: (OFStringEncoding)encoding
				mode: (OFRunLoopMode)mode

Modified src/OFStream.h from [cca724d404] to [cb4dc06f17].

48
49
50
51
52
53
54













55
56
57
58
59
60
61
 * @param length The length of the data that has been read
 * @param exception An exception which occurred while reading or `nil` on
 *		    success
 * @return A bool whether the same block should be used for the next read
 */
typedef bool (^OFStreamAsyncReadBlock)(size_t length, id _Nullable exception);














/**
 * @brief A block which is called when a line was read asynchronously from a
 *	  stream.
 *
 * @param line The line which has been read or `nil` when the end of stream
 *	       occurred
 * @param exception An exception which occurred while reading or `nil` on







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







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
 * @param length The length of the data that has been read
 * @param exception An exception which occurred while reading or `nil` on
 *		    success
 * @return A bool whether the same block should be used for the next read
 */
typedef bool (^OFStreamAsyncReadBlock)(size_t length, id _Nullable exception);

/**
 * @brief A block which is called when a string was read asynchronously from a
 *	  stream.
 *
 * @param string The string which has been read or `nil` when the end of stream
 *		 occurred
 * @param exception An exception which occurred while reading or `nil` on
 *		    success
 * @return A bool whether the same block should be used for the next read
 */
typedef bool (^OFStreamAsyncReadStringBlock)(OFString *_Nullable string,
    id _Nullable exception);

/**
 * @brief A block which is called when a line was read asynchronously from a
 *	  stream.
 *
 * @param line The line which has been read or `nil` when the end of stream
 *	       occurred
 * @param exception An exception which occurred while reading or `nil` on
112
113
114
115
116
117
118














119
120
121
122
123
124
125
 * @return A bool whether the read should be repeated
 */
-      (bool)stream: (OFStream *)stream
  didReadIntoBuffer: (void *)buffer
	     length: (size_t)length
	  exception: (nullable id)exception;















/**
 * @brief This method is called when a line was read asynchronously from a
 *	  stream.
 *
 * @param stream The stream on which a line was read
 * @param line The line which has been read or `nil` when the end of stream
 *	       occurred







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







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 * @return A bool whether the read should be repeated
 */
-      (bool)stream: (OFStream *)stream
  didReadIntoBuffer: (void *)buffer
	     length: (size_t)length
	  exception: (nullable id)exception;

/**
 * @brief This method is called when a string was read asynchronously from a
 *	  stream.
 *
 * @param stream The stream on which a line was read
 * @param string The string which has been read or `nil` when the end of stream
 *	       occurred
 * @param exception An exception that occurred while reading, or nil on success
 * @return A bool whether the read should be repeated
 */
-  (bool)stream: (OFStream *)stream
  didReadString: (nullable OFString *)string
      exception: (nullable id)exception;

/**
 * @brief This method is called when a line was read asynchronously from a
 *	  stream.
 *
 * @param stream The stream on which a line was read
 * @param line The line which has been read or `nil` when the end of stream
 *	       occurred
768
769
770
771
772
773
774

































775
776
777
778
779
780
781
 * @throw OFInvalidEncodingException The string read from the stream has
 *				     invalid encoding
 * @throw OFNotOpenException The stream is not open
 */
- (nullable OFString *)readLineWithEncoding: (OFStringEncoding)encoding;

#ifdef OF_HAVE_SOCKETS

































/**
 * @brief Asynchronously reads until a newline, `\0`, end of stream or an
 *	  exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 */







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







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
 * @throw OFInvalidEncodingException The string read from the stream has
 *				     invalid encoding
 * @throw OFNotOpenException The stream is not open
 */
- (nullable OFString *)readLineWithEncoding: (OFStringEncoding)encoding;

#ifdef OF_HAVE_SOCKETS
/**
 * @brief Asynchronously reads until a `\0`, end of stream or an exception
 *	  occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 */
- (void)asyncReadString;

/**
 * @brief Asynchronously reads with the specified encoding until a `\0`, end of
 *	  stream or an exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *
 * @param encoding The encoding used by the stream
 */
- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding;

/**
 * @brief Asynchronously reads with the specified encoding until a `\0`, end of
 *	  stream or an exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *
 * @param encoding The encoding used by the stream
 * @param runLoopMode The run loop mode in which to perform the async read
 */
- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
			runLoopMode: (OFRunLoopMode)runLoopMode;

/**
 * @brief Asynchronously reads until a newline, `\0`, end of stream or an
 *	  exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 */
802
803
804
805
806
807
808



















































809
810
811
812
813
814
815
 * @param encoding The encoding used by the stream
 * @param runLoopMode The run loop mode in which to perform the async read
 */
- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding
		      runLoopMode: (OFRunLoopMode)runLoopMode;

# ifdef OF_HAVE_BLOCKS



















































/**
 * @brief Asynchronously reads until a newline, `\0`, end of stream or an
 *	  exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *







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







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
917
918
919
920
921
922
923
924
925
926
 * @param encoding The encoding used by the stream
 * @param runLoopMode The run loop mode in which to perform the async read
 */
- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding
		      runLoopMode: (OFRunLoopMode)runLoopMode;

# ifdef OF_HAVE_BLOCKS
/**
 * @brief Asynchronously reads until a `\0`, end of stream or an exception
 *	  occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *
 * @param block The block to call when the data has been received.
 *		If the block returns true, it will be called again when the
 *		next string has been received. If you want the next block in
 *		the queue to handle the next string, you need to return false
 *		from the block.
 */
- (void)asyncReadStringWithBlock: (OFStreamAsyncReadStringBlock)block;

/**
 * @brief Asynchronously reads with the specified encoding until a `\0`, end of
 *	  stream or an exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *
 * @param encoding The encoding used by the stream
 * @param block The block to call when the data has been received.
 *		If the block returns true, it will be called again when the
 *		next string has been received. If you want the next block in
 *		the queue to handle the next string, you need to return false
 *		from the block.
 */
- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
			      block: (OFStreamAsyncReadStringBlock)block;

/**
 * @brief Asynchronously reads with the specified encoding until a `\0`, end of
 *	  stream or an exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *
 * @param encoding The encoding used by the stream
 * @param runLoopMode The run loop mode in which to perform the async read
 * @param block The block to call when the data has been received.
 *		If the block returns true, it will be called again when the
 *		next string has been received. If you want the next block in
 *		the queue to handle the next string, you need to return false
 *		from the block.
 */
- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
			runLoopMode: (OFRunLoopMode)runLoopMode
			      block: (OFStreamAsyncReadStringBlock)block;

/**
 * @brief Asynchronously reads until a newline, `\0`, end of stream or an
 *	  exception occurs.
 *
 * @note The stream must conform to @ref OFReadyForReadingObserving in order
 *	 for this to work!
 *

Modified src/OFStream.m from [9664024d76] to [8516a05a5a].

628
629
630
631
632
633
634



























635
636
637
638
639
640
641
		if (self.atEndOfStream)
			return nil;

	return line;
}

#ifdef OF_HAVE_SOCKETS



























- (void)asyncReadLine
{
	[self asyncReadLineWithEncoding: OFStringEncodingUTF8
			    runLoopMode: OFDefaultRunLoopMode];
}

- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding







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







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
		if (self.atEndOfStream)
			return nil;

	return line;
}

#ifdef OF_HAVE_SOCKETS
- (void)asyncReadString
{
	[self asyncReadStringWithEncoding: OFStringEncodingUTF8
			      runLoopMode: OFDefaultRunLoopMode];
}

- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
{
	[self asyncReadStringWithEncoding: encoding
			      runLoopMode: OFDefaultRunLoopMode];
}

- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
			runLoopMode: (OFRunLoopMode)runLoopMode
{
	OFStream <OFReadyForReadingObserving> *stream =
	    (OFStream <OFReadyForReadingObserving> *)self;

	[OFRunLoop of_addAsyncReadStringForStream: stream
					 encoding: encoding
					     mode: runLoopMode
# ifdef OF_HAVE_BLOCKS
					    block: NULL
# endif
					 delegate: _delegate];
}

- (void)asyncReadLine
{
	[self asyncReadLineWithEncoding: OFStringEncodingUTF8
			    runLoopMode: OFDefaultRunLoopMode];
}

- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding
656
657
658
659
660
661
662





























663
664
665
666
667
668
669
# ifdef OF_HAVE_BLOCKS
					  block: NULL
# endif
				       delegate: _delegate];
}

# ifdef OF_HAVE_BLOCKS





























- (void)asyncReadLineWithBlock: (OFStreamAsyncReadLineBlock)block
{
	[self asyncReadLineWithEncoding: OFStringEncodingUTF8
			    runLoopMode: OFDefaultRunLoopMode
				  block: block];
}








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







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
718
719
720
721
722
723
724
725
# ifdef OF_HAVE_BLOCKS
					  block: NULL
# endif
				       delegate: _delegate];
}

# ifdef OF_HAVE_BLOCKS
- (void)asyncReadStringWithBlock: (OFStreamAsyncReadStringBlock)block
{
	[self asyncReadStringWithEncoding: OFStringEncodingUTF8
			      runLoopMode: OFDefaultRunLoopMode
				    block: block];
}

- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
			      block: (OFStreamAsyncReadStringBlock)block
{
	[self asyncReadStringWithEncoding: encoding
			      runLoopMode: OFDefaultRunLoopMode
				    block: block];
}

- (void)asyncReadStringWithEncoding: (OFStringEncoding)encoding
			runLoopMode: (OFRunLoopMode)runLoopMode
			      block: (OFStreamAsyncReadStringBlock)block
{
	OFStream <OFReadyForReadingObserving> *stream =
	    (OFStream <OFReadyForReadingObserving> *)self;

	[OFRunLoop of_addAsyncReadStringForStream: stream
					 encoding: encoding
					     mode: runLoopMode
					    block: block
					 delegate: nil];
}

- (void)asyncReadLineWithBlock: (OFStreamAsyncReadLineBlock)block
{
	[self asyncReadLineWithEncoding: OFStringEncodingUTF8
			    runLoopMode: OFDefaultRunLoopMode
				  block: block];
}