[444] | 1 | /* |
---|
| 2 | FUNCTION |
---|
| 3 | <<_mblen_r>>---reentrant minimal multibyte length function |
---|
| 4 | |
---|
| 5 | INDEX |
---|
| 6 | _mblen_r |
---|
| 7 | |
---|
| 8 | SYNOPSIS |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | int _mblen_r(struct _reent *<[r]>, const char *<[s]>, size_t <[n]>, int *<[state]>); |
---|
| 11 | |
---|
| 12 | DESCRIPTION |
---|
| 13 | When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming |
---|
| 14 | implementation of <<_mblen_r>>. In this case, the |
---|
| 15 | only ``multi-byte character sequences'' recognized are single bytes, |
---|
| 16 | and thus <<1>> is returned unless <[s]> is the null pointer or |
---|
| 17 | has a length of 0 or is the empty string. |
---|
| 18 | |
---|
| 19 | When _MB_CAPABLE is defined, this routine calls <<_mbtowc_r>> to perform |
---|
| 20 | the conversion, passing a state variable to allow state dependent |
---|
| 21 | decoding. The result is based on the locale setting which may |
---|
| 22 | be restricted to a defined set of locales. |
---|
| 23 | |
---|
| 24 | RETURNS |
---|
| 25 | This implementation of <<_mblen_r>> returns <<0>> if |
---|
| 26 | <[s]> is <<NULL>> or the empty string; it returns <<1>> if not _MB_CAPABLE or |
---|
| 27 | the character is a single-byte character; it returns <<-1>> |
---|
| 28 | if the multi-byte character is invalid; otherwise it returns |
---|
| 29 | the number of bytes in the multibyte character. |
---|
| 30 | |
---|
| 31 | PORTABILITY |
---|
| 32 | <<_mblen>> is required in the ANSI C standard. However, the precise |
---|
| 33 | effects vary with the locale. |
---|
| 34 | |
---|
| 35 | <<_mblen_r>> requires no supporting OS subroutines. |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include <newlib.h> |
---|
| 39 | #include <stdlib.h> |
---|
| 40 | #include <wchar.h> |
---|
| 41 | #include "local.h" |
---|
| 42 | |
---|
| 43 | int |
---|
| 44 | _mblen_r (struct _reent *r, |
---|
| 45 | const char *s, |
---|
| 46 | size_t n, |
---|
| 47 | mbstate_t *state) |
---|
| 48 | { |
---|
| 49 | #ifdef _MB_CAPABLE |
---|
| 50 | int retval; |
---|
| 51 | retval = __MBTOWC (r, NULL, s, n, state); |
---|
| 52 | |
---|
| 53 | if (retval < 0) |
---|
| 54 | { |
---|
| 55 | state->__count = 0; |
---|
| 56 | return -1; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | return retval; |
---|
| 60 | #else /* not _MB_CAPABLE */ |
---|
| 61 | if (s == NULL || *s == '\0') |
---|
| 62 | return 0; |
---|
| 63 | if (n == 0) |
---|
| 64 | return -1; |
---|
| 65 | return 1; |
---|
| 66 | #endif /* not _MB_CAPABLE */ |
---|
| 67 | } |
---|
| 68 | |
---|