[444] | 1 | /* |
---|
| 2 | FUNCTION |
---|
| 3 | <<wcstombs>>---minimal wide char string to multibyte string converter |
---|
| 4 | |
---|
| 5 | INDEX |
---|
| 6 | wcstombs |
---|
| 7 | |
---|
| 8 | SYNOPSIS |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | size_t wcstombs(char *restrict <[s]>, const wchar_t *restrict <[pwc]>, size_t <[n]>); |
---|
| 11 | |
---|
| 12 | DESCRIPTION |
---|
| 13 | When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming |
---|
| 14 | implementation of <<wcstombs>>. In this case, |
---|
| 15 | all wide-characters are expected to represent single bytes and so |
---|
| 16 | are converted simply by casting to char. |
---|
| 17 | |
---|
| 18 | When _MB_CAPABLE is defined, this routine calls <<_wcstombs_r>> to perform |
---|
| 19 | the conversion, passing a state variable to allow state dependent |
---|
| 20 | decoding. The result is based on the locale setting which may |
---|
| 21 | be restricted to a defined set of locales. |
---|
| 22 | |
---|
| 23 | RETURNS |
---|
| 24 | This implementation of <<wcstombs>> returns <<0>> if |
---|
| 25 | <[s]> is <<NULL>> or is the empty string; |
---|
| 26 | it returns <<-1>> if _MB_CAPABLE and one of the |
---|
| 27 | wide-char characters does not represent a valid multi-byte character; |
---|
| 28 | otherwise it returns the minimum of: <<n>> or the |
---|
| 29 | number of bytes that are transferred to <<s>>, not including the |
---|
| 30 | nul terminator. |
---|
| 31 | |
---|
| 32 | If the return value is -1, the state of the <<pwc>> string is |
---|
| 33 | indeterminate. If the input has a length of 0, the output |
---|
| 34 | string will be modified to contain a wchar_t nul terminator if |
---|
| 35 | <<n>> > 0. |
---|
| 36 | |
---|
| 37 | PORTABILITY |
---|
| 38 | <<wcstombs>> is required in the ANSI C standard. However, the precise |
---|
| 39 | effects vary with the locale. |
---|
| 40 | |
---|
| 41 | <<wcstombs>> requires no supporting OS subroutines. |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | #ifndef _REENT_ONLY |
---|
| 45 | |
---|
| 46 | #include <newlib.h> |
---|
| 47 | #include <stdlib.h> |
---|
| 48 | #include <wchar.h> |
---|
| 49 | |
---|
| 50 | size_t |
---|
| 51 | wcstombs (char *__restrict s, |
---|
| 52 | const wchar_t *__restrict pwcs, |
---|
| 53 | size_t n) |
---|
| 54 | { |
---|
| 55 | #ifdef _MB_CAPABLE |
---|
| 56 | mbstate_t state; |
---|
| 57 | state.__count = 0; |
---|
| 58 | |
---|
| 59 | return _wcstombs_r (_REENT, s, pwcs, n, &state); |
---|
| 60 | #else /* not _MB_CAPABLE */ |
---|
| 61 | int count = 0; |
---|
| 62 | |
---|
| 63 | if (n != 0) { |
---|
| 64 | do { |
---|
| 65 | if ((*s++ = (char) *pwcs++) == 0) |
---|
| 66 | break; |
---|
| 67 | count++; |
---|
| 68 | } while (--n != 0); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | return count; |
---|
| 72 | #endif /* not _MB_CAPABLE */ |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | #endif /* !_REENT_ONLY */ |
---|