diff options
-rw-r--r-- | host/matelight/font.h | 4 | ||||
-rw-r--r-- | host/matelight/main.c | 10 |
2 files changed, 13 insertions, 1 deletions
diff --git a/host/matelight/font.h b/host/matelight/font.h index 658609a..e3e2e60 100644 --- a/host/matelight/font.h +++ b/host/matelight/font.h @@ -1,4 +1,7 @@ +#ifndef __FONT_H__ +#define __FONT_H__ + #include <stdint.h> #include <stdio.h> @@ -19,3 +22,4 @@ int read_bdf(FILE *f, glyph_t **glyph_table, unsigned int glyph_table_size); // Requires buf to point to a buffer at least of size glyph->width*glyph->height. void render_glyph(glyph_t *glyph, uint8_t *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy); +#endif//__FONT_H__ diff --git a/host/matelight/main.c b/host/matelight/main.c index a32706c..7a636fc 100644 --- a/host/matelight/main.c +++ b/host/matelight/main.c @@ -6,6 +6,7 @@ #include <errno.h> #include <string.h> #include <wchar.h> +#include <locale.h> /* CAUTION: REQUIRES INPUT TO BE \0-TERMINATED */ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size){ @@ -23,8 +24,14 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size printf(" (%s)\n", s); wchar_t c; + mbstate_t ps = {0}; + memset(&ps, 0, sizeof(mbstate_t)); + if(!setlocale(LC_CTYPE, "en_US.utf8")){ + fprintf(stderr, "Cannot set locale\n"); + goto error; + } for(;;){ - size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL); + size_t inc = mbrtowc(&c, p, MB_CUR_MAX, &ps); // MB_CUR_MAX is safe since p is \0-terminated printf("Converted %lx (%x) remaining length %d to %lc rv %d\n", p, (unsigned char)*p, (s+len+1)-p, c, inc); if(inc == -1 || inc == -2){ fprintf(stderr, "Error rendering string: No valid UTF-8 input.\n"); @@ -63,6 +70,7 @@ int console_render(char *s, glyph_t **glyph_table, unsigned int glyph_table_size unsigned int x = 0; p = s; + memset(&ps, 0, sizeof(mbstate_t)); for(;;){ size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL); // If p contained |