Change I/O signals to pass a unique window ID
[projects/chimara/chimara.git] / libchimara / strio.c
index f668cb9484637431ad849f62246b0365cadc5c79..2a2f231d18a6733cf5ae4abdd43bfcb9bf703ccf 100644 (file)
@@ -1,3 +1,4 @@
+#include <config.h>
 #include "charset.h"
 #include "magic.h"
 #include "stream.h"
@@ -6,6 +7,24 @@
 #include <string.h>
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <glib/gi18n-lib.h>
+
+/* Internal function: ensure that an fseek() is called on a file pointer in
+ between reading and writing operations, and vice versa. This will only come up
+ for ReadWrite or WriteAppend files. */
+static void
+ensure_file_operation(strid_t str, glui32 op)
+{
+       if(str->lastop != 0 && str->lastop != op)
+       {
+               long pos = ftell(str->file_pointer);
+               if(pos == -1)
+                       WARNING_S("ftell() failed", g_strerror(errno));
+               if(fseek(str->file_pointer, pos, SEEK_SET) != 0)
+                       WARNING_S("fseek() failed", g_strerror(errno));
+       }
+       str->lastop = op; /* Not 0, because we are about to do the operation anyway */
+}
 
 /*
  *
@@ -26,11 +45,14 @@ write_utf8_to_window_buffer(winid_t win, gchar *s)
        // Write to the buffer  
        g_string_append(win->buffer, s);
 }
-       
+
 /* Internal function: flush a window's text buffer to the screen. */
 void
 flush_window_buffer(winid_t win)
 {
+#ifdef DEBUG_STYLES
+       g_printf("%s\n", win->buffer->str);
+#endif
        if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
                return;
 
@@ -44,24 +66,18 @@ flush_window_buffer(winid_t win)
        switch(win->type) {
        case wintype_TextBuffer:
        {
-               GtkTextIter iter;
-               gtk_text_buffer_get_end_iter(buffer, &iter);
-
-               GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
-               GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
-
-               if(win->window_stream->hyperlink_mode) {
-                       GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
-                       GtkTextTag *link_tag = win->current_hyperlink->tag;
-                       gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, style_tag, link_style_tag, link_tag, NULL);
-               } else {
-                       gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, style_tag, NULL);
-               }
+               GtkTextIter start, end;
+               gtk_text_buffer_get_end_iter(buffer, &end);
+               gint start_offset;
+
+               start_offset = gtk_text_iter_get_offset(&end);
+               gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
+               gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
+               style_apply(win, &start, &end);
 
                ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
                g_assert(glk);
-               g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
-
+               g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->librock, win->buffer->str);
        }
                break;
 
@@ -74,51 +90,36 @@ flush_window_buffer(winid_t win)
                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 start, insert;
+               gint start_offset;
+
+               gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
+
+               while(chars_left > 0 && !gtk_text_iter_is_end(&insert))
                {
-                       GtkTextIter end = start;
-                       gtk_text_iter_forward_to_line_end(&end);
-                       gtk_text_buffer_delete(buffer, &start, &end);
-
-                       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
-                       GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
-
-                       if(win->window_stream->hyperlink_mode) {
-                               GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
-                               GtkTextTag *link_tag = win->current_hyperlink->tag;
-                               gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, link_style_tag, link_tag, NULL);
-                       } else {
-                               gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, NULL);
-                       }
+                       /* Spaces available on this line */
+                       gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
+               
+                       GtkTextIter end = insert;
+                       if(chars_left <= available_space)
+                               gtk_text_iter_forward_chars(&end, chars_left);
+                       else
+                               gtk_text_iter_forward_to_line_end(&end);
 
+                       gtk_text_buffer_delete(buffer, &insert, &end);
+
+                       start_offset = gtk_text_iter_get_offset(&insert);
+                       gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), MIN(chars_left, available_space));
+                       gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
+                       style_apply(win, &start, &insert);
+                       
                        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);
-
-                       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
-                       GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
-
-                       if(win->window_stream->hyperlink_mode) {
-                               GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
-                               GtkTextTag *link_tag = win->current_hyperlink->tag;
-                               gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, link_style_tag, link_tag, NULL);
-                       } else {
-                               gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, NULL);
-                       }
+
+                       if(gtk_text_iter_get_line_offset(&insert) >= win->width)
+                               gtk_text_iter_forward_line(&insert);
                }
-               
-               gtk_text_buffer_move_mark(buffer, cursor, &start);
+
+               gtk_text_buffer_move_mark(buffer, cursor, &insert);
        }
                break;
        }
@@ -147,6 +148,44 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                        
                            /* Text grid/buffer windows */
                            case wintype_TextGrid:
+                               {
+                               gchar *utf8 = convert_latin1_to_utf8(buf, len);
+                               if(utf8 != NULL) {
+                                               /* Deal with newlines */
+                                               int i;
+                                               gchar *line = utf8;
+                                               for(i=0; i<len; i++) {
+                                                       if(utf8[i] == '\n') {
+                                                               utf8[i] = '\0';
+                                                               write_utf8_to_window_buffer(str->window, line);
+                                                               flush_window_buffer(str->window);
+
+                                                               /* Move cursor position forward to the next line */
+                                                               gdk_threads_enter();
+                                                               GtkTextIter cursor_pos;
+                                                               GtkTextView *textview = GTK_TEXT_VIEW(str->window->widget);
+                                                               GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
+                                                               GtkTextMark *cursor_mark = gtk_text_buffer_get_mark(buffer, "cursor_position");
+
+                                                           gtk_text_buffer_get_iter_at_mark( buffer, &cursor_pos, cursor_mark);
+                                                               gtk_text_view_forward_display_line(textview, &cursor_pos);
+                                                               gtk_text_view_backward_display_line_start(textview, &cursor_pos);
+                                                               gtk_text_buffer_move_mark(buffer, cursor_mark, &cursor_pos);
+                                                               gdk_threads_leave();
+
+                                                               line = utf8 + (i < len-1 ? (i+1):(len-1));
+                                                       }
+                                               }
+                                                               
+                                               /* No more newlines left. */
+                                               write_utf8_to_window_buffer(str->window, line);
+                                               g_free(utf8);
+                                       }
+
+                                       str->write_count += len;
+                               }
+                                       break;
+
                                case wintype_TextBuffer:
                            {
                                gchar *utf8 = convert_latin1_to_utf8(buf, len);
@@ -181,6 +220,10 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                str->mark += copycount;
                        }
 
+                       /* Move the EOF marker if we wrote past it */
+                       if(str->mark > str->endmark)
+                               str->endmark = str->mark;
+
                        str->write_count += len;
                        break;
                        
@@ -190,11 +233,13 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                if(str->unicode) 
                                {
                                        gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
+                                       ensure_file_operation(str, filemode_Write);
                                        fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
                                        g_free(writebuffer);
                                } 
                                else /* Regular file */
                                {
+                                       ensure_file_operation(str, filemode_Write);
                                        fwrite(buf, sizeof(gchar), len, str->file_pointer);
                                }
                        }
@@ -203,6 +248,7 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                gchar *utf8 = convert_latin1_to_utf8(buf, len);
                                if(utf8 != NULL)
                                {
+                                       ensure_file_operation(str, filemode_Write);
                                        g_fprintf(str->file_pointer, "%s", utf8);
                                        g_free(utf8);
                                }
@@ -210,6 +256,9 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                        
                        str->write_count += len;
                        break;
+               case STREAM_TYPE_RESOURCE:
+                       ILLEGAL(_("Writing to a resource stream is illegal."));
+                       break;
                default:
                        ILLEGAL_PARAM("Unknown stream type: %u", str->type);
        }
@@ -258,7 +307,6 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                        if(str->unicode && str->ubuffer)
                        {
                                int copycount = MIN(len, str->buflen - str->mark);
-                               g_printerr("Attempting to copy %d units; %d units of space left in buffer\n", len, str->buflen - str->mark);
                                memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
                                str->mark += copycount;
                        }
@@ -266,12 +314,15 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                        {
                                gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
                                int copycount = MIN(len, str->buflen - str->mark);
-                               g_printerr("Attempting to copy %d units; %d units of space left in buffer\n", len, str->buflen - str->mark);
                                memmove(str->buffer + str->mark, latin1, copycount);
                                g_free(latin1);
                                str->mark += copycount;
                        }
 
+                       /* Move the EOF marker if we wrote past it */
+                       if(str->mark > str->endmark)
+                               str->endmark = str->mark;
+
                        str->write_count += len;
                        break;
                        
@@ -281,12 +332,14 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                if(str->unicode) 
                                {
                                        gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
+                                       ensure_file_operation(str, filemode_Write);
                                        fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
                                        g_free(writebuffer);
                                } 
                                else /* Regular file */
                                {
                                        gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
+                                       ensure_file_operation(str, filemode_Write);
                                        fwrite(latin1, sizeof(gchar), len, str->file_pointer);
                                        g_free(latin1);
                                }
@@ -296,6 +349,7 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                gchar *utf8 = convert_ucs4_to_utf8(buf, len);
                                if(utf8 != NULL) 
                                {
+                                       ensure_file_operation(str, filemode_Write);
                                        g_fprintf(str->file_pointer, "%s", utf8);
                                        g_free(utf8);
                                }
@@ -303,6 +357,9 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                        
                        str->write_count += len;
                        break;
+               case STREAM_TYPE_RESOURCE:
+                       ILLEGAL(_("Writing to a resource stream is illegal."));
+                       break;
                default:
                        ILLEGAL_PARAM("Unknown stream type: %u", str->type);
        }
@@ -444,10 +501,11 @@ glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
 /* Internal function: Read one big-endian four-byte character from file fp and
 return it as a Unicode code point, or -1 on EOF */
 static glsi32
-read_ucs4be_char_from_file(FILE *fp)
+read_ucs4be_char_from_file(strid_t str)
 {
        unsigned char readbuffer[4];
-       if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
+       ensure_file_operation(str, filemode_Read);
+       if(fread(readbuffer, sizeof(unsigned char), 4, str->file_pointer) < 4)
                return -1; /* EOF */
        return
                readbuffer[0] << 24 | 
@@ -459,14 +517,15 @@ read_ucs4be_char_from_file(FILE *fp)
 /* Internal function: Read one UTF-8 character, which may be more than one byte,
 from file fp and return it as a Unicode code point, or -1 on EOF */
 static glsi32
-read_utf8_char_from_file(FILE *fp)
+read_utf8_char_from_file(strid_t str)
 {
        gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
        int foo;
        gunichar charresult = (gunichar)-2;
+       ensure_file_operation(str, filemode_Read);
        for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++) 
        {
-               int ch = fgetc(fp);
+               int ch = fgetc(str->file_pointer);
                if(ch == EOF)
                        return -1;
                readbuffer[foo] = (gchar)ch;
@@ -480,21 +539,75 @@ read_utf8_char_from_file(FILE *fp)
        return charresult;
 }
 
+/* Internal function: Read one UTF-8 character, which may be more than one byte,
+from a memory stream @str, and return it as a Unicode code point. */
+static glsi32
+read_utf8_char_from_buffer(strid_t str)
+{
+       size_t foo;
+       gunichar charresult = (gunichar)-2;
+       char *buffer = str->buffer + str->mark;
+       size_t maxlen = str->buflen - str->mark;
+
+       if(maxlen == 0)
+               return -1;
+
+       for(foo = 1; foo <= maxlen; foo++)
+       {
+               charresult = g_utf8_get_char_validated(buffer, foo);
+               /* charresult is -1 if invalid, -2 if incomplete, and the
+               Unicode code point otherwise */
+               if(charresult != (gunichar)-2)
+                       break;
+       }
+       str->mark += foo;
+       str->read_count++;
+
+       /* Return -1 on EOS */
+       if(charresult == (gunichar)-2)
+               return -1;
+       /* Silently return unknown characters as 0xFFFD, Replacement Character */
+       if(charresult == (gunichar)-1)
+               return 0xFFFD;
+       return charresult;
+}
+
+/* Internal function: Read one big-endian four-byte character from memory and
+return it as a Unicode code point, or -1 on EOF */
+static glsi32
+read_ucs4be_char_from_buffer(strid_t str)
+{
+       glui32 ch = str->buffer[str->mark++];
+       if(str->mark >= str->buflen)
+               return -1;
+       ch = (ch << 8) | (str->buffer[str->mark++] & 0xFF);
+       if(str->mark >= str->buflen)
+               return -1;
+       ch = (ch << 8) | (str->buffer[str->mark++] & 0xFF);
+       if(str->mark >= str->buflen)
+               return -1;
+       ch = (ch << 8) | (str->buffer[str->mark++] & 0xFF);
+       str->read_count++;
+       return ch;
+}
+
 /* Internal function: Tell whether this code point is a Unicode newline. The
 file pointer and eight-bit flag are included in case the newline is a CR 
 (U+000D). If the next character is LF (U+000A) then it also belongs to the
 newline. */
 static gboolean
-is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
+is_unicode_newline(glsi32 ch, strid_t str, gboolean utf8)
 {
        if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
                return TRUE;
        if(ch == 0x0D) {
-               glsi32 ch2 = utf8? read_utf8_char_from_file(fp) : 
-                       read_ucs4be_char_from_file(fp);
-               if(ch2 != 0x0A)
-                       if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
+               glsi32 ch2 = utf8? read_utf8_char_from_file(str) :
+                       read_ucs4be_char_from_file(str);
+               if(ch2 != 0x0A) {
+                       if(fseek(str->file_pointer, utf8? -1 : -4, SEEK_CUR) == -1)
                                WARNING_S("Seek failed on stream", g_strerror(errno) );
+                       str->lastop = 0; /* can read or write after a seek */
+               }
                return TRUE;
        }
        return FALSE;
@@ -508,6 +621,18 @@ get_char_stream_common(strid_t str)
 {
        switch(str->type)
        {
+               case STREAM_TYPE_RESOURCE:
+                       if(str->unicode)
+                       {
+                               if(!str->buffer || str->mark >= str->buflen)
+                                       return -1;
+                               if(str->binary)
+                                       /* Cheap big-endian stream */
+                                       return read_ucs4be_char_from_buffer(str);
+                               /* slightly less cheap UTF8 stream */
+                               return read_utf8_char_from_buffer(str);
+                       }
+                       /* for text streams, fall through to memory case */
                case STREAM_TYPE_MEMORY:
                        if(str->unicode)
                        {
@@ -532,7 +657,7 @@ get_char_stream_common(strid_t str)
                        {
                                if(str->unicode) 
                                {
-                                       glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
+                                       glsi32 ch = read_ucs4be_char_from_file(str);
                                        if(ch == -1)
                                                return -1;
                                        str->read_count++;
@@ -540,6 +665,7 @@ get_char_stream_common(strid_t str)
                                }
                                else /* Regular file */
                                {
+                                       ensure_file_operation(str, filemode_Read);
                                        int ch = fgetc(str->file_pointer);
                                        if(ch == EOF)
                                                return -1;
@@ -550,7 +676,7 @@ get_char_stream_common(strid_t str)
                        }
                        else /* Text mode is the same for Unicode and regular files */
                        {
-                               glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                               glsi32 ch = read_utf8_char_from_file(str);
                                if(ch == -1)
                                        return -1;
                                        
@@ -605,8 +731,8 @@ glk_get_char_stream(strid_t str)
  * 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.
+ * Reads one character from the stream @str. 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.
  */
@@ -639,6 +765,24 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
        
        switch(str->type)
        {
+               case STREAM_TYPE_RESOURCE:
+               {
+                       int copycount = 0;
+                       if(str->unicode)
+                       {
+                               while(copycount < len && str->buffer && str->mark < str->buflen)
+                               {
+                                       glui32 ch;
+                                       if(str->binary)
+                                               ch = read_ucs4be_char_from_buffer(str);
+                                       else
+                                               ch = read_utf8_char_from_buffer(str);
+                                       buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
+                               }
+                               return copycount;
+                       }
+                       /* for text streams, fall through to memory case */
+               }
                case STREAM_TYPE_MEMORY:
                {
                        int copycount = 0;
@@ -668,6 +812,7 @@ 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);
+                                       ensure_file_operation(str, filemode_Read);
                                        size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
                                        /* If there was an incomplete character */
                                        if(count % 4 != 0) 
@@ -691,6 +836,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                }
                                else /* Regular binary file */
                                {
+                                       ensure_file_operation(str, filemode_Read);
                                        size_t count = fread(buf, sizeof(char), len, str->file_pointer);
                                        str->read_count += count;
                                        return count;
@@ -702,7 +848,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                int foo;
                                for(foo = 0; foo < len; foo++)
                                {
-                                       glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                                       glsi32 ch = read_utf8_char_from_file(str);
                                        if(ch == -1)
                                                break;
                                        str->read_count++;
@@ -736,6 +882,24 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
        
        switch(str->type)
        {
+               case STREAM_TYPE_RESOURCE:
+               {
+                       int copycount = 0;
+                       if(str->unicode)
+                       {
+                               while(copycount < len && str->buffer && str->mark < str->buflen)
+                               {
+                                       glui32 ch;
+                                       if(str->binary)
+                                               ch = read_ucs4be_char_from_buffer(str);
+                                       else
+                                               ch = read_utf8_char_from_buffer(str);
+                                       buf[copycount++] = ch;
+                               }
+                               return copycount;
+                       }
+                       /* for text streams, fall through to memory case */
+               }
                case STREAM_TYPE_MEMORY:
                {
                        int copycount = 0;
@@ -765,6 +929,7 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                {
                                        /* Read len characters of 4 bytes each */
                                        unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
+                                       ensure_file_operation(str, filemode_Read);
                                        size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
                                        /* If there was an incomplete character */
                                        if(count % 4 != 0) 
@@ -786,6 +951,7 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                else /* Regular binary file */
                                {
                                        unsigned char *readbuffer = g_new0(unsigned char, len);
+                                       ensure_file_operation(str, filemode_Read);
                                        size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
                                        int foo;
                                        for(foo = 0; foo < count; foo++)
@@ -801,7 +967,7 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                int foo;
                                for(foo = 0; foo < len; foo++)
                                {
-                                       glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                                       glsi32 ch = read_utf8_char_from_file(str);
                                        if(ch == -1)
                                                break;
                                        str->read_count++;
@@ -827,7 +993,7 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
  *   <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
+ * terminal null (<code>'\0'</code>) character on
  * the end. It returns the number of characters actually read, including the
  * newline (if there is one) but not including the terminal null.
  *
@@ -842,6 +1008,40 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
 
        switch(str->type)
        {
+               case STREAM_TYPE_RESOURCE:
+               {
+                       int copycount = 0;
+                       if(str->unicode)
+                       {
+                               /* Do it character-by-character */
+                               while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
+                               {
+                                       glsi32 ch;
+                                       if(str->binary)
+                                               ch = read_ucs4be_char_from_buffer(str);
+                                       else
+                                               ch = read_utf8_char_from_buffer(str);
+                                       /* 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->buffer[str->mark] == 0x0A)
+                                                       str->mark++; /* skip past next newline */
+                                               buf[copycount++] = '\n';
+                                               break;
+                                       }
+                                       buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
+                               }
+                               buf[copycount] = '\0';
+                               return copycount;
+                       }
+                       /* for text streams, fall through to the memory case */
+               }
                case STREAM_TYPE_MEMORY:
                {
                        int copycount = 0;
@@ -889,29 +1089,30 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                                if(str->unicode) /* Binary file with 4-byte characters */
                                {
                                        /* Do it character-by-character */
-                                       int foo;
-                                       for(foo = 0; foo < len - 1; foo++)
+                                       int copycount;
+                                       for(copycount = 0; copycount < len - 1; copycount++)
                                        {
-                                               glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
+                                               glsi32 ch = read_ucs4be_char_from_file(str);
                                                if(ch == -1) 
                                                {
-                                                       buf[foo] = '\0';
-                                                       return foo - 1;
+                                                       buf[copycount] = '\0';
+                                                       return copycount;
                                                }
                                                str->read_count++;
-                                               if(is_unicode_newline(ch, str->file_pointer, FALSE))
+                                               if(is_unicode_newline(ch, str, FALSE))
                                                {
-                                                       buf[foo] = '\n';
-                                                       buf[foo + 1] = '\0';
-                                                       return foo;
+                                                       buf[copycount++] = '\n';
+                                                       buf[copycount] = '\0';
+                                                       return copycount;
                                                }
-                                               buf[foo] = (ch > 0xFF)? '?' : (char)ch;
+                                               buf[copycount] = (ch > 0xFF)? '?' : (char)ch;
                                        }
                                        buf[len] = '\0';
-                                       return foo;
+                                       return copycount;
                                }
                                else /* Regular binary file */
                                {
+                                       ensure_file_operation(str, filemode_Read);
                                        if( !fgets(buf, len, str->file_pointer) ) {
                                                *buf = 0;
                                                return 0;
@@ -928,14 +1129,14 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                                int foo;
                                for(foo = 0; foo < len - 1; foo++)
                                {
-                                       glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                                       glsi32 ch = read_utf8_char_from_file(str);
                                        if(ch == -1)
                                        {
                                                buf[foo] = '\0';
                                                return foo - 1;
                                        }
                                        str->read_count++;
-                                       if(is_unicode_newline(ch, str->file_pointer, TRUE))
+                                       if(is_unicode_newline(ch, str, TRUE))
                                        {
                                                buf[foo] = '\n';
                                                buf[foo + 1] = '\0';
@@ -978,6 +1179,40 @@ glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
 
        switch(str->type)
        {
+               case STREAM_TYPE_RESOURCE:
+               {
+                       int copycount = 0;
+                       if(str->unicode)
+                       {
+                               /* Do it character-by-character */
+                               while(copycount < len - 1 && str->buffer && str->mark < str->buflen) 
+                               {
+                                       glsi32 ch;
+                                       if(str->binary)
+                                               ch = read_ucs4be_char_from_buffer(str);
+                                       else
+                                               ch = read_utf8_char_from_buffer(str);
+                                       /* 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';
+                               return copycount;
+                       }
+                       /* for text streams, fall through to the memory case */
+               }
                case STREAM_TYPE_MEMORY:
                {
                        int copycount = 0;
@@ -1031,30 +1266,31 @@ glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                if(str->unicode) /* Binary file with 4-byte characters */
                                {
                                        /* Do it character-by-character */
-                                       int foo;
-                                       for(foo = 0; foo < len - 1; foo++)
+                                       int copycount;
+                                       for(copycount = 0; copycount < len - 1; copycount++)
                                        {
-                                               glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
+                                               glsi32 ch = read_ucs4be_char_from_file(str);
                                                if(ch == -1) 
                                                {
-                                                       buf[foo] = 0;
-                                                       return foo - 1;
+                                                       buf[copycount] = 0;
+                                                       return copycount;
                                                }
                                                str->read_count++;
-                                               if(is_unicode_newline(ch, str->file_pointer, FALSE))
+                                               if(is_unicode_newline(ch, str, FALSE))
                                                {
-                                                       buf[foo] = ch; /* Preserve newline types??? */
-                                                       buf[foo + 1] = 0;
-                                                       return foo;
+                                                       buf[copycount++] = ch; /* Preserve newline types??? */
+                                                       buf[copycount] = 0;
+                                                       return copycount;
                                                }
-                                               buf[foo] = ch;
+                                               buf[copycount] = ch;
                                        }
                                        buf[len] = 0;
-                                       return foo;
+                                       return copycount;
                                }
                                else /* Regular binary file */
                                {
                                        gchar *readbuffer = g_new0(gchar, len);
+                                       ensure_file_operation(str, filemode_Read);
                                        if( !fgets(readbuffer, len, str->file_pointer) ) {
                                                *buf = 0;
                                                return 0;
@@ -1074,14 +1310,14 @@ glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                int foo;
                                for(foo = 0; foo < len - 1; foo++)
                                {
-                                       glsi32 ch = read_utf8_char_from_file(str->file_pointer);
+                                       glsi32 ch = read_utf8_char_from_file(str);
                                        if(ch == -1)
                                        {
                                                buf[foo] = 0;
                                                return foo - 1;
                                        }
                                        str->read_count++;
-                                       if(is_unicode_newline(ch, str->file_pointer, TRUE))
+                                       if(is_unicode_newline(ch, str, TRUE))
                                        {
                                                buf[foo] = ch; /* Preserve newline types??? */
                                                buf[foo + 1] = 0;
@@ -1127,6 +1363,14 @@ glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
  *   good for much anyhow.
  * </para></note>
  *
+ * glk_stream_get_position() on a window stream will always return zero.
+ *
+ * <note><para>
+ *   It might make more sense to return the number of characters written to the
+ *   window, but existing libraries do not support this and it's not really
+ *   worth adding the feature.
+ * </para></note>
+ *
  * Returns: position of the read/write mark in @str.
  */
 glui32
@@ -1137,9 +1381,12 @@ glk_stream_get_position(strid_t str)
        switch(str->type)
        {
                case STREAM_TYPE_MEMORY:
+               case STREAM_TYPE_RESOURCE:
                        return str->mark;
                case STREAM_TYPE_FILE:
                        return ftell(str->file_pointer);
+               case STREAM_TYPE_WINDOW:
+                       return 0;
                default:
                        ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
                        return 0;
@@ -1171,6 +1418,9 @@ glk_stream_get_position(strid_t str)
  *
  * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
  * characters are 32-bit words, or four bytes each.
+ *
+ * A window stream doesn't have a movable mark, so calling
+ * glk_stream_set_position() has no effect.
  */
 void
 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
@@ -1181,12 +1431,13 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
        
        switch(str->type)
        {
+               case STREAM_TYPE_RESOURCE:
                case STREAM_TYPE_MEMORY:
                        switch(seekmode)
                        {
                                case seekmode_Start:   str->mark = pos;  break;
                                case seekmode_Current: str->mark += pos; break;
-                               case seekmode_End:     str->mark = str->buflen + pos; break;
+                               case seekmode_End:     str->mark = str->endmark + pos; break;
                                default:
                                        g_return_if_reached();
                                        return;
@@ -1206,8 +1457,11 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
                        }
                        if(fseek(str->file_pointer, pos, whence) == -1)
                                WARNING("Seek failed on file stream");
+                       str->lastop = 0; /* Either reading or writing is legal after fseek() */
                        break;
                }
+               case STREAM_TYPE_WINDOW:
+                       break; /* Quietly do nothing */
                default:
                        ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
                        return;