Eliminated warnings about static functions declared with G_GNUC_INTERNAL
[projects/chimara/chimara.git] / src / strio.c
index 22a9dc3656de96561be4523d418b607d45546b1d..921b75cc771f0331828220061dbfc9030726fcd1 100644 (file)
@@ -1,81 +1,96 @@
+#include "charset.h"
+#include "magic.h"
 #include "stream.h"
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 
-#define min(x,y) ( (x > y)? y : x )
-
 /*
  *
  **************** WRITING FUNCTIONS ********************************************
  *
  */
 
-/* Internal function: change illegal (control) characters in a string to a
-placeholder character. Must free returned string afterwards. */
-static gchar *
-remove_latin1_control_characters(unsigned char *s, gsize len)
-{
-       /* If len == 0, then return an empty string, not NULL */
-       if(len == 0)
-               return g_strdup("");
-                       
-       gchar *retval = g_new0(gchar, len);
-       int i;
-       for(i = 0; i < len; i++)
-               if( (s[i] < 32 && s[i] != 10) || (s[i] >= 127 && s[i] <= 159) )
-                       retval[i] = '?';
-                       /* Our placeholder character is '?'; other options are possible,
-                       like printing "0x7F" or something */
-               else
-                       retval[i] = s[i];
-       return retval;
-}
-
-/* Internal function: convert a Latin-1 string to a UTF-8 string, replacing
-Latin-1 control characters by a placeholder first. The UTF-8 string must be
-freed afterwards. Returns NULL on error. */
-static gchar *
-convert_latin1_to_utf8(gchar *s, gsize len)
+/* Internal function: write a UTF-8 string to a text grid window's text buffer. */
+static void
+write_utf8_to_grid(winid_t win, gchar *s)
 {
-       GError *error = NULL;
-       gchar *utf8;
-       gchar *canonical = remove_latin1_control_characters( (unsigned char *)s,
-               len);
-       utf8 = g_convert(canonical, len, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
-       g_free(canonical);
-       
-       if(utf8 == NULL)
+       if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
        {
-               error_dialog(NULL, error, "Error during latin1->utf8 conversion: ");
-               return NULL;
+               ILLEGAL("Tried to print to a text grid window with line input pending.");
+               return;
        }
        
-       return utf8;
+    /* Number of characters to insert */
+    glong length = g_utf8_strlen(s, -1);
+    glong chars_left = length;
+    
+    gdk_threads_enter();
+    
+    GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
+    GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
+    
+    /* Get cursor position */
+    GtkTextIter start;
+    gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
+    /* Spaces available on this line */
+    gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
+    
+    while(chars_left > available_space && !gtk_text_iter_is_end(&start))
+    {
+        GtkTextIter end = start;
+        gtk_text_iter_forward_to_line_end(&end);
+        gtk_text_buffer_delete(buffer, &start, &end);
+        gtk_text_buffer_insert(buffer, &start, s + (length - chars_left), available_space);
+        chars_left -= available_space;
+        gtk_text_iter_forward_line(&start);
+        available_space = win->width;
+    }
+    if(!gtk_text_iter_is_end(&start))
+    {
+        GtkTextIter end = start;
+        gtk_text_iter_forward_chars(&end, chars_left);
+        gtk_text_buffer_delete(buffer, &start, &end);
+        gtk_text_buffer_insert(buffer, &start, s + (length - chars_left), -1);
+    }
+    
+    gtk_text_buffer_move_mark(buffer, cursor, &start);
+    
+    gdk_threads_leave();
 }
 
-/* Internal function: write a UTF-8 string to a window's text buffer. */
+/* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
 static void
 write_utf8_to_window(winid_t win, gchar *s)
 {
-       GtkTextBuffer *buffer = 
-               gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
+       if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
+       {
+               ILLEGAL("Tried to print to a text buffer window with line input pending.");
+               return;
+       }
+       
+       gdk_threads_enter();
+
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
 
        GtkTextIter iter;
        gtk_text_buffer_get_end_iter(buffer, &iter);
-       gtk_text_buffer_insert(buffer, &iter, s, -1);
+       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, s, -1, win->window_stream->style, NULL);
+
+       gdk_threads_leave();
 }
 
-/* Internal function: write a UTF-8 buffer with length to a stream. */
+/* Internal function: write a Latin-1 buffer with length to a stream. */
 static void
 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
 {
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_WINDOW:
                        /* Each window type has a different way of printing to it */
-                       switch(str->window->window_type)
+                       switch(str->window->type)
                        {
                                /* Printing to these windows' streams does nothing */
                                case wintype_Blank:
@@ -83,11 +98,26 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                case wintype_Graphics:
                                        str->write_count += len;
                                        break;
+                                       
+                           /* Text grid window */
+                           case wintype_TextGrid:
+                           {
+                               gchar *utf8 = convert_latin1_to_utf8(buf, len);
+                               if(utf8 != NULL)
+                               {
+                                   /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
+                                   write_utf8_to_grid(str->window, utf8);
+                                   g_free(utf8);
+                               }
+                           }
+                               str->write_count += len;
+                               break;
+                                       
                                /* Text buffer window */        
                                case wintype_TextBuffer:
                                {
                                        gchar *utf8 = convert_latin1_to_utf8(buf, len);
-                                       if(utf8)
+                                       if(utf8 != NULL)
                                        {
                                                write_utf8_to_window(str->window, utf8);
                                                g_free(utf8);
@@ -96,8 +126,7 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                        str->write_count += len;
                                        break;
                                default:
-                                       g_warning("%s: Writing to this kind of window unsupported.",
-                                               __func__);
+                                       ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
                        }
                        
                        /* Now write the same buffer to the window's echo stream */
@@ -115,7 +144,7 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                        }
                        if(!str->unicode && str->buffer)
                        {
-                               int copycount = min(len, str->buflen - str->mark);
+                               int copycount = MIN(len, str->buflen - str->mark);
                                memmove(str->buffer + str->mark, buf, copycount);
                                str->mark += copycount;
                        }
@@ -128,13 +157,9 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                        {
                                if(str->unicode) 
                                {
-                                       /* Convert to four-byte big-endian */
-                                       gchar *writebuffer = g_new0(gchar, len * 4);
-                                       int i;
-                                       for(i = 0; i < len; i++)
-                                               writebuffer[i * 4 + 3] = buf[i];
-                                       fwrite(writebuffer, sizeof(gchar), len * 4, 
-                                               str->file_pointer);
+                                       gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
+                                       fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
+                                       g_free(writebuffer);
                                } 
                                else /* Regular file */
                                {
@@ -144,15 +169,122 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                        else /* Text mode is the same for Unicode and regular files */
                        {
                                gchar *utf8 = convert_latin1_to_utf8(buf, len);
-                               g_fprintf(str->file_pointer, "%s", utf8);
-                               g_free(utf8);
+                               if(utf8 != NULL)
+                               {
+                                       g_fprintf(str->file_pointer, "%s", utf8);
+                                       g_free(utf8);
+                               }
+                       }
+                       
+                       str->write_count += len;
+                       break;
+               default:
+                       ILLEGAL_PARAM("Unknown stream type: %u", str->type);
+       }
+}
+
+/* Internal function: write a Unicode buffer with length to a stream. */
+static void
+write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
+{
+       switch(str->type)
+       {
+               case STREAM_TYPE_WINDOW:
+                       /* Each window type has a different way of printing to it */
+                       switch(str->window->type)
+                       {
+                               /* Printing to these windows' streams does nothing */
+                               case wintype_Blank:
+                               case wintype_Pair:
+                               case wintype_Graphics:
+                                       str->write_count += len;
+                                       break;
+                                       
+                           /* Text grid window */
+                           case wintype_TextGrid:
+                           {
+                               gchar *utf8 = convert_ucs4_to_utf8(buf, len);
+                               if(utf8 != NULL)
+                               {
+                                   /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
+                                   write_utf8_to_grid(str->window, utf8);
+                                   g_free(utf8);
+                               }
+                           }
+                               str->write_count += len;
+                               break;
+                                       
+                               /* Text buffer window */        
+                               case wintype_TextBuffer:
+                               {
+                                       gchar *utf8 = convert_ucs4_to_utf8(buf, len);
+                                       if(utf8 != NULL)
+                                       {
+                                               write_utf8_to_window(str->window, utf8);
+                                               g_free(utf8);
+                                       }
+                               }       
+                                       str->write_count += len;
+                                       break;
+                               default:
+                                       ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
+                       }
+                       
+                       /* Now write the same buffer to the window's echo stream */
+                       if(str->window->echo_stream != NULL)
+                               write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
+                       
+                       break;
+                       
+               case STREAM_TYPE_MEMORY:
+                       if(str->unicode && str->ubuffer)
+                       {
+                               int copycount = MIN(len, str->buflen - str->mark);
+                               memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
+                               str->mark += copycount;
+                       }
+                       if(!str->unicode && str->buffer)
+                       {
+                               gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
+                               int copycount = MIN(len, str->buflen - str->mark);
+                               memmove(str->buffer + str->mark, latin1, copycount);
+                               g_free(latin1);
+                               str->mark += copycount;
+                       }
+
+                       str->write_count += len;
+                       break;
+                       
+               case STREAM_TYPE_FILE:
+                       if(str->binary) 
+                       {
+                               if(str->unicode) 
+                               {
+                                       gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
+                                       fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
+                                       g_free(writebuffer);
+                               } 
+                               else /* Regular file */
+                               {
+                                       gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
+                                       fwrite(latin1, sizeof(gchar), len, str->file_pointer);
+                                       g_free(latin1);
+                               }
+                       }
+                       else /* Text mode is the same for Unicode and regular files */
+                       {
+                               gchar *utf8 = convert_ucs4_to_utf8(buf, len);
+                               if(utf8 != NULL) 
+                               {
+                                       g_fprintf(str->file_pointer, "%s", utf8);
+                                       g_free(utf8);
+                               }
                        }
                        
                        str->write_count += len;
                        break;
                default:
-                       g_warning("%s: Writing to this kind of stream unsupported.",
-                               __func__);
+                       ILLEGAL_PARAM("Unknown stream type: %u", str->type);
        }
 }
 
@@ -161,33 +293,76 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
  * @str: An output stream.
  * @ch: A character in Latin-1 encoding.
  *
- * Prints one character @ch to the stream @str. It is illegal for @str to be
- * %NULL, or an input-only stream.
+ * The same as glk_put_char(), except that you specify a stream @str to print 
+ * to, instead of using the current stream. It is illegal for @str to be %NULL,
+ * or an input-only stream.
  */
 void
 glk_put_char_stream(strid_t str, unsigned char ch)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
        write_buffer_to_stream(str, (gchar *)&ch, 1);
 }
 
+/**
+ * glk_put_char_stream_uni:
+ * @str: An output stream.
+ * @ch: A Unicode code point.
+ *
+ * The same as glk_put_char_uni(), except that you specify a stream @str to
+ * print to, instead of using the current stream. It is illegal for @str to be 
+ * %NULL, or an input-only stream.
+ */
+void
+glk_put_char_stream_uni(strid_t str, glui32 ch)
+{
+       VALID_STREAM(str, return);
+       g_return_if_fail(str->file_mode != filemode_Read);
+       
+       write_buffer_to_stream_uni(str, &ch, 1);
+}
+
 /**
  * glk_put_string_stream:
  * @str: An output stream.
  * @s: A null-terminated string in Latin-1 encoding.
  *
- * Prints @s to the stream @str. It is illegal for @str to be %NULL, or an
- * input-only stream.
+ * The same as glk_put_string(), except that you specify a stream @str to print 
+ * to, instead of using the current stream. It is illegal for @str to be %NULL,
+ * or an input-only stream.
  */
 void
 glk_put_string_stream(strid_t str, char *s)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
 
-       write_buffer_to_stream(str, (gchar *)s, strlen(s));
+       write_buffer_to_stream(str, s, strlen(s));
+}
+
+/**
+ * glk_put_string_stream_uni:
+ * @str: An output stream.
+ * @s: A null-terminated array of Unicode code points.
+ *
+ * The same as glk_put_string_uni(), except that you specify a stream @str to
+ * print to, instead of using the current stream. It is illegal for @str to be 
+ * %NULL, or an input-only stream.
+ */
+void
+glk_put_string_stream_uni(strid_t str, glui32 *s)
+{
+       VALID_STREAM(str, return);
+       g_return_if_fail(str->file_mode != filemode_Read);
+       
+       /* An impromptu strlen() for glui32 arrays */
+       glong len = 0;
+       glui32 *ptr = s;
+       while(*ptr++)
+               len++;
+       write_buffer_to_stream_uni(str, s, len);
 }
 
 /**
@@ -196,16 +371,36 @@ glk_put_string_stream(strid_t str, char *s)
  * @buf: An array of characters in Latin-1 encoding.
  * @len: Length of @buf.
  *
- * Prints @buf to the stream @str. It is illegal for @str to be %NULL, or an
- * input-only stream.
+ * The same as glk_put_buffer(), except that you specify a stream @str to print 
+ * to, instead of using the current stream. It is illegal for @str to be %NULL,
+ * or an input-only stream.
  */
 void
 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
+       g_return_if_fail(str->file_mode != filemode_Read);
+       
+       write_buffer_to_stream(str, buf, len);
+}
+
+/**
+ * glk_put_buffer_stream_uni:
+ * @str: An output stream.
+ * @buf: An array of Unicode code points.
+ * @len: Length of @buf.
+ *
+ * The same as glk_put_buffer_uni(), except that you specify a stream @str to
+ * print to, instead of using the current stream. It is illegal for @str to be 
+ * %NULL, or an input-only stream.
+ */
+void
+glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
+{
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
-       write_buffer_to_stream(str, (gchar *)buf, len);
+       write_buffer_to_stream_uni(str, buf, len);
 }
 
 /*
@@ -266,39 +461,20 @@ is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
                glsi32 ch2 = utf8? read_utf8_char_from_file(fp) : 
                        read_ucs4be_char_from_file(fp);
                if(ch2 != 0x0A)
-                       fseek(fp, utf8? -1 : -4, SEEK_CUR);
+                       if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
+                               WARNING_S("Seek failed on stream", g_strerror(errno) );
                return TRUE;
        }
        return FALSE;
 }
 
-/**
- * glk_get_char_stream:
- * @str: An input stream.
- *
- * Reads one character from the stream @str. (There is no notion of a ``current
- * input stream.'') It is illegal for @str to be %NULL, or an output-only
- * stream.
- *
- * The result will be between 0 and 255. As with all basic text functions, Glk
- * assumes the Latin-1 encoding. If the end of the stream has been reached, the
- * result will be -1. Note that high-bit characters (128..255) are
- * <emphasis>not</emphasis> returned as negative numbers.
- *
- * If the stream contains Unicode data --- for example, if it was created with
- * glk_stream_open_file_uni() or glk_stream_open_memory_uni() --- then
- * characters beyond 255 will be returned as 0x3F ("?").
- *
- * Returns: A character value between 0 and 255, or -1 on end of stream.
- */
-glsi32
-glk_get_char_stream(strid_t str)
+/* Internal function: Read one character from a stream. Returns a value which
+ can be returned unchanged by glk_get_char_stream_uni(), but 
+ glk_get_char_stream() must replace high values by the placeholder character. */
+static glsi32
+get_char_stream_common(strid_t str)
 {
-       g_return_val_if_fail(str != NULL, -1);
-       g_return_val_if_fail(str->file_mode == filemode_Read
-               || str->file_mode == filemode_ReadWrite, -1);
-       
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_MEMORY:
                        if(str->unicode)
@@ -307,13 +483,13 @@ glk_get_char_stream(strid_t str)
                                        return -1;
                                glui32 ch = str->ubuffer[str->mark++];
                                str->read_count++;
-                               return (ch > 0xFF)? 0x3F : ch;
+                               return ch;
                        }
                        else
                        {
                                if(!str->buffer || str->mark >= str->buflen)
                                        return -1;
-                               char ch = str->buffer[str->mark++];
+                               unsigned char ch = str->buffer[str->mark++];
                                str->read_count++;
                                return ch;
                        }
@@ -328,7 +504,7 @@ glk_get_char_stream(strid_t str)
                                        if(ch == -1)
                                                return -1;
                                        str->read_count++;
-                                       return (ch > 0xFF)? 0x3F : ch;
+                                       return ch;
                                }
                                else /* Regular file */
                                {
@@ -347,15 +523,70 @@ glk_get_char_stream(strid_t str)
                                        return -1;
                                        
                                str->read_count++;
-                               return (ch > 0xFF)? 0x3F : ch;
+                               return ch;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.",
-                               __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return -1;
        }
 }
 
+/**
+ * glk_get_char_stream:
+ * @str: An input stream.
+ *
+ * Reads one character from the stream @str. (There is no notion of a
+ * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
+ * an output-only stream.
+ *
+ * The result will be between 0 and 255. As with all basic text functions, Glk
+ * assumes the Latin-1 encoding. See <link 
+ * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
+ * of the stream has been reached, the result will be -1. 
+ *
+ * <note><para>
+ *   Note that high-bit characters (128..255) are <emphasis>not</emphasis>
+ *   returned as negative numbers.
+ * </para></note>
+ *
+ * If the stream contains Unicode data &mdash; for example, if it was created
+ * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() &mdash; then
+ * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
+ *
+ * It is usually more efficient to read several characters at once with
+ * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
+ * glk_get_char_stream() several times.
+ *
+ * Returns: A character value between 0 and 255, or -1 on end of stream.
+ */
+glsi32
+glk_get_char_stream(strid_t str)
+{
+       VALID_STREAM(str, return -1);
+       g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
+       
+       glsi32 ch = get_char_stream_common(str);
+       return (ch > 0xFF)? PLACEHOLDER : ch;
+}
+
+/**
+ * glk_get_char_stream_uni:
+ * @str: An input stream.
+ *
+ * Reads one character from the stream @str. The result will be between 0 and 
+ * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
+ *
+ * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
+ */
+glsi32
+glk_get_char_stream_uni(strid_t str)
+{
+       VALID_STREAM(str, return -1);
+       g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
+       
+       return get_char_stream_common(str);
+}
+
 /**
  * glk_get_buffer_stream:
  * @str: An input stream.
@@ -370,20 +601,18 @@ glk_get_char_stream(strid_t str)
 glui32
 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
 {
-       g_return_val_if_fail(str != NULL, 0);
-       g_return_val_if_fail(str->file_mode == filemode_Read
-               || str->file_mode == filemode_ReadWrite, 0);
+       VALID_STREAM(str, return 0);
+       g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
        g_return_val_if_fail(buf != NULL, 0);
        
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_MEMORY:
                {
                        int copycount = 0;
                        if(str->unicode)
                        {
-                               while(copycount < len && str->ubuffer 
-                                       && str->mark < str->buflen) 
+                               while(copycount < len && str->ubuffer && str->mark < str->buflen) 
                                {
                                        glui32 ch = str->ubuffer[str->mark++];
                                        buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
@@ -392,7 +621,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                        else
                        {
                                if(str->buffer) /* if not, copycount stays 0 */
-                                       copycount = min(len, str->buflen - str->mark);
+                                       copycount = MIN(len, str->buflen - str->mark);
                                memmove(buf, str->buffer + str->mark, copycount);
                                str->mark += copycount;
                        }
@@ -407,17 +636,14 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                {
                                        /* Read len characters of 4 bytes each */
                                        unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
-                                       size_t count = fread(readbuffer, sizeof(unsigned char), 
-                                               4 * len, str->file_pointer);
+                                       size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
                                        /* If there was an incomplete character */
                                        if(count % 4 != 0) 
                                        {
                                                count -= count % 4;
-                                               g_warning("%s: Incomplete character in binary Unicode "
-                                                       "file.", __func__);
+                                               WARNING("Incomplete character in binary Unicode file");
                                        }
                                        
-                                       str->read_count += count / 4;
                                        int foo;
                                        for(foo = 0; foo < count; foo += 4)
                                        {
@@ -428,12 +654,12 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                                buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
                                        }
                                        g_free(readbuffer);
+                                       str->read_count += count / 4;
                                        return count / 4;
                                }
                                else /* Regular binary file */
                                {
-                                       size_t count = fread(buf, sizeof(char), len, 
-                                               str->file_pointer);
+                                       size_t count = fread(buf, sizeof(char), len, str->file_pointer);
                                        str->read_count += count;
                                        return count;
                                }
@@ -453,8 +679,106 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                return foo;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.",
-                               __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
+                       return 0;
+       }
+}
+
+/**
+ * glk_get_buffer_stream_uni:
+ * @str: An input stream.
+ * @buf: A buffer with space for at least @len Unicode code points.
+ * @len: The number of characters to read.
+ *
+ * Reads @len Unicode characters from @str, unless the end of stream is reached 
+ * first. No terminal null is placed in the buffer.
+ *
+ * Returns: The number of Unicode characters actually read.
+ */
+glui32
+glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
+{
+       VALID_STREAM(str, return 0);
+       g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
+       g_return_val_if_fail(buf != NULL, 0);
+       
+       switch(str->type)
+       {
+               case STREAM_TYPE_MEMORY:
+               {
+                       int copycount = 0;
+                       if(str->unicode)
+                       {
+                               if(str->ubuffer) /* if not, copycount stays 0 */
+                                       copycount = MIN(len, str->buflen - str->mark);
+                               memmove(buf, str->ubuffer + str->mark, copycount * 4);
+                               str->mark += copycount;
+                       }
+                       else
+                       {
+                               while(copycount < len && str->buffer && str->mark < str->buflen)
+                               {
+                                       unsigned char ch = str->buffer[str->mark++];
+                                       buf[copycount++] = ch;
+                               }
+                       }
+
+                       str->read_count += copycount;           
+                       return copycount;
+               }       
+               case STREAM_TYPE_FILE:
+                       if(str->binary) 
+                       {
+                               if(str->unicode) /* Binary file with 4-byte characters */
+                               {
+                                       /* Read len characters of 4 bytes each */
+                                       unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
+                                       size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
+                                       /* If there was an incomplete character */
+                                       if(count % 4 != 0) 
+                                       {
+                                               count -= count % 4;
+                                               WARNING("Incomplete character in binary Unicode file");
+                                       }
+                                       
+                                       int foo;
+                                       for(foo = 0; foo < count; foo += 4)
+                                               buf[foo / 4] = readbuffer[foo] << 24
+                                                       | readbuffer[foo + 1] << 16
+                                                       | readbuffer[foo + 2] << 8
+                                                       | readbuffer[foo + 3];
+                                       g_free(readbuffer);
+                                       str->read_count += count / 4;
+                                       return count / 4;
+                               }
+                               else /* Regular binary file */
+                               {
+                                       unsigned char *readbuffer = g_new0(unsigned char, len);
+                                       size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
+                                       int foo;
+                                       for(foo = 0; foo < count; foo++)
+                                               buf[foo] = readbuffer[foo];
+                                       g_free(readbuffer);
+                                       str->read_count += count;
+                                       return count;
+                               }
+                       }
+                       else /* Text mode is the same for Unicode and regular files */
+                       {
+                               /* Do it character-by-character */
+                               int foo;
+                               for(foo = 0; foo < len; foo++)
+                               {
+                                       glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                                       if(ch == -1)
+                                               break;
+                                       str->read_count++;
+                                       buf[foo] = ch;
+                               }
+                               return foo;
+                       }
+               default:
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -465,26 +789,26 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
  * @buf: A buffer with space for at least @len characters.
  * @len: The number of characters to read, plus one.
  *
- * Reads characters from @str, until either @len - 1 characters have been read
- * or a newline has been read. It then puts a terminal null ('\0') aracter on
+ * Reads characters from @str, until either 
+ * <inlineequation>
+ *   <alt>@len - 1</alt>
+ *   <mathphrase>@len - 1</mathphrase>
+ * </inlineequation>
+ * characters have been read or a newline has been read. It then puts a
+ * terminal null (<code>'\0'</code>) aracter on
  * the end. It returns the number of characters actually read, including the
  * newline (if there is one) but not including the terminal null.
  *
- * It is usually more efficient to read several characters at once with
- * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
- * glk_get_char_stream() several times.
- *
  * Returns: The number of characters actually read.
  */
 glui32
 glk_get_line_stream(strid_t str, char *buf, glui32 len)
 {
-       g_return_val_if_fail(str != NULL, 0);
-       g_return_val_if_fail(str->file_mode == filemode_Read
-               || str->file_mode == filemode_ReadWrite, 0);
+       VALID_STREAM(str, return 0);
+       g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
        g_return_val_if_fail(buf != NULL, 0);
 
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_MEMORY:
                {
@@ -492,14 +816,12 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                        if(str->unicode)
                        {
                                /* Do it character-by-character */
-                               while(copycount < len - 1 && str->ubuffer 
-                                       && str->mark < str->buflen) 
+                               while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen) 
                                {
                                        glui32 ch = str->ubuffer[str->mark++];
                                        /* Check for Unicode newline; slightly different than
                                        in file streams */
-                                       if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 
-                                               || ch == 0x2029)
+                                       if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
                                        {
                                                buf[copycount++] = '\n';
                                                break;
@@ -518,9 +840,8 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                        else
                        {
                                if(str->buffer) /* if not, copycount stays 0 */
-                                       copycount = min(len - 1, str->buflen - str->mark);
-                               char *endptr = memccpy(buf, str->buffer + str->mark, '\n',
-                                       copycount);
+                                       copycount = MIN(len - 1, str->buflen - str->mark);
+                               char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
                                if(endptr) /* newline was found */
                                        copycount = endptr - buf; /* Real copy count */
                                buf[copycount] = '\0';
@@ -539,8 +860,7 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                                        int foo;
                                        for(foo = 0; foo < len - 1; foo++)
                                        {
-                                               glsi32 ch = 
-                                                       read_ucs4be_char_from_file(str->file_pointer);
+                                               glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
                                                if(ch == -1) 
                                                {
                                                        buf[foo] = '\0';
@@ -590,8 +910,149 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                                return foo;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.",
-                               __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
+                       return 0;
+       }
+}
+
+/**
+ * glk_get_line_stream_uni:
+ * @str: An input stream.
+ * @buf: A buffer with space for at least @len Unicode code points.
+ * @len: The number of characters to read, plus one.
+ *
+ * Reads Unicode characters from @str, until either 
+ * <inlineequation>
+ *   <alt>@len - 1</alt>
+ *   <mathphrase>@len - 1</mathphrase>
+ * </inlineequation> 
+ * Unicode characters have been read or a newline has been read. It then puts a
+ * terminal null (a zero value) on the end.
+ *
+ * Returns: The number of characters actually read, including the newline (if
+ * there is one) but not including the terminal null.
+ */
+glui32
+glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
+{
+       VALID_STREAM(str, return 0);
+       g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
+       g_return_val_if_fail(buf != NULL, 0);
+
+       switch(str->type)
+       {
+               case STREAM_TYPE_MEMORY:
+               {
+                       int copycount = 0;
+                       if(str->unicode)
+                       {
+                               /* Do it character-by-character */
+                               while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen) 
+                               {
+                                       glui32 ch = str->ubuffer[str->mark++];
+                                       /* Check for Unicode newline; slightly different than
+                                       in file streams */
+                                       if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
+                                       {
+                                               buf[copycount++] = '\n';
+                                               break;
+                                       }
+                                       if(ch == 0x0D)
+                                       {
+                                               if(str->ubuffer[str->mark] == 0x0A)
+                                                       str->mark++; /* skip past next newline */
+                                               buf[copycount++] = '\n';
+                                               break;
+                                       }
+                                       buf[copycount++] = ch;
+                               }
+                               buf[copycount] = '\0';
+                       }
+                       else
+                       {
+                               /* No recourse to memccpy(), so do it character-by-character */
+                               while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
+                               {
+                                       gchar ch = str->buffer[str->mark++];
+                                       /* Check for newline */
+                                       if(ch == '\n') /* Also check for \r and \r\n? */
+                                       {
+                                               buf[copycount++] = '\n';
+                                               break;
+                                       }
+                                       buf[copycount++] = (unsigned char)ch;
+                               }
+                               buf[copycount] = 0;
+                       }
+                       
+                       str->read_count += copycount;
+                       return copycount;
+               }       
+               case STREAM_TYPE_FILE:
+                       if(str->binary) 
+                       {
+                               if(str->unicode) /* Binary file with 4-byte characters */
+                               {
+                                       /* Do it character-by-character */
+                                       int foo;
+                                       for(foo = 0; foo < len - 1; foo++)
+                                       {
+                                               glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
+                                               if(ch == -1) 
+                                               {
+                                                       buf[foo] = 0;
+                                                       return foo - 1;
+                                               }
+                                               str->read_count++;
+                                               if(is_unicode_newline(ch, str->file_pointer, FALSE))
+                                               {
+                                                       buf[foo] = ch; /* Preserve newline types??? */
+                                                       buf[foo + 1] = 0;
+                                                       return foo;
+                                               }
+                                               buf[foo] = ch;
+                                       }
+                                       buf[len] = 0;
+                                       return foo;
+                               }
+                               else /* Regular binary file */
+                               {
+                                       gchar *readbuffer = g_new0(gchar, len);
+                                       fgets(readbuffer, len, str->file_pointer);
+                                       glui32 count = strlen(readbuffer) + 1; /* Copy terminator */
+                                       int foo;
+                                       for(foo = 0; foo < count; foo++)
+                                               buf[foo] = (unsigned char)(readbuffer[foo]);
+                                       str->read_count += count;
+                                       return count;
+                               }
+                       }
+                       else /* Text mode is the same for Unicode and regular files */
+                       {
+                               /* Do it character-by-character */
+                               int foo;
+                               for(foo = 0; foo < len - 1; foo++)
+                               {
+                                       glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                                       if(ch == -1)
+                                       {
+                                               buf[foo] = 0;
+                                               return foo - 1;
+                                       }
+                                       str->read_count++;
+                                       if(is_unicode_newline(ch, str->file_pointer, TRUE))
+                                       {
+                                               buf[foo] = ch; /* Preserve newline types??? */
+                                               buf[foo + 1] = 0;
+                                               return foo;
+                                       }
+                                       buf[foo] = ch;
+                               }
+                               buf[len] = 0;
+                               return foo;
+                       }
+               default:
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -620,22 +1081,26 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
  * a 32-bit word. So in a binary Unicode file, positions are multiples of four
  * bytes.
  *
+ * <note><para>
+ *   If this bothers you, don't use binary Unicode files. I don't think they're
+ *   good for much anyhow.
+ * </para></note>
+ *
  * Returns: position of the read/write mark in @str.
  */
 glui32
 glk_stream_get_position(strid_t str)
 {
-       g_return_val_if_fail(str != NULL, 0);
+       VALID_STREAM(str, return 0);
        
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_MEMORY:
                        return str->mark;
                case STREAM_TYPE_FILE:
                        return ftell(str->file_pointer);
                default:
-                       g_warning("%s: Seeking not supported on this type of stream.",
-                               __func__);
+                       ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -647,27 +1112,21 @@ glk_stream_get_position(strid_t str)
  * @seekmode: One of #seekmode_Start, #seekmode_Current, or #seekmode_End.
  *
  * Sets the position of the read/write mark in @str. The position is controlled
- * by @pos, and the meaning of @pos is controlled by @seekmode:
- * <itemizedlist>
- *  <listitem>#seekmode_Start: @pos characters after the beginning of the file.
- *  </listitem>
- *  <listitem>#seekmode_Current: @pos characters after the current position
- *  (moving backwards if @pos is negative.)</listitem>
- *  <listitem>#seekmode_End: @pos characters after the end of the file. (@pos
- *  should always be zero or negative, so that this will move backwards to a
- *  position within the file.</listitem>
- * </itemizedlist>
+ * by @pos, and the meaning of @pos is controlled by @seekmode. See the
+ * <code>seekmode_</code> constants below.
+ *
  * It is illegal to specify a position before the beginning or after the end of
  * the file.
  *
- * In binary files, the mark position is exact --- it corresponds with the
+ * In binary files, the mark position is exact &mdash; it corresponds with the
  * number of characters you have read or written. In text files, this mapping 
  * can vary, because of linefeed conventions or other character-set 
- * approximations. glk_stream_set_position() and glk_stream_get_position()
- * measure positions in the platform's native encoding --- after character
- * cookery. Therefore, in a text stream, it is safest to use
- * glk_stream_set_position() only to move to the beginning or end of a file, or
- * to a position determined by glk_stream_get_position().
+ * approximations. See <link linkend="chimara-Streams">Streams</link>.
+ * glk_stream_set_position() and glk_stream_get_position() measure positions in
+ * the platform's native encoding &mdash; after character cookery. Therefore,
+ * in a text stream, it is safest to use glk_stream_set_position() only to move
+ * to the beginning or end of a file, or to a position determined by
+ * glk_stream_get_position().
  *
  * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
  * characters are 32-bit words, or four bytes each.
@@ -675,11 +1134,11 @@ glk_stream_get_position(strid_t str)
 void
 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
        g_return_if_fail(!(seekmode == seekmode_End || pos > 0));
        
-       switch(str->stream_type)
+       switch(str->type)
        {
                case STREAM_TYPE_MEMORY:
                        switch(seekmode)
@@ -688,7 +1147,7 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
                                case seekmode_Current: str->mark += pos; break;
                                case seekmode_End:     str->mark = str->buflen + pos; break;
                                default:
-                                       g_assert_not_reached();
+                                       g_return_if_reached();
                                        return;
                        }
                        break;
@@ -701,16 +1160,16 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
                                case seekmode_Current: whence = SEEK_CUR; break;
                                case seekmode_End:     whence = SEEK_END; break;
                                default:
-                                       g_assert_not_reached();
+                                       g_return_if_reached();
                                        return;
                        }
-                       fseek(str->file_pointer, pos, whence);
+                       if(fseek(str->file_pointer, pos, whence) == -1)
+                               WARNING("Seek failed on file stream");
                        break;
                }
                default:
-                       g_warning("%s: Seeking not supported on this type of stream.",
-                               __func__);
+                       ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
                        return;
        }
 }
+