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

  1. /***
  2. *strnicoll.c - Collate locale strings without regard to case
  3. *
  4. *       Copyright (c) 1988-1998, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compare two strings using the locale LC_COLLATE information.
  8. *       Compares at most n characters of two strings.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14.  
  15. #ifdef _WIN32
  16. #include <windows.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <locale.h>
  20. #include <setlocal.h>
  21. #include <mtdll.h>
  22. #include <errno.h>
  23. #include <awint.h>
  24. #endif  /* _WIN32 */
  25.  
  26. /***
  27. *int _strnicoll() - Collate locale strings without regard to case
  28. *
  29. *Purpose:
  30. *       Compare two strings using the locale LC_COLLATE information
  31. *       without regard to case.
  32. *       Compares at most n characters of two strings.
  33. *
  34. *Entry:
  35. *       const char *s1 = pointer to the first string
  36. *       const char *s2 = pointer to the second string
  37. *       size_t count - maximum number of characters to compare
  38. *
  39. *Exit:
  40. *       Less than 0    = first string less than second string
  41. *       0              = strings are equal
  42. *       Greater than 0 = first string greater than second string
  43. *
  44. *Exceptions:
  45. *       _NLSCMPERROR    = error
  46. *       errno = EINVAL
  47. *
  48. *******************************************************************************/
  49.  
  50. int __cdecl _strnicoll (
  51.         const char *_string1,
  52.         const char *_string2,
  53.         size_t count
  54.         )
  55. {
  56. #if defined (_WIN32)
  57.  
  58.         int ret;
  59. #if defined (_MT)
  60.         int local_lock_flag;
  61. #endif  /* defined (_MT) */
  62.  
  63.         if (!count)
  64.             return 0;
  65.  
  66.         _lock_locale( local_lock_flag )
  67.  
  68.         if (__lc_handle[LC_COLLATE] == _CLOCALEHANDLE) {
  69.                 _unlock_locale( local_lock_flag )
  70.                 return _strnicmp(_string1, _string2, count);
  71.         }
  72.  
  73.         if ( 0 == (ret = __crtCompareStringA( __lc_handle[LC_COLLATE],
  74.                                               SORT_STRINGSORT | NORM_IGNORECASE,
  75.                                               _string1,
  76.                                               count,
  77.                                               _string2,
  78.                                               count,
  79.                                               __lc_collate_cp )) )
  80.             goto error_cleanup;
  81.  
  82.         _unlock_locale( local_lock_flag )
  83.         return (ret - 2);
  84.  
  85. error_cleanup:
  86.         _unlock_locale( local_lock_flag )
  87.         errno = EINVAL;
  88.         return _NLSCMPERROR;
  89.  
  90. #else  /* defined (_WIN32) */
  91.  
  92.         return _strnicmp(_string1, _string2, count);
  93.  
  94. #endif  /* defined (_WIN32) */
  95. }
  96.