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

  1. /***
  2. *mbtowc.c - Convert multibyte char to wide char.
  3. *
  4. *       Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Convert a multibyte character into the equivalent wide character.
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #include <cruntime.h>
  13. #include <stdlib.h>
  14. #include <mtdll.h>
  15. #include <errno.h>
  16. #include <dbgint.h>
  17. #include <ctype.h>
  18.  
  19. #ifndef _MAC
  20. #include <internal.h>
  21. #include <locale.h>
  22. #include <setlocal.h>
  23. #endif  /* _MAC */
  24.  
  25. /***
  26. *int mbtowc() - Convert multibyte char to wide character.
  27. *
  28. *Purpose:
  29. *       Convert a multi-byte character into the equivalent wide character,
  30. *       according to the LC_CTYPE category of the current locale.
  31. *       [ANSI].
  32. *
  33. *       NOTE:  Currently, the C libraries support the "C" locale only.
  34. *              Non-C locale support now available under _INTL switch.
  35. *Entry:
  36. *       wchar_t  *pwc = pointer to destination wide character
  37. *       const char *s = pointer to multibyte character
  38. *       size_t      n = maximum length of multibyte character to consider
  39. *
  40. *Exit:
  41. *       If s = NULL, returns 0, indicating we only use state-independent
  42. *       character encodings.
  43. *       If s != NULL, returns:  0 (if *s = null char)
  44. *                               -1 (if the next n or fewer bytes not valid mbc)
  45. *                               number of bytes comprising converted mbc
  46. *
  47. *Exceptions:
  48. *
  49. *******************************************************************************/
  50.  
  51. #ifdef _MT
  52. int __cdecl mbtowc(
  53.         wchar_t  *pwc,
  54.         const char *s,
  55.         size_t n
  56.         )
  57. {
  58.         int retval;
  59.         int local_lock_flag;
  60.  
  61.         _lock_locale( local_lock_flag )
  62.         retval = _mbtowc_lk(pwc, s, n);
  63.         _unlock_locale( local_lock_flag )
  64.         return retval;
  65. }
  66. #endif  /* _MT */
  67.  
  68. #ifdef _MT
  69. int __cdecl _mbtowc_lk
  70. #else  /* _MT */
  71. int __cdecl mbtowc
  72. #endif  /* _MT */
  73.         (
  74.         wchar_t  *pwc,
  75.         const char *s,
  76.         size_t n
  77.         )
  78. {
  79.         _ASSERTE (MB_CUR_MAX == 1 || MB_CUR_MAX == 2);
  80.  
  81.         if ( !s || n == 0 )
  82.             /* indicate do not have state-dependent encodings,
  83.                handle zero length string */
  84.             return 0;
  85.  
  86.         if ( !*s )
  87.         {
  88.             /* handle NULL char */
  89.             if (pwc)
  90.                 *pwc = 0;
  91.             return 0;
  92.         }
  93.  
  94. #ifndef _MAC
  95.  
  96.         if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE )
  97.         {
  98.             if (pwc)
  99.                 *pwc = (wchar_t)(unsigned char)*s;
  100.             return sizeof(char);
  101.         }
  102.  
  103.         if ( isleadbyte((unsigned char)*s) )
  104.         {
  105.             /* multi-byte char */
  106.  
  107.             if ( (MB_CUR_MAX <= 1) || ((int)n < MB_CUR_MAX) ||
  108.                  (MultiByteToWideChar( __lc_codepage,
  109.                                       MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
  110.                                       s,
  111.                                       MB_CUR_MAX,
  112.                                       pwc,
  113.                                       (pwc) ? 1 : 0 ) == 0) )
  114.             {
  115.                 /* validate high byte of mbcs char */
  116.                 if ( (n < (size_t)MB_CUR_MAX) || (!*(s + 1)) )
  117.                 {
  118.                     errno = EILSEQ;
  119.                     return -1;
  120.                 }
  121.             }
  122.             return MB_CUR_MAX;
  123.         }
  124.         else {
  125.             /* single byte char */
  126.  
  127.             if ( MultiByteToWideChar( __lc_codepage,
  128.                                       MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
  129.                                       s,
  130.                                       1,
  131.                                       pwc,
  132.                                       (pwc) ? 1 : 0 ) == 0 )
  133.             {
  134.                 errno = EILSEQ;
  135.                 return -1;
  136.             }
  137.             return sizeof(char);
  138.         }
  139.  
  140. #else  /* _MAC */
  141.  
  142.         /* stuck the "C" locale again */
  143.         if (pwc)
  144.             *pwc = (wchar_t)(unsigned char)*s;
  145.         return sizeof(char);
  146.  
  147. #endif  /* _MAC */
  148. }
  149.