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

  1. /***
  2. *mbsnbcmp.c - Compare n bytes of two MBCS strings
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compare n bytes of two MBCS strings
  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 <string.h>
  18. #include <mbstring.h>
  19.  
  20. /***
  21. *int mbsnbcmp(s1, s2, n) - Compare n bytes of two MBCS strings
  22. *
  23. *Purpose:
  24. *       Compares up to n bytes of two strings for lexical order.
  25. *
  26. *Entry:
  27. *       unsigned char *s1, *s2 = strings to compare
  28. *       size_t n = maximum number of bytes to compare
  29. *
  30. *Exit:
  31. *       returns <0 if s1 < s2
  32. *       returns  0 if s1 == s2
  33. *       returns >0 if s1 > s2
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. int __cdecl _mbsnbcmp(
  40.         const unsigned char *s1,
  41.         const unsigned char *s2,
  42.         size_t n
  43.         )
  44. {
  45.         unsigned short c1, c2;
  46.  
  47.         if (n==0)
  48.                 return(0);
  49.  
  50.         if ( _ISNOTMBCP )
  51.             return strncmp(s1, s2, n);
  52.  
  53.         _mlock(_MB_CP_LOCK);
  54.  
  55.         while (n--) {
  56.  
  57.                 c1 = *s1++;
  58.                 if (_ISLEADBYTE(c1)) {
  59.                         if (n==0)
  60.                 {
  61.                     c1 = 0; /* 'naked' lead - end of string */
  62.                     c2 = _ISLEADBYTE(*s2) ? 0 : *s2;
  63.                     goto test;
  64.                 }
  65.                         c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
  66.                 }
  67.  
  68.                 c2 = *s2++;
  69.                 if (_ISLEADBYTE(c2)) {
  70.                         if (n==0)
  71.                 {
  72.                     c2 = 0; /* 'naked' lead - end of string */
  73.                     goto test;
  74.                 }
  75.                 --n;
  76.                         c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
  77.                 }
  78. test:
  79.                 if (c1 != c2)
  80.             {
  81.                 _munlock(_MB_CP_LOCK);
  82.                         return( (c1 > c2) ? 1 : -1);
  83.             }
  84.  
  85.                 if (c1 == 0)
  86.             {
  87.                 _munlock(_MB_CP_LOCK);
  88.                         return(0);
  89.             }
  90.         }
  91.  
  92.         _munlock(_MB_CP_LOCK);
  93.         return(0);
  94. }
  95.  
  96. #endif  /* _MBCS */
  97.