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

  1. /***
  2. *strnicmp.c - compare n chars of strings, ignoring case
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _strnicmp() - Compares at most n characters of two strings,
  8. *       without regard to case.
  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. /***
  23. *int _strnicmp(first, last, count) - compares count char of strings, ignore case
  24. *
  25. *Purpose:
  26. *       Compare the two strings for lexical order.  Stops the comparison
  27. *       when the following occurs: (1) strings differ, (2) the end of the
  28. *       strings is reached, or (3) count characters have been compared.
  29. *       For the purposes of the comparison, upper case characters are
  30. *       converted to lower case.
  31. *
  32. *Entry:
  33. *       char *first, *last - strings to compare
  34. *       size_t count - maximum number of characters to compare
  35. *
  36. *Exit:
  37. *       returns <0 if first < last
  38. *       returns 0 if first == last
  39. *       returns >0 if first > last
  40. *
  41. *Exceptions:
  42. *
  43. *******************************************************************************/
  44.  
  45. int __cdecl _strnicmp (
  46.         const char * first,
  47.         const char * last,
  48.         size_t count
  49.         )
  50. {
  51.         int f,l;
  52. #ifdef _MT
  53.         int local_lock_flag;
  54. #endif  /* _MT */
  55.  
  56.         if ( count )
  57.         {
  58. #if defined (_WIN32)
  59.             if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  60. #endif  /* defined (_WIN32) */
  61.  
  62.                 do {
  63.                     if ( ((f = (unsigned char)(*(first++))) >= 'A') &&
  64.                          (f <= 'Z') )
  65.                         f -= 'A' - 'a';
  66.  
  67.                     if ( ((l = (unsigned char)(*(last++))) >= 'A') &&
  68.                          (l <= 'Z') )
  69.                         l -= 'A' - 'a';
  70.  
  71.                 } while ( --count && f && (f == l) );
  72. #if defined (_WIN32)
  73.             }
  74.             else {
  75.                 _lock_locale( local_lock_flag )
  76.  
  77.                 do {
  78.                     f = _tolower_lk( (unsigned char)(*(first++)) );
  79.                     l = _tolower_lk( (unsigned char)(*(last++)) );
  80.                 } while (--count && f && (f == l) );
  81.  
  82.                 _unlock_locale( local_lock_flag )
  83.             }
  84. #endif  /* defined (_WIN32) */
  85.  
  86.             return( f - l );
  87.         }
  88.  
  89.         return( 0 );
  90. }
  91.