From ed5dac111cfdb3805d259fd0a1997c36c8807722 Mon Sep 17 00:00:00 2001 From: "P. F. Chimento" Date: Sat, 18 Jun 2011 18:51:57 +0200 Subject: [PATCH] Fix read count of glk_get_line_stream(_uni) Newline was not included in read count. Fixes #29 --- libchimara/strio.c | 36 ++++++++++++++++++------------------ tests/Makefile.am | 6 +++++- tests/fileio.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 tests/fileio.c diff --git a/libchimara/strio.c b/libchimara/strio.c index a6f1566..24899c5 100644 --- a/libchimara/strio.c +++ b/libchimara/strio.c @@ -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 */ { diff --git a/tests/Makefile.am b/tests/Makefile.am index f2f1e87..6d12176 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000..ba5d80b --- /dev/null +++ b/tests/fileio.c @@ -0,0 +1,35 @@ +/* Test for file I/O bug */ + +#include +#include +#include + +#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); +} -- 2.30.2