-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTape.h
469 lines (372 loc) · 12.2 KB
/
Tape.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#pragma once
#include <stdio.h>
#include <string.h>
#include "Config.h"
#include "Math/Base.h"
#include "Memory/Base.h"
#include "Memory/RawBuffer.h"
#include "Str.h"
#include "Traits.h"
enum class ReadTapeMode : u32
{
Read,
Seek,
End,
};
/**
* i64 proc(void* dst, i64 size, Mode mode, void* usr)
*
* @param dst The destination buffer
* @param size The size of the destination buffer
* @param Mode The mode:
* - Read: read to the destination buffer
* - Seek: seek by size
* - End: return 1 if at end
* @return The number of bytes read, or the number of bytes moved
*/
#define PROC_READ_TAPE_READ(name) \
i64 name(void* dst, i64 size, ReadTapeMode mode, void* usr)
typedef PROC_READ_TAPE_READ(ProcReadTapeRead);
struct MOKLIB_API ReadTape {
ReadTape(ProcReadTapeRead* proc, void* usr) : proc(proc), usr(usr) {}
ProcReadTapeRead* proc;
void* usr;
_inline i64 read(void* dst, i64 size)
{
return proc(dst, size, ReadTapeMode::Read, usr);
}
_inline bool seek(i64 offset) { return try_seek(offset) == offset; }
_inline i64 try_seek(i64 offset)
{
return proc(0, offset, ReadTapeMode::Seek, usr);
}
_inline bool end() { return proc(0, 0, ReadTapeMode::End, usr) == 1; }
_inline char read_char()
{
char result;
if (read(&result, sizeof(result)) != 1) {
return EOF;
}
return result;
}
_inline bool peek_char(char match)
{
char result;
if (read(&result, sizeof(result)) != 1) {
return false;
}
if (result == match) {
return true;
} else {
seek(-1);
return false;
}
}
_inline Str read_str(Allocator& allocator, u64 len)
{
umm ptr = (umm)allocator.reserve(len);
if (!ptr) return Str::NullStr;
i64 num_read = read(ptr, (i64)len);
if (num_read == 0) return Str::NullStr;
return Str(ptr, (u64)num_read, false);
}
template <typename T>
_inline bool read_struct(T& out)
{
return read(&out, sizeof(T)) == sizeof(T);
}
};
enum class WriteTapeMode : u32
{
Write,
Seek,
End
};
/**
* i64 proc(void* src, i64 size, Mode mode, void* usr)
*
* @param src The source buffer
* @param size The size of the source buffer
* @param Mode The mode:
* - Write: write from the source buffer
* - Seek: seek by size
* - End: return 1 if at end. Note that some tapes may never be at their
* end
* @return The number of bytes written, or the number of bytes moved
*/
#define PROC_WRITE_TAPE_WRITE(name) \
i64 name(void* src, i64 size, WriteTapeMode mode, void* usr)
typedef PROC_WRITE_TAPE_WRITE(ProcWriteTapeWrite);
struct MOKLIB_API WriteTape {
WriteTape(ProcWriteTapeWrite* proc, void* usr) : proc(proc), usr(usr) {}
_inline i64 write(void* src, i64 size)
{
return proc(src, size, WriteTapeMode::Write, usr);
}
_inline bool write_char(char c) { return write(&c, 1) == 1; }
_inline bool write_raw(const Raw& raw)
{
return write(raw.buffer, raw.size);
}
_inline bool write_str(const Str& str) { return write_raw((Raw)str); }
_inline bool seek(i64 offset) { return try_seek(offset) == offset; }
_inline i64 try_seek(i64 offset)
{
return proc(0, offset, WriteTapeMode::Seek, usr);
}
ProcWriteTapeWrite* proc;
void* usr;
};
struct MOKLIB_API RawReadTape : public ReadTape {
RawReadTape(Raw buffer)
: buffer(buffer)
, offset(0)
, ReadTape(RawReadTape::read_proc, (void*)this)
{}
static PROC_READ_TAPE_READ(read_proc)
{
RawReadTape* self = (RawReadTape*)usr;
switch (mode) {
case ReadTapeMode::Read: {
if (size <= 0) return -1;
u64 sizeu64 = (u64)size;
if ((sizeu64 + self->offset) > self->buffer.size) {
sizeu64 = self->buffer.size - self->offset;
}
memcpy(dst, (u8*)self->buffer.buffer + self->offset, sizeu64);
self->offset += sizeu64;
return (i64)sizeu64;
} break;
case ReadTapeMode::Seek: {
if (size < 0) {
u64 sizeu64 = (u64)(-size);
if (sizeu64 > self->offset) sizeu64 = self->offset;
self->offset -= sizeu64;
return -((i64)sizeu64);
} else {
u64 sizeu64 = (u64)size;
if ((sizeu64 + self->offset) > self->buffer.size)
sizeu64 = self->buffer.size - self->offset;
self->offset += sizeu64;
return (i64)sizeu64;
}
} break;
case ReadTapeMode::End: {
return (self->offset == self->buffer.size) ? 1 : 0;
} break;
}
return 0;
}
u64 offset;
Raw buffer;
};
struct MOKLIB_API RawWriteTape : public WriteTape {
RawWriteTape(Raw buffer)
: buffer(buffer), offset(0), WriteTape(write_proc, (void*)this)
{}
static PROC_WRITE_TAPE_WRITE(write_proc)
{
RawWriteTape* self = (RawWriteTape*)usr;
switch (mode) {
case WriteTapeMode::Write: {
if (size <= 0) return -1;
u64 sizeu64 = (u64)size;
if ((sizeu64 + self->offset) > self->buffer.size) {
sizeu64 = self->buffer.size - self->offset;
}
memcpy((u8*)self->buffer.buffer + self->offset, src, sizeu64);
self->offset += sizeu64;
return (i64)sizeu64;
} break;
case WriteTapeMode::Seek: {
if (size < 0) {
u64 sizeu64 = (u64)(-size);
if (sizeu64 > self->offset) sizeu64 = self->offset;
self->offset -= sizeu64;
return -((i64)sizeu64);
} else {
u64 sizeu64 = (u64)size;
if ((sizeu64 + self->offset) > self->buffer.size)
sizeu64 = self->buffer.size - self->offset;
self->offset += sizeu64;
return (i64)sizeu64;
}
} break;
case WriteTapeMode::End: {
return (self->offset == self->buffer.size) ? 1 : 0;
} break;
}
return 0;
}
u64 offset;
Raw buffer;
};
struct MOKLIB_API ParseReadTape : public ReadTape {
ParseReadTape(ReadTape& tape)
: tape(tape), num_read(0), ReadTape(read_proc, (void*)this)
{}
static PROC_READ_TAPE_READ(read_proc)
{
ParseReadTape* self = (ParseReadTape*)usr;
switch (mode) {
case ReadTapeMode::Read: {
i64 off = self->tape.proc(dst, size, mode, self->tape.usr);
self->num_read += off;
return off;
} break;
case ReadTapeMode::Seek: {
i64 off = self->tape.proc(0, size, mode, self->tape.usr);
self->num_read += off;
return off;
} break;
case ReadTapeMode::End: {
return self->tape.proc(0, 0, mode, self->tape.usr);
} break;
}
return 0;
}
void restore() { tape.seek(-num_read); }
ReadTape& tape;
i64 num_read;
};
struct MOKLIB_API MeasureWriteTape : public WriteTape {
MeasureWriteTape() : WriteTape(write_proc, (void*)this) {}
static PROC_WRITE_TAPE_WRITE(write_proc)
{
MeasureWriteTape* self = (MeasureWriteTape*)usr;
switch (mode) {
case WriteTapeMode::Write: {
if (size <= 0) return 0;
self->num_written += size;
self->current_offset += size;
return size;
} break;
case WriteTapeMode::Seek: {
if (size < 0) {
u64 sizeu64 = (u64)(-size);
if (sizeu64 > self->current_offset) {
sizeu64 = self->current_offset;
}
self->current_offset -= sizeu64;
return -((i64)sizeu64);
} else {
self->current_offset += size;
return size;
}
} break;
case WriteTapeMode::End: {
return 0;
} break;
default: {
return 0;
} break;
}
}
/** The offset from the resulting writes + seeks */
u64 current_offset = 0;
/** The total number of bytes written */
u64 num_written = 0;
};
struct MOKLIB_API Tape {
// Interface
virtual u64 read(void* destination, u64 amount) = 0;
virtual bool write(const void* src, u64 num_bytes) = 0;
virtual bool end() = 0;
virtual void move(i64 offset) = 0;
char read_char();
bool peek_char(char match);
Str read_line(Allocator& alloc);
// Helpers
template <typename T>
bool read_struct(T* destination)
{
return read(destination, sizeof(T)) == sizeof(T);
}
int64 read_raw(Raw& destination)
{
return read(destination.buffer, u32(destination.size));
}
bool write_char(char c) { return write(&c, 1); }
bool write_str(const Str& str) { return write(str.data, str.len); }
};
struct SizedTape : public Tape {
// Interface
u64 current_offset, size;
virtual bool end() override { return current_offset >= size; }
virtual void move(i64 offset) override
{
if (offset < 0) {
current_offset -= u64(-offset);
} else {
current_offset += offset;
}
}
};
struct RawTape : public SizedTape {
Raw raw;
RawTape(const Raw& raw) : raw(raw)
{
current_offset = 0;
size = raw.size;
}
RawTape(const Str& str) : RawTape(Raw((void*)str.data, str.len)) {}
u64 read(void* destination, u64 amount) override
{
if (current_offset == size) {
return NumProps<u64>::max;
}
u64 offset = current_offset;
memcpy(destination, umm(raw.buffer) + offset, amount);
current_offset += amount;
return amount;
}
bool write(const void* src, u64 amount) override
{
if ((current_offset + amount) > size) {
return false;
}
u64 offset = min(current_offset, size - current_offset);
memcpy((umm)raw.buffer + current_offset, src, amount);
current_offset += amount;
return true;
}
};
struct MeasureTape : public Tape {
u64 num_written = 0;
// Interface
virtual u64 read(void* destination, u64 amount) override { return 0; }
virtual bool write(const void* src, u64 num_bytes) override
{
num_written += num_bytes;
return true;
}
virtual bool end() override { return false; }
virtual void move(i64 offset) override {}
};
/**
* Tape that keeps track of how many characters were read, and can reset the
* included tape. Simplifies parsing.
*/
struct ParseTape : public Tape {
i64 num_read = 0;
Tape* tape;
ParseTape(Tape* in_tape) : tape(in_tape) {}
// Interface
virtual u64 read(void* destination, u64 amount) override
{
u64 rd = tape->read(destination, amount);
num_read -= rd;
return rd;
}
virtual bool write(const void* src, u64 num_bytes) override
{
return false;
}
virtual bool end() override { return tape->end(); }
virtual void move(i64 offset) override
{
tape->move(offset);
num_read -= offset;
}
void restore() { tape->move(num_read); }
};