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

  1. /***
  2. *memicmp.c - compare memory, ignore case
  3. *
  4. *   Copyright (c) 1988-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _memicmp() - compare two blocks of memory for lexical
  8. *       order.  Case is ignored in the comparison.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14.  
  15. #ifdef _WIN32
  16. #include <mtdll.h>
  17. #include <ctype.h>
  18. #include <setlocal.h>
  19. #include <locale.h>
  20. #endif  /* _WIN32 */
  21.  
  22. #define _TOLOWER(c) ( ((c) >= 'A') && ((c) <= 'Z') ? ((c) - 'A' + 'a') :\
  23.               (c) )
  24.  
  25. /***
  26. *int _memicmp(first, last, count) - compare two blocks of memory, ignore case
  27. *
  28. *Purpose:
  29. *   Compares count bytes of the two blocks of memory stored at first
  30. *   and last.  The characters are converted to lowercase before
  31. *   comparing (not permanently), so case is ignored in the search.
  32. *
  33. *Entry:
  34. *   char *first, *last - memory buffers to compare
  35. *   unsigned count - maximum length to compare
  36. *
  37. *Exit:
  38. *   returns < 0 if first < last
  39. *   returns 0 if first == last
  40. *   returns > 0 if first > last
  41. *
  42. *Uses:
  43. *
  44. *Exceptions:
  45. *
  46. *******************************************************************************/
  47.  
  48. int __cdecl _memicmp (
  49.         const void * first,
  50.         const void * last,
  51.         unsigned int count
  52.         )
  53. {
  54.         int f = 0;
  55.         int l = 0;
  56. #ifdef _MT
  57.         int local_lock_flag;
  58. #endif  /* _MT */
  59.  
  60. #if defined (_WIN32)
  61.         if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  62. #endif  /* defined (_WIN32) */
  63.             while ( count-- )
  64.             {
  65.                 if ( (*(unsigned char *)first == *(unsigned char *)last) ||
  66.                      ((f = _TOLOWER( *(unsigned char *)first )) ==
  67.                       (l = _TOLOWER( *(unsigned char *)last ))) )
  68.                 {
  69.                     first = (char *)first + 1;
  70.                     last = (char *)last + 1;
  71.                 }
  72.                 else
  73.                     break;
  74.             }
  75. #if defined (_WIN32)
  76.         }
  77.         else {
  78.             _lock_locale( local_lock_flag )
  79.             while ( count-- )
  80.                 if ( (*(unsigned char *)first == *(unsigned char *)last) ||
  81.                      ((f = _tolower_lk( *(unsigned char *)first )) ==
  82.                       (l = _tolower_lk( *(unsigned char *)last ))) )
  83.                 {
  84.                     first = (char *)first + 1;
  85.                     last = (char *)last + 1;
  86.                 }
  87.                 else
  88.                     break;
  89.             _unlock_locale( local_lock_flag )
  90.         }
  91. #endif  /* defined (_WIN32) */
  92.  
  93.         return ( f - l );
  94. }
  95.