Got Gtk-Doc working. Now all the fancy /** comments before the functions
[rodin/chimara.git] / src / stream.c
index f7bee3243db3a94061afeecf44f138b4d60f4571..bab5b5a2ebc8895eb7facb81d13da2d0e59a9615 100644 (file)
@@ -3,41 +3,36 @@
 #include <glib.h>
 #include <glib/gstdio.h>
 
-/* Global current stream */
-static strid_t current_stream = NULL;
-/* List of streams currently in existence */
-static GList *stream_list = NULL;
+#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)
 {
        /* Create stream and connect it to window */
-       strid_t s = g_new0(struct glk_stream_struct, 1);
-       s->file_mode = filemode_Write;
-       s->stream_type = STREAM_TYPE_WINDOW;
-       s->window = window;
+       strid_t str = g_new0(struct glk_stream_struct, 1);
+       str->file_mode = filemode_Write;
+       str->type = STREAM_TYPE_WINDOW;
+       str->window = window;
+       
        /* Add it to the global stream list */
-       stream_list = g_list_prepend(stream_list, s);
-       s->stream_list = stream_list;
+       glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
+       str->stream_list = glk_data->stream_list;
 
-       return s;
+       return str;
 }
 
 /**
  * glk_stream_iterate:
- * @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.
+ * @str: A stream, or %NULL.
+ * @rockptr: Return location for the next window's rock, or %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 <link
+ * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
+ * Objects</link>.
  *
- * Returns: the next stream, or #NULL if there are no more.
+ * Returns: the next stream, or %NULL if there are no more.
  */
 strid_t
 glk_stream_iterate(strid_t str, glui32 *rockptr)
@@ -45,7 +40,7 @@ glk_stream_iterate(strid_t str, glui32 *rockptr)
        GList *retnode;
        
        if(str == NULL)
-               retnode = stream_list;
+               retnode = glk_data->stream_list;
        else
                retnode = str->stream_list->next;
        strid_t retval = retnode? (strid_t)retnode->data : NULL;
@@ -61,7 +56,8 @@ 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 <link 
+ * linkend="chimara-Rocks">Rocks</link>.
  *
  * Returns: A rock value.
  */
@@ -74,62 +70,99 @@ glk_stream_get_rock(strid_t str)
 
 /**
  * glk_stream_set_current:
- * @str: An output stream, or NULL.
+ * @str: An output stream, or %NULL.
  *
- * Sets the current stream to @str, or to nothing if @str is #NULL.
+ * Sets the current stream to @str, which must be an output stream. You may set
+ * the current stream to %NULL, which means the current stream is not set to
+ * anything. 
  */
 void
 glk_stream_set_current(strid_t str)
 {
        if(str != NULL && str->file_mode == filemode_Read)
        {
-               g_warning("glk_stream_set_current: "
-                       "Cannot set current stream to non output stream");
+               g_warning("%s: Cannot set current stream to non output stream", __func__);
                return;
        }
 
-       current_stream = str;
+       glk_data->current_stream = str;
 }
 
 /**
  * glk_stream_get_current:
  * 
- * Returns the current stream, or #NULL if there is none.
+ * Returns the current stream, or %NULL if there is none.
  *
- * Returns: A stream.
+ * Returns: A stream, or %NULL.
  */
 strid_t
 glk_stream_get_current()
 {
-       return current_stream;
+       return glk_data->current_stream;
 }
 
 /**
  * glk_put_char:
  * @ch: A character in Latin-1 encoding.
  *
- * Prints one character @ch to the current stream.
+ * Prints one character to the current stream. As with all basic functions, the
+ * character is assumed to be in the Latin-1 character encoding. See <link
+ * linkend="chimara-Character-Encoding">Character Encoding</link>.
  */
 void
 glk_put_char(unsigned char ch)
 {
-       /* Illegal to print to the current stream if it is NULL */
-       g_return_if_fail(current_stream != NULL);
-       glk_put_char_stream(current_stream, ch);
+       g_return_if_fail(glk_data->current_stream != NULL);
+       glk_put_char_stream(glk_data->current_stream, ch);
+}
+
+/**
+ * glk_put_char_uni:
+ * @ch: A Unicode code point.
+ *
+ * Prints one character to the current stream. The character is assumed to be a
+ * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
+ * Encoding</link>.
+ */
+void
+glk_put_char_uni(glui32 ch)
+{
+       g_return_if_fail(glk_data->current_stream != NULL);
+       glk_put_char_stream_uni(glk_data->current_stream, ch);
 }
 
 /**
  * glk_put_string:
  * @s: A null-terminated string in Latin-1 encoding.
  *
- * Prints @s to the current stream.
+ * Prints a null-terminated string to the current stream. It is exactly
+ * equivalent to
+ * <informalexample><programlisting>
+ * for (ptr = @s; *ptr; ptr++)
+ *     #glk_put_char(*ptr);
+ * </programlisting></informalexample>
+ * However, it may be more efficient.
  */
 void
 glk_put_string(char *s)
 {
-       /* Illegal to print to the current stream if it is NULL */
-       g_return_if_fail(current_stream != NULL);
-       glk_put_string_stream(current_stream, s);
+       g_return_if_fail(glk_data->current_stream != NULL);
+       glk_put_string_stream(glk_data->current_stream, s);
+}
+
+/**
+ * glk_put_string_uni:
+ * @s: A zero-terminated string of Unicode code points.
+ * 
+ * Prints a string of Unicode characters to the current stream. It is equivalent
+ * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
+ * value is 0.
+ */
+void
+glk_put_string_uni(glui32 *s)
+{
+       g_return_if_fail(glk_data->current_stream != NULL);
+       glk_put_string_stream_uni(glk_data->current_stream, s);
 }
 
 /**
@@ -137,14 +170,34 @@ glk_put_string(char *s)
  * @buf: An array of characters in Latin-1 encoding.
  * @len: Length of @buf.
  *
- * Prints @buf to the current stream.
+ * Prints a block of characters to the current stream. It is exactly equivalent
+ * to:
+ * <informalexample><programlisting>
+ * for (i = 0; i < @len; i++)
+ *     #glk_put_char(@buf[i]);
+ * </programlisting></informalexample>
+ * However, it may be more efficient.
  */
 void
 glk_put_buffer(char *buf, glui32 len)
 {
-       /* Illegal to print to the current stream if it is NULL */
-       g_return_if_fail(current_stream != NULL);
-       glk_put_buffer_stream(current_stream, buf, len);
+       g_return_if_fail(glk_data->current_stream != NULL);
+       glk_put_buffer_stream(glk_data->current_stream, buf, len);
+}
+
+/**
+ * glk_put_buffer_uni:
+ * @buf: An array of Unicode code points.
+ * @len: Length of @buf.
+ *
+ * Prints a block of Unicode characters to the current stream. It is equivalent
+ * to a series of glk_put_char_uni() calls.
+ */
+void
+glk_put_buffer_uni(glui32 *buf, glui32 len)
+{
+       g_return_if_fail(glk_data->current_stream != NULL);
+       glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
 }
 
 /**
@@ -159,53 +212,30 @@ glk_put_buffer(char *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 <emphasis>
- * everything</emphasis> 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 (<code>"?"</code>) 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 s = g_new0(struct glk_stream_struct, 1);
-       s->rock = rock;
-       s->file_mode = fmode;
-       s->stream_type = STREAM_TYPE_MEMORY;
-       s->buffer = buf;
-       s->mark = 0;
-       s->buflen = buflen;
-       s->unicode = FALSE;
+       strid_t str = g_new0(struct glk_stream_struct, 1);
+       str->rock = rock;
+       str->file_mode = fmode;
+       str->type = STREAM_TYPE_MEMORY;
+       str->buffer = buf;
+       str->mark = 0;
+       str->buflen = buflen;
+       str->unicode = FALSE;
 
        /* Add it to the global stream list */
-       stream_list = g_list_prepend(stream_list, s);
-       s->stream_list = stream_list;
+       glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
+       str->stream_list = glk_data->stream_list;
 
-       return s;
+       return str;
 }
 
 /**
@@ -220,27 +250,28 @@ 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)
+glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
 {
        g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
        
-       strid_t s = g_new0(struct glk_stream_struct, 1);
-       s->rock = rock;
-       s->file_mode = fmode;
-       s->stream_type = STREAM_TYPE_MEMORY;
-       s->ubuffer = buf;
-       s->mark = 0;
-       s->buflen = buflen;
-       s->unicode = TRUE;
+       strid_t str = g_new0(struct glk_stream_struct, 1);
+       str->rock = rock;
+       str->file_mode = fmode;
+       str->type = STREAM_TYPE_MEMORY;
+       str->ubuffer = buf;
+       str->mark = 0;
+       str->buflen = buflen;
+       str->unicode = TRUE;
 
        /* Add it to the global stream list */
-       stream_list = g_list_prepend(stream_list, s);
-       s->stream_list = stream_list;
+       glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
+       str->stream_list = glk_data->stream_list;
 
-       return s;
+       return str;
 }
 
 /* Internal function: create a stream using the given parameters. */
@@ -269,7 +300,11 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
                        modestr = g_strdup(binary? "ab" : "a");
                        break;
                case filemode_ReadWrite:
-                       modestr = g_strdup(binary? "r+b" : "r+");
+                       if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
+                               modestr = g_strdup(binary? "r+b" : "r+");
+                       } else {
+                               modestr = g_strdup(binary? "w+b" : "w+");
+                       }
                        break;
                default:
                        g_warning("glk_stream_open_file: Invalid file mode");
@@ -286,32 +321,37 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
        /* If they opened a file in write mode but didn't specifically get
        permission to do so, complain if the file already exists */
        if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
+               gdk_threads_enter();
+
                GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
                        GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
                        "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);
                        return NULL;
                }
        }
        
-       strid_t s = g_new0(struct glk_stream_struct, 1);
-       s->rock = rock;
-       s->file_mode = fmode;
-       s->stream_type = STREAM_TYPE_FILE;
-       s->file_pointer = fp;
-       s->binary = binary;
-       s->unicode = unicode;
-       s->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
-       if(s->filename == NULL)
-               s->filename = g_strdup("Unknown file name"); /* fail silently */
+       strid_t str = g_new0(struct glk_stream_struct, 1);
+       str->rock = rock;
+       str->file_mode = fmode;
+       str->type = 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 */
-       stream_list = g_list_prepend(stream_list, s);
-       s->stream_list = stream_list;
+       glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
+       str->stream_list = glk_data->stream_list;
 
-       return s;
+       return str;
 }
 
 /**
@@ -327,14 +367,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
+ * (<code>"?"</code>) 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.
  */
@@ -355,7 +393,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.
  */
@@ -377,7 +419,9 @@ 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 <link
+ * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
+ * Closing, and Constraints</link>.
  */
 void 
 glk_stream_close(strid_t str, stream_result_t *result)
@@ -385,7 +429,7 @@ glk_stream_close(strid_t str, stream_result_t *result)
        g_return_if_fail(str != NULL);
        
        /* Free resources associated with one specific type of stream */
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_WINDOW:
                        g_warning("%s: Attempted to close a window stream. Use glk_window_"
@@ -407,18 +451,28 @@ glk_stream_close(strid_t str, stream_result_t *result)
                                __func__);
                        return;
        }
-       
+
+       stream_close_common(str, result);
+}
+
+/* Internal function: Stuff to do upon closing any type of stream. */
+void
+stream_close_common(strid_t str, stream_result_t *result)
+{
        /* Remove the stream from the global stream list */
-       stream_list = g_list_delete_link(stream_list, str->stream_list);
+       glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
+       
        /* If it was the current output stream, set that to NULL */
-       if(current_stream == str)
-               current_stream = NULL;
+       if(glk_data->current_stream == str)
+               glk_data->current_stream = NULL;
+               
        /* If it was one or more windows' echo streams, set those to NULL */
        winid_t win;
        for(win = glk_window_iterate(NULL, NULL); win; 
                win = glk_window_iterate(win, NULL))
                if(win->echo_stream == str)
                        win->echo_stream = NULL;
+                       
        /* Return the character counts */
        if(result) 
        {
@@ -427,4 +481,3 @@ glk_stream_close(strid_t str, stream_result_t *result)
        }
        g_free(str);
 }
-