7 #include <glib/gstdio.h>
9 #include "chimara-glk-private.h"
10 extern ChimaraGlkPrivate *glk_data;
12 /* Internal function: create a stream with a specified rock value */
14 stream_new_common(glui32 rock, glui32 fmode, enum StreamType type)
16 strid_t str = g_new0(struct glk_stream_struct, 1);
17 str->magic = MAGIC_STREAM;
19 str->file_mode = fmode;
22 /* Add it to the global stream list */
23 glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
24 str->stream_list = glk_data->stream_list;
29 /* Internal function: create a window stream to go with window. */
31 window_stream_new(winid_t window)
33 /* Create stream and connect it to window */
34 strid_t str = stream_new_common(0, filemode_Write, STREAM_TYPE_WINDOW);
41 * @str: A stream, or %NULL.
42 * @rockptr: Return location for the next window's rock, or %NULL.
44 * Iterates through all the existing streams. See <link
45 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
48 * Returns: the next stream, or %NULL if there are no more.
51 glk_stream_iterate(strid_t str, glui32 *rockptr)
53 VALID_STREAM_OR_NULL(str, return NULL);
58 retnode = glk_data->stream_list;
60 retnode = str->stream_list->next;
61 strid_t retval = retnode? (strid_t)retnode->data : NULL;
63 /* Store the stream's rock in rockptr */
65 *rockptr = glk_stream_get_rock(retval);
71 * glk_stream_get_rock:
74 * Retrieves the stream @str's rock value. See <link
75 * linkend="chimara-Rocks">Rocks</link>.
77 * Returns: A rock value.
80 glk_stream_get_rock(strid_t str)
82 VALID_STREAM(str, return 0);
87 * glk_stream_set_current:
88 * @str: An output stream, or %NULL.
90 * Sets the current stream to @str, which must be an output stream. You may set
91 * the current stream to %NULL, which means the current stream is not set to
95 glk_stream_set_current(strid_t str)
97 VALID_STREAM_OR_NULL(str, return);
99 if(str != NULL && str->file_mode == filemode_Read)
101 ILLEGAL("Cannot set current stream to non output stream");
105 glk_data->current_stream = str;
109 * glk_stream_get_current:
111 * Returns the current stream, or %NULL if there is none.
113 * Returns: A stream, or %NULL.
116 glk_stream_get_current()
118 return glk_data->current_stream;
123 * @ch: A character in Latin-1 encoding.
125 * Prints one character to the current stream. As with all basic functions, the
126 * character is assumed to be in the Latin-1 character encoding. See <link
127 * linkend="chimara-Character-Encoding">Character Encoding</link>.
130 glk_put_char(unsigned char ch)
132 VALID_STREAM(glk_data->current_stream, return);
133 glk_put_char_stream(glk_data->current_stream, ch);
138 * @ch: A Unicode code point.
140 * Prints one character to the current stream. The character is assumed to be a
141 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
145 glk_put_char_uni(glui32 ch)
147 VALID_STREAM(glk_data->current_stream, return);
148 glk_put_char_stream_uni(glk_data->current_stream, ch);
153 * @s: A null-terminated string in Latin-1 encoding.
155 * Prints a null-terminated string to the current stream. It is exactly
158 * for (ptr = @s; *ptr; ptr++)
159 * #glk_put_char(*ptr);
161 * However, it may be more efficient.
164 glk_put_string(char *s)
166 VALID_STREAM(glk_data->current_stream, return);
167 glk_put_string_stream(glk_data->current_stream, s);
171 * glk_put_string_uni:
172 * @s: A zero-terminated string of Unicode code points.
174 * Prints a string of Unicode characters to the current stream. It is equivalent
175 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
179 glk_put_string_uni(glui32 *s)
181 VALID_STREAM(glk_data->current_stream, return);
182 glk_put_string_stream_uni(glk_data->current_stream, s);
187 * @buf: An array of characters in Latin-1 encoding.
188 * @len: Length of @buf.
190 * Prints a block of characters to the current stream. It is exactly equivalent
193 * for (i = 0; i < @len; i++)
194 * #glk_put_char(@buf[i]);
196 * However, it may be more efficient.
199 glk_put_buffer(char *buf, glui32 len)
201 VALID_STREAM(glk_data->current_stream, return);
202 glk_put_buffer_stream(glk_data->current_stream, buf, len);
206 * glk_put_buffer_uni:
207 * @buf: An array of Unicode code points.
208 * @len: Length of @buf.
210 * Prints a block of Unicode characters to the current stream. It is equivalent
211 * to a series of glk_put_char_uni() calls.
214 glk_put_buffer_uni(glui32 *buf, glui32 len)
216 VALID_STREAM(glk_data->current_stream, return);
217 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
221 * glk_stream_open_memory:
222 * @buf: An allocated buffer, or %NULL.
223 * @buflen: Length of @buf.
224 * @fmode: Mode in which the buffer will be opened. Must be one of
225 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
226 * @rock: The new stream's rock value.
228 * Opens a stream which reads from or writes to a space in memory. @buf points
229 * to the buffer where output will be read from or written to. @buflen is the
230 * length of the buffer.
232 * Unicode values (characters greater than 255) cannot be written to the buffer.
233 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
235 * Returns: the new stream, or %NULL on error.
238 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
240 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
242 strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_MEMORY);
245 str->buflen = buflen;
246 str->unicode = FALSE;
251 * glk_stream_open_memory_uni:
252 * @buf: An allocated buffer, or %NULL.
253 * @buflen: Length of @buf.
254 * @fmode: Mode in which the buffer will be opened. Must be one of
255 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
256 * @rock: The new stream's rock value.
258 * Works just like glk_stream_open_memory(), except that the buffer is an array
259 * of 32-bit words, instead of bytes. This allows you to write and read any
260 * Unicode character. The @buflen is the number of words, not the number of
263 * Returns: the new stream, or %NULL on error.
266 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
268 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
270 strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_MEMORY);
273 str->buflen = buflen;
278 /* Internal function: create a stream using the given parameters. */
280 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
282 VALID_FILEREF(fileref, return NULL);
285 /* Binary mode is 0x000, text mode 0x100 */
286 gboolean binary = !(fileref->usage & fileusage_TextMode);
290 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
291 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
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 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
314 FILE *fp = g_fopen(fileref->filename, modestr);
317 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
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) {
336 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
341 strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_FILE);
342 str->file_pointer = fp;
343 str->binary = binary;
344 str->unicode = unicode;
345 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
346 if(str->filename == NULL)
347 str->filename = g_strdup("Unknown file name"); /* fail silently */
353 * glk_stream_open_file:
354 * @fileref: Indicates the file which will be opened.
355 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
356 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
357 * @rock: The new stream's rock value.
359 * Opens a stream which reads to or writes from a disk file. If @fmode is
360 * #filemode_Read, the file must already exist; for the other modes, an empty
361 * file is created if none exists. If @fmode is #filemode_Write, and the file
362 * already exists, it is truncated down to zero length (an empty file). If
363 * @fmode is #filemode_WriteAppend, the file mark is set to the end of the
366 * When writing in binary mode, Unicode values (characters greater than 255)
367 * cannot be written to the file. If you try, they will be stored as 0x3F
368 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
369 * exactly, approximated, or abbreviated, depending on what the platform's text
372 * Returns: A new stream, or %NULL if the file operation failed.
375 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
377 return file_stream_new(fileref, fmode, rock, FALSE);
381 * glk_stream_open_file_uni:
382 * @fileref: Indicates the file which will be opened.
383 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
384 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
385 * @rock: The new stream's rock value.
387 * This works just like glk_stream_open_file(), except that in binary mode,
388 * characters are written and read as four-byte (big-endian) values. This
389 * allows you to write any Unicode character.
391 * In text mode, the file is written and read in a platform-dependent way, which
392 * may or may not handle all Unicode characters. A text-mode file created with
393 * glk_stream_open_file_uni() may have the same format as a text-mode file
394 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
397 * Returns: A new stream, or %NULL if the file operation failed.
400 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
402 return file_stream_new(fileref, fmode, rock, TRUE);
407 * @str: Stream to close.
408 * @result: Pointer to a #stream_result_t, or %NULL.
410 * Closes the stream @str. The @result argument points to a structure which is
411 * filled in with the final character counts of the stream. If you do not care
412 * about these, you may pass %NULL as the @result argument.
414 * If @str is the current output stream, the current output stream is set to
417 * You cannot close window streams; use glk_window_close() instead. See <link
418 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
419 * Closing, and Constraints</link>.
422 glk_stream_close(strid_t str, stream_result_t *result)
424 VALID_STREAM(str, return);
426 /* Free resources associated with one specific type of stream */
429 case STREAM_TYPE_WINDOW:
430 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
433 case STREAM_TYPE_MEMORY:
437 case STREAM_TYPE_FILE:
438 if(fclose(str->file_pointer) != 0)
439 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
440 g_free(str->filename);
443 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
447 stream_close_common(str, result);
450 /* Internal function: Stuff to do upon closing any type of stream. */
452 stream_close_common(strid_t str, stream_result_t *result)
454 /* Remove the stream from the global stream list */
455 glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
457 /* If it was the current output stream, set that to NULL */
458 if(glk_data->current_stream == str)
459 glk_data->current_stream = NULL;
461 /* If it was one or more windows' echo streams, set those to NULL */
463 for(win = glk_window_iterate(NULL, NULL); win;
464 win = glk_window_iterate(win, NULL))
465 if(win->echo_stream == str)
466 win->echo_stream = NULL;
468 /* Return the character counts */
471 result->readcount = str->read_count;
472 result->writecount = str->write_count;
475 str->magic = MAGIC_FREE;