2 * Copyright 2010-2012 Chris Spiegel.
4 * This file is part of Bocfel.
6 * Bocfel is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version
8 * 2 or 3, as published by the Free Software Foundation.
10 * Bocfel is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Bocfel. If not, see <http://www.gnu.org/licenses/>.
37 /* Generally speaking, UNICODE_LINEFEED (10) is used as a newline. Glk
38 * requires this (Glk API 0.7.0 §2.2), and when Unicode is available, we
39 * write characters out by hand even with stdio, so no translation can
40 * be done. However, when stdio is being used, Unicode is not
41 * available, and the file usage will be for a transcript or
42 * command-script, use '\n' as a newline so translation can be done;
43 * this is the only case where streams are opened in text mode.
45 * zterp_io_stdio() and zterp_io_stdout() are considered text-mode if
46 * Unicode is not available, binary otherwise.
48 #define textmode(io) (!use_utf8_io && ((io->mode) & (ZTERP_IO_TRANS | ZTERP_IO_INPUT)))
52 enum type { IO_STDIO, IO_GLK } type;
61 /* Glk does not like you to be able to pass a full filename to
62 * glk_fileref_create_by_name(); this means that Glk cannot be used to
63 * open arbitrary files. However, Glk is still required to prompt for
64 * files, such as in a save game situation. To allow zterp_io to work
65 * for opening files both with and without a prompt, it will use stdio
66 * when either Glk is not available, or when Glk is available but
67 * prompting is not necessary.
69 * This is needed because the IFF parser is required for both opening
70 * games (zblorb files) and for saving/restoring. The former needs to
71 * be able to access any file on the filesystem, and the latter needs to
72 * prompt. This is a headache.
74 * Prompting is assumed to be necessary if “filename” is NULL.
76 zterp_io *zterp_io_open(const char *filename, int mode)
81 io = malloc(sizeof *io);
82 if(io == NULL) goto err;
85 if (mode & ZTERP_IO_RDONLY) smode[0] = 'r';
86 else if(mode & ZTERP_IO_APPEND) smode[0] = 'a';
88 if(textmode(io)) smode[1] = 0;
91 int usage = fileusage_BinaryMode, filemode;
93 if (mode & ZTERP_IO_SAVE) usage |= fileusage_SavedGame;
94 else if(mode & ZTERP_IO_TRANS) usage |= fileusage_Transcript;
95 else if(mode & ZTERP_IO_INPUT) usage |= fileusage_InputRecord;
96 else usage |= fileusage_Data;
98 if (mode & ZTERP_IO_RDONLY) filemode = filemode_Read;
99 else if(mode & ZTERP_IO_WRONLY) filemode = filemode_Write;
100 else if(mode & ZTERP_IO_APPEND) filemode = filemode_WriteAppend;
106 if (mode & ZTERP_IO_SAVE) prompt = "Enter filename for save game: ";
107 else if(mode & ZTERP_IO_TRANS) prompt = "Enter filename for transcript: ";
108 else if(mode & ZTERP_IO_INPUT) prompt = "Enter filename for command record: ";
109 else prompt = "Enter filename for data: ";
112 /* No need to prompt. */
116 io->fp = fopen(filename, smode);
117 if(io->fp == NULL) goto err;
125 ref = glk_fileref_create_by_prompt(usage, filemode, 0);
126 if(ref == NULL) goto err;
129 io->file = glk_stream_open_file(ref, filemode, 0);
130 glk_fileref_destroy(ref);
131 if(io->file == NULL) goto err;
133 char fn[MAX_PATH], *p;
135 printf("\n%s", prompt);
137 if(fgets(fn, sizeof fn, stdin) == NULL || fn[0] == '\n') goto err;
138 p = strchr(fn, '\n');
139 if(p != NULL) *p = 0;
142 io->fp = fopen(fn, smode);
143 if(io->fp == NULL) goto err;
155 /* The zterp_os_reopen_binary() calls attempt to reopen stdin/stdout as
156 * binary streams so that reading/writing UTF-8 doesn’t cause unwanted
157 * translations. The mode of ZTERP_IO_TRANS is set when Unicode is
158 * unavailable as a way to signal that these are text streams.
160 const zterp_io *zterp_io_stdin(void)
167 io.mode = ZTERP_IO_RDONLY;
168 if(use_utf8_io) zterp_os_reopen_binary(stdin);
169 else io.mode |= ZTERP_IO_TRANS;
176 const zterp_io *zterp_io_stdout(void)
183 io.mode = ZTERP_IO_WRONLY;
184 if(use_utf8_io) zterp_os_reopen_binary(stdout);
185 else io.mode |= ZTERP_IO_TRANS;
192 void zterp_io_close(zterp_io *io)
195 if(io->type == IO_GLK)
197 glk_stream_close(io->file, NULL);
208 int zterp_io_seek(const zterp_io *io, long offset, int whence)
210 /* To smooth over differences between Glk and standard I/O, don’t
211 * allow seeking in append-only streams.
213 if(io->mode & ZTERP_IO_APPEND) return -1;
216 if(io->type == IO_GLK)
218 glk_stream_set_position(io->file, offset, whence == SEEK_SET ? seekmode_Start : whence == SEEK_CUR ? seekmode_Current : seekmode_End);
219 return 0; /* dammit */
224 return fseek(io->fp, offset, whence);
228 long zterp_io_tell(const zterp_io *io)
231 if(io->type == IO_GLK)
233 return glk_stream_get_position(io->file);
238 return ftell(io->fp);
242 /* zterp_io_read() and zterp_io_write() always operate in terms of
243 * bytes, whether or not Unicode is available.
245 size_t zterp_io_read(const zterp_io *io, void *buf, size_t n)
248 if(io->type == IO_GLK)
250 glui32 s = glk_get_buffer_stream(io->file, buf, n);
251 /* This should only happen if io->file is invalid. */
252 if(s == (glui32)-1) s = 0;
258 return fread(buf, 1, n, io->fp);
262 size_t zterp_io_write(const zterp_io *io, const void *buf, size_t n)
265 if(io->type == IO_GLK)
267 glk_put_buffer_stream(io->file, (char *)buf, n);
268 return n; /* dammit */
273 return fwrite(buf, 1, n, io->fp);
277 int zterp_io_read16(const zterp_io *io, uint16_t *v)
281 if(zterp_io_read(io, buf, sizeof buf) != sizeof buf) return 0;
283 *v = (buf[0] << 8) | buf[1];
288 int zterp_io_read32(const zterp_io *io, uint32_t *v)
292 if(zterp_io_read(io, buf, sizeof buf) != sizeof buf) return 0;
294 *v = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
299 /* Read a byte and make sure it’s part of a valid UTF-8 sequence. */
300 static int read_byte(const zterp_io *io, uint8_t *c)
302 if(zterp_io_read(io, c, sizeof *c) != sizeof *c) return 0;
303 if((*c & 0x80) != 0x80) return 0;
308 /* zterp_io_getc() and zterp_io_putc() are meant to operate in terms of
309 * characters, not bytes. That is, unlike C, bytes and characters are
310 * not equivalent as far as Zterp’s I/O system is concerned.
313 /* Read a UTF-8 character, returning it.
314 * -1 is returned on EOF.
316 * If there is a problem reading the UTF-8 (either from an invalid
317 * sequence or from a too-large value), a question mark is returned.
319 * If Unicode is not available, read a single byte (assumed to be
321 * If Unicode is not available, IO_STDIO is in use, and text mode is
322 * set, do newline translation. Text mode is likely to always be
323 * set—this function really shouldn’t be used in binary mode.
325 long zterp_io_getc(const zterp_io *io)
332 if(io->type == IO_GLK)
334 ret = glk_get_char_stream(io->file);
342 if(c == EOF) ret = -1;
345 if(textmode(io) && c == '\n') ret = UNICODE_LINEFEED;
352 if(zterp_io_read(io, &c, sizeof c) != sizeof c)
356 else if((c & 0x80) == 0) /* One byte. */
360 else if((c & 0xe0) == 0xc0) /* Two bytes. */
362 ret = (c & 0x1f) << 6;
364 if(!read_byte(io, &c)) return UNICODE_QUESTIONMARK;
368 else if((c & 0xf0) == 0xe0) /* Three bytes. */
370 ret = (c & 0x0f) << 12;
372 if(!read_byte(io, &c)) return UNICODE_QUESTIONMARK;
374 ret |= ((c & 0x3f) << 6);
376 if(!read_byte(io, &c)) return UNICODE_QUESTIONMARK;
380 else if((c & 0xf8) == 0xf0) /* Four bytes. */
382 /* The Z-machine doesn’t support Unicode this large, but at
383 * least try not to leave a partial character in the stream.
385 zterp_io_seek(io, 3, SEEK_CUR);
387 ret = UNICODE_QUESTIONMARK;
389 else /* Invalid value. */
391 ret = UNICODE_QUESTIONMARK;
395 if(ret > UINT16_MAX) ret = UNICODE_QUESTIONMARK;
400 /* Write a Unicode character as UTF-8.
402 * If Unicode is not available, write the value out as a single Latin-1
403 * byte. If it is too large for a byte, write out a question mark.
405 * If Unicode is not available, IO_STDIO is in use, and text mode is
406 * set, do newline translation.
408 * Text mode is likely to always be set—this function really shouldn’t
409 * be used in binary mode.
411 void zterp_io_putc(const zterp_io *io, uint16_t c)
415 if(c > UINT8_MAX) c = UNICODE_QUESTIONMARK;
417 if(io->type == IO_GLK)
419 glk_put_char_stream(io->file, c);
424 if(textmode(io) && c == UNICODE_LINEFEED) c = '\n';
430 uint8_t hi = c >> 8, lo = c & 0xff;
432 #define WRITE(c) zterp_io_write(io, &(uint8_t){ c }, sizeof (uint8_t))
439 WRITE(0xc0 | (hi << 2) | (lo >> 6));
440 WRITE(0x80 | (lo & 0x3f));
444 WRITE(0xe0 | (hi >> 4));
445 WRITE(0x80 | ((hi << 2) & 0x3f) | (lo >> 6));
446 WRITE(0x80 | (lo & 0x3f));
452 long zterp_io_readline(const zterp_io *io, uint16_t *buf, size_t len)
456 if(len > LONG_MAX) return -1;
458 for(ret = 0; ret < len; ret++)
460 long c = zterp_io_getc(io);
462 /* EOF before newline means there was a problem. */
463 if(c == -1) return -1;
465 /* Don’t count the newline. */
466 if(c == UNICODE_LINEFEED) break;
474 long zterp_io_filesize(const zterp_io *io)
476 if(io->type == IO_STDIO && !textmode(io))
478 return zterp_os_filesize(io->fp);
486 void zterp_io_flush(const zterp_io *io)
488 if(io == NULL || io->type != IO_STDIO || !(io->mode & (ZTERP_IO_WRONLY | ZTERP_IO_APPEND))) return;