-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.h
258 lines (236 loc) · 5.17 KB
/
stream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* #include <string.h> */
enum
{
STREAM_SEEK_BEG,
/* STREAM_SEEK_SET, */
STREAM_SEEK_CUR,
STREAM_SEEK_END
};
/* enum */
/* { */
/* STREAM_RESULT_OK, */
/* STREAM_RESULT_ERR, */
/* }; */
/* typedef int32_t StreamResult; */
typedef struct Stream_s
{
/* char filename[256]; */
/* unsigned char *buffer; */
/* unsigned int offset, length; */
void *ctx;
int64_t (*tell)(struct Stream_s *s);
/* This function returns zero if successful, or else it returns a non-zero value. */
int (*seek)(struct Stream_s *s, int64_t offset, int whence);
int (*name)(struct Stream_s *stream, char *buffer, size_t size);
int (*eof)(struct Stream_s *stream);
size_t (*read)(struct Stream_s *stream, void *ptr, size_t size, size_t nmemb);
/* void (*close)(struct Stream_s *stream); */
size_t (*write)(struct Stream_s *stream, const void *ptr, size_t size, size_t nmemb);
} Stream;
static size_t stream_read_buffer(Stream *s, void *ptr, size_t n)
{
return s->read(s, ptr, n, 1);
}
#define stream_read(s, ptr) stream_read_buffer(&(s), &(ptr), sizeof(ptr))
static int stream_measure_line(Stream *s, size_t *n)
{
*n = 0;
int eol = 0;
int eof = 0;
size_t offset = 0;
while(!eol)
{
uint8_t ch = 0;
if(0 == s->read(s, &ch, 1, 1) || !ch)
{
eof = 1;
break;
}
switch(ch)
{
case '\r': eol = 1; break; // In this case, match \r as eol because we don't want carriage returns in our output.
case '\n': eol = 1; break;
default: *n += 1; break;
}
}
return eof;
}
static int stream_read_line_cr(Stream *s, char *line, size_t max_line_length, bool *carriage_return)
{
*carriage_return = false;
size_t n = 0;
line[n] = 0;
int eol = 0;
int eof = 0;
size_t offset = 0;
while(!eol)
{
uint8_t ch = 0;
if(0 == s->read(s, &ch, 1, 1) || !ch)
{
// If we haven't read anything yet then this is the "real" EOF
// Had we encountered a \0 or EOF at the end of a line then it would have been one line too early
if(n == 0)
eof = 1;
break;
}
if(n + 1 >= max_line_length) // n + 1 account for \0
{
fprintf(stderr, "Error line length %d is larger than the maximum length of a line.\n", max_line_length);
exit(-1);
}
switch(ch)
{
case '\r': *carriage_return = true; break;
case '\n': eol = 1; break;
default:
*carriage_return = false;
line[n++] = ch;
break;
}
}
line[n] = 0;
return eof;
}
static int stream_read_line(Stream *s, char *line, size_t max_line_length)
{
size_t n = 0;
line[n] = 0;
int eol = 0;
int eof = 0;
size_t offset = 0;
while(!eol)
{
uint8_t ch = 0;
if(0 == s->read(s, &ch, 1, 1) || !ch)
{
// If we haven't read anything yet then this is the "real" EOF
// Had we encountered a \0 or EOF at the end of a line then it would have been one line too early
if(n == 0)
eof = 1;
break;
}
// Instead of stop parsing halfway through the line, just truncate the output instead
// if(n + 1 >= max_line_length) // n + 1 account for \0
// {
// break;
// }
switch(ch)
{
case '\r': break;
case '\n': eol = 1; break;
default:
if(n + 1 < max_line_length) // account for \0
line[n++] = ch;
break;
}
}
line[n] = 0;
return eof;
}
static void stream_unget(Stream *s)
{
s->seek(s, s->tell(s) - 1, STREAM_SEEK_BEG);
}
static void steam_advance(Stream *s)
{
s->seek(s, s->tell(s) + 1, STREAM_SEEK_BEG);
}
static uint8_t stream_advance(Stream *s)
{
uint8_t ch = 0;
s->read(s, &ch, 1, 1);
return ch;
}
static uint8_t stream_current(Stream *s)
{
uint8_t ch = 0;
if(1 == s->read(s, &ch, 1, 1))
{
stream_unget(s);
}
return ch;
}
static void stream_print(Stream *s, const char *text)
{
s->write(s, text, 1, strlen(text) + 1);
stream_unget(s);
}
static void stream_printf(Stream *s, const char *fmt, ...)
{
char text[2048] = { 0 };
if(fmt)
{
va_list va;
va_start(va, fmt);
vsnprintf(text, sizeof(text), fmt, va);
va_end(va);
}
s->write(s, text, 1, strlen(text) + 1);
stream_unget(s);
}
static void stream_skip_characters(Stream *s, const char *chars)
{
while(1)
{
uint8_t ch = 0;
if(0 == s->read(s, &ch, 1, 1) || !ch)
{
break;
}
bool skip = false;
for(size_t i = 0; chars[i]; ++i)
{
if(chars[i] == ch)
{
skip = true;
break;
}
}
if(!skip)
{
stream_unget(s);
break;
}
}
}
/* static size_t stream_read_(struct Stream_s *stream, void *ptr, size_t size, size_t nmemb) */
/* { */
/* size_t nb = size * nmemb; */
/* if(stream->offset + nb >= stream->length) */
/* return 0; // EOF */
/* memcpy(ptr, &stream->buffer[stream->offset], nb); */
/* stream->offset += nb; */
/* return nb; */
/* } */
/* static int stream_from_memory(Stream *stream, void *ptr, size_t size, const char *filename) */
/* { */
/* stream->buffer = ptr; */
/* stream->length = size; */
/* stream->offset = 0; */
/* stream->read = stream_read_; */
/* if(filename) */
/* { */
/* snprintf(stream->filename, sizeof(stream->filename), "%s", filename); */
/* } */
/* return 0; */
/* } */
#if 0
int stream_open(Stream *stream, const char *filename, const char *mode)
{
//...
return 1;
}
void stream_close(Stream *stream)
{
//...
}
#endif