aboutsummaryrefslogtreecommitdiff
path: root/host/matelight
diff options
context:
space:
mode:
authorjaseg <jaseg@jaseg.net>2014-01-02 16:40:26 +0100
committerjaseg <jaseg@jaseg.net>2014-01-02 16:40:26 +0100
commit3e1895b71c26267b87a0ccfa1d5248f5a207c47f (patch)
treeb4d5401474c4fab4c09a0e088930350559d11827 /host/matelight
parent2ce31fb1333ce6a8ffe62c49cb9f42aac5547ae2 (diff)
downloadmatelight-3e1895b71c26267b87a0ccfa1d5248f5a207c47f.tar.gz
matelight-3e1895b71c26267b87a0ccfa1d5248f5a207c47f.tar.bz2
matelight-3e1895b71c26267b87a0ccfa1d5248f5a207c47f.zip
Fixed multibyte handling.
Diffstat (limited to 'host/matelight')
-rw-r--r--host/matelight/font.h4
-rw-r--r--host/matelight/main.c10
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