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

  1. /***
  2. *mbsicmp.c - Case-insensitive string comparision routine (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Case-insensitive string comparision routine (MBCS)
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #if defined (_WIN32)
  14. #include <awint.h>
  15. #endif  /* defined (_WIN32) */
  16.  
  17. #include <mtdll.h>
  18. #include <cruntime.h>
  19. #include <mbdata.h>
  20. #include <mbctype.h>
  21. #include <string.h>
  22. #include <mbstring.h>
  23.  
  24. /***
  25. * _mbsicmp - Case-insensitive string comparision routine (MBCS)
  26. *
  27. *Purpose:
  28. *       Compares two strings for lexical order without regard to case.
  29. *       Strings are compared on a character basis, not a byte basis.
  30. *
  31. *Entry:
  32. *       char *s1, *s2 = strings to compare
  33. *
  34. *Exit:
  35. *       returns <0 if s1 < s2
  36. *       returns  0 if s1 == s2
  37. *       returns >0 if s1 > s2
  38. *       returns _NLSCMPERROR if NLS error
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. int __cdecl _mbsicmp (const unsigned char *s1, const unsigned char *s2)
  45. {
  46.         unsigned short c1, c2;
  47. #if defined (_WIN32)
  48.         int    retval;
  49.         unsigned char szResult[4];
  50. #endif  /* defined (_WIN32) */
  51.  
  52.         if ( _ISNOTMBCP )
  53.             return _stricmp(s1, s2);
  54.  
  55.         _mlock(_MB_CP_LOCK);
  56.  
  57.         for (;;)
  58.         {
  59.             c1 = *s1++;
  60.             if (_ISLEADBYTE(c1))
  61.             {
  62.                 if (*s1 == '\0')
  63.                     c1 = 0;
  64.                 else
  65.                 {
  66. #if defined (_WIN32)
  67.                     retval = __crtLCMapStringA(__mblcid, LCMAP_UPPERCASE,
  68.                                                s1 - 1, 2, szResult, 2,
  69.                                                __mbcodepage, TRUE);
  70.                     if (retval == 1)
  71.                         c1 = szResult[0];
  72.                     else if (retval == 2)
  73.                         c1 = (szResult[0] << 8) + szResult[1];
  74.                     else
  75.                     {
  76.                         _munlock(_MB_CP_LOCK);
  77.                         return _NLSCMPERROR;
  78.                     }
  79.                     s1++;
  80. #else  /* defined (_WIN32) */
  81.                     c1 = ((c1 << 8) | *s1++);
  82.                     if (c1 >= _MBUPPERLOW1 && c1 <= _MBUPPERHIGH1)
  83.                         c1 += _MBCASEDIFF1;
  84.                     else if (c1 >= _MBUPPERLOW2 && c1 <= _MBUPPERHIGH2)
  85.                         c1 += _MBCASEDIFF2;
  86. #endif  /* defined (_WIN32) */
  87.                 }
  88.             }
  89.             else
  90.                 c1 = _mbbtolower(c1);
  91.  
  92.             c2 = *s2++;
  93.             if (_ISLEADBYTE(c2))
  94.             {
  95.                 if (*s2 == '\0')
  96.                     c2 = 0;
  97.                 else
  98.                 {
  99. #if defined (_WIN32)
  100.                     retval = __crtLCMapStringA(__mblcid, LCMAP_UPPERCASE,
  101.                                                s2 - 1, 2, szResult, 2,
  102.                                                __mbcodepage, TRUE);
  103.                     if (retval == 1)
  104.                         c2 = szResult[0];
  105.                     else if (retval == 2)
  106.                         c2 = (szResult[0] << 8) + szResult[1];
  107.                     else
  108.                     {
  109.                         _munlock(_MB_CP_LOCK);
  110.                         return _NLSCMPERROR;
  111.                     }
  112.                     s2++;
  113. #else  /* defined (_WIN32) */
  114.                     c2 = ((c2 << 8) | *s2++);
  115.                     if (c2 >= _MBUPPERLOW1 && c2 <= _MBUPPERHIGH1)
  116.                         c2 += _MBCASEDIFF1;
  117.                     else if (c2 >= _MBUPPERLOW2 && c2 <= _MBUPPERHIGH2)
  118.                         c2 += _MBCASEDIFF2;
  119. #endif  /* defined (_WIN32) */
  120.                 }
  121.             }
  122.             else
  123.                 c2 = _mbbtolower(c2);
  124.  
  125.             if (c1 != c2)
  126.             {
  127.                 _munlock(_MB_CP_LOCK);
  128.                 return( (c1 > c2) ? 1 : -1 );
  129.             }
  130.  
  131.             if (c1 == 0)
  132.             {
  133.                 _munlock(_MB_CP_LOCK);
  134.                 return(0);
  135.             }
  136.         }
  137. }
  138.  
  139. #endif  /* _MBCS */
  140.