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

  1. /***
  2. *mbsncmp.c - Compare n characters of two MBCS strings
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compare n characters 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 mbsncmp(s1, s2, n) - Compare n characters of two MBCS strings
  22. *
  23. *Purpose:
  24. *       Compares up to n charcters of two strings for lexical order.
  25. *       Strings are compared on a character basis, not a byte basis.
  26. *
  27. *Entry:
  28. *       unsigned char *s1, *s2 = strings to compare
  29. *       size_t n = maximum number of characters to compare
  30. *
  31. *Exit:
  32. *       returns <0 if s1 < s2
  33. *       returns  0 if s1 == s2
  34. *       returns >0 if s1 > s2
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39.  
  40. int __cdecl _mbsncmp(
  41.         const unsigned char *s1,
  42.         const unsigned char *s2,
  43.         size_t n
  44.         )
  45. {
  46.         unsigned short c1, c2;
  47.  
  48.         if (n==0)
  49.             return(0);
  50.  
  51.         if ( _ISNOTMBCP )
  52.             return strncmp(s1, s2, n);
  53.  
  54.         _mlock(_MB_CP_LOCK);
  55.  
  56.         while (n--) {
  57.  
  58.             c1 = *s1++;
  59.             if (_ISLEADBYTE(c1))
  60.                 c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
  61.  
  62.             c2 = *s2++;
  63.             if (_ISLEADBYTE(c2))
  64.                 c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
  65.  
  66.             if (c1 != c2)
  67.             {
  68.                 _munlock(_MB_CP_LOCK);
  69.                 return( (c1 > c2) ? 1 : -1);
  70.             }
  71.  
  72.             if (c1 == 0)
  73.             {
  74.                 _munlock(_MB_CP_LOCK);
  75.                 return(0);
  76.             }
  77.         }
  78.  
  79.         _munlock(_MB_CP_LOCK);
  80.         return(0);
  81. }
  82.  
  83. #endif  /* _MBCS */
  84.