ObjFW
OFStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #ifndef __STDC_LIMIT_MACROS
18 # define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 # define __STDC_CONSTANT_MACROS
22 #endif
23 
24 #include <stdarg.h>
25 
26 #import "OFObject.h"
27 #import "OFString.h"
28 #ifdef OF_HAVE_SOCKETS
29 # import "OFKernelEventObserver.h"
30 #endif
31 
32 OF_ASSUME_NONNULL_BEGIN
33 
36 @class OFStream;
37 @class OFDataArray;
38 @class OFException;
39 
40 #if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_BLOCKS)
41 
50 typedef bool (^of_stream_async_read_block_t)(OFStream *stream, void *buffer,
51  size_t length, OFException *OF_NULLABLE exception);
52 
63  OFString *OF_NULLABLE line, OFException *OF_NULLABLE exception);
64 #endif
65 
86 @interface OFStream: OFObject <
87 #ifdef OF_HAVE_SOCKETS
89 #endif
90  OFCopying>
91 {
92 #if !defined(OF_SEEKABLE_STREAM_M) && !defined(OF_TCP_SOCKET_M)
93 @private
94 #endif
95  char *_readBuffer, *_writeBuffer;
96  size_t _readBufferLength, _writeBufferLength;
97  bool _writeBuffered, _waitingForDelimiter;
98 @protected
99  bool _blocking;
100 }
101 
102 #ifdef OF_HAVE_PROPERTIES
103 @property (getter=isWriteBuffered) bool writeBuffered;
104 @property (getter=isBlocking) bool blocking;
105 @property (readonly, getter=isAtEndOfStream) bool atEndOfStream;
106 #endif
107 
113 - (bool)isAtEndOfStream;
114 
129 - (size_t)readIntoBuffer: (void*)buffer
130  length: (size_t)length;
131 
147  - (void)readIntoBuffer: (void*)buffer
148  exactLength: (size_t)length;
149 
150 #ifdef OF_HAVE_SOCKETS
151 
178 - (void)asyncReadIntoBuffer: (void*)buffer
179  length: (size_t)length
180  target: (id)target
181  selector: (SEL)selector;
182 
208 - (void)asyncReadIntoBuffer: (void*)buffer
209  exactLength: (size_t)length
210  target: (id)target
211  selector: (SEL)selector;
212 
213 # ifdef OF_HAVE_BLOCKS
214 
237 - (void)asyncReadIntoBuffer: (void*)buffer
238  length: (size_t)length
239  block: (of_stream_async_read_block_t)block;
240 
262  - (void)asyncReadIntoBuffer: (void*)buffer
263  exactLength: (size_t)length
264  block: (of_stream_async_read_block_t)block;
265 # endif
266 #endif
267 
276 - (uint8_t)readInt8;
277 
286 - (uint16_t)readBigEndianInt16;
287 
296 - (uint32_t)readBigEndianInt32;
297 
306 - (uint64_t)readBigEndianInt64;
307 
316 - (float)readBigEndianFloat;
317 
326 - (double)readBigEndianDouble;
327 
340 - (size_t)readBigEndianInt16sIntoBuffer: (uint16_t*)buffer
341  count: (size_t)count;
342 
355 - (size_t)readBigEndianInt32sIntoBuffer: (uint32_t*)buffer
356  count: (size_t)count;
357 
370 - (size_t)readBigEndianInt64sIntoBuffer: (uint64_t*)buffer
371  count: (size_t)count;
372 
385 - (size_t)readBigEndianFloatsIntoBuffer: (float*)buffer
386  count: (size_t)count;
387 
400 - (size_t)readBigEndianDoublesIntoBuffer: (double*)buffer
401  count: (size_t)count;
402 
411 - (uint16_t)readLittleEndianInt16;
412 
421 - (uint32_t)readLittleEndianInt32;
422 
431 - (uint64_t)readLittleEndianInt64;
432 
441 - (float)readLittleEndianFloat;
442 
451 - (double)readLittleEndianDouble;
452 
465 - (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t*)buffer
466  count: (size_t)count;
467 
480 - (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t*)buffer
481  count: (size_t)count;
482 
495 - (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t*)buffer
496  count: (size_t)count;
497 
510 - (size_t)readLittleEndianFloatsIntoBuffer: (float*)buffer
511  count: (size_t)count;
512 
525 - (size_t)readLittleEndianDoublesIntoBuffer: (double*)buffer
526  count: (size_t)count;
527 
538 - (OFDataArray*)readDataArrayWithCount: (size_t)count;
539 
551 - (OFDataArray*)readDataArrayWithItemSize: (size_t)itemSize
552  count: (size_t)count;
553 
560 - (OFDataArray*)readDataArrayTillEndOfStream;
561 
576 - (OFString*)readStringWithLength: (size_t)length;
577 
593 - (OFString*)readStringWithLength: (size_t)length
594  encoding: (of_string_encoding_t)encoding;
595 
602 - (nullable OFString*)readLine;
603 
612 - (nullable OFString*)readLineWithEncoding: (of_string_encoding_t)encoding;
613 
614 #ifdef OF_HAVE_SOCKETS
615 
631 - (void)asyncReadLineWithTarget: (id)target
632  selector: (SEL)selector;
633 
651 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
652  target: (id)target
653  selector: (SEL)selector;
654 
655 # ifdef OF_HAVE_BLOCKS
656 
669 - (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block;
670 
685 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
686  block: (of_stream_async_read_line_block_t)block;
687 # endif
688 #endif
689 
697 - (nullable OFString*)tryReadLine;
698 
708 - (nullable OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding;
709 
718 - (nullable OFString*)readTillDelimiter: (OFString*)delimiter;
719 
729 - (nullable OFString*)readTillDelimiter: (OFString*)delimiter
730  encoding: (of_string_encoding_t)encoding;
731 
741 - (nullable OFString*)tryReadTillDelimiter: (OFString*)delimiter;
742 
753 - (nullable OFString*)tryReadTillDelimiter: (OFString*)delimiter
754  encoding: (of_string_encoding_t)encoding;
755 
761 - (bool)isWriteBuffered;
762 
768 - (void)setWriteBuffered: (bool)enable;
769 
773 - (void)flushWriteBuffer;
774 
781 - (void)writeBuffer: (const void*)buffer
782  length: (size_t)length;
783 
789 - (void)writeInt8: (uint8_t)int8;
790 
796 - (void)writeBigEndianInt16: (uint16_t)int16;
797 
803 - (void)writeBigEndianInt32: (uint32_t)int32;
804 
810 - (void)writeBigEndianInt64: (uint64_t)int64;
811 
817 - (void)writeBigEndianFloat: (float)float_;
818 
824 - (void)writeBigEndianDouble: (double)double_;
825 
835 - (size_t)writeBigEndianInt16s: (const uint16_t*)buffer
836  count: (size_t)count;
837 
847 - (size_t)writeBigEndianInt32s: (const uint32_t*)buffer
848  count: (size_t)count;
849 
859 - (size_t)writeBigEndianInt64s: (const uint64_t*)buffer
860  count: (size_t)count;
861 
871 - (size_t)writeBigEndianFloats: (const float*)buffer
872  count: (size_t)count;
873 
883 - (size_t)writeBigEndianDoubles: (const double*)buffer
884  count: (size_t)count;
885 
891 - (void)writeLittleEndianInt16: (uint16_t)int16;
892 
898 - (void)writeLittleEndianInt32: (uint32_t)int32;
899 
905 - (void)writeLittleEndianInt64: (uint64_t)int64;
906 
912 - (void)writeLittleEndianFloat: (float)float_;
913 
919 - (void)writeLittleEndianDouble: (double)double_;
920 
930 - (size_t)writeLittleEndianInt16s: (const uint16_t*)buffer
931  count: (size_t)count;
932 
942 - (size_t)writeLittleEndianInt32s: (const uint32_t*)buffer
943  count: (size_t)count;
944 
954 - (size_t)writeLittleEndianInt64s: (const uint64_t*)buffer
955  count: (size_t)count;
956 
966 - (size_t)writeLittleEndianFloats: (const float*)buffer
967  count: (size_t)count;
968 
978 - (size_t)writeLittleEndianDoubles: (const double*)buffer
979  count: (size_t)count;
980 
987 - (size_t)writeDataArray: (OFDataArray*)dataArray;
988 
995 - (size_t)writeString: (OFString*)string;
996 
1005 - (size_t)writeString: (OFString*)string
1006  encoding: (of_string_encoding_t)encoding;
1007 
1014 - (size_t)writeLine: (OFString*)string;
1015 
1024 - (size_t)writeLine: (OFString*)string
1025  encoding: (of_string_encoding_t)encoding;
1026 
1036 - (size_t)writeFormat: (OFConstantString*)format, ...;
1037 
1048 - (size_t)writeFormat: (OFConstantString*)format
1049  arguments: (va_list)arguments;
1050 
1056 - (bool)hasDataInReadBuffer;
1057 
1063 - (bool)isBlocking;
1064 
1073 - (void)setBlocking: (bool)enable;
1074 
1080 - (int)fileDescriptorForReading;
1081 
1087 - (int)fileDescriptorForWriting;
1088 
1089 #ifdef OF_HAVE_SOCKETS
1090 
1099 - (void)cancelAsyncRequests;
1100 #endif
1101 
1123 - (void)unreadFromBuffer: (const void*)buffer
1124  length: (size_t)length;
1125 
1129 - (void)close;
1130 
1142 - (size_t)lowlevelReadIntoBuffer: (void*)buffer
1143  length: (size_t)length;
1144 
1155 - (void)lowlevelWriteBuffer: (const void*)buffer
1156  length: (size_t)length;
1157 
1168 - (bool)lowlevelIsAtEndOfStream;
1169 @end
1170 
1171 OF_ASSUME_NONNULL_END
This protocol is implemented by classes which can be observed for readiness for reading by OFKernelEv...
Definition: OFKernelEventObserver.h:73
of_string_encoding_t
The encoding of a string.
Definition: OFString.h:50
bool(^ of_stream_async_read_block_t)(OFStream *stream, void *buffer, size_t length, OFException *OF_NULLABLE exception)
A block which is called when data was read from the stream.
Definition: OFStream.h:50
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:37
The root class for all other classes inside ObjFW.
Definition: OFObject.h:364
A base class for different types of streams.
Definition: OFStream.h:86
bool(^ of_stream_async_read_line_block_t)(OFStream *stream, OFString *OF_NULLABLE line, OFException *OF_NULLABLE exception)
A block which is called when a line was read from the stream.
Definition: OFStream.h:62
A class for handling strings.
Definition: OFString.h:91
A class for storing arbitrary data in an array.
Definition: OFDataArray.h:37
The base class for all exceptions in ObjFW.
Definition: OFException.h:144
This protocol is implemented by classes which can be observed for readiness for writing by OFKernelEv...
Definition: OFKernelEventObserver.h:91
A protocol for the creation of copies.
Definition: OFObject.h:896