-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.cpp
618 lines (531 loc) · 16 KB
/
io.cpp
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright 2010-2021 Chris Spiegel.
//
// This file is part of Bocfel.
//
// Bocfel is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version
// 2 or 3, as published by the Free Software Foundation.
//
// Bocfel is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Bocfel. If not, see <http://www.gnu.org/licenses/>.
#include <climits>
#include <cstdio>
#include <cstring>
#include <new>
#include <stdexcept>
#include <vector>
#ifdef ZTERP_GLK
extern "C" {
#include <glk.h>
}
#else
#include <iostream>
#endif
#include "io.h"
#include "osdep.h"
#include "types.h"
#include "unicode.h"
#include "util.h"
// Due to C++’s less-than-ideal type system, there’s no way to guarantee
// that an enum class actually contains a valid value. When checking the
// value of the I/O object’s type, this method is used as a sort of
// run-time type checker.
void IO::bad_type() const {
die("internal error: unknown IO type %d", static_cast<int>(m_type));
}
// Certain streams are intended for use in text mode: stdin/stdout,
// transcripts, and command scripts. It is reasonable for users to
// expect newline translation to be properly handled in these cases,
// even though UNICODE_LINEFEED (10), as required by Glk (Glk API 0.7.0
// §2.2), is used internally.
bool IO::textmode() const {
return m_purpose == Purpose::Transcript || m_purpose == Purpose::Input;
}
// Glk does not like you to be able to pass a full filename to
// glk_fileref_create_by_name(); this means that Glk cannot be used to
// open arbitrary files. However, Glk is still required to prompt for
// files, such as in a save game situation. To allow I/O to work for
// opening files both with and without a prompt, it will use stdio when
// either Glk is not available, or when Glk is available but prompting
// is not necessary.
//
// This is needed because the IFF parser is required for both opening
// games (zblorb files) and for saving/restoring. The former needs to
// be able to access any file on the filesystem, and the latter needs to
// prompt. This is a headache.
//
// Prompting is assumed to be necessary if “filename” is null.
IO::IO(const std::string *filename, Mode mode, Purpose purpose) :
m_mode(mode),
m_purpose(purpose)
{
char smode[] = "wb";
if (m_mode == Mode::ReadOnly) {
smode[0] = 'r';
} else if (m_mode == Mode::Append) {
smode[0] = 'a';
}
if (textmode()) {
smode[1] = 0;
}
// No need to prompt.
if (filename != nullptr) {
m_type = Type::StandardIO;
m_file = File(std::fopen(filename->c_str(), smode), true);
if (m_file.stdio == nullptr) {
throw OpenError();
}
} else { // Prompt.
#ifdef ZTERP_GLK
frefid_t ref;
glui32 usage, filemode;
usage = fileusage_BinaryMode;
switch (m_purpose) {
case Purpose::Data:
usage |= fileusage_Data;
break;
case Purpose::Save:
usage |= fileusage_SavedGame;
break;
case Purpose::Transcript:
usage |= fileusage_Transcript;
break;
case Purpose::Input:
usage |= fileusage_InputRecord;
break;
default:
throw OpenError();
}
switch (m_mode) {
case Mode::ReadOnly:
filemode = filemode_Read;
break;
case Mode::WriteOnly:
filemode = filemode_Write;
break;
case Mode::Append:
filemode = filemode_WriteAppend;
break;
default:
throw OpenError();
}
ref = glk_fileref_create_by_prompt(usage, filemode, 0);
if (ref == nullptr) {
throw OpenError();
}
m_type = Type::Glk;
m_file = File(glk_stream_open_file(ref, filemode, 0));
glk_fileref_destroy(ref);
if (m_file.glk == nullptr) {
throw OpenError();
}
#else
std::string fn, prompt;
switch (m_purpose) {
case Purpose::Data:
prompt = "Enter filename for data: ";
break;
case Purpose::Save:
prompt = "Enter filename for save game: ";
break;
case Purpose::Transcript:
prompt = "Enter filename for transcript: ";
break;
case Purpose::Input:
prompt = "Enter filename for command record: ";
break;
default:
throw OpenError();
}
std::cout << std::endl << prompt << std::flush;
if (!std::getline(std::cin, fn) || fn.empty()) {
throw OpenError();
}
m_type = Type::StandardIO;
m_file = File(std::fopen(fn.c_str(), smode), true);
if (m_file.stdio == nullptr) {
throw OpenError();
}
#endif
}
}
// Instead of being file-backed, indicate that this I/O object is
// memory-backed. This allows internal save states (for @save_undo as
// well as meta-saves created by /ps) to use Quetzal as their save
// format. This is helpful because meta-saves have to track extra
// information, and reusing the Quetzal code (plus extensions)
// eliminates the need for code duplication.
//
// The I/O object starts out with the contents of the passed-in buffer,
// which may be empty. The offset always starts at 0.
IO::IO(std::vector<uint8_t> buf, Mode mode) :
m_file(File(std::move(buf))),
m_type(Type::Memory),
m_mode(mode),
m_purpose(Purpose::Data)
{
// Append isn’t used with memory-backed I/O, so it’s not supported.
if (m_mode != Mode::ReadOnly && m_mode != Mode::WriteOnly) {
throw OpenError();
}
}
// Return a reference to the I/O instance’s internal buffer. This
// represents the state of the “file” at the time the function is
// called. The reference is only valid until the next call to an I/O
// method on this same I/O instance.
const std::vector<uint8_t> &IO::get_memory()
{
if (m_type != Type::Memory) {
throw std::runtime_error("not a memory object");
}
return m_file.backing.memory;
}
void IO::seek(long offset, SeekFrom whence)
{
// To smooth over differences between Glk and standard I/O, don’t
// allow seeking in append-only streams.
if (m_mode == Mode::Append) {
throw IOError();
}
switch (m_type) {
case Type::StandardIO:
if (std::fseek(m_file.stdio.get(), offset, whence == SeekFrom::Start ? SEEK_SET : whence == SeekFrom::Current ? SEEK_CUR : SEEK_END) != 0) {
throw IOError();
}
return;
case Type::Memory:
// Negative offsets are unsupported because they aren’t used.
if (offset < 0) {
throw IOError();
}
if (whence == SeekFrom::Current) {
// Overflow.
if (LONG_MAX - offset < m_file.backing.offset) {
throw IOError();
}
offset = m_file.backing.offset + offset;
} else if (whence == SeekFrom::End) {
// SeekFrom::End is only used to seek directly to the end.
if (offset != 0) {
throw IOError();
}
offset = m_file.backing.memory.size();
} else if (whence == SeekFrom::Start) {
// Do nothing; offset is where it should be.
}
// If seeking beyond the end, write zeros.
while (offset > m_file.backing.memory.size()) {
write8(0);
}
m_file.backing.offset = offset;
return;
#ifdef ZTERP_GLK
case Type::Glk:
glk_stream_set_position(m_file.glk.get(), offset, whence == SeekFrom::Start ? seekmode_Start : whence == SeekFrom::Current ? seekmode_Current : seekmode_End);
return; // glk_stream_set_position can’t signal failure
#endif
default:
bad_type();
}
}
long IO::tell()
{
switch (m_type) {
case Type::StandardIO: {
auto offset = std::ftell(m_file.stdio.get());
if (offset == -1) {
throw IOError();
}
return offset;
}
case Type::Memory:
return m_file.backing.offset;
#ifdef ZTERP_GLK
case Type::Glk: {
auto offset = glk_stream_get_position(m_file.glk.get());
#if LONG_MAX < UINT32_MAX
if (offset > LONG_MAX) {
throw IOError();
}
#endif
return offset;
}
#endif
default:
bad_type();
}
}
// read() and write() always operate in terms of bytes, not characters.
size_t IO::read(void *buf, size_t n)
{
size_t total = 0;
while (total < n) {
size_t s;
switch (m_type) {
case Type::StandardIO:
s = std::fread(buf, 1, n - total, m_file.stdio.get());
break;
case Type::Memory: {
Backing *b = &m_file.backing;
auto remaining = b->memory.size() - b->offset;
if (m_mode != Mode::ReadOnly) {
return 0;
}
s = remaining < n ? remaining : n;
if (s != 0) {
std::memcpy(buf, &b->memory[b->offset], s);
b->offset += s;
}
break;
}
#ifdef ZTERP_GLK
case Type::Glk: {
glui32 s32 = glk_get_buffer_stream(m_file.glk.get(), static_cast<char *>(buf), n - total);
// This should only happen if m_file.glk is invalid.
if (s32 == static_cast<glui32>(-1)) {
s = 0;
} else {
s = s32;
}
break;
}
#endif
default:
bad_type();
}
if (s == 0) {
break;
}
total += s;
buf = (static_cast<char *>(buf)) + s;
}
return total;
}
void IO::read_exact(void *buf, size_t n)
{
if (read(buf, n) != n) {
throw IOError();
}
}
size_t IO::write(const void *buf, size_t n)
{
if (n == 0) {
return 0;
}
switch (m_type) {
case Type::StandardIO: {
size_t s, total = 0;
while (total < n && (s = std::fwrite(buf, 1, n - total, m_file.stdio.get())) > 0) {
total += s;
buf = (static_cast<const char *>(buf)) + s;
}
return total;
}
case Type::Memory: {
Backing *b = &m_file.backing;
auto remaining = b->memory.size() - b->offset;
if (m_mode != Mode::WriteOnly) {
return 0;
}
if (n > remaining) {
try {
b->memory.resize(b->memory.size() + (n - remaining));
} catch (const std::bad_alloc &) {
throw IOError();
}
}
std::memcpy(&b->memory[b->offset], buf, n);
b->offset += n;
return n;
}
#ifdef ZTERP_GLK
case Type::Glk:
glk_put_buffer_stream(m_file.glk.get(), const_cast<char *>(static_cast<const char *>(buf)), n);
return n; // glk_put_buffer_stream() can’t signal a short write
#endif
default:
bad_type();
}
}
void IO::write_exact(const void *buf, size_t n)
{
if (write(buf, n) != n) {
throw IOError();
}
}
uint8_t IO::read8()
{
uint8_t v;
read_exact(&v, sizeof v);
return v;
}
uint16_t IO::read16()
{
uint8_t buf[2];
read_exact(buf, sizeof buf);
return ((static_cast<uint16_t>(buf[0]) << 8)) | (static_cast<uint16_t>(buf[1]));
}
uint32_t IO::read32()
{
uint8_t buf[4];
read_exact(buf, sizeof buf);
return ((static_cast<uint32_t>(buf[0])) << 24) |
((static_cast<uint32_t>(buf[1])) << 16) |
((static_cast<uint32_t>(buf[2])) << 8) |
((static_cast<uint32_t>(buf[3])) );
}
void IO::write8(uint8_t v)
{
return write_exact(&v, sizeof v);
}
void IO::write16(uint16_t v)
{
uint8_t buf[2];
buf[0] = v >> 8;
buf[1] = v & 0xff;
return write_exact(buf, sizeof buf);
}
void IO::write32(uint32_t v)
{
uint8_t buf[4];
buf[0] = (v >> 24) & 0xff;
buf[1] = (v >> 16) & 0xff;
buf[2] = (v >> 8) & 0xff;
buf[3] = (v >> 0) & 0xff;
return write_exact(buf, sizeof buf);
}
// getc() and putc() are meant to operate in terms of characters, not
// bytes. That is, unlike C++, bytes and characters are not equivalent
// as far as Zterp’s I/O system is concerned.
// Read a UTF-8 character, returning it. If limit16 is true, any Unicode
// values which are greater than UINT16_MAX will be converted to the
// Unicode replacement character. Otherwise, values are returned as-is.
// -1 is returned on EOF.
//
// If an invalid UTF-8 sequence is found, the Unicode replacement
// character is returned.
long IO::getc(bool limit16)
{
long ret;
uint8_t c;
struct NotUnicode : std::exception {};
try {
c = read8();
} catch (const IOError &) {
return -1;
}
// Read a byte and make sure it’s part of a valid UTF-8 sequence.
auto read_byte = [this] {
uint8_t b = read8();
if ((b & 0x80) != 0x80) {
throw NotUnicode();
} else {
return b;
}
};
try {
if ((c & 0x80) == 0) { // One byte.
ret = c;
} else if ((c & 0xe0) == 0xc0) { // Two bytes.
ret = (c & 0x1f) << 6;
ret |= (read_byte() & 0x3f);
} else if ((c & 0xf0) == 0xe0) { // Three bytes.
ret = (c & 0x0f) << 12;
ret |= (read_byte() & 0x3f) << 6;
ret |= (read_byte() & 0x3f);
} else if ((c & 0xf8) == 0xf0) { // Four bytes.
ret = (static_cast<long>(c) & 0x07) << 18;
ret |= (read_byte() & 0x3f) << 12;
ret |= (read_byte() & 0x3f) << 6;
ret |= (read_byte() & 0x3f);
} else { // Invalid value.
ret = UNICODE_REPLACEMENT;
}
} catch (const IOError &) {
return -1;
} catch (const NotUnicode &) {
return UNICODE_REPLACEMENT;
}
if (limit16 && ret > UINT16_MAX) {
ret = UNICODE_REPLACEMENT;
}
if (textmode() && ret == '\n') {
ret = UNICODE_LINEFEED;
}
return ret;
}
// Write a Unicode character as UTF-8. If this fails it may write a
// partial character.
void IO::putc(uint32_t c)
{
if (textmode() && c == UNICODE_LINEFEED) {
c = '\n';
}
if (c >= 0x110000) {
c = UNICODE_REPLACEMENT;
}
if (c < 0x80) {
write8(c);
} else if (c < 0x800) {
write8(0xc0 | ((c >> 6) & 0x1f));
write8(0x80 | ((c ) & 0x3f));
} else if (c < 0x10000) {
write8(0xe0 | ((c >> 12) & 0x0f));
write8(0x80 | ((c >> 6) & 0x3f));
write8(0x80 | ((c ) & 0x3f));
} else if (c < 0x110000) {
write8(0xf0 | ((c >> 18) & 0x07));
write8(0x80 | ((c >> 12) & 0x3f));
write8(0x80 | ((c >> 6) & 0x3f));
write8(0x80 | ((c ) & 0x3f));
}
}
// Read and return a line, where a line is defined as a collection of
// characters terminated by a Unicode linefeed (0xa). If EOF is
// encountered at any point (including after characters have been read,
// but before a linefeed), EndOfFile is thrown, which means that all
// lines, including the last one, must end in a linefeed. Any characters
// read before the EOF can be considered lost and unrecoverable.
std::vector<uint16_t> IO::readline()
{
std::vector<uint16_t> result;
while (true) {
long c = getc(true);
if (c == -1) {
throw EndOfFile();
}
if (c == UNICODE_LINEFEED) {
break;
}
result.push_back(c);
}
return result;
}
long IO::filesize()
{
switch (m_type) {
case Type::StandardIO:
if (!textmode()) {
return zterp_os_filesize(m_file.stdio.get());
}
break;
case Type::Memory:
return m_file.backing.memory.size();
#ifdef ZTERP_GLK
case Type::Glk:
break;
#endif
}
return -1;
}
void IO::flush()
{
if (m_type == Type::StandardIO && (m_mode == Mode::WriteOnly || m_mode == Mode::Append)) {
std::fflush(m_file.stdio.get());
}
}