1 /***********************************************************************************************************************************
2 IO Read Interface
3
4 Objects that read from some IO source (file, socket, etc.) are implemented using this interface. All objects are required to
5 implement IoReadProcess and can optionally implement IoReadOpen, IoReadClose, or IoReadEof. IoReadOpen and IoReadClose can be used
6 to allocate/open or deallocate/free resources. If IoReadEof is not implemented then ioReadEof() will always return false. An
7 example of an IoRead object is IoBufferRead.
8 ***********************************************************************************************************************************/
9 #ifndef COMMON_IO_READ_H
10 #define COMMON_IO_READ_H
11
12 /***********************************************************************************************************************************
13 Object type
14 ***********************************************************************************************************************************/
15 typedef struct IoRead IoRead;
16
17 #include "common/io/filter/group.h"
18 #include "common/io/read.intern.h"
19 #include "common/type/buffer.h"
20 #include "common/type/object.h"
21
22 /***********************************************************************************************************************************
23 Getters/Setters
24 ***********************************************************************************************************************************/
25 // Do reads block when more bytes are requested than are available to read?
26 __attribute__((always_inline)) static inline bool
ioReadBlock(const IoRead * const this)27 ioReadBlock(const IoRead *const this)
28 {
29 return THIS_PUB(IoRead)->interface.block;
30 }
31
32 // Is IO at EOF? All driver reads are complete and all data has been flushed from the filters (if any).
33 __attribute__((always_inline)) static inline bool
ioReadEof(const IoRead * const this)34 ioReadEof(const IoRead *const this)
35 {
36 ASSERT_INLINE(THIS_PUB(IoRead)->opened && !THIS_PUB(IoRead)->closed);
37 return THIS_PUB(IoRead)->eofAll;
38 }
39
40 // Get filter group if filters need to be added
41 __attribute__((always_inline)) static inline IoFilterGroup *
ioReadFilterGroup(IoRead * const this)42 ioReadFilterGroup(IoRead *const this)
43 {
44 return THIS_PUB(IoRead)->filterGroup;
45 }
46
47 // File descriptor for the read object. Not all read objects have a file descriptor and -1 will be returned in that case.
48 int ioReadFd(const IoRead *this);
49
50 /***********************************************************************************************************************************
51 Functions
52 ***********************************************************************************************************************************/
53 // Open the IO
54 bool ioReadOpen(IoRead *this);
55
56 // Read data from IO and process filters
57 size_t ioRead(IoRead *this, Buffer *buffer);
58
59 // Same as ioRead() but optimized for small reads (intended for making repetitive reads that are smaller than ioBufferSize())
60 size_t ioReadSmall(IoRead *this, Buffer *buffer);
61
62 // Read linefeed-terminated string and optionally error on eof
63 String *ioReadLineParam(IoRead *this, bool allowEof);
64
65 // Read linefeed-terminated string
66 __attribute__((always_inline)) static inline String *
ioReadLine(IoRead * const this)67 ioReadLine(IoRead *const this)
68 {
69 return ioReadLineParam(this, false);
70 }
71
72 // Are there bytes ready to read immediately? There are no guarantees on how much data is available to read but it must be at least
73 // one byte.
74 typedef struct IoReadReadyParam
75 {
76 VAR_PARAM_HEADER;
77 bool error; // Error when read not ready
78 } IoReadReadyParam;
79
80 #define ioReadReadyP(this, ...) \
81 ioReadReady(this, (IoReadReadyParam){VAR_PARAM_INIT, __VA_ARGS__})
82
83 bool ioReadReady(IoRead *this, IoReadReadyParam param);
84
85 // Close the IO
86 void ioReadClose(IoRead *this);
87
88 /***********************************************************************************************************************************
89 Destructor
90 ***********************************************************************************************************************************/
91 __attribute__((always_inline)) static inline void
ioReadFree(IoRead * const this)92 ioReadFree(IoRead *const this)
93 {
94 objFree(this);
95 }
96
97 /***********************************************************************************************************************************
98 Macros for function logging
99 ***********************************************************************************************************************************/
100 #define FUNCTION_LOG_IO_READ_TYPE \
101 IoRead *
102 #define FUNCTION_LOG_IO_READ_FORMAT(value, buffer, bufferSize) \
103 objToLog(value, "IoRead", buffer, bufferSize)
104
105 #endif
106