From 0f339252697e6dcfc9e00be6cd8272d4260b90d2 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Thu, 5 Apr 2018 12:50:58 +0200 Subject: manual: Move mbstouwcs to an example C file --- manual/examples/mbstouwcs.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 manual/examples/mbstouwcs.c (limited to 'manual/examples') diff --git a/manual/examples/mbstouwcs.c b/manual/examples/mbstouwcs.c new file mode 100644 index 0000000000..5d223da2ae --- /dev/null +++ b/manual/examples/mbstouwcs.c @@ -0,0 +1,28 @@ +#include +#include +#include + +/* Do not include the above headers in the example. +*/ +wchar_t * +mbstouwcs (const char *s) +{ + size_t len = strlen (s); + wchar_t *result = malloc ((len + 1) * sizeof (wchar_t)); + wchar_t *wcp = result; + wchar_t tmp[1]; + mbstate_t state; + size_t nbytes; + + memset (&state, '\0', sizeof (state)); + while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0) + { + if (nbytes >= (size_t) -2) + /* Invalid input string. */ + return NULL; + *wcp++ = towupper (tmp[0]); + len -= nbytes; + s += nbytes; + } + return result; +} -- cgit 1.4.1