7 #include <glib/gstdio.h>
9 #include "chimara-glk-private.h"
10 extern GPrivate *glk_data_key;
12 /* Internal function: create a stream with a specified rock value */
14 stream_new_common(glui32 rock)
16 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
18 strid_t str = g_new0(struct glk_stream_struct, 1);
19 str->magic = MAGIC_STREAM;
21 if(glk_data->register_obj)
22 str->disprock = (*glk_data->register_obj)(str, gidisp_Class_Stream);
24 /* Add it to the global stream list */
25 glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
26 str->stream_list = glk_data->stream_list;
31 /* Internal function: Stuff to do upon closing any type of stream. Call only
34 stream_close_common(strid_t str, stream_result_t *result)
36 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
38 if(glk_data->unregister_obj)
40 (*glk_data->unregister_obj)(str, gidisp_Class_Stream, str->disprock);
41 str->disprock.ptr = NULL;
44 /* If the stream was one or more windows' echo streams, set those to NULL */
46 for(win = glk_window_iterate(NULL, NULL); win;
47 win = glk_window_iterate(win, NULL))
48 if(win->echo_stream == str)
49 win->echo_stream = NULL;
51 /* Return the character counts */
54 result->readcount = str->read_count;
55 result->writecount = str->write_count;
58 /* Remove the stream from the global stream list */
59 glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
61 /* If it was the current output stream, set that to NULL */
62 if(glk_data->current_stream == str)
63 glk_data->current_stream = NULL;
65 str->magic = MAGIC_FREE;
71 * @str: A stream, or %NULL.
72 * @rockptr: Return location for the next window's rock, or %NULL.
74 * Iterates through all the existing streams. See <link
75 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
78 * Returns: the next stream, or %NULL if there are no more.
81 glk_stream_iterate(strid_t str, glui32 *rockptr)
83 VALID_STREAM_OR_NULL(str, return NULL);
85 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
89 retnode = glk_data->stream_list;
91 retnode = str->stream_list->next;
92 strid_t retval = retnode? (strid_t)retnode->data : NULL;
94 /* Store the stream's rock in rockptr */
96 *rockptr = glk_stream_get_rock(retval);
102 * glk_stream_get_rock:
105 * Retrieves the stream @str's rock value. See <link
106 * linkend="chimara-Rocks">Rocks</link>. Window streams always have rock 0; all
107 * other streams return whatever rock you created them with.
109 * Returns: A rock value.
112 glk_stream_get_rock(strid_t str)
114 VALID_STREAM(str, return 0);
119 * glk_stream_set_current:
120 * @str: An output stream, or %NULL.
122 * Sets the current stream to @str, which must be an output stream. You may set
123 * the current stream to %NULL, which means the current stream is not set to
127 glk_stream_set_current(strid_t str)
129 VALID_STREAM_OR_NULL(str, return);
131 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
133 if(str != NULL && str->file_mode == filemode_Read)
135 ILLEGAL("Cannot set current stream to non output stream");
139 glk_data->current_stream = str;
143 * glk_stream_get_current:
145 * Returns the current stream, or %NULL if there is none.
147 * Returns: A stream, or %NULL.
150 glk_stream_get_current()
152 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
153 return glk_data->current_stream;
158 * @ch: A character in Latin-1 encoding.
160 * Prints one character to the current stream. As with all basic functions, the
161 * character is assumed to be in the Latin-1 character encoding. See <link
162 * linkend="chimara-Character-Encoding">Character Encoding</link>.
165 glk_put_char(unsigned char ch)
167 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
168 VALID_STREAM(glk_data->current_stream, return);
169 glk_put_char_stream(glk_data->current_stream, ch);
174 * @ch: A Unicode code point.
176 * Prints one character to the current stream. The character is assumed to be a
177 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
181 glk_put_char_uni(glui32 ch)
183 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
184 VALID_STREAM(glk_data->current_stream, return);
185 glk_put_char_stream_uni(glk_data->current_stream, ch);
190 * @s: A null-terminated string in Latin-1 encoding.
192 * Prints a null-terminated string to the current stream. It is exactly
195 * for (ptr = s; *ptr; ptr++)
196 * glk_put_char(*ptr);
198 * However, it may be more efficient.
201 glk_put_string(char *s)
203 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
204 VALID_STREAM(glk_data->current_stream, return);
205 glk_put_string_stream(glk_data->current_stream, s);
209 * glk_put_string_uni:
210 * @s: A zero-terminated string of Unicode code points.
212 * Prints a string of Unicode characters to the current stream. It is equivalent
213 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
217 glk_put_string_uni(glui32 *s)
219 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
220 VALID_STREAM(glk_data->current_stream, return);
221 glk_put_string_stream_uni(glk_data->current_stream, s);
226 * @buf: An array of characters in Latin-1 encoding.
227 * @len: Length of @buf.
229 * Prints a block of characters to the current stream. It is exactly equivalent
232 * for (i = 0; i < len; i++)
233 * glk_put_char(buf[i]);
235 * However, it may be more efficient.
238 glk_put_buffer(char *buf, glui32 len)
240 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
241 VALID_STREAM(glk_data->current_stream, return);
242 glk_put_buffer_stream(glk_data->current_stream, buf, len);
246 * glk_put_buffer_uni:
247 * @buf: An array of Unicode code points.
248 * @len: Length of @buf.
250 * Prints a block of Unicode characters to the current stream. It is equivalent
251 * to a series of glk_put_char_uni() calls.
254 glk_put_buffer_uni(glui32 *buf, glui32 len)
256 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
257 VALID_STREAM(glk_data->current_stream, return);
258 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
262 * glk_stream_open_memory:
263 * @buf: An allocated buffer, or %NULL.
264 * @buflen: Length of @buf.
265 * @fmode: Mode in which the buffer will be opened. Must be one of
266 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
267 * @rock: The new stream's rock value.
269 * Opens a stream which reads from or writes to a space in memory. @buf points
270 * to the buffer where output will be read from or written to. @buflen is the
271 * length of the buffer.
273 * Unicode values (characters greater than 255) cannot be written to the buffer.
274 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
276 * Returns: the new stream, or %NULL on error.
279 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
281 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
283 strid_t str = stream_new_common(rock);
284 str->file_mode = fmode;
285 str->type = STREAM_TYPE_MEMORY;
288 str->unicode = FALSE;
292 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
294 str->buflen = buflen;
295 if(glk_data->register_arr)
296 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Cn");
303 * glk_stream_open_memory_uni:
304 * @buf: An allocated buffer, or %NULL.
305 * @buflen: Length of @buf.
306 * @fmode: Mode in which the buffer will be opened. Must be one of
307 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
308 * @rock: The new stream's rock value.
310 * Works just like glk_stream_open_memory(), except that the buffer is an array
311 * of 32-bit words, instead of bytes. This allows you to write and read any
312 * Unicode character. The @buflen is the number of words, not the number of
315 * Returns: the new stream, or %NULL on error.
318 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
320 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
322 strid_t str = stream_new_common(rock);
323 str->file_mode = fmode;
324 str->type = STREAM_TYPE_MEMORY;
331 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
333 str->buflen = buflen;
334 if(glk_data->register_arr)
335 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Iu");
341 /* Internal function: create a stream using the given parameters. */
343 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
345 VALID_FILEREF(fileref, return NULL);
347 const gchar *modestr;
348 /* Binary mode is 0x000, text mode 0x100 */
349 gboolean binary = !(fileref->usage & fileusage_TextMode);
353 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
354 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
357 modestr = binary? "rb" : "r";
360 modestr = binary? "wb" : "w";
362 case filemode_WriteAppend:
363 case filemode_ReadWrite:
365 /* We have to open the file first and then close it, in order to
366 both make sure it exists and be able to seek in it later */
367 FILE *fp = g_fopen(fileref->filename, binary? "ab" : "a");
368 if(fclose(fp) != 0) {
369 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
372 modestr = binary? "r+b" : "r+";
376 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
380 FILE *fp = g_fopen(fileref->filename, modestr);
382 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
386 /* Fast-forward to the end if we are appending */
387 if(fmode == filemode_WriteAppend && fseek(fp, 0, SEEK_END) != 0) {
388 IO_WARNING("Error fast-forwarding file to end", fileref->filename, g_strerror(errno));
392 /* If they opened a file in write mode but didn't specifically get
393 permission to do so, complain if the file already exists */
394 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
397 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
398 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
399 "File '%s' already exists. Overwrite?", fileref->filename);
400 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
401 gtk_widget_destroy(dialog);
405 if(response != GTK_RESPONSE_YES) {
407 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
412 strid_t str = stream_new_common(rock);
413 str->file_mode = fmode;
414 str->type = STREAM_TYPE_FILE;
415 str->file_pointer = fp;
416 str->binary = binary;
417 str->unicode = unicode;
418 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
419 if(str->filename == NULL)
420 str->filename = g_strdup("Unknown file name"); /* fail silently */
426 * glk_stream_open_file:
427 * @fileref: Indicates the file which will be opened.
428 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
429 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
430 * @rock: The new stream's rock value.
432 * Opens a stream which reads to or writes from a disk file. If @fmode is
433 * %filemode_Read, the file must already exist; for the other modes, an empty
434 * file is created if none exists. If @fmode is %filemode_Write, and the file
435 * already exists, it is truncated down to zero length (an empty file); the
436 * other modes do not truncate. If @fmode is %filemode_WriteAppend, the file
437 * mark is set to the end of the file.
440 * Note, again, that this doesn't match stdio's fopen() call very well. See
441 * <link linkend="filemode-WriteAppend">the file mode constants</link>.
444 * If the filemode requires the file to exist, but the file does not exist,
445 * glk_stream_open_file() returns %NULL.
447 * The file may be read or written in text or binary mode; this is determined
448 * by the @fileref argument. Similarly, platform-dependent attributes such as
449 * file type are determined by @fileref. See <link
450 * linkend="chimara-File-References">File References</link>.
452 * When writing in binary mode, Unicode values (characters greater than 255)
453 * cannot be written to the file. If you try, they will be stored as 0x3F
454 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
455 * exactly, approximated, or abbreviated, depending on what the platform's text
458 * Returns: A new stream, or %NULL if the file operation failed.
461 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
463 return file_stream_new(fileref, fmode, rock, FALSE);
467 * glk_stream_open_file_uni:
468 * @fileref: Indicates the file which will be opened.
469 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
470 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
471 * @rock: The new stream's rock value.
473 * This works just like glk_stream_open_file(), except that in binary mode,
474 * characters are written and read as four-byte (big-endian) values. This
475 * allows you to write any Unicode character.
477 * In text mode, the file is written and read in a platform-dependent way, which
478 * may or may not handle all Unicode characters. A text-mode file created with
479 * glk_stream_open_file_uni() may have the same format as a text-mode file
480 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
483 * Returns: A new stream, or %NULL if the file operation failed.
486 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
488 return file_stream_new(fileref, fmode, rock, TRUE);
492 * glk_stream_open_resource:
493 * @filenum: Resource chunk number to open.
494 * @rock: The new stream's rock value.
496 * Open the given data resource for reading (only), as a normal stream.
499 * Note that there is no notion of file usage — the resource does not
500 * have to be specified as <quote>saved game</quote> or whatever.
503 * If no resource chunk of the given number exists, the open function returns
506 * As with file streams, a binary resource stream reads the resource as bytes. A
507 * text resource stream reads characters encoded as Latin-1.
509 * When reading from a resource stream, newlines are not remapped, even if they
510 * normally would be when reading from a text file on the host OS. If you read a
511 * line (glk_get_line_stream() or glk_get_line_stream_uni()), a Unix newline
512 * (0x0A) terminates the line.
514 * Returns: the new stream, or %NULL.
517 glk_stream_open_resource(glui32 filenum, glui32 rock)
519 g_warning("Not implemented");
524 * glk_stream_open_resource_uni:
525 * @filenum: Resource chunk number to open.
526 * @rock: The new stream's rock value.
528 * Open the given data resource for reading (only), as a Unicode stream. See
529 * glk_stream_open_resource() for more information.
531 * As with file streams, a binary resource stream reads the resource as
532 * four-byte (big-endian) words. A text resource stream reads characters encoded
536 * Thus, the difference between text and binary resources is only important
537 * when opened as a Unicode stream.
540 * Returns: the new stream, or %NULL.
543 glk_stream_open_resource_uni(glui32 filenum, glui32 rock)
545 g_warning("Not implemented");
551 * @str: Stream to close.
552 * @result: Pointer to a #stream_result_t, or %NULL.
554 * Closes the stream @str. The @result argument points to a structure which is
555 * filled in with the final character counts of the stream. If you do not care
556 * about these, you may pass %NULL as the @result argument.
558 * If @str is the current output stream, the current output stream is set to
561 * You cannot close window streams; use glk_window_close() instead. See <link
562 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
563 * Closing, and Constraints</link>.
566 glk_stream_close(strid_t str, stream_result_t *result)
568 VALID_STREAM(str, return);
570 /* Free resources associated with one specific type of stream */
573 case STREAM_TYPE_WINDOW:
574 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
577 case STREAM_TYPE_MEMORY:
579 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
580 if(glk_data->unregister_arr)
583 (*glk_data->unregister_arr)(str->ubuffer, str->buflen, "&+#!Iu", str->buffer_rock);
585 (*glk_data->unregister_arr)(str->buffer, str->buflen, "&+#!Cn", str->buffer_rock);
590 case STREAM_TYPE_FILE:
591 if(fclose(str->file_pointer) != 0)
592 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
593 g_free(str->filename);
596 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
600 stream_close_common(str, result);