Keep an EOF mark for memory streams
authorP. F. Chimento <philip.chimento@gmail.com>
Sat, 18 Jun 2011 20:45:11 +0000 (22:45 +0200)
committerP. F. Chimento <philip.chimento@gmail.com>
Sat, 18 Jun 2011 20:45:11 +0000 (22:45 +0200)
Seeking to seekmode_End in a memory stream should move relative to
the last written position in the buffer, not the end of the memory
buffer. This brings the behavior of memory streams into line with
what would happen in a file stream.

Fixes #30.

libchimara/stream.c
libchimara/stream.h
libchimara/strio.c

index 0113b989edfe9245cbf66d025758e42f35141ca4..96ab73fb08dcc97c8857541c0d91bafedbe7b33b 100644 (file)
@@ -284,6 +284,7 @@ glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
        str->file_mode = fmode;
        str->type = STREAM_TYPE_MEMORY;
        str->mark = 0;
+       str->endmark = 0;
        str->unicode = FALSE;
 
        if(buf && buflen) 
@@ -322,6 +323,7 @@ glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock
        str->file_mode = fmode;
        str->type = STREAM_TYPE_MEMORY;
        str->mark = 0;
+       str->endmark = 0;
        str->unicode = TRUE;
 
        if(buf && buflen) 
index 325b668bb22c96527b3199af9ee892342d875113..7ff7618c61ef44dc78a75ebc1b60033f45c81718 100644 (file)
@@ -42,6 +42,7 @@ struct glk_stream_struct
        gchar *buffer;
        glui32 *ubuffer;
        glui32 mark;
+       glui32 endmark;
        glui32 buflen;
        gidispatch_rock_t buffer_rock;
        /* Specific to file streams */
index 039e189a5d8676b6c34506ebeb63a02add52ab9e..b0d0fd16582dbc8f480391cf8288e081605ec01f 100644 (file)
@@ -310,6 +310,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;
                        
@@ -402,6 +406,10 @@ write_buffer_to_stream_uni(strid_t str, glui32 *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;
                        
@@ -1343,7 +1351,7 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 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;