From: fliep Date: Sun, 17 Aug 2008 22:29:43 +0000 (+0000) Subject: Testen en debuggen van alle file en memory stream functies X-Git-Tag: v0.9~456 X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=6a9f71ee1be9500a421a96014627f569e115ecf1;p=projects%2Fchimara%2Fchimara.git Testen en debuggen van alle file en memory stream functies --- diff --git a/src/fileref.c b/src/fileref.c index b277bb3..1b7cf57 100644 --- a/src/fileref.c +++ b/src/fileref.c @@ -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; diff --git a/src/model.c b/src/model.c index 6b8e758..ce844bc 100644 --- a/src/model.c +++ b/src/model.c @@ -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(); diff --git a/src/stream.c b/src/stream.c index 28ea3ec..f7bee32 100644 --- a/src/stream.c +++ b/src/stream.c @@ -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: diff --git a/src/strio.c b/src/strio.c index 3db5e3e..22a9dc3 100644 --- a/src/strio.c +++ b/src/strio.c @@ -15,8 +15,12 @@ /* 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;