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

  1. /***
  2. *mbscmp.c - Compare MBCS strings
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compare 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. * _mbscmp - Compare MBCS strings
  22. *
  23. *Purpose:
  24. *       Compares two strings for lexical order.   Strings
  25. *       are compared on a character basis, not a byte basis.
  26. *
  27. *Entry:
  28. *       char *s1, *s2 = strings 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 _mbscmp(
  40.         const unsigned char *s1,
  41.         const unsigned char *s2
  42.         )
  43. {
  44.         unsigned short c1, c2;
  45.  
  46.         if ( _ISNOTMBCP )
  47.             return strcmp(s1, s2);
  48.  
  49.         _mlock(_MB_CP_LOCK);
  50.  
  51.         for (;;) {
  52.  
  53.             c1 = *s1++;
  54.             if (_ISLEADBYTE(c1))
  55.                 c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
  56.  
  57.             c2 = *s2++;
  58.             if (_ISLEADBYTE(c2))
  59.                 c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
  60.  
  61.             if (c1 != c2)
  62.             {
  63.                 _munlock(_MB_CP_LOCK);
  64.                 return (c1 > c2) ? 1 : -1;
  65.             }
  66.  
  67.             if (c1 == 0)
  68.             {
  69.                 _munlock(_MB_CP_LOCK);
  70.                 return 0;
  71.             }
  72.  
  73.         }
  74.  
  75. }
  76.  
  77. #endif  /* _MBCS */
  78.