Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. FUNCTION
  3. <<wcstombs>>---minimal wide char string to multibyte string converter
  4.  
  5. INDEX
  6.         wcstombs
  7.  
  8. ANSI_SYNOPSIS
  9.         #include <stdlib.h>
  10.         size_t wcstombs(char *restrict <[s]>, const wchar_t *restrict <[pwc]>, size_t <[n]>);
  11.  
  12. TRAD_SYNOPSIS
  13.         #include <stdlib.h>
  14.         size_t wcstombs(<[s]>, <[pwc]>, <[n]>)
  15.         char *<[s]>;
  16.         const wchar_t *<[pwc]>;
  17.         size_t <[n]>;
  18.  
  19. DESCRIPTION
  20. When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
  21. implementation of <<wcstombs>>.  In this case,
  22. all wide-characters are expected to represent single bytes and so
  23. are converted simply by casting to char.
  24.  
  25. When _MB_CAPABLE is defined, this routine calls <<_wcstombs_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 <<wcstombs>> returns <<0>> if
  32. <[s]> is <<NULL>> or is the empty string;
  33. it returns <<-1>> if _MB_CAPABLE and one of the
  34. wide-char characters does not represent a valid multi-byte character;
  35. otherwise it returns the minimum of: <<n>> or the
  36. number of bytes that are transferred to <<s>>, not including the
  37. nul terminator.
  38.  
  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 if
  42. <<n>> > 0.
  43.  
  44. PORTABILITY
  45. <<wcstombs>> is required in the ANSI C standard.  However, the precise
  46. effects vary with the locale.
  47.  
  48. <<wcstombs>> requires no supporting OS subroutines.
  49. */
  50.  
  51. #ifndef _REENT_ONLY
  52.  
  53. #include <newlib.h>
  54. #include <stdlib.h>
  55. #include <wchar.h>
  56.  
  57. size_t
  58. _DEFUN (wcstombs, (s, pwcs, n),
  59.         char          *__restrict s    _AND
  60.         const wchar_t *__restrict pwcs _AND
  61.         size_t         n)
  62. {
  63. #ifdef _MB_CAPABLE
  64.   mbstate_t state;
  65.   state.__count = 0;
  66.  
  67.   return _wcstombs_r (_REENT, s, pwcs, n, &state);
  68. #else /* not _MB_CAPABLE */
  69.   int count = 0;
  70.  
  71.   if (n != 0) {
  72.     do {
  73.       if ((*s++ = (char) *pwcs++) == 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.