4 #include <glib/gstdio.h>
6 /* Global current stream */
7 static strid_t current_stream = NULL;
8 /* List of streams currently in existence */
9 static GList *stream_list = NULL;
11 /* Internal function: create a window stream to go with window. */
13 window_stream_new(winid_t window)
15 /* Create stream and connect it to window */
16 strid_t str = g_new0(struct glk_stream_struct, 1);
17 str->file_mode = filemode_Write;
18 str->type = STREAM_TYPE_WINDOW;
21 /* Add it to the global stream list */
22 stream_list = g_list_prepend(stream_list, str);
23 str->stream_list = stream_list;
30 * @str: A stream, or %NULL.
31 * @rockptr: Return location for the next window's rock, or %NULL.
33 * Iterates over the list of streams; if @str is %NULL, it returns the first
34 * stream, otherwise the next stream after @str. If there are no more, it
35 * returns %NULL. The stream's rock is stored in @rockptr. If you don't want
36 * the rocks to be returned, you may set @rockptr to %NULL.
38 * The order in which streams are returned is arbitrary. The order may change
39 * every time you create or destroy a stream, invalidating the iteration.
41 * Returns: the next stream, or %NULL if there are no more.
44 glk_stream_iterate(strid_t str, glui32 *rockptr)
49 retnode = stream_list;
51 retnode = str->stream_list->next;
52 strid_t retval = retnode? (strid_t)retnode->data : NULL;
54 /* Store the stream's rock in rockptr */
56 *rockptr = glk_stream_get_rock(retval);
62 * glk_stream_get_rock:
65 * Returns the stream @str's rock value.
67 * Returns: A rock value.
70 glk_stream_get_rock(strid_t str)
72 g_return_val_if_fail(str != NULL, 0);
77 * glk_stream_set_current:
78 * @str: An output stream, or %NULL.
80 * Sets the current stream to @str, which must be an output stream. You may set
81 * the current stream to %NULL, which means the current stream is not set to
85 glk_stream_set_current(strid_t str)
87 if(str != NULL && str->file_mode == filemode_Read)
89 g_warning("%s: Cannot set current stream to non output stream", __func__);
97 * glk_stream_get_current:
99 * Returns the current stream, or %NULL if there is none.
104 glk_stream_get_current()
106 return current_stream;
111 * @ch: A character in Latin-1 encoding.
113 * Prints one character to the current stream. As with all basic functions, the
114 * character is assumed to be in the Latin-1 character encoding.
117 glk_put_char(unsigned char ch)
119 g_return_if_fail(current_stream != NULL);
120 glk_put_char_stream(current_stream, ch);
125 * @s: A null-terminated string in Latin-1 encoding.
127 * Prints a null-terminated string to the current stream. It is exactly
129 * <informalexample><programlisting>
130 * for (ptr = s; *ptr; ptr++)
131 * glk_put_char(*ptr);
132 * </programlisting></informalexample>
133 * However, it may be more efficient.
136 glk_put_string(char *s)
138 g_return_if_fail(current_stream != NULL);
139 glk_put_string_stream(current_stream, s);
144 * @buf: An array of characters in Latin-1 encoding.
145 * @len: Length of @buf.
147 * Prints a block of characters to the current stream. It is exactly equivalent
149 * <informalexample><programlisting>
150 * for (i = 0; i < len; i++)
151 * glk_put_char(buf[i]);
152 * </programlisting></informalexample>
153 * However, it may be more efficient.
156 glk_put_buffer(char *buf, glui32 len)
158 g_return_if_fail(current_stream != NULL);
159 glk_put_buffer_stream(current_stream, buf, len);
163 * glk_stream_open_memory:
164 * @buf: An allocated buffer, or %NULL.
165 * @buflen: Length of @buf.
166 * @fmode: Mode in which the buffer will be opened. Must be one of
167 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
168 * @rock: The new stream's rock value.
170 * Opens a stream which reads from or writes to a space in memory. @buf points
171 * to the buffer where output will be read from or written to. @buflen is the
172 * length of the buffer.
174 * When outputting, if more than @buflen characters are written to the stream,
175 * all of them beyond the buffer length will be thrown away, so as not to
176 * overwrite the buffer. (The character count of the stream will still be
177 * maintained correctly. That is, it will count the number of characters written
178 * into the stream, not the number that fit into the buffer.)
180 * If @buf is %NULL, or for that matter if @buflen is zero, then <emphasis>
181 * everything</emphasis> written to the stream is thrown away. This may be
182 * useful if you are interested in the character count.
184 * When inputting, if more than @buflen characters are read from the stream, the
185 * stream will start returning -1 (signalling end-of-file.) If @buf is %NULL,
186 * the stream will always return end-of-file.
188 * The data is written to the buffer exactly as it was passed to the printing
189 * functions (glk_put_char(), etc.); input functions will read the data exactly
190 * as it exists in memory. No platform-dependent cookery will be done on it.
191 * (You can write a disk file in text mode, but a memory stream is effectively
192 * always in binary mode.)
194 * Unicode values (characters greater than 255) cannot be written to the buffer.
195 * If you try, they will be stored as 0x3F ("?") characters.
197 * Whether reading or writing, the contents of the buffer are undefined until
198 * the stream is closed. The library may store the data there as it is written,
199 * or deposit it all in a lump when the stream is closed. It is illegal to
200 * change the contents of the buffer while the stream is open.
203 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
205 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
207 strid_t str = g_new0(struct glk_stream_struct, 1);
209 str->file_mode = fmode;
210 str->type = STREAM_TYPE_MEMORY;
213 str->buflen = buflen;
214 str->unicode = FALSE;
216 /* Add it to the global stream list */
217 stream_list = g_list_prepend(stream_list, str);
218 str->stream_list = stream_list;
224 * glk_stream_open_memory_uni:
225 * @buf: An allocated buffer, or %NULL.
226 * @buflen: Length of @buf.
227 * @fmode: Mode in which the buffer will be opened. Must be one of
228 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
229 * @rock: The new stream's rock value.
231 * Works just like glk_stream_open_memory(), except that the buffer is an array
232 * of 32-bit words, instead of bytes. This allows you to write and read any
233 * Unicode character. The @buflen is the number of words, not the number of
237 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
239 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
241 strid_t str = g_new0(struct glk_stream_struct, 1);
243 str->file_mode = fmode;
244 str->type = STREAM_TYPE_MEMORY;
247 str->buflen = buflen;
250 /* Add it to the global stream list */
251 stream_list = g_list_prepend(stream_list, str);
252 str->stream_list = stream_list;
257 /* Internal function: create a stream using the given parameters. */
259 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
261 g_return_val_if_fail(fileref != NULL, NULL);
264 /* Binary mode is 0x000, text mode 0x100 */
265 gboolean binary = !(fileref->usage & fileusage_TextMode);
269 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
270 g_warning("glk_stream_open_file: Tried to open a file in read "
271 "mode that didn't exist!");
274 modestr = g_strdup(binary? "rb" : "r");
277 modestr = g_strdup(binary? "wb" : "w");
279 case filemode_WriteAppend:
280 modestr = g_strdup(binary? "ab" : "a");
282 case filemode_ReadWrite:
283 if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
284 modestr = g_strdup(binary? "r+b" : "r+");
286 modestr = g_strdup(binary? "w+b" : "w+");
290 g_warning("glk_stream_open_file: Invalid file mode");
294 FILE *fp = g_fopen(fileref->filename, modestr);
297 g_warning("glk_stream_open_file: Error opening file");
301 /* If they opened a file in write mode but didn't specifically get
302 permission to do so, complain if the file already exists */
303 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
306 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
307 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
308 "File %s already exists. Overwrite?", fileref->filename);
309 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
310 gtk_widget_destroy(dialog);
314 if(response != GTK_RESPONSE_YES) {
320 strid_t str = g_new0(struct glk_stream_struct, 1);
322 str->file_mode = fmode;
323 str->type = STREAM_TYPE_FILE;
324 str->file_pointer = fp;
325 str->binary = binary;
326 str->unicode = unicode;
327 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
328 if(str->filename == NULL)
329 str->filename = g_strdup("Unknown file name"); /* fail silently */
330 /* Add it to the global stream list */
331 stream_list = g_list_prepend(stream_list, str);
332 str->stream_list = stream_list;
338 * glk_stream_open_file:
339 * @fileref: Indicates the file which will be opened.
340 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
341 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
342 * @rock: The new stream's rock value.
344 * Opens a stream which reads to or writes from a disk file. If @fmode is
345 * #filemode_Read, the file must already exist; for the other modes, an empty
346 * file is created if none exists. If @fmode is #filemode_Write, and the file
347 * already exists, it is truncated down to zero length (an empty file). If
348 * @fmode is #filemode_WriteAppend, the file mark is set to the end of the
351 * The file may be written in text or binary mode; this is determined by the
352 * @fileref argument. Similarly, platform-dependent attributes such as file
353 * type are determined by @fileref.
355 * When writing in binary mode, Unicode values (characters greater than 255)
356 * cannot be written to the file. If you try, they will be stored as 0x3F ("?")
357 * characters. In text mode, Unicode values are stored in UTF-8.
359 * Returns: A new stream, or %NULL if the file operation failed.
362 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
364 return file_stream_new(fileref, fmode, rock, FALSE);
368 * glk_stream_open_file_uni:
369 * @fileref: Indicates the file which will be opened.
370 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
371 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
372 * @rock: The new stream's rock value.
374 * This works just like glk_stream_open_file(), except that in binary mode,
375 * characters are written and read as four-byte (big-endian) values. This
376 * allows you to write any Unicode character.
378 * In text mode, the file is written and read in UTF-8.
380 * Returns: A new stream, or %NULL if the file operation failed.
383 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
385 return file_stream_new(fileref, fmode, rock, TRUE);
390 * @str: Stream to close.
391 * @result: Pointer to a #stream_result_t, or %NULL.
393 * Closes the stream @str. The @result argument points to a structure which is
394 * filled in with the final character counts of the stream. If you do not care
395 * about these, you may pass %NULL as the @result argument.
397 * If @str is the current output stream, the current output stream is set to
400 * You cannot close window streams; use glk_window_close() instead.
403 glk_stream_close(strid_t str, stream_result_t *result)
405 g_return_if_fail(str != NULL);
407 /* Free resources associated with one specific type of stream */
410 case STREAM_TYPE_WINDOW:
411 g_warning("%s: Attempted to close a window stream. Use glk_window_"
412 "close() instead.", __func__);
415 case STREAM_TYPE_MEMORY:
419 case STREAM_TYPE_FILE:
420 if(fclose(str->file_pointer) != 0)
421 g_warning("%s: Failed to close file '%s'.", __func__,
423 g_free(str->filename);
426 g_warning("%s: Closing this type of stream not supported.",
431 stream_close_common(str, result);
434 /* Internal function: Stuff to do upon closing any type of stream. */
436 stream_close_common(strid_t str, stream_result_t *result)
438 /* Remove the stream from the global stream list */
439 stream_list = g_list_delete_link(stream_list, str->stream_list);
441 /* If it was the current output stream, set that to NULL */
442 if(current_stream == str)
443 current_stream = NULL;
445 /* If it was one or more windows' echo streams, set those to NULL */
447 for(win = glk_window_iterate(NULL, NULL); win;
448 win = glk_window_iterate(win, NULL))
449 if(win->echo_stream == str)
450 win->echo_stream = NULL;
452 /* Return the character counts */
455 result->readcount = str->read_count;
456 result->writecount = str->write_count;