Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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