Testen en debuggen van alle file en memory stream functies
authorPhilip Chimento <philip.chimento@gmail.com>
Sun, 17 Aug 2008 22:29:43 +0000 (22:29 +0000)
committerPhilip Chimento <philip.chimento@gmail.com>
Sun, 17 Aug 2008 22:29:43 +0000 (22:29 +0000)
git-svn-id: http://lassie.dyndns-server.com/svn/gargoyle-gtk@9 ddfedd41-794f-dd11-ae45-00112f111e67

src/fileref.c
src/model.c
src/stream.c
src/strio.c

index b277bb33f974c34be360c306b77df057dc41289c..1b7cf57cfc435d1af8fea1092d2c619e841dac94 100644 (file)
@@ -69,7 +69,7 @@ fileref_new(gchar *filename, glui32 rock, glui32 usage, glui32 orig_filemode)
        f->filename = g_strdup(filename);
        f->usage = usage;
        f->orig_filemode = orig_filemode;
-
+       
        /* Add it to the global fileref list */
        fileref_list = g_list_prepend(fileref_list, f);
        f->fileref_list = fileref_list;
index 6b8e758a9912db4582c801168e811425f0fecb5d..ce844bcb63e72ff1d911e5789b4563a093ec35a4 100644 (file)
@@ -12,27 +12,42 @@ void glk_main(void)
         return; 
     }
     
-    /* Set the current output stream to print to it. */
-    glk_set_window(mainwin);
-    
-    unsigned char buffer[256];
+    char buffer[256];
     int i;
     for(i = 0; i < 256; i++)
-       buffer[i] = glk_char_to_upper(i);
+       buffer[i] = (char)glk_char_to_upper(i);
+    
+    /*frefid_t f = glk_fileref_create_temp(fileusage_BinaryMode, 0);
+    if(f) 
+    {*/
     
-    glk_put_string("Philip en Marijn zijn vet goed.\n");
-    glk_put_buffer((gchar *)buffer, 256);
+    char memorybuffer[100];
     
-    frefid_t f = 
-       glk_fileref_create_by_prompt(fileusage_TextMode, filemode_Write, 0);
-    if(f) {
-               if( glk_fileref_does_file_exist(f) )
-                       glk_put_string("\n\nFile exists!\n");
-               else
-                       glk_put_string("\n\nFile does not exist!\n");
+       strid_t s = glk_stream_open_memory(memorybuffer, 100, 
+               filemode_ReadWrite, 0);
+       glk_stream_set_current(s);
+       glk_put_char('X');
+       glk_put_string("Philip en Marijn zijn vet goed.\n");
+       glk_put_buffer(buffer, 256);
+
+       glk_stream_set_position(s, 0, seekmode_Start);
+       glk_set_window(mainwin);
+       glk_put_char( glk_get_char_stream(s) );
+       glk_put_char('\n');
+       g_printerr("Line read: %d\n", glk_get_line_stream(s, buffer, 256));
+       glk_put_string(buffer);
+       int count = glk_get_buffer_stream(s, buffer, 256);
+       g_printerr("Buffer read: %d\n", count);
+       glk_put_buffer(buffer, count);          
+       
+       stream_result_t result;
+       glk_stream_close(s, &result);
+       
+       g_printerr("Read count: %d\nWrite count: %d\n", result.readcount,
+               result.writecount);
+/*
                glk_fileref_destroy(f);
-       } else
-               glk_put_string("\n\nCancel was clicked!\n");
+       }*/
        
        /* Bye bye */
        glk_exit();
index 28ea3ec91933981bf7554897da0d22196fe6cf8d..f7bee3243db3a94061afeecf44f138b4d60f4571 100644 (file)
@@ -81,7 +81,7 @@ glk_stream_get_rock(strid_t str)
 void
 glk_stream_set_current(strid_t str)
 {
-       if(str != NULL && str->file_mode != filemode_Write)
+       if(str != NULL && str->file_mode == filemode_Read)
        {
                g_warning("glk_stream_set_current: "
                        "Cannot set current stream to non output stream");
@@ -250,7 +250,8 @@ file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
        g_return_val_if_fail(fileref != NULL, NULL);
        
        gchar *modestr;
-       gboolean binary = fileref->usage & fileusage_BinaryMode;
+       /* Binary mode is 0x000, text mode 0x100 */
+       gboolean binary = !(fileref->usage & fileusage_TextMode);
        switch(fmode) 
        {
                case filemode_Read:
index 3db5e3e3ca6f85351794a6badb05399302a34b00..22a9dc3656de96561be4523d418b607d45546b1d 100644 (file)
 /* 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, gssize len)
+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++)
@@ -33,7 +37,7 @@ remove_latin1_control_characters(unsigned char *s, gssize len)
 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, gssize len)
+convert_latin1_to_utf8(gchar *s, gsize len)
 {
        GError *error = NULL;
        gchar *utf8;
@@ -83,8 +87,11 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                case wintype_TextBuffer:
                                {
                                        gchar *utf8 = convert_latin1_to_utf8(buf, len);
-                                       write_utf8_to_window(str->window, utf8);
-                                       g_free(utf8);
+                                       if(utf8)
+                                       {
+                                               write_utf8_to_window(str->window, utf8);
+                                               g_free(utf8);
+                                       }
                                }       
                                        str->write_count += len;
                                        break;
@@ -104,12 +111,13 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                        {
                                int foo = 0;
                                while(str->mark < str->buflen && foo < len)
-                                       str->ubuffer[str->mark++] = (glui32)buf[foo++];
+                                       str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
                        }
                        if(!str->unicode && str->buffer)
                        {
-                               memmove(str->buffer + str->mark, buf, 
-                                       min(len, str->buflen - str->mark));
+                               int copycount = min(len, str->buflen - str->mark);
+                               memmove(str->buffer + str->mark, buf, copycount);
+                               str->mark += copycount;
                        }
 
                        str->write_count += len;
@@ -235,7 +243,7 @@ read_utf8_char_from_file(FILE *fp)
                if(ch == EOF)
                        return -1;
                readbuffer[foo] = (gchar)ch;
-               charresult = g_utf8_get_char_validated(readbuffer, foo);
+               charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
                /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
                point otherwise */
        }
@@ -386,6 +394,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                if(str->buffer) /* if not, copycount stays 0 */
                                        copycount = min(len, str->buflen - str->mark);
                                memmove(buf, str->buffer + str->mark, copycount);
+                               str->mark += copycount;
                        }
 
                        str->read_count += copycount;           
@@ -509,8 +518,13 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                        else
                        {
                                if(str->buffer) /* if not, copycount stays 0 */
-                                       copycount = min(len, str->buflen - str->mark);
-                               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';
+                               str->mark += copycount;
                        }
                        
                        str->read_count += copycount;