-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_stream.h
344 lines (289 loc) · 8.71 KB
/
rjd_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#define RJD_STREAM_H 1
// rjd_istream API design based on Fabien Giesen's Buffer-Centric IO:
// https://fgiesen.wordpress.com/2011/11/21/buffer-centric-io/
struct rjd_istream;
typedef struct rjd_result rjd_istream_refill_func(struct rjd_istream* stream);
typedef void rjd_istream_close_func(struct rjd_istream* stream);
struct rjd_istream
{
const uint8_t* start;
const uint8_t* end;
const uint8_t* cursor;
void* userdata;
struct rjd_result result;
rjd_istream_refill_func* refill;
rjd_istream_close_func* close;
};
// Pass this as buffer_size in rjd_istream_from_file to read the entire file into memory
enum
{
RJD_ISTREAM_FILE_BUFFER_SIZE_ALL = 0,
};
enum rjd_ostream_type
{
RJD_OSTREAM_TYPE_MEMORY,
RJD_OSTREAM_TYPE_FILE,
};
struct rjd_ostream_memory
{
uint8_t* buffer;
uint32_t size;
uint64_t cursor;
};
struct rjd_ostream_file
{
FILE* file;
};
struct rjd_ostream
{
enum rjd_ostream_type type;
union {
struct rjd_ostream_memory memory;
struct rjd_ostream_file file;
} state;
};
enum rjd_ostream_mode
{
RJD_OSTREAM_MODE_REPLACE,
RJD_OSTREAM_MODE_APPEND,
};
struct rjd_mem_allocator;
struct rjd_istream rjd_istream_from_zeroes(void);
struct rjd_istream rjd_istream_from_memory(const void* buffer, size_t size);
struct rjd_istream rjd_istream_from_file(const char* filepath, struct rjd_mem_allocator* allocator, size_t buffer_size);
struct rjd_result rjd_istream_read(struct rjd_istream* stream, void* buffer, size_t size);
void rjd_istream_close(struct rjd_istream* stream);
struct rjd_ostream rjd_ostream_from_memory(void* buffer, size_t size);
struct rjd_ostream rjd_ostream_from_file(const char* filepath, enum rjd_ostream_mode);
struct rjd_result rjd_ostream_write(struct rjd_ostream* stream, const void* buffer, size_t size);
void rjd_ostream_close(struct rjd_ostream* stream);
#if RJD_IMPL
static struct rjd_result rjd_istream_fail(struct rjd_istream* stream, const char* reason);
static struct rjd_result rjd_istream_refill_zeroes(struct rjd_istream* stream);
static struct rjd_result rjd_istream_refill_memory(struct rjd_istream* stream);
static struct rjd_result rjd_istream_refill_file(struct rjd_istream* stream);
static void rjd_istream_close_file(struct rjd_istream* stream);
struct rjd_istream rjd_istream_from_zeroes()
{
struct rjd_istream stream =
{
.start = NULL,
.end = NULL,
.cursor = NULL,
.userdata = NULL,
.result = RJD_RESULT_OK(),
.refill = rjd_istream_refill_zeroes,
.close = NULL,
};
stream.refill(&stream);
return stream;
}
struct rjd_istream rjd_istream_from_memory(const void* buffer, size_t size)
{
const uint8_t* cbuffer = buffer;
struct rjd_istream stream =
{
.start = cbuffer,
.end = cbuffer + size,
.cursor = cbuffer,
.userdata = NULL,
.result = RJD_RESULT_OK(),
.refill = rjd_istream_refill_memory,
.close = NULL,
};
return stream;
}
struct rjd_istream rjd_istream_from_file(const char* filepath, struct rjd_mem_allocator* allocator, size_t buffer_size)
{
RJD_ASSERT(filepath);
RJD_ASSERT(allocator);
uint8_t* buffer = NULL;
struct rjd_result result = RJD_RESULT_OK();
FILE* file = fopen(filepath, "rb");
if (file) {
if (buffer_size == RJD_ISTREAM_FILE_BUFFER_SIZE_ALL) {
result = RJD_RESULT("Failed to get file length");
if (fseek(file, 0, SEEK_END) == 0) {
long int length = ftell(file);
if (length >= 0) {
if (fseek(file, 0, SEEK_SET) == 0) {
buffer_size = (size_t)length;
result = RJD_RESULT_OK();
}
}
}
}
} else {
result = RJD_RESULT("Failed to open file");
}
if (rjd_result_isok(result)) {
buffer = rjd_mem_alloc_array(uint8_t, buffer_size, allocator);
}
struct rjd_istream stream =
{
.start = buffer,
.end = buffer + buffer_size,
.cursor = buffer,
.userdata = file,
.refill = rjd_istream_refill_file,
.close = rjd_istream_close_file,
};
stream.result = stream.refill(&stream);
return stream;
}
struct rjd_result rjd_istream_read(struct rjd_istream* stream, void* buffer, size_t size)
{
RJD_ASSERT(stream);
RJD_ASSERT(buffer);
RJD_ASSERT(size > 0);
uint8_t* offset_buffer = buffer;
size_t bytes_remaining = size;
while (bytes_remaining > 0) {
if (stream->cursor == stream->end) {
stream->result = stream->refill(stream);
}
RJD_ASSERT(stream->end >= stream->cursor)
size_t buffersize = rjd_math_truncate_u64_to_sizet(stream->end - stream->cursor);
size_t readsize = rjd_math_min(buffersize, bytes_remaining);
memcpy(offset_buffer, stream->cursor, readsize);
offset_buffer += readsize;
bytes_remaining -= readsize;
stream->cursor += readsize;
}
return stream->result;
}
void rjd_istream_close(struct rjd_istream* stream)
{
RJD_ASSERT(stream);
if (stream->close) {
stream->close(stream);
}
}
struct rjd_ostream rjd_ostream_from_memory(void* buffer, size_t size)
{
struct rjd_ostream stream =
{
.type = RJD_OSTREAM_TYPE_MEMORY,
.state = {
.memory = { buffer, (uint32_t)size, 0 },
},
};
return stream;
}
struct rjd_ostream rjd_ostream_from_file(const char* filepath, enum rjd_ostream_mode mode)
{
const char* writemode = NULL;
switch (mode)
{
case RJD_OSTREAM_MODE_REPLACE: writemode = "wb"; break;
case RJD_OSTREAM_MODE_APPEND: writemode = "ab"; break;
}
RJD_ASSERT(writemode);
FILE* file = fopen(filepath, writemode);
struct rjd_ostream stream =
{
.type = RJD_OSTREAM_TYPE_FILE,
.state = {
.file = { file },
},
};
return stream;
}
struct rjd_result rjd_ostream_write(struct rjd_ostream* stream, const void* buffer, size_t size)
{
if (stream->type == RJD_OSTREAM_TYPE_MEMORY) {
struct rjd_ostream_memory* state = &stream->state.memory;
RJD_ASSERT(state->size >= state->cursor);
const size_t bytes_remaining = rjd_math_truncate_u64_to_sizet(state->size - state->cursor);
if (size > bytes_remaining) {
return RJD_RESULT("attempted to write more data than the buffer can hold");
}
const uint8_t* cbuffer = buffer;
RJD_ASSERTMSG(!(cbuffer >= state->buffer && cbuffer < state->buffer + state->size),
"source and destination buffers must not overlap");
memcpy(state->buffer + state->cursor, buffer, size);
state->cursor += size;
} else if (stream->type == RJD_OSTREAM_TYPE_FILE) {
struct rjd_ostream_file* state = &stream->state.file;
if (state->file == NULL) {
return RJD_RESULT("failed to open file for writing");
}
size_t bytes_written = fwrite(buffer, 1, size, state->file);
if (bytes_written != size) {
return RJD_RESULT("failed to write all data to file");
}
}
return RJD_RESULT_OK();
}
void rjd_ostream_close(struct rjd_ostream* stream)
{
if (stream->type == RJD_OSTREAM_TYPE_MEMORY) {
stream->state.memory.buffer = NULL;
} else if (stream->type == RJD_OSTREAM_TYPE_FILE) {
fclose(stream->state.file.file);
}
}
////////////////////////////////////////////////////////////////////////////////
// static helpers
static struct rjd_result rjd_istream_fail(struct rjd_istream* stream, const char* reason)
{
stream->result = (struct rjd_result){reason};
stream->refill = rjd_istream_refill_zeroes;
return stream->refill(stream);
}
static struct rjd_result rjd_istream_refill_zeroes(struct rjd_istream* stream)
{
static uint8_t zeroes[128] = {0};
stream->start = zeroes;
stream->end = zeroes + sizeof(zeroes);
stream->cursor = stream->start;
return stream->result;
}
static struct rjd_result rjd_istream_refill_memory(struct rjd_istream* stream)
{
return rjd_istream_fail(stream, "reached end of memory buffer");
}
static struct rjd_result rjd_istream_refill_file(struct rjd_istream* stream)
{
FILE* file = (FILE*)stream->userdata;
// cast to non-const since we know the file buffer is ok to write to
void* writable_buffer = (void*)stream->start;
ptrdiff_t bytes_wanted = stream->end - stream->start;
int32_t bytes_read = (int32_t)fread(writable_buffer, 1, bytes_wanted, file);
RJD_ASSERT(bytes_read <= bytes_wanted);
if (bytes_read < bytes_wanted) {
if (feof(file)) {
if (bytes_read == 0) {
rjd_istream_close_file(stream);
rjd_istream_fail(stream, "end of file reached, no more data available");
} else {
stream->end = stream->start + bytes_read;
stream->cursor = stream->start;
stream->result = RJD_RESULT_OK();
}
} else {
rjd_istream_close_file(stream);
rjd_istream_fail(stream, "error reading file contents into memory");
}
} else {
if (feof(file)) {
rjd_istream_fail(stream, "end of file reached, no more data available");
} else {
stream->cursor = stream->start;
}
}
return stream->result;
}
static void rjd_istream_close_file(struct rjd_istream* stream)
{
rjd_mem_free(stream->start);
FILE* file = (FILE*)stream->userdata;
if (file) {
fclose(file);
}
stream->userdata = NULL;
stream->refill = rjd_istream_refill_zeroes;
stream->close = NULL;
stream->refill(stream);
}
#endif