X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=printer.c;h=7e6e8f5d1582941048d66deabe2797f6ecb15a8f;hb=37f1ce3da5d6b4d308af26d117805fa8b357e277;hp=29705e622378cd86a3c40aaf96b1cc512076d676;hpb=1b580cf22ca849edc62afa24304ff0432954877a;p=cparser diff --git a/printer.c b/printer.c index 29705e6..7e6e8f5 100644 --- a/printer.c +++ b/printer.c @@ -26,6 +26,11 @@ static FILE* out; +static void print_char_file(const char c) +{ + fputc(c, out); +} + static void print_string_file(const char *str) { fputs(str, out); @@ -36,26 +41,6 @@ static void print_vformat_file(const char *format, va_list ap) vfprintf(out, format, ap); } -static void print_char_file(wchar_rep_t c) -{ - const unsigned tc = (unsigned) c; - if (tc < 0x80) { - fputc(tc, out); - } else if (tc < 0x800) { - fputc(0xC0 | (tc >> 6), out); - fputc(0x80 | (tc & 0x3F), out); - } else if (tc < 0x10000) { - fputc(0xE0 | ( tc >> 12), out); - fputc(0x80 | ((tc >> 6) & 0x3F), out); - fputc(0x80 | ( tc & 0x3F), out); - } else { - fputc(0xF0 | ( tc >> 18), out); - fputc(0x80 | ((tc >> 12) & 0x3F), out); - fputc(0x80 | ((tc >> 6) & 0x3F), out); - fputc(0x80 | ( tc & 0x3F), out); - } -} - void print_to_file(FILE *new_out) { out = new_out; @@ -68,6 +53,11 @@ void print_to_file(FILE *new_out) static struct obstack *obst; +static void print_char_obstack(const char c) +{ + obstack_1grow(obst, c); +} + static void print_string_obstack(const char *str) { size_t len = strlen(str); @@ -79,26 +69,6 @@ static void print_vformat_obstack(const char *format, va_list ap) obstack_vprintf(obst, format, ap); } -static void print_char_obstack(wchar_rep_t c) -{ - const unsigned tc = (unsigned) c; - if (tc < 0x80) { - obstack_1grow(obst, tc); - } else if (tc < 0x800) { - obstack_1grow(obst, 0xC0 | (tc >> 6)); - obstack_1grow(obst, 0x80 | (tc & 0x3F)); - } else if (tc < 0x10000) { - obstack_1grow(obst, 0xE0 | ( tc >> 12)); - obstack_1grow(obst, 0x80 | ((tc >> 6) & 0x3F)); - obstack_1grow(obst, 0x80 | ( tc & 0x3F)); - } else { - obstack_1grow(obst, 0xF0 | ( tc >> 18)); - obstack_1grow(obst, 0x80 | ((tc >> 12) & 0x3F)); - obstack_1grow(obst, 0x80 | ((tc >> 6) & 0x3F)); - obstack_1grow(obst, 0x80 | ( tc & 0x3F)); - } -} - void print_to_obstack(struct obstack *new_obst) { obst = new_obst; @@ -112,54 +82,31 @@ void print_to_obstack(struct obstack *new_obst) static char *buffer_pos; static char *buffer_end; -static void print_string_buffer(const char *str) -{ - for (const char *c = str; *c != '\0'; ++c) { - if (buffer_pos == buffer_end) { - return; - } - *buffer_pos++ = *c; - } -} - -static void print_vformat_buffer(const char *format, va_list ap) -{ - size_t size = buffer_end - buffer_pos + 1; - int written = vsnprintf(buffer_pos, size, format, ap); - buffer_pos += written; -} - -static inline void buffer_add_char(int c) +static void print_char_buffer(const char c) { if (buffer_pos == buffer_end) return; *buffer_pos++ = c; } -static void print_char_buffer(wchar_rep_t c) +static void print_string_buffer(const char *str) { - const unsigned tc = (unsigned) c; - if (tc < 0x80) { - buffer_add_char(tc); - } else if (tc < 0x800) { - buffer_add_char(0xC0 | (tc >> 6)); - buffer_add_char(0x80 | (tc & 0x3F)); - } else if (tc < 0x10000) { - buffer_add_char(0xE0 | ( tc >> 12)); - buffer_add_char(0x80 | ((tc >> 6) & 0x3F)); - buffer_add_char(0x80 | ( tc & 0x3F)); - } else { - buffer_add_char(0xF0 | ( tc >> 18)); - buffer_add_char(0x80 | ((tc >> 12) & 0x3F)); - buffer_add_char(0x80 | ((tc >> 6) & 0x3F)); - buffer_add_char(0x80 | ( tc & 0x3F)); + for (const char *c = str; *c != '\0'; ++c) { + print_char_buffer(*c); } } +static void print_vformat_buffer(const char *format, va_list ap) +{ + size_t size = buffer_end - buffer_pos; + size_t written = (size_t) vsnprintf(buffer_pos, size, format, ap); + buffer_pos += written < size ? written : size; +} + void print_to_buffer(char *buffer, size_t buffer_size) { buffer_pos = buffer; - buffer_end = buffer + buffer_size - 1; + buffer_end = buffer + buffer_size - 2; print_string = print_string_buffer; print_vformat = print_vformat_buffer; @@ -176,7 +123,7 @@ void finish_print_to_buffer(void) void (*print_string)(const char *str) = print_string_file; void (*print_vformat)(const char *format, va_list ap) = print_vformat_file; -void (*print_char)(wchar_rep_t c) = print_char_file; +void (*print_char)(const char c) = print_char_file; void printer_push(void) {