X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=src%2Fstrio.c;h=de6ccb48f5ed9057cb15dd5606078b7b527a1a77;hb=1d98f490554273292d4ce29e4ea42e843c2f0c20;hp=3db5e3e3ca6f85351794a6badb05399302a34b00;hpb=d7b854580e288ecab39bc1ab40818060037ebdc9;p=projects%2Fchimara%2Fchimara.git diff --git a/src/strio.c b/src/strio.c index 3db5e3e..de6ccb4 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; @@ -55,12 +59,16 @@ convert_latin1_to_utf8(gchar *s, gssize len) static void write_utf8_to_window(winid_t win, gchar *s) { + gdk_threads_enter(); + GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) ); GtkTextIter iter; gtk_text_buffer_get_end_iter(buffer, &iter); gtk_text_buffer_insert(buffer, &iter, s, -1); + + gdk_threads_leave(); } /* Internal function: write a UTF-8 buffer with length to a stream. */ @@ -83,8 +91,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 +115,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 +247,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 +398,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 +522,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;