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>.
108 * Returns: A rock value.
111 glk_stream_get_rock(strid_t str)
113 VALID_STREAM(str, return 0);
118 * glk_stream_set_current:
119 * @str: An output stream, or %NULL.
121 * Sets the current stream to @str, which must be an output stream. You may set
122 * the current stream to %NULL, which means the current stream is not set to
126 glk_stream_set_current(strid_t str)
128 VALID_STREAM_OR_NULL(str, return);
130 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
132 if(str != NULL && str->file_mode == filemode_Read)
134 ILLEGAL("Cannot set current stream to non output stream");
138 glk_data->current_stream = str;
142 * glk_stream_get_current:
144 * Returns the current stream, or %NULL if there is none.
146 * Returns: A stream, or %NULL.
149 glk_stream_get_current()
151 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
152 return glk_data->current_stream;
157 * @ch: A character in Latin-1 encoding.
159 * Prints one character to the current stream. As with all basic functions, the
160 * character is assumed to be in the Latin-1 character encoding. See <link
161 * linkend="chimara-Character-Encoding">Character Encoding</link>.
164 glk_put_char(unsigned char ch)
166 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
167 VALID_STREAM(glk_data->current_stream, return);
168 glk_put_char_stream(glk_data->current_stream, ch);
173 * @ch: A Unicode code point.
175 * Prints one character to the current stream. The character is assumed to be a
176 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
180 glk_put_char_uni(glui32 ch)
182 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
183 VALID_STREAM(glk_data->current_stream, return);
184 glk_put_char_stream_uni(glk_data->current_stream, ch);
189 * @s: A null-terminated string in Latin-1 encoding.
191 * Prints a null-terminated string to the current stream. It is exactly
194 * for (ptr = s; *ptr; ptr++)
195 * glk_put_char(*ptr);
197 * However, it may be more efficient.
200 glk_put_string(char *s)
202 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
203 VALID_STREAM(glk_data->current_stream, return);
204 glk_put_string_stream(glk_data->current_stream, s);
208 * glk_put_string_uni:
209 * @s: A zero-terminated string of Unicode code points.
211 * Prints a string of Unicode characters to the current stream. It is equivalent
212 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
216 glk_put_string_uni(glui32 *s)
218 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
219 VALID_STREAM(glk_data->current_stream, return);
220 glk_put_string_stream_uni(glk_data->current_stream, s);
225 * @buf: An array of characters in Latin-1 encoding.
226 * @len: Length of @buf.
228 * Prints a block of characters to the current stream. It is exactly equivalent
231 * for (i = 0; i < len; i++)
232 * glk_put_char(buf[i]);
234 * However, it may be more efficient.
237 glk_put_buffer(char *buf, glui32 len)
239 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
240 VALID_STREAM(glk_data->current_stream, return);
241 glk_put_buffer_stream(glk_data->current_stream, buf, len);
245 * glk_put_buffer_uni:
246 * @buf: An array of Unicode code points.
247 * @len: Length of @buf.
249 * Prints a block of Unicode characters to the current stream. It is equivalent
250 * to a series of glk_put_char_uni() calls.
253 glk_put_buffer_uni(glui32 *buf, glui32 len)
255 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
256 VALID_STREAM(glk_data->current_stream, return);
257 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
261 * glk_stream_open_memory:
262 * @buf: An allocated buffer, or %NULL.
263 * @buflen: Length of @buf.
264 * @fmode: Mode in which the buffer will be opened. Must be one of
265 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
266 * @rock: The new stream's rock value.
268 * Opens a stream which reads from or writes to a space in memory. @buf points
269 * to the buffer where output will be read from or written to. @buflen is the
270 * length of the buffer.
272 * Unicode values (characters greater than 255) cannot be written to the buffer.
273 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
275 * Returns: the new stream, or %NULL on error.
278 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
280 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
282 strid_t str = stream_new_common(rock);
283 str->file_mode = fmode;
284 str->type = STREAM_TYPE_MEMORY;
286 str->unicode = FALSE;
290 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
292 str->buflen = buflen;
293 if(glk_data->register_arr)
294 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Cn");
301 * glk_stream_open_memory_uni:
302 * @buf: An allocated buffer, or %NULL.
303 * @buflen: Length of @buf.
304 * @fmode: Mode in which the buffer will be opened. Must be one of
305 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
306 * @rock: The new stream's rock value.
308 * Works just like glk_stream_open_memory(), except that the buffer is an array
309 * of 32-bit words, instead of bytes. This allows you to write and read any
310 * Unicode character. The @buflen is the number of words, not the number of
313 * Returns: the new stream, or %NULL on error.
316 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
318 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
320 strid_t str = stream_new_common(rock);
321 str->file_mode = fmode;
322 str->type = STREAM_TYPE_MEMORY;
328 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
330 str->buflen = buflen;
331 if(glk_data->register_arr)
332 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Iu");
338 /* Internal function: create a stream using the given parameters. */
340 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
342 VALID_FILEREF(fileref, return NULL);
345 /* Binary mode is 0x000, text mode 0x100 */
346 gboolean binary = !(fileref->usage & fileusage_TextMode);
350 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
351 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
354 modestr = g_strdup(binary? "rb" : "r");
357 modestr = g_strdup(binary? "wb" : "w");
359 case filemode_WriteAppend:
360 modestr = g_strdup(binary? "ab" : "a");
362 case filemode_ReadWrite:
363 if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
364 modestr = g_strdup(binary? "r+b" : "r+");
366 modestr = g_strdup(binary? "w+b" : "w+");
370 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
374 FILE *fp = g_fopen(fileref->filename, modestr);
377 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
381 /* If they opened a file in write mode but didn't specifically get
382 permission to do so, complain if the file already exists */
383 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
386 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
387 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
388 "File '%s' already exists. Overwrite?", fileref->filename);
389 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
390 gtk_widget_destroy(dialog);
394 if(response != GTK_RESPONSE_YES) {
396 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
401 strid_t str = stream_new_common(rock);
402 str->file_mode = fmode;
403 str->type = STREAM_TYPE_FILE;
404 str->file_pointer = fp;
405 str->binary = binary;
406 str->unicode = unicode;
407 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
408 if(str->filename == NULL)
409 str->filename = g_strdup("Unknown file name"); /* fail silently */
415 * glk_stream_open_file:
416 * @fileref: Indicates the file which will be opened.
417 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
418 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
419 * @rock: The new stream's rock value.
421 * Opens a stream which reads to or writes from a disk file. If @fmode is
422 * %filemode_Read, the file must already exist; for the other modes, an empty
423 * file is created if none exists. If @fmode is %filemode_Write, and the file
424 * already exists, it is truncated down to zero length (an empty file). If
425 * @fmode is %filemode_WriteAppend, the file mark is set to the end of the
428 * When writing in binary mode, Unicode values (characters greater than 255)
429 * cannot be written to the file. If you try, they will be stored as 0x3F
430 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
431 * exactly, approximated, or abbreviated, depending on what the platform's text
434 * Returns: A new stream, or %NULL if the file operation failed.
437 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
439 return file_stream_new(fileref, fmode, rock, FALSE);
443 * glk_stream_open_file_uni:
444 * @fileref: Indicates the file which will be opened.
445 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
446 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
447 * @rock: The new stream's rock value.
449 * This works just like glk_stream_open_file(), except that in binary mode,
450 * characters are written and read as four-byte (big-endian) values. This
451 * allows you to write any Unicode character.
453 * In text mode, the file is written and read in a platform-dependent way, which
454 * may or may not handle all Unicode characters. A text-mode file created with
455 * glk_stream_open_file_uni() may have the same format as a text-mode file
456 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
459 * Returns: A new stream, or %NULL if the file operation failed.
462 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
464 return file_stream_new(fileref, fmode, rock, TRUE);
469 * @str: Stream to close.
470 * @result: Pointer to a #stream_result_t, or %NULL.
472 * Closes the stream @str. The @result argument points to a structure which is
473 * filled in with the final character counts of the stream. If you do not care
474 * about these, you may pass %NULL as the @result argument.
476 * If @str is the current output stream, the current output stream is set to
479 * You cannot close window streams; use glk_window_close() instead. See <link
480 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
481 * Closing, and Constraints</link>.
484 glk_stream_close(strid_t str, stream_result_t *result)
486 VALID_STREAM(str, return);
488 /* Free resources associated with one specific type of stream */
491 case STREAM_TYPE_WINDOW:
492 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
495 case STREAM_TYPE_MEMORY:
497 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
498 if(glk_data->unregister_arr)
501 (*glk_data->unregister_arr)(str->ubuffer, str->buflen, "&+#!Iu", str->buffer_rock);
503 (*glk_data->unregister_arr)(str->buffer, str->buflen, "&+#!Cn", str->buffer_rock);
508 case STREAM_TYPE_FILE:
509 if(fclose(str->file_pointer) != 0)
510 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
511 g_free(str->filename);
514 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
518 stream_close_common(str, result);