Fix read count of glk_get_line_stream(_uni)
authorP. F. Chimento <philip.chimento@gmail.com>
Sat, 18 Jun 2011 16:51:57 +0000 (18:51 +0200)
committerP. F. Chimento <philip.chimento@gmail.com>
Sat, 18 Jun 2011 16:53:32 +0000 (18:53 +0200)
Newline was not included in read count. Fixes #29

libchimara/strio.c
tests/Makefile.am
tests/fileio.c [new file with mode: 0644]

index a6f15663294482e36a6eebd2275fd6313de4b0bc..24899c5e34c2516254897c44157f7a851949d478 100644 (file)
@@ -999,26 +999,26 @@ 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);
                                                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))
                                                {
-                                                       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 */
                                {
@@ -1141,26 +1141,26 @@ 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);
                                                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))
                                                {
-                                                       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 */
                                {
index f2f1e872d9676d4c6b556fe43009c1ffe8df7850..6d1217662a2392fbb77d70a012d6d9acb5b8ce80 100644 (file)
@@ -30,7 +30,7 @@ test_close_CFLAGS = @TEST_CFLAGS@ $(AM_CFLAGS)
 test_close_LDADD = @TEST_LIBS@ $(top_builddir)/libchimara/libchimara.la
 
 noinst_LTLIBRARIES = first.la model.la gridtest.la splittest.la multiwin.la \
-       styletest.la soundtest.la test-userstyle.la
+       styletest.la soundtest.la test-userstyle.la fileio.la
 
 first_la_SOURCES = first.c
 first_la_LDFLAGS = $(TEST_PLUGIN_LIBTOOL_FLAGS)
@@ -56,5 +56,9 @@ test_userstyle_la_LDFLAGS = $(TEST_PLUGIN_LIBTOOL_FLAGS)
 soundtest_la_SOURCES = soundtest.c
 soundtest_la_LDFLAGS = $(TEST_PLUGIN_LIBTOOL_FLAGS)
 
+fileio_la_SOURCES = fileio.c
+fileio_la_CFLAGS = @TEST_CFLAGS@ $(AM_CFLAGS)
+fileio_la_LDFLAGS = $(TEST_PLUGIN_LIBTOOL_FLAGS)
+
 
 -include $(top_srcdir)/git.mk
diff --git a/tests/fileio.c b/tests/fileio.c
new file mode 100644 (file)
index 0000000..ba5d80b
--- /dev/null
@@ -0,0 +1,35 @@
+/* Test for file I/O bug */
+
+#include <libchimara/glk.h>
+#include <glib.h>
+#include <string.h>
+
+#define MAGIC_STRING "Zapp\xF6licious.\n"
+#define BUFLEN 80
+
+void
+glk_main(void)
+{
+       char buffer[BUFLEN + 1];
+       
+       /* Open a temporary file */
+       frefid_t ref = glk_fileref_create_temp(fileusage_Data | fileusage_BinaryMode, 0);
+       strid_t file = glk_stream_open_file_uni(ref, filemode_Write, 0);
+       
+       /* Write the string to the file */
+       glk_put_string_stream(file, MAGIC_STRING);
+       
+       /* Close and check result counts */
+       stream_result_t counts;
+       glk_stream_close(file, &counts);
+       g_assert_cmpint(counts.readcount, ==, 0);
+       g_assert_cmpint(counts.writecount, ==, 14);
+       
+       file = glk_stream_open_file_uni(ref, filemode_Read, 0);
+       glui32 readcount = glk_get_line_stream(file, buffer, BUFLEN);
+       g_printerr("String: %s\n", buffer);
+       g_assert_cmpint(readcount, ==, strlen(buffer));
+       
+       glk_stream_close(file, &counts);
+       glk_fileref_destroy(ref);
+}