home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / wctomb.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  128 lines

  1. /***
  2. *wctomb.c - Convert wide character to multibyte character.
  3. *
  4. *       Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Convert a wide character into the equivalent multibyte character.
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #include <cruntime.h>
  13. #include <stdlib.h>
  14. #include <mtdll.h>
  15. #include <errno.h>
  16.  
  17. #ifndef _MAC
  18. #include <locale.h>
  19. #include <setlocal.h>
  20. #endif  /* _MAC */
  21.  
  22. /***
  23. *int wctomb() - Convert wide character to multibyte character.
  24. *
  25. *Purpose:
  26. *       Convert a wide character into the equivalent multi-byte character,
  27. *       according to the LC_CTYPE category of the current locale.
  28. *       [ANSI].
  29. *
  30. *       NOTE:  Currently, the C libraries support the "C" locale only.
  31. *              Non-C locale support now available under _INTL switch.
  32. *Entry:
  33. *       char *s          = pointer to multibyte character
  34. *       wchar_t wchar        = source wide character
  35. *
  36. *Exit:
  37. *       If s = NULL, returns 0, indicating we only use state-independent
  38. *       character encodings.
  39. *       If s != NULL, returns:
  40. *                   -1 (if error) or number of bytes comprising
  41. *                   converted mbc
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46.  
  47. #ifdef _MT
  48.  
  49. int __cdecl wctomb
  50.         (
  51.         char *s,
  52.         wchar_t wchar
  53.         )
  54. {
  55.         int retval;
  56.         int local_lock_flag;
  57.  
  58.         _lock_locale( local_lock_flag )
  59.         retval = _wctomb_lk(s, wchar);
  60.         _unlock_locale( local_lock_flag )
  61.         return retval;
  62. }
  63.  
  64. #endif  /* _MT */
  65.  
  66. #ifdef _MT
  67. int __cdecl _wctomb_lk
  68. #else  /* _MT */
  69. int __cdecl wctomb
  70. #endif  /* _MT */
  71.         (
  72.         char *s,
  73.         wchar_t wchar
  74.         )
  75. {
  76.         if ( !s )
  77.             /* indicate do not have state-dependent encodings */
  78.             return 0;
  79.  
  80. #ifndef _MAC
  81.  
  82.  
  83.         if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE )
  84.         {
  85.             if ( wchar > 255 )  /* validate high byte */
  86.             {
  87.                 errno = EILSEQ;
  88.                 return -1;
  89.             }
  90.  
  91.             *s = (char) wchar;
  92.             return sizeof(char);
  93.         } else {
  94.             int size;
  95.             BOOL defused = 0;
  96.  
  97.             if ( ((size = WideCharToMultiByte( __lc_codepage,
  98.                                                WC_COMPOSITECHECK |
  99.                                                 WC_SEPCHARS,
  100.                                                &wchar,
  101.                                                1,
  102.                                                s,
  103.                                                MB_CUR_MAX,
  104.                                                NULL,
  105.                                                &defused) ) == 0) ||
  106.                  (defused) )
  107.             {
  108.                 errno = EILSEQ;
  109.                 return -1;
  110.             }
  111.  
  112.             return size;
  113.         }
  114.  
  115. #else  /* _MAC */
  116.  
  117.         if ( wchar > 255 )  /* validate high byte */
  118.         {
  119.             errno = EILSEQ;
  120.             return -1;
  121.         }
  122.  
  123.         *s = (char) wchar;
  124.         return sizeof(char);
  125.  
  126. #endif  /* _MAC */
  127. }
  128.