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

  1. /***
  2. *mbsnbcnt.c - Returns byte count of MBCS string
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Returns byte count of MBCS string
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <mtdll.h>
  14. #include <cruntime.h>
  15. #include <mbdata.h>
  16. #include <mbctype.h>
  17. #include <mbstring.h>
  18.  
  19.  
  20. /***
  21. * _mbsnbcnt - Returns byte count of MBCS string
  22. *
  23. *Purpose:
  24. *       Returns the number of bytes between the start of the supplied
  25. *       string and the char count supplied.  That is, this routine
  26. *       indicates how many bytes are in the first "ccnt" characters
  27. *       of the string.
  28. *
  29. *Entry:
  30. *       unsigned char *string = pointer to string
  31. *       unsigned int ccnt = number of characters to scan
  32. *
  33. *Exit:
  34. *       Returns number of bytes between string and ccnt.
  35. *
  36. *       If the end of the string is encountered before ccnt chars were
  37. *       scanned, then the length of the string in bytes is returned.
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43. size_t __cdecl _mbsnbcnt(
  44.     const unsigned char *string,
  45.     size_t ccnt
  46.     )
  47. {
  48.         unsigned char *p;
  49.  
  50.         _mlock(_MB_CP_LOCK);
  51.  
  52.         for (p = (char *)string; (ccnt-- && *p); p++) {
  53.  
  54.                 if (_ISLEADBYTE(*p)) {
  55.                         if (*++p == '\0') {
  56.                                 --p;
  57.                                 break;
  58.                         }
  59.                 }
  60.         }
  61.  
  62.         _munlock(_MB_CP_LOCK);
  63.         return ((size_t) ((char *)p - (char *)string));
  64. }
  65.  
  66. #endif  /* _MBCS */
  67.