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);
345 const gchar *modestr;
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 = binary? "rb" : "r";
358 modestr = 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 = binary? "r+b" : "r+";
374 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
378 FILE *fp = g_fopen(fileref->filename, modestr);
380 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
384 /* Fast-forward to the end if we are appending */
385 if(fmode == filemode_WriteAppend && fseek(fp, 0, SEEK_END) != 0) {
386 IO_WARNING("Error fast-forwarding file to end", fileref->filename, g_strerror(errno));
390 /* If they opened a file in write mode but didn't specifically get
391 permission to do so, complain if the file already exists */
392 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
395 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
396 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
397 "File '%s' already exists. Overwrite?", fileref->filename);
398 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
399 gtk_widget_destroy(dialog);
403 if(response != GTK_RESPONSE_YES) {
405 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
410 strid_t str = stream_new_common(rock);
411 str->file_mode = fmode;
412 str->type = STREAM_TYPE_FILE;
413 str->file_pointer = fp;
414 str->binary = binary;
415 str->unicode = unicode;
416 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
417 if(str->filename == NULL)
418 str->filename = g_strdup("Unknown file name"); /* fail silently */
424 * glk_stream_open_file:
425 * @fileref: Indicates the file which will be opened.
426 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
427 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
428 * @rock: The new stream's rock value.
430 * Opens a stream which reads to or writes from a disk file. If @fmode is
431 * %filemode_Read, the file must already exist; for the other modes, an empty
432 * file is created if none exists. If @fmode is %filemode_Write, and the file
433 * already exists, it is truncated down to zero length (an empty file); the
434 * other modes do not truncate. If @fmode is %filemode_WriteAppend, the file
435 * mark is set to the end of the file.
438 * Note, again, that this doesn't match stdio's fopen() call very well. See
439 * <link linkend="filemode-WriteAppend">the file mode constants</link>.
442 * If the filemode requires the file to exist, but the file does not exist,
443 * glk_stream_open_file() returns %NULL.
445 * The file may be read or written in text or binary mode; this is determined
446 * by the @fileref argument. Similarly, platform-dependent attributes such as
447 * file type are determined by @fileref. See <link
448 * linkend="chimara-File-References">File References</link>.
450 * When writing in binary mode, Unicode values (characters greater than 255)
451 * cannot be written to the file. If you try, they will be stored as 0x3F
452 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
453 * exactly, approximated, or abbreviated, depending on what the platform's text
456 * Returns: A new stream, or %NULL if the file operation failed.
459 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
461 return file_stream_new(fileref, fmode, rock, FALSE);
465 * glk_stream_open_file_uni:
466 * @fileref: Indicates the file which will be opened.
467 * @fmode: Mode in which the file will be opened. Can be any of %filemode_Read,
468 * %filemode_Write, %filemode_WriteAppend, or %filemode_ReadWrite.
469 * @rock: The new stream's rock value.
471 * This works just like glk_stream_open_file(), except that in binary mode,
472 * characters are written and read as four-byte (big-endian) values. This
473 * allows you to write any Unicode character.
475 * In text mode, the file is written and read in a platform-dependent way, which
476 * may or may not handle all Unicode characters. A text-mode file created with
477 * glk_stream_open_file_uni() may have the same format as a text-mode file
478 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
481 * Returns: A new stream, or %NULL if the file operation failed.
484 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
486 return file_stream_new(fileref, fmode, rock, TRUE);
491 * @str: Stream to close.
492 * @result: Pointer to a #stream_result_t, or %NULL.
494 * Closes the stream @str. The @result argument points to a structure which is
495 * filled in with the final character counts of the stream. If you do not care
496 * about these, you may pass %NULL as the @result argument.
498 * If @str is the current output stream, the current output stream is set to
501 * You cannot close window streams; use glk_window_close() instead. See <link
502 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
503 * Closing, and Constraints</link>.
506 glk_stream_close(strid_t str, stream_result_t *result)
508 VALID_STREAM(str, return);
510 /* Free resources associated with one specific type of stream */
513 case STREAM_TYPE_WINDOW:
514 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
517 case STREAM_TYPE_MEMORY:
519 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
520 if(glk_data->unregister_arr)
523 (*glk_data->unregister_arr)(str->ubuffer, str->buflen, "&+#!Iu", str->buffer_rock);
525 (*glk_data->unregister_arr)(str->buffer, str->buflen, "&+#!Cn", str->buffer_rock);
530 case STREAM_TYPE_FILE:
531 if(fclose(str->file_pointer) != 0)
532 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
533 g_free(str->filename);
536 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
540 stream_close_common(str, result);