diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2021-11-22 09:47:19 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2021-11-25 08:43:59 +0100 |
commit | d3e90b58e7a5cbb1b74da98aed9dd4ec18c84d77 (patch) | |
tree | f018f982c97ec7f1785ab03f8d4ce56d07fc8a8f | |
parent | 9b8e0f5b12231d9983b1af9da156e84f4ef26fb0 (diff) | |
download | barebox-d3e90b58e7a5cbb1b74da98aed9dd4ec18c84d77.tar.gz barebox-d3e90b58e7a5cbb1b74da98aed9dd4ec18c84d77.tar.xz |
lib: wchar: add wctomb and mbtowc
We currently convert wchar_t to char by truncating to 8-bit.
In future, we may want to do UTF-16 to UTF-8 conversion.
Prepare for this by wrapping each conversion direction in a function.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20211122084732.2597109-18-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r-- | include/wchar.h | 5 | ||||
-rw-r--r-- | lib/wchar.c | 22 |
2 files changed, 25 insertions, 2 deletions
diff --git a/include/wchar.h b/include/wchar.h index b601cc6207..fb9b127a8c 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -17,4 +17,9 @@ char *strdup_wchar_to_char(const wchar_t *src); size_t wcslen(const wchar_t *s); +#define MB_CUR_MAX 4 + +int mbtowc(wchar_t *pwc, const char *s, size_t n); +int wctomb(char *s, wchar_t wc); + #endif /* __WCHAR_H */ diff --git a/lib/wchar.c b/lib/wchar.c index 4d49431e86..3be228b5a7 100644 --- a/lib/wchar.c +++ b/lib/wchar.c @@ -44,12 +44,30 @@ wchar_t *strdup_wchar(const wchar_t *src) return tmp; } +int mbtowc(wchar_t *pwc, const char *s, size_t n) +{ + if (!s) + return 0; /* we don't mantain a non-trivial shift state */ + + if (n < 1) + return -1; + + *pwc = *s; + return 1; +} + +int wctomb(char *s, wchar_t wc) +{ + *s = wc & 0xFF; + return 1; +} + char *strcpy_wchar_to_char(char *dst, const wchar_t *src) { char *ret = dst; while (*src) - *dst++ = *src++ & 0xff; + wctomb(dst++, *src++); *dst = 0; @@ -61,7 +79,7 @@ wchar_t *strcpy_char_to_wchar(wchar_t *dst, const char *src) wchar_t *ret = dst; while (*src) - *dst++ = *src++; + mbtowc(dst++, src++, 1); *dst = 0; |