From 83ea4dfd306469d4ddb2050f1c41909e9aa5619e Mon Sep 17 00:00:00 2001 From: cancel Date: Sat, 4 Jan 2020 00:26:06 +0900 Subject: [PATCH] Change cboard_pase() to use fread instead of fgetc --- cboard.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/cboard.c b/cboard.c index 7cf44c3..e219eb8 100644 --- a/cboard.c +++ b/cboard.c @@ -24,22 +24,24 @@ Cboard_error cboard_paste(Glyph* gbuffer, Usz height, Usz width, Usz y, Usz x) { Usz start_x = x; if (!fp) return Cboard_error_popen_failed; + char inbuff[512]; for (;;) { - int c = fgetc(fp); - if (c == EOF) - break; - if (c == '\r' || c == '\n') { - y++; - x = start_x; - continue; - } - if (c != ' ' && y < height && x < width) { - Glyph g = c <= CHAR_MAX && c >= CHAR_MIN && is_valid_glyph((Glyph)c) - ? (Glyph)c - : '.'; - gbuffer_poke(gbuffer, height, width, y, x, g); + size_t n = fread(inbuff, 1, sizeof inbuff, fp); + for (size_t i = 0; i < n; i++) { + char c = inbuff[i]; + if (c == '\r' || c == '\n') { + y++; + x = start_x; + continue; + } + if (c != ' ' && y < height && x < width) { + Glyph g = is_valid_glyph((Glyph)c) ? (Glyph)c : '.'; + gbuffer_poke(gbuffer, height, width, y, x, g); + } + x++; } - x++; + if (n < sizeof inbuff) + break; } int status = pclose(fp); return status ? Cboard_error_process_exit_error : Cboard_error_none;