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);
36 str->style = "normal";
42 * @str: A stream, or %NULL.
43 * @rockptr: Return location for the next window's rock, or %NULL.
45 * Iterates through all the existing streams. See <link
46 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
49 * Returns: the next stream, or %NULL if there are no more.
52 glk_stream_iterate(strid_t str, glui32 *rockptr)
54 VALID_STREAM_OR_NULL(str, return NULL);
59 retnode = glk_data->stream_list;
61 retnode = str->stream_list->next;
62 strid_t retval = retnode? (strid_t)retnode->data : NULL;
64 /* Store the stream's rock in rockptr */
66 *rockptr = glk_stream_get_rock(retval);
72 * glk_stream_get_rock:
75 * Retrieves the stream @str's rock value. See <link
76 * linkend="chimara-Rocks">Rocks</link>.
78 * Returns: A rock value.
81 glk_stream_get_rock(strid_t str)
83 VALID_STREAM(str, return 0);
88 * glk_stream_set_current:
89 * @str: An output stream, or %NULL.
91 * Sets the current stream to @str, which must be an output stream. You may set
92 * the current stream to %NULL, which means the current stream is not set to
96 glk_stream_set_current(strid_t str)
98 VALID_STREAM_OR_NULL(str, return);
100 if(str != NULL && str->file_mode == filemode_Read)
102 ILLEGAL("Cannot set current stream to non output stream");
106 glk_data->current_stream = str;
110 * glk_stream_get_current:
112 * Returns the current stream, or %NULL if there is none.
114 * Returns: A stream, or %NULL.
117 glk_stream_get_current()
119 return glk_data->current_stream;
124 * @ch: A character in Latin-1 encoding.
126 * Prints one character to the current stream. As with all basic functions, the
127 * character is assumed to be in the Latin-1 character encoding. See <link
128 * linkend="chimara-Character-Encoding">Character Encoding</link>.
131 glk_put_char(unsigned char ch)
133 VALID_STREAM(glk_data->current_stream, return);
134 glk_put_char_stream(glk_data->current_stream, ch);
139 * @ch: A Unicode code point.
141 * Prints one character to the current stream. The character is assumed to be a
142 * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
146 glk_put_char_uni(glui32 ch)
148 VALID_STREAM(glk_data->current_stream, return);
149 glk_put_char_stream_uni(glk_data->current_stream, ch);
154 * @s: A null-terminated string in Latin-1 encoding.
156 * Prints a null-terminated string to the current stream. It is exactly
159 * for (ptr = @s; *ptr; ptr++)
160 * #glk_put_char(*ptr);
162 * However, it may be more efficient.
165 glk_put_string(char *s)
167 VALID_STREAM(glk_data->current_stream, return);
168 glk_put_string_stream(glk_data->current_stream, s);
172 * glk_put_string_uni:
173 * @s: A zero-terminated string of Unicode code points.
175 * Prints a string of Unicode characters to the current stream. It is equivalent
176 * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
180 glk_put_string_uni(glui32 *s)
182 VALID_STREAM(glk_data->current_stream, return);
183 glk_put_string_stream_uni(glk_data->current_stream, s);
188 * @buf: An array of characters in Latin-1 encoding.
189 * @len: Length of @buf.
191 * Prints a block of characters to the current stream. It is exactly equivalent
194 * for (i = 0; i < @len; i++)
195 * #glk_put_char(@buf[i]);
197 * However, it may be more efficient.
200 glk_put_buffer(char *buf, glui32 len)
202 VALID_STREAM(glk_data->current_stream, return);
203 glk_put_buffer_stream(glk_data->current_stream, buf, len);
207 * glk_put_buffer_uni:
208 * @buf: An array of Unicode code points.
209 * @len: Length of @buf.
211 * Prints a block of Unicode characters to the current stream. It is equivalent
212 * to a series of glk_put_char_uni() calls.
215 glk_put_buffer_uni(glui32 *buf, glui32 len)
217 VALID_STREAM(glk_data->current_stream, return);
218 glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
222 * glk_stream_open_memory:
223 * @buf: An allocated buffer, or %NULL.
224 * @buflen: Length of @buf.
225 * @fmode: Mode in which the buffer will be opened. Must be one of
226 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
227 * @rock: The new stream's rock value.
229 * Opens a stream which reads from or writes to a space in memory. @buf points
230 * to the buffer where output will be read from or written to. @buflen is the
231 * length of the buffer.
233 * Unicode values (characters greater than 255) cannot be written to the buffer.
234 * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
236 * Returns: the new stream, or %NULL on error.
239 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
241 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
243 strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_MEMORY);
246 str->buflen = buflen;
247 str->unicode = FALSE;
252 * glk_stream_open_memory_uni:
253 * @buf: An allocated buffer, or %NULL.
254 * @buflen: Length of @buf.
255 * @fmode: Mode in which the buffer will be opened. Must be one of
256 * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
257 * @rock: The new stream's rock value.
259 * Works just like glk_stream_open_memory(), except that the buffer is an array
260 * of 32-bit words, instead of bytes. This allows you to write and read any
261 * Unicode character. The @buflen is the number of words, not the number of
264 * Returns: the new stream, or %NULL on error.
267 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
269 g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
271 strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_MEMORY);
274 str->buflen = buflen;
279 /* Internal function: create a stream using the given parameters. */
281 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
283 VALID_FILEREF(fileref, return NULL);
286 /* Binary mode is 0x000, text mode 0x100 */
287 gboolean binary = !(fileref->usage & fileusage_TextMode);
291 if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
292 ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename);
295 modestr = g_strdup(binary? "rb" : "r");
298 modestr = g_strdup(binary? "wb" : "w");
300 case filemode_WriteAppend:
301 modestr = g_strdup(binary? "ab" : "a");
303 case filemode_ReadWrite:
304 if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
305 modestr = g_strdup(binary? "r+b" : "r+");
307 modestr = g_strdup(binary? "w+b" : "w+");
311 ILLEGAL_PARAM("Invalid file mode: %u", fmode);
315 FILE *fp = g_fopen(fileref->filename, modestr);
318 IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) );
322 /* If they opened a file in write mode but didn't specifically get
323 permission to do so, complain if the file already exists */
324 if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
327 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
328 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
329 "File '%s' already exists. Overwrite?", fileref->filename);
330 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
331 gtk_widget_destroy(dialog);
335 if(response != GTK_RESPONSE_YES) {
337 IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) );
342 strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_FILE);
343 str->file_pointer = fp;
344 str->binary = binary;
345 str->unicode = unicode;
346 str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
347 if(str->filename == NULL)
348 str->filename = g_strdup("Unknown file name"); /* fail silently */
354 * glk_stream_open_file:
355 * @fileref: Indicates the file which will be opened.
356 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
357 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
358 * @rock: The new stream's rock value.
360 * Opens a stream which reads to or writes from a disk file. If @fmode is
361 * #filemode_Read, the file must already exist; for the other modes, an empty
362 * file is created if none exists. If @fmode is #filemode_Write, and the file
363 * already exists, it is truncated down to zero length (an empty file). If
364 * @fmode is #filemode_WriteAppend, the file mark is set to the end of the
367 * When writing in binary mode, Unicode values (characters greater than 255)
368 * cannot be written to the file. If you try, they will be stored as 0x3F
369 * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
370 * exactly, approximated, or abbreviated, depending on what the platform's text
373 * Returns: A new stream, or %NULL if the file operation failed.
376 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
378 return file_stream_new(fileref, fmode, rock, FALSE);
382 * glk_stream_open_file_uni:
383 * @fileref: Indicates the file which will be opened.
384 * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
385 * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
386 * @rock: The new stream's rock value.
388 * This works just like glk_stream_open_file(), except that in binary mode,
389 * characters are written and read as four-byte (big-endian) values. This
390 * allows you to write any Unicode character.
392 * In text mode, the file is written and read in a platform-dependent way, which
393 * may or may not handle all Unicode characters. A text-mode file created with
394 * glk_stream_open_file_uni() may have the same format as a text-mode file
395 * created with glk_stream_open_file(); or it may use a more Unicode-friendly
398 * Returns: A new stream, or %NULL if the file operation failed.
401 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
403 return file_stream_new(fileref, fmode, rock, TRUE);
408 * @str: Stream to close.
409 * @result: Pointer to a #stream_result_t, or %NULL.
411 * Closes the stream @str. The @result argument points to a structure which is
412 * filled in with the final character counts of the stream. If you do not care
413 * about these, you may pass %NULL as the @result argument.
415 * If @str is the current output stream, the current output stream is set to
418 * You cannot close window streams; use glk_window_close() instead. See <link
419 * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
420 * Closing, and Constraints</link>.
423 glk_stream_close(strid_t str, stream_result_t *result)
425 VALID_STREAM(str, return);
427 /* Free resources associated with one specific type of stream */
430 case STREAM_TYPE_WINDOW:
431 ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead.");
434 case STREAM_TYPE_MEMORY:
438 case STREAM_TYPE_FILE:
439 if(fclose(str->file_pointer) != 0)
440 IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) );
441 g_free(str->filename);
444 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
448 stream_close_common(str, result);
451 /* Internal function: Stuff to do upon closing any type of stream. */
453 stream_close_common(strid_t str, stream_result_t *result)
455 /* Remove the stream from the global stream list */
456 glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
458 /* If it was the current output stream, set that to NULL */
459 if(glk_data->current_stream == str)
460 glk_data->current_stream = NULL;
462 /* If it was one or more windows' echo streams, set those to NULL */
464 for(win = glk_window_iterate(NULL, NULL); win;
465 win = glk_window_iterate(win, NULL))
466 if(win->echo_stream == str)
467 win->echo_stream = NULL;
469 /* Return the character counts */
472 result->readcount = str->read_count;
473 result->writecount = str->write_count;
476 str->magic = MAGIC_FREE;
481 glkunix_stream_open_pathname(char *pathname, glui32 usage, glui32 rock)
483 printf("making new fileref: %s\n", pathname);
484 frefid_t fileref = fileref_new(pathname, rock, usage, filemode_ReadWrite);
485 printf("makeing new stream:\n");
486 return file_stream_new(fileref, filemode_ReadWrite, rock, FALSE);