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 */
{
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 */
{
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)
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
--- /dev/null
+/* 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);
+}