X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=src%2Fstream.c;h=804399213ae7cbe87ebcbb96134130d5d4d51dd6;hb=5a06246277a255fe6e0e465ce0f190541b7b0e16;hp=7e61dc93b9a4b5183b8e99cc3b9c82571aea6546;hpb=8bf874cd9d56a5850bc474df37642170a4c20b28;p=rodin%2Fchimara.git diff --git a/src/stream.c b/src/stream.c index 7e61dc9..8043992 100644 --- a/src/stream.c +++ b/src/stream.c @@ -1,25 +1,39 @@ #include "stream.h" #include "fileref.h" +#include "magic.h" +#include +#include #include #include #include "chimara-glk-private.h" extern ChimaraGlkPrivate *glk_data; -/* Internal function: create a window stream to go with window. */ -strid_t -window_stream_new(winid_t window) +/* Internal function: create a stream with a specified rock value */ +static strid_t +stream_new_common(glui32 rock, glui32 fmode, enum StreamType type) { - /* Create stream and connect it to window */ strid_t str = g_new0(struct glk_stream_struct, 1); - str->file_mode = filemode_Write; - str->type = STREAM_TYPE_WINDOW; - str->window = window; - + str->magic = MAGIC_STREAM; + str->rock = rock; + str->file_mode = fmode; + str->type = type; + /* Add it to the global stream list */ glk_data->stream_list = g_list_prepend(glk_data->stream_list, str); str->stream_list = glk_data->stream_list; + + return str; +} +/* Internal function: create a window stream to go with window. */ +strid_t +window_stream_new(winid_t window) +{ + /* Create stream and connect it to window */ + strid_t str = stream_new_common(0, filemode_Write, STREAM_TYPE_WINDOW); + str->window = window; + str->style = "normal"; return str; } @@ -28,19 +42,17 @@ window_stream_new(winid_t window) * @str: A stream, or %NULL. * @rockptr: Return location for the next window's rock, or %NULL. * - * Iterates over the list of streams; if @str is %NULL, it returns the first - * stream, otherwise the next stream after @str. If there are no more, it - * returns %NULL. The stream's rock is stored in @rockptr. If you don't want - * the rocks to be returned, you may set @rockptr to %NULL. - * - * The order in which streams are returned is arbitrary. The order may change - * every time you create or destroy a stream, invalidating the iteration. + * Iterates through all the existing streams. See Iterating Through Opaque + * Objects. * * Returns: the next stream, or %NULL if there are no more. */ strid_t glk_stream_iterate(strid_t str, glui32 *rockptr) { + VALID_STREAM_OR_NULL(str, return NULL); + GList *retnode; if(str == NULL) @@ -60,14 +72,15 @@ glk_stream_iterate(strid_t str, glui32 *rockptr) * glk_stream_get_rock: * @str: A stream. * - * Returns the stream @str's rock value. + * Retrieves the stream @str's rock value. See Rocks. * * Returns: A rock value. */ glui32 glk_stream_get_rock(strid_t str) { - g_return_val_if_fail(str != NULL, 0); + VALID_STREAM(str, return 0); return str->rock; } @@ -82,9 +95,11 @@ glk_stream_get_rock(strid_t str) void glk_stream_set_current(strid_t str) { + VALID_STREAM_OR_NULL(str, return); + if(str != NULL && str->file_mode == filemode_Read) { - g_warning("%s: Cannot set current stream to non output stream", __func__); + ILLEGAL("Cannot set current stream to non output stream"); return; } @@ -96,7 +111,7 @@ glk_stream_set_current(strid_t str) * * Returns the current stream, or %NULL if there is none. * - * Returns: A stream. + * Returns: A stream, or %NULL. */ strid_t glk_stream_get_current() @@ -109,12 +124,13 @@ glk_stream_get_current() * @ch: A character in Latin-1 encoding. * * Prints one character to the current stream. As with all basic functions, the - * character is assumed to be in the Latin-1 character encoding. + * character is assumed to be in the Latin-1 character encoding. See Character Encoding. */ void glk_put_char(unsigned char ch) { - g_return_if_fail(glk_data->current_stream != NULL); + VALID_STREAM(glk_data->current_stream, return); glk_put_char_stream(glk_data->current_stream, ch); } @@ -123,12 +139,13 @@ glk_put_char(unsigned char ch) * @ch: A Unicode code point. * * Prints one character to the current stream. The character is assumed to be a - * Unicode code point. + * Unicode code point. See Character + * Encoding. */ void glk_put_char_uni(glui32 ch) { - g_return_if_fail(glk_data->current_stream != NULL); + VALID_STREAM(glk_data->current_stream, return); glk_put_char_stream_uni(glk_data->current_stream, ch); } @@ -137,17 +154,17 @@ glk_put_char_uni(glui32 ch) * @s: A null-terminated string in Latin-1 encoding. * * Prints a null-terminated string to the current stream. It is exactly - * equivalent to: - * - * for (ptr = s; *ptr; ptr++) - * glk_put_char(*ptr); - * + * equivalent to + * |[ + * for (ptr = @s; *ptr; ptr++) + * #glk_put_char(*ptr); + * ]| * However, it may be more efficient. */ void glk_put_string(char *s) { - g_return_if_fail(glk_data->current_stream != NULL); + VALID_STREAM(glk_data->current_stream, return); glk_put_string_stream(glk_data->current_stream, s); } @@ -162,7 +179,7 @@ glk_put_string(char *s) void glk_put_string_uni(glui32 *s) { - g_return_if_fail(glk_data->current_stream != NULL); + VALID_STREAM(glk_data->current_stream, return); glk_put_string_stream_uni(glk_data->current_stream, s); } @@ -173,16 +190,16 @@ glk_put_string_uni(glui32 *s) * * Prints a block of characters to the current stream. It is exactly equivalent * to: - * - * for (i = 0; i < len; i++) - * glk_put_char(buf[i]); - * + * |[ + * for (i = 0; i < @len; i++) + * #glk_put_char(@buf[i]); + * ]| * However, it may be more efficient. */ void glk_put_buffer(char *buf, glui32 len) { - g_return_if_fail(glk_data->current_stream != NULL); + VALID_STREAM(glk_data->current_stream, return); glk_put_buffer_stream(glk_data->current_stream, buf, len); } @@ -197,7 +214,7 @@ glk_put_buffer(char *buf, glui32 len) void glk_put_buffer_uni(glui32 *buf, glui32 len) { - g_return_if_fail(glk_data->current_stream != NULL); + VALID_STREAM(glk_data->current_stream, return); glk_put_buffer_stream_uni(glk_data->current_stream, buf, len); } @@ -213,52 +230,21 @@ glk_put_buffer_uni(glui32 *buf, glui32 len) * to the buffer where output will be read from or written to. @buflen is the * length of the buffer. * - * When outputting, if more than @buflen characters are written to the stream, - * all of them beyond the buffer length will be thrown away, so as not to - * overwrite the buffer. (The character count of the stream will still be - * maintained correctly. That is, it will count the number of characters written - * into the stream, not the number that fit into the buffer.) - * - * If @buf is %NULL, or for that matter if @buflen is zero, then - * everything written to the stream is thrown away. This may be - * useful if you are interested in the character count. - * - * When inputting, if more than @buflen characters are read from the stream, the - * stream will start returning -1 (signalling end-of-file.) If @buf is %NULL, - * the stream will always return end-of-file. - * - * The data is written to the buffer exactly as it was passed to the printing - * functions (glk_put_char(), etc.); input functions will read the data exactly - * as it exists in memory. No platform-dependent cookery will be done on it. - * (You can write a disk file in text mode, but a memory stream is effectively - * always in binary mode.) - * * Unicode values (characters greater than 255) cannot be written to the buffer. - * If you try, they will be stored as 0x3F ("?") characters. + * If you try, they will be stored as 0x3F ("?") characters. * - * Whether reading or writing, the contents of the buffer are undefined until - * the stream is closed. The library may store the data there as it is written, - * or deposit it all in a lump when the stream is closed. It is illegal to - * change the contents of the buffer while the stream is open. + * Returns: the new stream, or %NULL on error. */ strid_t glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock) { g_return_val_if_fail(fmode != filemode_WriteAppend, NULL); - strid_t str = g_new0(struct glk_stream_struct, 1); - str->rock = rock; - str->file_mode = fmode; - str->type = STREAM_TYPE_MEMORY; + strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_MEMORY); str->buffer = buf; str->mark = 0; str->buflen = buflen; str->unicode = FALSE; - - /* Add it to the global stream list */ - glk_data->stream_list = g_list_prepend(glk_data->stream_list, str); - str->stream_list = glk_data->stream_list; - return str; } @@ -274,25 +260,19 @@ glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock) * of 32-bit words, instead of bytes. This allows you to write and read any * Unicode character. The @buflen is the number of words, not the number of * bytes. + * + * Returns: the new stream, or %NULL on error. */ strid_t glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock) { g_return_val_if_fail(fmode != filemode_WriteAppend, NULL); - strid_t str = g_new0(struct glk_stream_struct, 1); - str->rock = rock; - str->file_mode = fmode; - str->type = STREAM_TYPE_MEMORY; + strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_MEMORY); str->ubuffer = buf; str->mark = 0; str->buflen = buflen; str->unicode = TRUE; - - /* Add it to the global stream list */ - glk_data->stream_list = g_list_prepend(glk_data->stream_list, str); - str->stream_list = glk_data->stream_list; - return str; } @@ -300,7 +280,7 @@ glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock static strid_t file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode) { - g_return_val_if_fail(fileref != NULL, NULL); + VALID_FILEREF(fileref, return NULL); gchar *modestr; /* Binary mode is 0x000, text mode 0x100 */ @@ -309,8 +289,7 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode) { case filemode_Read: if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) { - g_warning("glk_stream_open_file: Tried to open a file in read " - "mode that didn't exist!"); + ILLEGAL_PARAM("Tried to open a nonexistent file, '%s', in read mode", fileref->filename); return NULL; } modestr = g_strdup(binary? "rb" : "r"); @@ -329,14 +308,14 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode) } break; default: - g_warning("glk_stream_open_file: Invalid file mode"); + ILLEGAL_PARAM("Invalid file mode: %u", fmode); return NULL; } FILE *fp = g_fopen(fileref->filename, modestr); g_free(modestr); if(fp == NULL) { - g_warning("glk_stream_open_file: Error opening file"); + IO_WARNING( "Error opening file", fileref->filename, g_strerror(errno) ); return NULL; } @@ -347,31 +326,26 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode) GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, - "File %s already exists. Overwrite?", fileref->filename); + "File '%s' already exists. Overwrite?", fileref->filename); gint response = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); gdk_threads_leave(); if(response != GTK_RESPONSE_YES) { - fclose(fp); + if(fclose(fp) != 0) + IO_WARNING( "Error closing file", fileref->filename, g_strerror(errno) ); return NULL; } } - strid_t str = g_new0(struct glk_stream_struct, 1); - str->rock = rock; - str->file_mode = fmode; - str->type = STREAM_TYPE_FILE; + strid_t str = stream_new_common(rock, fmode, STREAM_TYPE_FILE); str->file_pointer = fp; str->binary = binary; str->unicode = unicode; str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL); if(str->filename == NULL) str->filename = g_strdup("Unknown file name"); /* fail silently */ - /* Add it to the global stream list */ - glk_data->stream_list = g_list_prepend(glk_data->stream_list, str); - str->stream_list = glk_data->stream_list; return str; } @@ -389,14 +363,12 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode) * already exists, it is truncated down to zero length (an empty file). If * @fmode is #filemode_WriteAppend, the file mark is set to the end of the * file. - * - * The file may be written in text or binary mode; this is determined by the - * @fileref argument. Similarly, platform-dependent attributes such as file - * type are determined by @fileref. * * When writing in binary mode, Unicode values (characters greater than 255) - * cannot be written to the file. If you try, they will be stored as 0x3F ("?") - * characters. In text mode, Unicode values are stored in UTF-8. + * cannot be written to the file. If you try, they will be stored as 0x3F + * ("?") characters. In text mode, Unicode values may be stored + * exactly, approximated, or abbreviated, depending on what the platform's text + * files support. * * Returns: A new stream, or %NULL if the file operation failed. */ @@ -417,7 +389,11 @@ glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock) * characters are written and read as four-byte (big-endian) values. This * allows you to write any Unicode character. * - * In text mode, the file is written and read in UTF-8. + * In text mode, the file is written and read in a platform-dependent way, which + * may or may not handle all Unicode characters. A text-mode file created with + * glk_stream_open_file_uni() may have the same format as a text-mode file + * created with glk_stream_open_file(); or it may use a more Unicode-friendly + * format. * * Returns: A new stream, or %NULL if the file operation failed. */ @@ -439,19 +415,20 @@ glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock) * If @str is the current output stream, the current output stream is set to * %NULL. * - * You cannot close window streams; use glk_window_close() instead. + * You cannot close window streams; use glk_window_close() instead. See Window Opening, + * Closing, and Constraints. */ void glk_stream_close(strid_t str, stream_result_t *result) { - g_return_if_fail(str != NULL); + VALID_STREAM(str, return); /* Free resources associated with one specific type of stream */ switch(str->type) { case STREAM_TYPE_WINDOW: - g_warning("%s: Attempted to close a window stream. Use glk_window_" - "close() instead.", __func__); + ILLEGAL("Attempted to close a window stream. Use glk_window_close() instead."); return; case STREAM_TYPE_MEMORY: @@ -460,13 +437,11 @@ glk_stream_close(strid_t str, stream_result_t *result) case STREAM_TYPE_FILE: if(fclose(str->file_pointer) != 0) - g_warning("%s: Failed to close file '%s'.", __func__, - str->filename); + IO_WARNING( "Failed to close file", str->filename, g_strerror(errno) ); g_free(str->filename); break; default: - g_warning("%s: Closing this type of stream not supported.", - __func__); + ILLEGAL_PARAM("Unknown stream type: %u", str->type); return; } @@ -497,5 +472,7 @@ stream_close_common(strid_t str, stream_result_t *result) result->readcount = str->read_count; result->writecount = str->write_count; } + + str->magic = MAGIC_FREE; g_free(str); }