Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.3 KiB

  1. /***
  2. *mbsnbcnt.c - Returns byte count of MBCS string
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Returns byte count of MBCS string
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  12. * 04-15-93 CFW Add _MB_CP_LOCK.
  13. * 05-19-94 CFW Enable non-Win32.
  14. * 04-15-98 GJF Revised multithread support based on threadmbcinfo
  15. * structs
  16. *
  17. *******************************************************************************/
  18. #ifdef _MBCS
  19. #include <mtdll.h>
  20. #include <cruntime.h>
  21. #include <mbdata.h>
  22. #include <mbctype.h>
  23. #include <mbstring.h>
  24. /***
  25. * _mbsnbcnt - Returns byte count of MBCS string
  26. *
  27. *Purpose:
  28. * Returns the number of bytes between the start of the supplied
  29. * string and the char count supplied. That is, this routine
  30. * indicates how many bytes are in the first "ccnt" characters
  31. * of the string.
  32. *
  33. *Entry:
  34. * unsigned char *string = pointer to string
  35. * unsigned int ccnt = number of characters to scan
  36. *
  37. *Exit:
  38. * Returns number of bytes between string and ccnt.
  39. *
  40. * If the end of the string is encountered before ccnt chars were
  41. * scanned, then the length of the string in bytes is returned.
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46. size_t __cdecl _mbsnbcnt(
  47. const unsigned char *string,
  48. size_t ccnt
  49. )
  50. {
  51. #ifdef _MT
  52. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  53. if ( ptmbci != __ptmbcinfo )
  54. ptmbci = __updatetmbcinfo();
  55. return __mbsnbcnt_mt(ptmbci, string, ccnt);
  56. }
  57. size_t __cdecl __mbsnbcnt_mt(
  58. pthreadmbcinfo ptmbci,
  59. const unsigned char *string,
  60. size_t ccnt
  61. )
  62. {
  63. #endif
  64. unsigned char *p;
  65. for (p = (char *)string; (ccnt-- && *p); p++) {
  66. #ifdef _MT
  67. if ( __ismbblead_mt(ptmbci, *p) ) {
  68. #else
  69. if ( _ismbblead(*p) ) {
  70. #endif
  71. if (*++p == '\0') {
  72. --p;
  73. break;
  74. }
  75. }
  76. }
  77. return ((size_t) ((char *)p - (char *)string));
  78. }
  79. #endif /* _MBCS */