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

  1. /***
  2. *strncoll.c - Collate locale strings
  3. *
  4. *       Copyright (c) 1994-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 _strncoll() - Collate locale strings
  28. *
  29. *Purpose:
  30. *       Compare two strings using the locale LC_COLLATE information.
  31. *       Compares at most n characters of two strings.
  32. *
  33. *Entry:
  34. *       const char *s1 = pointer to the first string
  35. *       const char *s2 = pointer to the second string
  36. *       size_t count - maximum number of characters to compare
  37. *
  38. *Exit:
  39. *       Less than 0    = first string less than second string
  40. *       0              = strings are equal
  41. *       Greater than 0 = first string greater than second string
  42. *
  43. *Exceptions:
  44. *       _NLSCMPERROR    = error
  45. *       errno = EINVAL
  46. *
  47. *******************************************************************************/
  48.  
  49. int __cdecl _strncoll (
  50.         const char *_string1,
  51.         const char *_string2,
  52.         size_t count
  53.         )
  54. {
  55. #if defined (_WIN32)
  56.  
  57.         int ret;
  58. #if defined (_MT)
  59.         int local_lock_flag;
  60. #endif  /* defined (_MT) */
  61.  
  62.         if (!count)
  63.             return 0;
  64.  
  65.         if (__lc_handle[LC_COLLATE] == _CLOCALEHANDLE) {
  66.                 return strncmp(_string1, _string2, count);
  67.         }
  68.  
  69.         _lock_locale( local_lock_flag )
  70.  
  71. #if defined (_MT)
  72.         if (__lc_handle[LC_COLLATE] == _CLOCALEHANDLE) {
  73.                 _unlock_locale( local_lock_flag )
  74.                 return strncmp(_string1, _string2, count);
  75.         }
  76. #endif  /* defined (_MT) */
  77.  
  78.         if ( 0 == (ret = __crtCompareStringA( __lc_handle[LC_COLLATE],
  79.                                               SORT_STRINGSORT,
  80.                                               _string1,
  81.                                               count,
  82.                                               _string2,
  83.                                               count,
  84.                                               __lc_collate_cp )) )
  85.             goto error_cleanup;
  86.  
  87.         _unlock_locale( local_lock_flag )
  88.         return (ret - 2);
  89.  
  90. error_cleanup:
  91.         _unlock_locale( local_lock_flag )
  92.         errno = EINVAL;
  93.         return _NLSCMPERROR;
  94.  
  95. #else  /* defined (_WIN32) */
  96.  
  97.         return strncmp(_string1, _string2, count);
  98.  
  99. #endif  /* defined (_WIN32) */
  100. }
  101.