Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. FUNCTION
  3. <<mblen>>---minimal multibyte length function
  4.  
  5. INDEX
  6.         mblen
  7.  
  8. ANSI_SYNOPSIS
  9.         #include <stdlib.h>
  10.         int mblen(const char *<[s]>, size_t <[n]>);
  11.  
  12. TRAD_SYNOPSIS
  13.         #include <stdlib.h>
  14.         int mblen(<[s]>, <[n]>)
  15.         const char *<[s]>;
  16.         size_t <[n]>;
  17.  
  18. DESCRIPTION
  19. When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
  20. implementation of <<mblen>>.  In this case, the
  21. only ``multi-byte character sequences'' recognized are single bytes,
  22. and thus <<1>> is returned unless <[s]> is the null pointer or
  23. has a length of 0 or is the empty string.
  24.  
  25. When _MB_CAPABLE is defined, this routine calls <<_mbtowc_r>> to perform
  26. the conversion, passing a state variable to allow state dependent
  27. decoding.  The result is based on the locale setting which may
  28. be restricted to a defined set of locales.
  29.  
  30. RETURNS
  31. This implementation of <<mblen>> returns <<0>> if
  32. <[s]> is <<NULL>> or the empty string; it returns <<1>> if not _MB_CAPABLE or
  33. the character is a single-byte character; it returns <<-1>>
  34. if the multi-byte character is invalid; otherwise it returns
  35. the number of bytes in the multibyte character.
  36.  
  37. PORTABILITY
  38. <<mblen>> is required in the ANSI C standard.  However, the precise
  39. effects vary with the locale.
  40.  
  41. <<mblen>> 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. #include "local.h"
  50.  
  51. int
  52. _DEFUN (mblen, (s, n),
  53.         const char *s _AND
  54.         size_t n)
  55. {
  56. #ifdef _MB_CAPABLE
  57.   int retval = 0;
  58.   struct _reent *reent = _REENT;
  59.   mbstate_t *state;
  60.  
  61.   _REENT_CHECK_MISC(reent);
  62.   state = &(_REENT_MBLEN_STATE(reent));
  63.   retval = __mbtowc (reent, NULL, s, n, __locale_charset (), state);
  64.   if (retval < 0)
  65.     {
  66.       state->__count = 0;
  67.       return -1;
  68.     }
  69.   else
  70.     return retval;
  71.  
  72. #else /* not _MB_CAPABLE */
  73.   if (s == NULL || *s == '\0')
  74.     return 0;
  75.   if (n == 0)
  76.     return -1;
  77.   return 1;
  78. #endif /* not _MB_CAPABLE */
  79. }
  80.  
  81. #endif /* !_REENT_ONLY */
  82.