Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. FUNCTION
  3. <<wctomb>>---minimal wide char to multibyte converter
  4.  
  5. INDEX
  6.         wctomb
  7.  
  8. ANSI_SYNOPSIS
  9.         #include <stdlib.h>
  10.         int wctomb(char *<[s]>, wchar_t <[wchar]>);
  11.  
  12. TRAD_SYNOPSIS
  13.         #include <stdlib.h>
  14.         int wctomb(<[s]>, <[wchar]>)
  15.         char *<[s]>;
  16.         wchar_t <[wchar]>;
  17.  
  18. DESCRIPTION
  19. When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
  20. implementation of <<wctomb>>.  The
  21. only ``wide characters'' recognized are single bytes,
  22. and they are ``converted'' to themselves.  
  23.  
  24. When _MB_CAPABLE is defined, this routine calls <<_wctomb_r>> to perform
  25. the conversion, passing a state variable to allow state dependent
  26. decoding.  The result is based on the locale setting which may
  27. be restricted to a defined set of locales.
  28.  
  29. Each call to <<wctomb>> modifies <<*<[s]>>> unless <[s]> is a null
  30. pointer or _MB_CAPABLE is defined and <[wchar]> is invalid.
  31.  
  32. RETURNS
  33. This implementation of <<wctomb>> returns <<0>> if
  34. <[s]> is <<NULL>>; it returns <<-1>> if _MB_CAPABLE is enabled
  35. and the wchar is not a valid multi-byte character, it returns <<1>>
  36. if _MB_CAPABLE is not defined or the wchar is in reality a single
  37. byte character, otherwise it returns the number of bytes in the
  38. multi-byte character.
  39.  
  40. PORTABILITY
  41. <<wctomb>> is required in the ANSI C standard.  However, the precise
  42. effects vary with the locale.
  43.  
  44. <<wctomb>> requires no supporting OS subroutines.
  45. */
  46.  
  47. #ifndef _REENT_ONLY
  48.  
  49. #include <newlib.h>
  50. #include <stdlib.h>
  51. #include <errno.h>
  52. #include "local.h"
  53.  
  54. int
  55. _DEFUN (wctomb, (s, wchar),
  56.         char *s _AND
  57.         wchar_t wchar)
  58. {
  59. #ifdef _MB_CAPABLE
  60.         struct _reent *reent = _REENT;
  61.  
  62.         _REENT_CHECK_MISC(reent);
  63.  
  64.         return __wctomb (reent, s, wchar, __locale_charset (),
  65.                          &(_REENT_WCTOMB_STATE(reent)));
  66. #else /* not _MB_CAPABLE */
  67.         if (s == NULL)
  68.                 return 0;
  69.  
  70.         /* Verify that wchar is a valid single-byte character.  */
  71.         if ((size_t)wchar >= 0x100) {
  72.                 errno = EILSEQ;
  73.                 return -1;
  74.         }
  75.  
  76.         *s = (char) wchar;
  77.         return 1;
  78. #endif /* not _MB_CAPABLE */
  79. }
  80.  
  81. #endif /* !_REENT_ONLY */
  82.