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

  1. /***
  2. *mblen.c - length of multibyte character
  3. *
  4. *       Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Return the number of bytes contained in a multibyte character.
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #ifndef _MAC
  13. #include <internal.h>
  14. #include <locale.h>
  15. #include <setlocal.h>
  16. #endif  /* _MAC */
  17.  
  18. #include <cruntime.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <mtdll.h>
  22. #include <dbgint.h>
  23.  
  24. /***
  25. *int mblen() - length of multibyte character
  26. *
  27. *Purpose:
  28. *       Return the number of bytes contained in a multibyte character.
  29. *       [ANSI].
  30. *
  31. *Entry:
  32. *       const char *s = pointer to multibyte character
  33. *       size_t      n = maximum length of multibyte character to consider
  34. *
  35. *Exit:
  36. *       If s = NULL, returns 0, indicating we use (only) state-independent
  37. *       character encodings.
  38. *
  39. *       If s != NULL, returns:   0  (if *s = null char),
  40. *                               -1  (if the next n or fewer bytes not valid
  41. *                                   mbc),
  42. *                               number of bytes contained in multibyte char
  43. *
  44. *Exceptions:
  45. *
  46. *******************************************************************************/
  47.  
  48. int __cdecl mblen
  49.         (
  50.         const char * s,
  51.         size_t n
  52.         )
  53. {
  54.         _ASSERTE (MB_CUR_MAX == 1 || MB_CUR_MAX == 2);
  55.  
  56.         if ( !s || !(*s) || (n == 0) )
  57.             /* indicate do not have state-dependent encodings,
  58.                empty string length is 0 */
  59.             return 0;
  60.  
  61.  
  62.         if ( isleadbyte((unsigned char)*s) )
  63.         {
  64.             /* multi-byte char */
  65.  
  66.             /* verify valid MB char */
  67. #ifndef _MAC
  68.             if ( MB_CUR_MAX <= 1 ||
  69.                  (int)n < MB_CUR_MAX ||
  70.                  MultiByteToWideChar( __lc_codepage,
  71.                                       MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
  72.                                       s,
  73.                                       MB_CUR_MAX,
  74.                                       NULL,
  75.                                       0 )
  76.                  == 0 )
  77. #else  /* _MAC */
  78.             /* validate high byte of mbcs char */
  79.             if ((n<(size_t)MB_CUR_MAX) || (!*(s+1)))
  80. #endif  /* _MAC */
  81.                 /* bad MB char */
  82.                 return -1;
  83.             else
  84.                 return MB_CUR_MAX;
  85.         }
  86.         else {
  87.             /* single byte char */
  88.  
  89. #ifndef _MAC
  90.             /* verify valid SB char */
  91.             if ( MultiByteToWideChar( __lc_codepage,
  92.                                       MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
  93.                                       s,
  94.                                       1,
  95.                                       NULL,
  96.                                       0 )
  97.                  == 0 )
  98.                 return -1;
  99. #endif  /* _MAC */
  100.             return sizeof(char);
  101.         }
  102.  
  103. }
  104.