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. */
33 stream_close_common(strid_t str, stream_result_t *result)
35 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
37 /* Remove the stream from the global stream list */
38 glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
40 /* If it was the current output stream, set that to NULL */
41 if(glk_data->current_stream == str)
42 glk_data->current_stream = NULL;
44 /* If it 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 if(glk_data->unregister_obj)
53 (*glk_data->unregister_obj)(str, gidisp_Class_Stream, str->disprock);
54 str->disprock.ptr = NULL;
57 /* Return the character counts */
60 result->readcount = str->read_count;
61 result->writecount = str->write_count;
64 str->magic = MAGIC_FREE;
70 * @str: A stream, or %NULL.
71 * @rockptr: Return location for the next window's rock, or %NULL.
73 * Iterates through all the existing streams. See <link
74 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
77 * Returns: the next stream, or %NULL if there are no more.
80 glk_stream_iterate(strid_t str, glui32 *rockptr)
82 VALID_STREAM_OR_NULL(str, return NULL);
84 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
88 retnode = glk_data->stream_list;
90 retnode = str->stream_list->next;
91 strid_t retval = retnode? (strid_t)retnode->data : NULL;
93 /* Store the stream's rock in rockptr */
95 *rockptr = glk_stream_get_rock(retval);
101 * glk_stream_get_rock:
104 * Retrieves the stream @str's rock value. See <link
105 * linkend="chimara-Rocks">Rocks</link>.
107 * Returns: A rock value.
110 glk_stream_get_rock(strid_t str)
112 VALID_STREAM(str, return 0);
117 * glk_stream_set_current:
118 * @str: An output stream, or %NULL.
120 * Sets the current stream to @str, which must be an output stream. You may set
121 * the current stream to %NULL, which means the current stream is not set to
125 glk_stream_set_current(strid_t str)
127 VALID_STREAM_OR_NULL(str, return);
129 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
131 if(str != NULL && str->file_mode == filemode_Read)
133 ILLEGAL("Cannot set current stream to non output stream");
137 glk_data->current_stream = str;
141 * glk_stream_get_current:
143 * Returns the current stream, or %NULL if there is none.
145 * Returns: A stream, or %NULL.
148 glk_stream_get_current()
150 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
151 return glk_data->current_stream;
156 * @ch: A character in Latin-1 encoding.
158 * Prints one character to the current stream. As with all basic functions, the
159 * character is assumed to be in the Latin-1 character encoding. See <link
160 * linkend="chimara-Character-Encoding">Character Encoding</link>.
163 glk_put_char(unsigned char ch)
165 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
166 VALID_STREAM(glk_data->current_stream, return);
167 glk_put_char_stream(glk_data->current_stream, ch);
172 * @ch: A Unicode code point.
174 * Prints one character to the current stream. The character is assumed to be a
175 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
179 glk_put_char_uni(glui32 ch)
181 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
182 VALID_STREAM(glk_data->current_stream, return);
183 glk_put_char_stream_uni(glk_data->current_stream, ch);
188 * @s: A null-terminated string in Latin-1 encoding.
190 * Prints a null-terminated string to the current stream. It is exactly
193 * for (ptr = s; *ptr; ptr++)
194 * #glk_put_char(*ptr);
196 * However, it may be more efficient.
199 glk_put_string(char *s)
201 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
202 VALID_STREAM(glk_data->current_stream, return);
203 glk_put_string_stream(glk_data->current_stream, s);
207 * glk_put_string_uni:
208 * @s: A zero-terminated string of Unicode code points.
210 * Prints a string of Unicode characters to the current stream. It is equivalent
211 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
215 glk_put_string_uni(glui32 *s)
217 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
218 VALID_STREAM(glk_data->current_stream, return);
219 glk_put_string_stream_uni(glk_data->current_stream, s);
224 * @buf: An array of characters in Latin-1 encoding.
225 * @len: Length of @buf.
227 * Prints a block of characters to the current stream. It is exactly equivalent
230 * for (i = 0; i < len; i++)
231 * #glk_put_char(buf[i]);
233 * However, it may be more efficient.
236 glk_put_buffer(char *buf, glui32 len)
238 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
239 VALID_STREAM(glk_data->current_stream, return);
240 glk_put_buffer_stream(glk_data->current_stream, buf, len);
244 * glk_put_buffer_uni:
245 * @buf: An array of Unicode code points.
246 * @len: Length of @buf.
248 * Prints a block of Unicode characters to the current stream. It is equivalent
249 * to a series of glk_put_char_uni() calls.
252 glk_put_buffer_uni(glui32 *buf, glui32 len)
254 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
255 VALID_STREAM(glk_data->current_stream, return);
256 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
260 * glk_stream_open_memory:
261 * @buf: An allocated buffer, or %NULL.
262 * @buflen: Length of @buf.
263 * @fmode: Mode in which the buffer will be opened. Must be one of
264 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
265 * @rock: The new stream's rock value.
267 * Opens a stream which reads from or writes to a space in memory. @buf points
268 * to the buffer where output will be read from or written to. @buflen is the
269 * length of the buffer.
271 * Unicode values (characters greater than 255) cannot be written to the buffer.
272 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
274 * Returns: the new stream, or %NULL on error.
277 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
279 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
281 strid_t str = stream_new_common(rock);
282 str->file_mode = fmode;
283 str->type = STREAM_TYPE_MEMORY;
285 str->unicode = FALSE;
289 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
291 str->buflen = buflen;
292 if(glk_data->register_arr)
293 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Cn");
300 * glk_stream_open_memory_uni:
301 * @buf: An allocated buffer, or %NULL.
302 * @buflen: Length of @buf.
303 * @fmode: Mode in which the buffer will be opened. Must be one of
304 * %filemode_Read, %filemode_Write, or %filemode_ReadWrite.
305 * @rock: The new stream's rock value.
307 * Works just like glk_stream_open_memory(), except that the buffer is an array
308 * of 32-bit words, instead of bytes. This allows you to write and read any
309 * Unicode character. The @buflen is the number of words, not the number of
312 * Returns: the new stream, or %NULL on error.
315 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
317 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
319 strid_t str = stream_new_common(rock);
320 str->file_mode = fmode;
321 str->type = STREAM_TYPE_MEMORY;
327 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
329 str->buflen = buflen;
330 if(glk_data->register_arr)
331 str->buffer_rock = (*glk_data->register_arr)(buf, buflen, "&+#!Iu");
337 /* Internal function: create a stream using the given parameters. */
339 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
341 VALID_FILEREF(fileref, return NULL);
344 /* Binary mode is 0x000, text mode 0x100 */
345 gboolean binary = !(fileref->usage & fileusage_TextMode);
349 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
350 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
353 modestr = g_strdup(binary? "rb" : "r");
356 modestr = g_strdup(binary? "wb" : "w");
358 case filemode_WriteAppend:
359 modestr = g_strdup(binary? "ab" : "a");
361 case filemode_ReadWrite:
362 if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
363 modestr = g_strdup(binary? "r+b" : "r+");
365 modestr = g_strdup(binary? "w+b" : "w+");
369 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
373 FILE *fp = g_fopen(fileref->filename, modestr);
376 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
380 /* If they opened a file in write mode but didn't specifically get
381 permission to do so, complain if the file already exists */
382 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
385 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
386 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
387 "File '%s' already exists. Overwrite?", fileref->filename);
388 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
389 gtk_widget_destroy(dialog);
393 if(response != GTK_RESPONSE_YES) {
395 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
400 strid_t str = stream_new_common(rock);
401 str->file_mode = fmode;
402 str->type = STREAM_TYPE_FILE;
403 str->file_pointer = fp;
404 str->binary = binary;
405 str->unicode = unicode;
406 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
407 if(str->filename == NULL)
408 str->filename = g_strdup("Unknown file name"); /* fail silently */
414 * glk_stream_open_file:
415 * @fileref: Indicates the file which will be opened.
416 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
417 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
418 * @rock: The new stream's rock value.
420 * Opens a stream which reads to or writes from a disk file. If @fmode is
421 * %filemode_Read, the file must already exist; for the other modes, an empty
422 * file is created if none exists. If @fmode is %filemode_Write, and the file
423 * already exists, it is truncated down to zero length (an empty file). If
424 * @fmode is %filemode_WriteAppend, the file mark is set to the end of the
427 * When writing in binary mode, Unicode values (characters greater than 255)
428 * cannot be written to the file. If you try, they will be stored as 0x3F
429 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
430 * exactly, approximated, or abbreviated, depending on what the platform's text
433 * Returns: A new stream, or %NULL if the file operation failed.
436 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
438 return file_stream_new(fileref, fmode, rock, FALSE);
442 * glk_stream_open_file_uni:
443 * @fileref: Indicates the file which will be opened.
444 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
445 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
446 * @rock: The new stream's rock value.
448 * This works just like glk_stream_open_file(), except that in binary mode,
449 * characters are written and read as four-byte (big-endian) values. This
450 * allows you to write any Unicode character.
452 * In text mode, the file is written and read in a platform-dependent way, which
453 * may or may not handle all Unicode characters. A text-mode file created with
454 * glk_stream_open_file_uni() may have the same format as a text-mode file
455 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
458 * Returns: A new stream, or %NULL if the file operation failed.
461 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
463 return file_stream_new(fileref, fmode, rock, TRUE);
468 * @str: Stream to close.
469 * @result: Pointer to a #stream_result_t, or %NULL.
471 * Closes the stream @str. The @result argument points to a structure which is
472 * filled in with the final character counts of the stream. If you do not care
473 * about these, you may pass %NULL as the @result argument.
475 * If @str is the current output stream, the current output stream is set to
478 * You cannot close window streams; use glk_window_close() instead. See <link
479 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
480 * Closing, and Constraints</link>.
483 glk_stream_close(strid_t str, stream_result_t *result)
485 VALID_STREAM(str, return);
487 /* Free resources associated with one specific type of stream */
490 case STREAM_TYPE_WINDOW:
491 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
494 case STREAM_TYPE_MEMORY:
496 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
497 if(glk_data->unregister_arr)
500 (*glk_data->unregister_arr)(str->ubuffer, str->buflen, "&+#!Iu", str->buffer_rock);
502 (*glk_data->unregister_arr)(str->buffer, str->buflen, "&+#!Cn", str->buffer_rock);
507 case STREAM_TYPE_FILE:
508 if(fclose(str->file_pointer) != 0)
509 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
510 g_free(str->filename);
513 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
517 stream_close_common(str, result);