uBlink  0.0.1
TheBlinkProtocol
blink_stream.h
1 /* Copyright (c) 2016 Cameron Harper
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of
4  * this software and associated documentation files (the "Software"), to deal in
5  * the Software without restriction, including without limitation the rights to
6  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7  * the Software, and to permit persons to whom the Software is furnished to do so,
8  * subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  *
20  *
21  * */
22 
23 #ifndef BLINK_STREAM_H
24 #define BLINK_STREAM_H
25 
36  #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /* includes ***********************************************************/
41 
42 #include <stdint.h>
43 #include <stddef.h>
44 #include <stdbool.h>
45 
46 /* types **************************************************************/
47 
49  bool (*read)(void *state, void *out, size_t bytesToRead);
50  bool (*write)(void *state, const void *in, size_t bytesToWrite);
51  uint32_t (*tell)(void *state);
52  bool (*peek)(void *state, void *c);
53  bool (*seekCur)(void *state, int32_t offset);
54  bool (*seekSet)(void *state, uint32_t offset);
55  bool (*eof)(void *state);
56 };
57 
58 struct blink_stream {
60  BLINK_STREAM_NULL = 0,
63  BLINK_STREAM_BOUNDED
64  } type;
66  struct {
67  const uint8_t *in;
68  uint8_t *out;
69  uint32_t max;
70  uint32_t pos;
71  bool eof;
72  } buffer;
73  struct {
74  struct blink_stream_user fn;
75  void *state;
76  } user;
77  struct {
78  struct blink_stream *stream;
79  uint32_t max;
80  uint32_t pos;
81  } bounded;
82  } value;
83 
84 };
85 
86 typedef struct blink_stream * blink_stream_t;
87 
88 
89 /* function prototypes ************************************************/
90 
100 bool BLINK_Stream_write(blink_stream_t self, const void *buf, size_t nbyte);
101 
111 bool BLINK_Stream_read(blink_stream_t self, void *buf, size_t nbyte);
112 
121 bool BLINK_Stream_peek(blink_stream_t self, void *buf);
122 
132 blink_stream_t BLINK_Stream_initBufferReadOnly(struct blink_stream *self, const void *buf, uint32_t max);
133 
143 blink_stream_t BLINK_Stream_initBuffer(struct blink_stream *self, void *buf, uint32_t max);
144 
154 blink_stream_t BLINK_Stream_initUser(struct blink_stream *self, void *state, struct blink_stream_user fn);
155 
165 blink_stream_t BLINK_Stream_initBounded(struct blink_stream *self, blink_stream_t stream, uint32_t max);
166 
173 uint32_t BLINK_Stream_tell(blink_stream_t self);
174 
185 bool BLINK_Stream_seekSet(blink_stream_t self, uint32_t offset);
186 
197 bool BLINK_Stream_seekCur(blink_stream_t self, int32_t offset);
198 
206 bool BLINK_Stream_eof(blink_stream_t self);
207 
217 size_t BLINK_Stream_max(blink_stream_t self);
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
225 #endif