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: stream closing stuff that is safe to call from either the
32 main thread or the Glk thread. */
34 trash_stream_thread_independent(ChimaraGlkPrivate *glk_data, strid_t str)
36 /* Remove the stream from the global stream list */
37 glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
39 /* If it was the current output stream, set that to NULL */
40 if(glk_data->current_stream == str)
41 glk_data->current_stream = NULL;
43 str->magic = MAGIC_FREE;
47 /* Internal function: Stuff to do upon closing any type of stream. Call only
50 stream_close_common(strid_t str, stream_result_t *result)
52 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
54 if(glk_data->unregister_obj)
56 (*glk_data->unregister_obj)(str, gidisp_Class_Stream, str->disprock);
57 str->disprock.ptr = NULL;
60 /* If the stream was one or more windows' echo streams, set those to NULL */
62 for(win = glk_window_iterate(NULL, NULL); win;
63 win = glk_window_iterate(win, NULL))
64 if(win->echo_stream == str)
65 win->echo_stream = NULL;
67 /* Return the character counts */
70 result->readcount = str->read_count;
71 result->writecount = str->write_count;
74 trash_stream_thread_independent(glk_data, str);
79 * @str: A stream, or %NULL.
80 * @rockptr: Return location for the next window's rock, or %NULL.
82 * Iterates through all the existing streams. See <link
83 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
86 * Returns: the next stream, or %NULL if there are no more.
89 glk_stream_iterate(strid_t str, glui32 *rockptr)
91 VALID_STREAM_OR_NULL(str, return NULL);
93 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
97 retnode = glk_data->stream_list;
99 retnode = str->stream_list->next;
100 strid_t retval = retnode? (strid_t)retnode->data : NULL;
102 /* Store the stream's rock in rockptr */
103 if(retval && rockptr)
104 *rockptr = glk_stream_get_rock(retval);
110 * glk_stream_get_rock:
113 * Retrieves the stream @str's rock value. See <link
114 * linkend="chimara-Rocks">Rocks</link>.
116 * Returns: A rock value.
119 glk_stream_get_rock(strid_t str)
121 VALID_STREAM(str, return 0);
126 * glk_stream_set_current:
127 * @str: An output stream, or %NULL.
129 * Sets the current stream to @str, which must be an output stream. You may set
130 * the current stream to %NULL, which means the current stream is not set to
134 glk_stream_set_current(strid_t str)
136 VALID_STREAM_OR_NULL(str, return);
138 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
140 if(str != NULL && str->file_mode == filemode_Read)
142 ILLEGAL("Cannot set current stream to non output stream");
146 glk_data->current_stream = str;
150 * glk_stream_get_current:
152 * Returns the current stream, or %NULL if there is none.
154 * Returns: A stream, or %NULL.
157 glk_stream_get_current()
159 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
160 return glk_data->current_stream;
165 * @ch: A character in Latin-1 encoding.
167 * Prints one character to the current stream. As with all basic functions, the
168 * character is assumed to be in the Latin-1 character encoding. See <link
169 * linkend="chimara-Character-Encoding">Character Encoding</link>.
172 glk_put_char(unsigned char ch)
174 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
175 VALID_STREAM(glk_data->current_stream, return);
176 glk_put_char_stream(glk_data->current_stream, ch);
181 * @ch: A Unicode code point.
183 * Prints one character to the current stream. The character is assumed to be a
184 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
188 glk_put_char_uni(glui32 ch)
190 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
191 VALID_STREAM(glk_data->current_stream, return);
192 glk_put_char_stream_uni(glk_data->current_stream, ch);
197 * @s: A null-terminated string in Latin-1 encoding.
199 * Prints a null-terminated string to the current stream. It is exactly
202 * for (ptr = s; *ptr; ptr++)
203 * #glk_put_char(*ptr);
205 * However, it may be more efficient.
208 glk_put_string(char *s)
210 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
211 VALID_STREAM(glk_data->current_stream, return);
212 glk_put_string_stream(glk_data->current_stream, s);
216 * glk_put_string_uni:
217 * @s: A zero-terminated string of Unicode code points.
219 * Prints a string of Unicode characters to the current stream. It is equivalent
220 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
224 glk_put_string_uni(glui32 *s)
226 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
227 VALID_STREAM(glk_data->current_stream, return);
228 glk_put_string_stream_uni(glk_data->current_stream, s);
233 * @buf: An array of characters in Latin-1 encoding.
234 * @len: Length of @buf.
236 * Prints a block of characters to the current stream. It is exactly equivalent
239 * for (i = 0; i < len; i++)
240 * #glk_put_char(buf[i]);
242 * However, it may be more efficient.
245 glk_put_buffer(char *buf, glui32 len)
247 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
248 VALID_STREAM(glk_data->current_stream, return);
249 glk_put_buffer_stream(glk_data->current_stream, buf, len);
253 * glk_put_buffer_uni:
254 * @buf: An array of Unicode code points.
255 * @len: Length of @buf.
257 * Prints a block of Unicode characters to the current stream. It is equivalent
258 * to a series of glk_put_char_uni() calls.
261 glk_put_buffer_uni(glui32 *buf, glui32 len)
263 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
264 VALID_STREAM(glk_data->current_stream, return);
265 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
269 * glk_stream_open_memory:
270 * @buf: An allocated buffer, or %NULL.
271 * @buflen: Length of @buf.
272 * @fmode: Mode in which the buffer will be opened. Must be one of
273 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
274 * @rock: The new stream's rock value.
276 * Opens a stream which reads from or writes to a space in memory. @buf points
277 * to the buffer where output will be read from or written to. @buflen is the
278 * length of the buffer.
280 * Unicode values (characters greater than 255) cannot be written to the buffer.
281 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
283 * Returns: the new stream, or %NULL on error.
286 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
288 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
290 strid_t str = stream_new_common(rock);
291 str->file_mode = fmode;
292 str->type = STREAM_TYPE_MEMORY;
294 str->unicode = FALSE;
298 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
300 str->buflen = buflen;
301 if(glk_data->register_arr)
302 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Cn");
309 * glk_stream_open_memory_uni:
310 * @buf: An allocated buffer, or %NULL.
311 * @buflen: Length of @buf.
312 * @fmode: Mode in which the buffer will be opened. Must be one of
313 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
314 * @rock: The new stream's rock value.
316 * Works just like glk_stream_open_memory(), except that the buffer is an array
317 * of 32-bit words, instead of bytes. This allows you to write and read any
318 * Unicode character. The @buflen is the number of words, not the number of
321 * Returns: the new stream, or %NULL on error.
324 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
326 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
328 strid_t str = stream_new_common(rock);
329 str->file_mode = fmode;
330 str->type = STREAM_TYPE_MEMORY;
336 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
338 str->buflen = buflen;
339 if(glk_data->register_arr)
340 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Iu");
346 /* Internal function: create a stream using the given parameters. */
348 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
350 VALID_FILEREF(fileref, return NULL);
353 /* Binary mode is 0x000, text mode 0x100 */
354 gboolean binary = !(fileref->usage & fileusage_TextMode);
358 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
359 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
362 modestr = g_strdup(binary? "rb" : "r");
365 modestr = g_strdup(binary? "wb" : "w");
367 case filemode_WriteAppend:
368 modestr = g_strdup(binary? "ab" : "a");
370 case filemode_ReadWrite:
371 if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
372 modestr = g_strdup(binary? "r+b" : "r+");
374 modestr = g_strdup(binary? "w+b" : "w+");
378 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
382 FILE *fp = g_fopen(fileref->filename, modestr);
385 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
389 /* If they opened a file in write mode but didn't specifically get
390 permission to do so, complain if the file already exists */
391 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
394 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
395 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
396 "File '%s' already exists. Overwrite?", fileref->filename);
397 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
398 gtk_widget_destroy(dialog);
402 if(response != GTK_RESPONSE_YES) {
404 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
409 strid_t str = stream_new_common(rock);
410 str->file_mode = fmode;
411 str->type = STREAM_TYPE_FILE;
412 str->file_pointer = fp;
413 str->binary = binary;
414 str->unicode = unicode;
415 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
416 if(str->filename == NULL)
417 str->filename = g_strdup("Unknown file name"); /* fail silently */
423 * glk_stream_open_file:
424 * @fileref: Indicates the file which will be opened.
425 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
426 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
427 * @rock: The new stream's rock value.
429 * Opens a stream which reads to or writes from a disk file. If @fmode is
430 * %filemode_Read, the file must already exist; for the other modes, an empty
431 * file is created if none exists. If @fmode is %filemode_Write, and the file
432 * already exists, it is truncated down to zero length (an empty file). If
433 * @fmode is %filemode_WriteAppend, the file mark is set to the end of the
436 * When writing in binary mode, Unicode values (characters greater than 255)
437 * cannot be written to the file. If you try, they will be stored as 0x3F
438 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
439 * exactly, approximated, or abbreviated, depending on what the platform's text
442 * Returns: A new stream, or %NULL if the file operation failed.
445 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
447 return file_stream_new(fileref, fmode, rock, FALSE);
451 * glk_stream_open_file_uni:
452 * @fileref: Indicates the file which will be opened.
453 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
454 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
455 * @rock: The new stream's rock value.
457 * This works just like glk_stream_open_file(), except that in binary mode,
458 * characters are written and read as four-byte (big-endian) values. This
459 * allows you to write any Unicode character.
461 * In text mode, the file is written and read in a platform-dependent way, which
462 * may or may not handle all Unicode characters. A text-mode file created with
463 * glk_stream_open_file_uni() may have the same format as a text-mode file
464 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
467 * Returns: A new stream, or %NULL if the file operation failed.
470 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
472 return file_stream_new(fileref, fmode, rock, TRUE);
477 * @str: Stream to close.
478 * @result: Pointer to a #stream_result_t, or %NULL.
480 * Closes the stream @str. The @result argument points to a structure which is
481 * filled in with the final character counts of the stream. If you do not care
482 * about these, you may pass %NULL as the @result argument.
484 * If @str is the current output stream, the current output stream is set to
487 * You cannot close window streams; use glk_window_close() instead. See <link
488 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
489 * Closing, and Constraints</link>.
492 glk_stream_close(strid_t str, stream_result_t *result)
494 VALID_STREAM(str, return);
496 /* Free resources associated with one specific type of stream */
499 case STREAM_TYPE_WINDOW:
500 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
503 case STREAM_TYPE_MEMORY:
505 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
506 if(glk_data->unregister_arr)
509 (*glk_data->unregister_arr)(str->ubuffer, str->buflen, "&+#!Iu", str->buffer_rock);
511 (*glk_data->unregister_arr)(str->buffer, str->buflen, "&+#!Cn", str->buffer_rock);
516 case STREAM_TYPE_FILE:
517 if(fclose(str->file_pointer) != 0)
518 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
519 g_free(str->filename);
522 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
526 stream_close_common(str, result);