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

  1. /***
  2. *mbsnicmp.c - Compare n characters of strings, ignoring case (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compare n characters of strings, ignoring case (MBCS)
  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. * _mbsnicmp - Compare n characters of strings, ignoring case (MBCS)
  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. *       Case of characters is not considered.
  27. *
  28. *Entry:
  29. *       unsigned char *s1, *s2 = strings to compare
  30. *       size_t n = maximum number of characters to compare
  31. *
  32. *Exit:
  33. *       returns <0 if s1 < s2
  34. *       returns  0 if s1 == s2
  35. *       returns >0 if s1 > s2
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. int __cdecl _mbsnicmp(
  42.         const unsigned char *s1,
  43.         const unsigned char *s2,
  44.         size_t n
  45.         )
  46. {
  47.         unsigned short c1, c2;
  48.  
  49.         if (n==0)
  50.             return(0);
  51.  
  52.         if ( _ISNOTMBCP )
  53.             return _strnicmp(s1, s2, n);
  54.  
  55.         _mlock(_MB_CP_LOCK);
  56.  
  57.         while (n--) {
  58.  
  59.             c1 = *s1++;
  60.             if (_ISLEADBYTE(c1)) {
  61.                 if (*s1 == '\0')
  62.                     c1 = 0;
  63.                 else {
  64.                     c1 = ((c1<<8) | *s1++);
  65.  
  66.                     if ( ((c1 >= _MBUPPERLOW1) && (c1 <= _MBUPPERHIGH1)) )
  67.                         c1 += _MBCASEDIFF1;
  68.  
  69.                     else if ( ((c1 >= _MBUPPERLOW2) && (c1 <= _MBUPPERHIGH2)) )
  70.                         c1 += _MBCASEDIFF2;
  71.                 }
  72.             }
  73.             else
  74.                 c1 = _mbbtolower(c1);
  75.  
  76.             c2 = *s2++;
  77.             if (_ISLEADBYTE(c2)) {
  78.                 if (*s2 == '\0')
  79.                     c2 = 0;
  80.                 else {
  81.                     c2 = ((c2<<8) | *s2++);
  82.  
  83.                     if ( ((c2 >= _MBUPPERLOW1) && (c2 <= _MBUPPERHIGH1)) )
  84.                         c2 += _MBCASEDIFF1;
  85.  
  86.                     else if ( ((c2 >= _MBUPPERLOW2) && (c2 <= _MBUPPERHIGH2)) )
  87.                         c2 += _MBCASEDIFF2;
  88.                 }
  89.             }
  90.             else
  91.                 c2 = _mbbtolower(c2);
  92.  
  93.             if (c1 != c2)
  94.             {
  95.                 _munlock(_MB_CP_LOCK);
  96.                 return( (c1 > c2) ? 1 : -1 );
  97.             }
  98.  
  99.             if (c1 == 0)
  100.             {
  101.                 _munlock(_MB_CP_LOCK);
  102.                 return(0);
  103.             }
  104.         }
  105.  
  106.         _munlock(_MB_CP_LOCK);
  107.         return(0);
  108. }
  109.  
  110. #endif  /* _MBCS */
  111.