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

  1. /***
  2. *wcsnicmp.c - compare n chars of wide-character strings, ignoring case
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _wcsnicmp() - Compares at most n characters of two wchar_t
  8. *       strings, without regard to case.
  9. *
  10. *******************************************************************************/
  11.  
  12.  
  13. #include <cruntime.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <locale.h>
  17. #include <setlocal.h>
  18. #include <mtdll.h>
  19.  
  20. /***
  21. *int _wcsnicmp(first, last, count) - compares count wchar_t of strings,
  22. *       ignore case
  23. *
  24. *Purpose:
  25. *       Compare the two strings for lexical order.  Stops the comparison
  26. *       when the following occurs: (1) strings differ, (2) the end of the
  27. *       strings is reached, or (3) count characters have been compared.
  28. *       For the purposes of the comparison, upper case characters are
  29. *       converted to lower case (wide-characters).
  30. *
  31. *Entry:
  32. *       wchar_t *first, *last - strings to compare
  33. *       size_t count - maximum number of characters to compare
  34. *
  35. *Exit:
  36. *       -1 if first < last
  37. *        0 if first == last
  38. *        1 if first > last
  39. *       This range of return values may differ from other *cmp/*coll functions.
  40. *
  41. *Exceptions:
  42. *
  43. *******************************************************************************/
  44.  
  45. int __cdecl _wcsnicmp (
  46.         const wchar_t * first,
  47.         const wchar_t * last,
  48.         size_t count
  49.         )
  50. {
  51.         wchar_t f,l;
  52.         int result = 0;
  53. #if defined (_MT)
  54.         int local_lock_flag;
  55. #endif  /* defined (_MT) */
  56.  
  57.         if ( count ) {
  58.             if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  59.                 do {
  60.                     f = ((*first <= L'Z') && (*first >= L'A'))
  61.                     ? *first - L'A' + L'a'
  62.                     : *first;
  63.                     l = ((*last <= L'Z') && (*last >= L'A'))
  64.                     ? *last - L'A' + L'a'
  65.                     : *last;
  66.                     first++;
  67.                     last++;
  68.                 } while ( (--count) && f && (f == l) );
  69.             }
  70.             else {
  71.                 _lock_locale( local_lock_flag )
  72.                 do {
  73.                     f = _towlower_lk( (unsigned short)(*(first++)) );
  74.                     l = _towlower_lk( (unsigned short)(*(last++)) );
  75.                 } while ( (--count) && f && (f == l) );
  76.                 _unlock_locale( local_lock_flag )
  77.             }
  78.  
  79.             result = (int)(f - l);
  80.         }
  81.         return result;
  82. }
  83.  
  84.