4 #include <glib/gstdio.h>
6 #include "chimara-glk-private.h"
7 extern ChimaraGlkPrivate *glk_data;
9 /* Internal function: create a window stream to go with window. */
11 window_stream_new(winid_t window)
13 /* Create stream and connect it to window */
14 strid_t str = g_new0(struct glk_stream_struct, 1);
15 str->file_mode = filemode_Write;
16 str->type = STREAM_TYPE_WINDOW;
19 /* Add it to the global stream list */
20 glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
21 str->stream_list = glk_data->stream_list;
28 * @str: A stream, or %NULL.
29 * @rockptr: Return location for the next window's rock, or %NULL.
31 * Iterates through all the existing streams. See <link
32 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
35 * Returns: the next stream, or %NULL if there are no more.
38 glk_stream_iterate(strid_t str, glui32 *rockptr)
43 retnode = glk_data->stream_list;
45 retnode = str->stream_list->next;
46 strid_t retval = retnode? (strid_t)retnode->data : NULL;
48 /* Store the stream's rock in rockptr */
50 *rockptr = glk_stream_get_rock(retval);
56 * glk_stream_get_rock:
59 * Retrieves the stream @str's rock value. See <link
60 * linkend="chimara-Rocks">Rocks</link>.
62 * Returns: A rock value.
65 glk_stream_get_rock(strid_t str)
67 g_return_val_if_fail(str != NULL, 0);
72 * glk_stream_set_current:
73 * @str: An output stream, or %NULL.
75 * Sets the current stream to @str, which must be an output stream. You may set
76 * the current stream to %NULL, which means the current stream is not set to
80 glk_stream_set_current(strid_t str)
82 if(str != NULL && str->file_mode == filemode_Read)
84 g_warning("%s: Cannot set current stream to non output stream", __func__);
88 glk_data->current_stream = str;
92 * glk_stream_get_current:
94 * Returns the current stream, or %NULL if there is none.
96 * Returns: A stream, or %NULL.
99 glk_stream_get_current()
101 return glk_data->current_stream;
106 * @ch: A character in Latin-1 encoding.
108 * Prints one character to the current stream. As with all basic functions, the
109 * character is assumed to be in the Latin-1 character encoding. See <link
110 * linkend="chimara-Character-Encoding">Character Encoding</link>.
113 glk_put_char(unsigned char ch)
115 g_return_if_fail(glk_data->current_stream != NULL);
116 glk_put_char_stream(glk_data->current_stream, ch);
121 * @ch: A Unicode code point.
123 * Prints one character to the current stream. The character is assumed to be a
124 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
128 glk_put_char_uni(glui32 ch)
130 g_return_if_fail(glk_data->current_stream != NULL);
131 glk_put_char_stream_uni(glk_data->current_stream, ch);
136 * @s: A null-terminated string in Latin-1 encoding.
138 * Prints a null-terminated string to the current stream. It is exactly
140 * <informalexample><programlisting>
141 * for (ptr = @s; *ptr; ptr++)
142 * #glk_put_char(*ptr);
143 * </programlisting></informalexample>
144 * However, it may be more efficient.
147 glk_put_string(char *s)
149 g_return_if_fail(glk_data->current_stream != NULL);
150 glk_put_string_stream(glk_data->current_stream, s);
154 * glk_put_string_uni:
155 * @s: A zero-terminated string of Unicode code points.
157 * Prints a string of Unicode characters to the current stream. It is equivalent
158 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
162 glk_put_string_uni(glui32 *s)
164 g_return_if_fail(glk_data->current_stream != NULL);
165 glk_put_string_stream_uni(glk_data->current_stream, s);
170 * @buf: An array of characters in Latin-1 encoding.
171 * @len: Length of @buf.
173 * Prints a block of characters to the current stream. It is exactly equivalent
175 * <informalexample><programlisting>
176 * for (i = 0; i < @len; i++)
177 * #glk_put_char(@buf[i]);
178 * </programlisting></informalexample>
179 * However, it may be more efficient.
182 glk_put_buffer(char *buf, glui32 len)
184 g_return_if_fail(glk_data->current_stream != NULL);
185 glk_put_buffer_stream(glk_data->current_stream, buf, len);
189 * glk_put_buffer_uni:
190 * @buf: An array of Unicode code points.
191 * @len: Length of @buf.
193 * Prints a block of Unicode characters to the current stream. It is equivalent
194 * to a series of glk_put_char_uni() calls.
197 glk_put_buffer_uni(glui32 *buf, glui32 len)
199 g_return_if_fail(glk_data->current_stream != NULL);
200 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
204 * glk_stream_open_memory:
205 * @buf: An allocated buffer, or %NULL.
206 * @buflen: Length of @buf.
207 * @fmode: Mode in which the buffer will be opened. Must be one of
208 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
209 * @rock: The new stream's rock value.
211 * Opens a stream which reads from or writes to a space in memory. @buf points
212 * to the buffer where output will be read from or written to. @buflen is the
213 * length of the buffer.
215 * Unicode values (characters greater than 255) cannot be written to the buffer.
216 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
218 * Returns: the new stream, or %NULL on error.
221 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
223 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
225 strid_t str = g_new0(struct glk_stream_struct, 1);
227 str->file_mode = fmode;
228 str->type = STREAM_TYPE_MEMORY;
231 str->buflen = buflen;
232 str->unicode = FALSE;
234 /* Add it to the global stream list */
235 glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
236 str->stream_list = glk_data->stream_list;
242 * glk_stream_open_memory_uni:
243 * @buf: An allocated buffer, or %NULL.
244 * @buflen: Length of @buf.
245 * @fmode: Mode in which the buffer will be opened. Must be one of
246 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
247 * @rock: The new stream's rock value.
249 * Works just like glk_stream_open_memory(), except that the buffer is an array
250 * of 32-bit words, instead of bytes. This allows you to write and read any
251 * Unicode character. The @buflen is the number of words, not the number of
254 * Returns: the new stream, or %NULL on error.
257 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
259 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
261 strid_t str = g_new0(struct glk_stream_struct, 1);
263 str->file_mode = fmode;
264 str->type = STREAM_TYPE_MEMORY;
267 str->buflen = buflen;
270 /* Add it to the global stream list */
271 glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
272 str->stream_list = glk_data->stream_list;
277 /* Internal function: create a stream using the given parameters. */
279 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
281 g_return_val_if_fail(fileref != NULL, NULL);
284 /* Binary mode is 0x000, text mode 0x100 */
285 gboolean binary = !(fileref->usage & fileusage_TextMode);
289 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
290 g_warning("glk_stream_open_file: Tried to open a file in read "
291 "mode that didn't exist!");
294 modestr = g_strdup(binary? "rb" : "r");
297 modestr = g_strdup(binary? "wb" : "w");
299 case filemode_WriteAppend:
300 modestr = g_strdup(binary? "ab" : "a");
302 case filemode_ReadWrite:
303 if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
304 modestr = g_strdup(binary? "r+b" : "r+");
306 modestr = g_strdup(binary? "w+b" : "w+");
310 g_warning("glk_stream_open_file: Invalid file mode");
314 FILE *fp = g_fopen(fileref->filename, modestr);
317 g_warning("glk_stream_open_file: Error opening file");
321 /* If they opened a file in write mode but didn't specifically get
322 permission to do so, complain if the file already exists */
323 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
326 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
327 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
328 "File %s already exists. Overwrite?", fileref->filename);
329 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
330 gtk_widget_destroy(dialog);
334 if(response != GTK_RESPONSE_YES) {
340 strid_t str = g_new0(struct glk_stream_struct, 1);
342 str->file_mode = fmode;
343 str->type = STREAM_TYPE_FILE;
344 str->file_pointer = fp;
345 str->binary = binary;
346 str->unicode = unicode;
347 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
348 if(str->filename == NULL)
349 str->filename = g_strdup("Unknown file name"); /* fail silently */
350 /* Add it to the global stream list */
351 glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
352 str->stream_list = glk_data->stream_list;
358 * glk_stream_open_file:
359 * @fileref: Indicates the file which will be opened.
360 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
361 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
362 * @rock: The new stream's rock value.
364 * Opens a stream which reads to or writes from a disk file. If @fmode is
365 * #filemode_Read, the file must already exist; for the other modes, an empty
366 * file is created if none exists. If @fmode is #filemode_Write, and the file
367 * already exists, it is truncated down to zero length (an empty file). If
368 * @fmode is #filemode_WriteAppend, the file mark is set to the end of the
371 * When writing in binary mode, Unicode values (characters greater than 255)
372 * cannot be written to the file. If you try, they will be stored as 0x3F
373 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
374 * exactly, approximated, or abbreviated, depending on what the platform's text
377 * Returns: A new stream, or %NULL if the file operation failed.
380 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
382 return file_stream_new(fileref, fmode, rock, FALSE);
386 * glk_stream_open_file_uni:
387 * @fileref: Indicates the file which will be opened.
388 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
389 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
390 * @rock: The new stream's rock value.
392 * This works just like glk_stream_open_file(), except that in binary mode,
393 * characters are written and read as four-byte (big-endian) values. This
394 * allows you to write any Unicode character.
396 * In text mode, the file is written and read in a platform-dependent way, which
397 * may or may not handle all Unicode characters. A text-mode file created with
398 * glk_stream_open_file_uni() may have the same format as a text-mode file
399 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
402 * Returns: A new stream, or %NULL if the file operation failed.
405 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
407 return file_stream_new(fileref, fmode, rock, TRUE);
412 * @str: Stream to close.
413 * @result: Pointer to a #stream_result_t, or %NULL.
415 * Closes the stream @str. The @result argument points to a structure which is
416 * filled in with the final character counts of the stream. If you do not care
417 * about these, you may pass %NULL as the @result argument.
419 * If @str is the current output stream, the current output stream is set to
422 * You cannot close window streams; use glk_window_close() instead. See <link
423 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
424 * Closing, and Constraints</link>.
427 glk_stream_close(strid_t str, stream_result_t *result)
429 g_return_if_fail(str != NULL);
431 /* Free resources associated with one specific type of stream */
434 case STREAM_TYPE_WINDOW:
435 g_warning("%s: Attempted to close a window stream. Use glk_window_"
436 "close() instead.", __func__);
439 case STREAM_TYPE_MEMORY:
443 case STREAM_TYPE_FILE:
444 if(fclose(str->file_pointer) != 0)
445 g_warning("%s: Failed to close file '%s'.", __func__,
447 g_free(str->filename);
450 g_warning("%s: Closing this type of stream not supported.",
455 stream_close_common(str, result);
458 /* Internal function: Stuff to do upon closing any type of stream. */
460 stream_close_common(strid_t str, stream_result_t *result)
462 /* Remove the stream from the global stream list */
463 glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
465 /* If it was the current output stream, set that to NULL */
466 if(glk_data->current_stream == str)
467 glk_data->current_stream = NULL;
469 /* If it was one or more windows' echo streams, set those to NULL */
471 for(win = glk_window_iterate(NULL, NULL); win;
472 win = glk_window_iterate(win, NULL))
473 if(win->echo_stream == str)
474 win->echo_stream = NULL;
476 /* Return the character counts */
479 result->readcount = str->read_count;
480 result->writecount = str->write_count;