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;
287 str->unicode = FALSE;
291 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
293 str->buflen = buflen;
294 if(glk_data->register_arr)
295 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Cn");
302 * glk_stream_open_memory_uni:
303 * @buf: An allocated buffer, or %NULL.
304 * @buflen: Length of @buf.
305 * @fmode: Mode in which the buffer will be opened. Must be one of
306 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
307 * @rock: The new stream's rock value.
309 * Works just like glk_stream_open_memory(), except that the buffer is an array
310 * of 32-bit words, instead of bytes. This allows you to write and read any
311 * Unicode character. The @buflen is the number of words, not the number of
314 * Returns: the new stream, or %NULL on error.
317 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
319 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
321 strid_t str = stream_new_common(rock);
322 str->file_mode = fmode;
323 str->type = STREAM_TYPE_MEMORY;
329 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
331 str->buflen = buflen;
332 if(glk_data->register_arr)
333 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Iu");
339 /* Internal function: create a stream using the given parameters. */
341 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
343 VALID_FILEREF(fileref, return NULL);
346 /* Binary mode is 0x000, text mode 0x100 */
347 gboolean binary = !(fileref->usage & fileusage_TextMode);
351 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
352 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
355 modestr = g_strdup(binary? "rb" : "r");
358 modestr = g_strdup(binary? "wb" : "w");
360 case filemode_WriteAppend:
361 case filemode_ReadWrite:
363 /* We have to open the file first and then close it, in order to
364 both make sure it exists and be able to seek in it later */
365 FILE *fp = g_fopen(fileref->filename, binary? "ab" : "a");
366 if(fclose(fp) != 0) {
367 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
370 modestr = g_strdup(binary? "r+b" : "r+");
374 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
378 FILE *fp = g_fopen(fileref->filename, modestr);
381 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
385 /* Fast-forward to the end if we are appending */
386 if(fmode == filemode_WriteAppend && fseek(fp, 0, SEEK_END) != 0) {
387 IO_WARNING("Error fast-forwarding file to end", fileref->filename, g_strerror(errno));
391 /* If they opened a file in write mode but didn't specifically get
392 permission to do so, complain if the file already exists */
393 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
396 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
397 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
398 "File '%s' already exists. Overwrite?", fileref->filename);
399 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
400 gtk_widget_destroy(dialog);
404 if(response != GTK_RESPONSE_YES) {
406 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
411 strid_t str = stream_new_common(rock);
412 str->file_mode = fmode;
413 str->type = STREAM_TYPE_FILE;
414 str->file_pointer = fp;
415 str->binary = binary;
416 str->unicode = unicode;
417 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
418 if(str->filename == NULL)
419 str->filename = g_strdup("Unknown file name"); /* fail silently */
425 * glk_stream_open_file:
426 * @fileref: Indicates the file which will be opened.
427 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
428 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
429 * @rock: The new stream's rock value.
431 * Opens a stream which reads to or writes from a disk file. If @fmode is
432 * %filemode_Read, the file must already exist; for the other modes, an empty
433 * file is created if none exists. If @fmode is %filemode_Write, and the file
434 * already exists, it is truncated down to zero length (an empty file); the
435 * other modes do not truncate. If @fmode is %filemode_WriteAppend, the file
436 * mark is set to the end of the file.
439 * Note, again, that this doesn't match stdio's fopen() call very well. See
440 * <link linkend="filemode-WriteAppend">the file mode constants</link>.
443 * If the filemode requires the file to exist, but the file does not exist,
444 * glk_stream_open_file() returns %NULL.
446 * The file may be read or written in text or binary mode; this is determined
447 * by the @fileref argument. Similarly, platform-dependent attributes such as
448 * file type are determined by @fileref. See <link
449 * linkend="chimara-File-References">File References</link>.
451 * When writing in binary mode, Unicode values (characters greater than 255)
452 * cannot be written to the file. If you try, they will be stored as 0x3F
453 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
454 * exactly, approximated, or abbreviated, depending on what the platform's text
457 * Returns: A new stream, or %NULL if the file operation failed.
460 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
462 return file_stream_new(fileref, fmode, rock, FALSE);
466 * glk_stream_open_file_uni:
467 * @fileref: Indicates the file which will be opened.
468 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
469 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
470 * @rock: The new stream's rock value.
472 * This works just like glk_stream_open_file(), except that in binary mode,
473 * characters are written and read as four-byte (big-endian) values. This
474 * allows you to write any Unicode character.
476 * In text mode, the file is written and read in a platform-dependent way, which
477 * may or may not handle all Unicode characters. A text-mode file created with
478 * glk_stream_open_file_uni() may have the same format as a text-mode file
479 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
482 * Returns: A new stream, or %NULL if the file operation failed.
485 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
487 return file_stream_new(fileref, fmode, rock, TRUE);
492 * @str: Stream to close.
493 * @result: Pointer to a #stream_result_t, or %NULL.
495 * Closes the stream @str. The @result argument points to a structure which is
496 * filled in with the final character counts of the stream. If you do not care
497 * about these, you may pass %NULL as the @result argument.
499 * If @str is the current output stream, the current output stream is set to
502 * You cannot close window streams; use glk_window_close() instead. See <link
503 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
504 * Closing, and Constraints</link>.
507 glk_stream_close(strid_t str, stream_result_t *result)
509 VALID_STREAM(str, return);
511 /* Free resources associated with one specific type of stream */
514 case STREAM_TYPE_WINDOW:
515 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
518 case STREAM_TYPE_MEMORY:
520 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
521 if(glk_data->unregister_arr)
524 (*glk_data->unregister_arr)(str->ubuffer, str->buflen, "&+#!Iu", str->buffer_rock);
526 (*glk_data->unregister_arr)(str->buffer, str->buflen, "&+#!Cn", str->buffer_rock);
531 case STREAM_TYPE_FILE:
532 if(fclose(str->file_pointer) != 0)
533 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
534 g_free(str->filename);
537 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
541 stream_close_common(str, result);