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

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