|
Packit |
33f14e |
/* Convert multibyte character to wide character.
|
|
Packit |
33f14e |
Copyright (C) 2011-2017 Free Software Foundation, Inc.
|
|
Packit |
33f14e |
Written by Bruno Haible <bruno@clisp.org>, 2011.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
33f14e |
it under the terms of the GNU General Public License as published by
|
|
Packit |
33f14e |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
33f14e |
(at your option) any later version.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is distributed in the hope that it will be useful,
|
|
Packit |
33f14e |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
33f14e |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
33f14e |
GNU General Public License for more details.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
You should have received a copy of the GNU General Public License
|
|
Packit |
33f14e |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* We don't need a static internal state, because the encoding is not state
|
|
Packit |
33f14e |
dependent, and when mbrtowc returns (size_t)(-2). we throw the result
|
|
Packit |
33f14e |
away. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
int
|
|
Packit |
33f14e |
mbtowc (wchar_t *pwc, const char *s, size_t n)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
if (s == NULL)
|
|
Packit |
33f14e |
return 0;
|
|
Packit |
33f14e |
else
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
mbstate_t state;
|
|
Packit |
33f14e |
wchar_t wc;
|
|
Packit |
33f14e |
size_t result;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
memset (&state, 0, sizeof (mbstate_t));
|
|
Packit |
33f14e |
result = mbrtowc (&wc, s, n, &state);
|
|
Packit |
33f14e |
if (result == (size_t)-1 || result == (size_t)-2)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
errno = EILSEQ;
|
|
Packit |
33f14e |
return -1;
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
if (pwc != NULL)
|
|
Packit |
33f14e |
*pwc = wc;
|
|
Packit |
33f14e |
return (wc == 0 ? 0 : result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
}
|