Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.3 KiB

  1. /***
  2. *xstrcoll.c - Collate locale strings
  3. *
  4. * Copyright (c) 1996-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Compare two strings using the locale LC_COLLATE information.
  8. *
  9. *Revision History:
  10. * 01-XX-96 PJP Created from strcoll.c January 1996 by P.J. Plauger
  11. * 04-17-96 GJF Updated for current locale locking. Also, reformatted
  12. * and made several cosmetic changes.
  13. * 05-14-96 JWM Bug fix to _Strcoll(): error path failed to unlock.
  14. * 09-26-96 GJF Made _GetColl() multithread safe.
  15. * 12-02-97 GJF Removed bogus codepage determination.
  16. * 01-12-98 GJF Use _lc_collate_cp codepage.
  17. * 01-05-99 GJF Changes for 64-bit size_t.
  18. * 05-11-99 PML Win64 fix: cast ptr diff to int
  19. * 05-17-99 PML Remove all Macintosh support.
  20. * 01-29-01 GB Added _func function version of data variable used in msvcprt.lib
  21. * to work with STATIC_CPPLIB
  22. *
  23. *******************************************************************************/
  24. #include <cruntime.h>
  25. #include <string.h>
  26. #include <xlocinfo.h> /* for _Collvec, _Strcoll */
  27. #include <windows.h>
  28. #include <stdlib.h>
  29. #include <malloc.h>
  30. #include <locale.h>
  31. #include <setlocal.h>
  32. #include <mtdll.h>
  33. #include <errno.h>
  34. #include <awint.h>
  35. /* Define _CRTIMP2 */
  36. #ifndef _CRTIMP2
  37. #ifdef CRTDLL2
  38. #define _CRTIMP2 __declspec(dllexport)
  39. #else /* ndef CRTDLL2 */
  40. #ifdef _DLL
  41. #define _CRTIMP2 __declspec(dllimport)
  42. #else /* ndef _DLL */
  43. #define _CRTIMP2
  44. #endif /* _DLL */
  45. #endif /* CRTDLL2 */
  46. #endif /* _CRTIMP2 */
  47. /***
  48. *int _Strcoll() - Collate locale strings
  49. *
  50. *Purpose:
  51. * Compare two strings using the locale LC_COLLATE information.
  52. * [ANSI].
  53. *
  54. * Non-C locale support available under _INTL switch.
  55. * In the C locale, strcoll() simply resolves to strcmp().
  56. *Entry:
  57. * const char *s1b = pointer to beginning of the first string
  58. * const char *s1e = pointer past end of the first string
  59. * const char *s2b = pointer to beginning of the second string
  60. * const char *s1e = pointer past end of the second string
  61. * const _Collvec *ploc = pointer to locale info
  62. *
  63. *Exit:
  64. * Less than 0 = first string less than second string
  65. * 0 = strings are equal
  66. * Greater than 0 = first string greater than second string
  67. *
  68. *Exceptions:
  69. * _NLSCMPERROR = error
  70. * errno = EINVAL
  71. *
  72. *******************************************************************************/
  73. _CRTIMP2 int __cdecl _Strcoll (
  74. const char *_string1,
  75. const char *_end1,
  76. const char *_string2,
  77. const char *_end2,
  78. const _Collvec *ploc
  79. )
  80. {
  81. int ret;
  82. LCID handle;
  83. #ifdef _MT
  84. int local_lock_flag;
  85. #endif
  86. int n1 = (int)(_end1 - _string1);
  87. int n2 = (int)(_end2 - _string2);
  88. _lock_locale( local_lock_flag )
  89. if (ploc == 0)
  90. handle = ___lc_handle_func()[LC_COLLATE];
  91. else
  92. handle = ploc->_Hand;
  93. if (handle == _CLOCALEHANDLE) {
  94. int ans;
  95. _unlock_locale( local_lock_flag )
  96. ans = memcmp(_string1, _string2, n1 < n2 ? n1 : n2);
  97. return ans != 0 || n1 == n2 ? ans : n1 < n2 ? -1 : +1;
  98. }
  99. if ( 0 == (ret = __crtCompareStringA( handle,
  100. 0,
  101. _string1,
  102. n1,
  103. _string2,
  104. n2,
  105. ___lc_collate_cp_func() )) )
  106. goto error_cleanup;
  107. _unlock_locale( local_lock_flag )
  108. return (ret - 2);
  109. error_cleanup:
  110. _unlock_locale( local_lock_flag )
  111. errno = EINVAL;
  112. return _NLSCMPERROR;
  113. }
  114. /***
  115. *_Collvec _Getcoll() - get collation info for current locale
  116. *
  117. *Purpose:
  118. *
  119. *Entry:
  120. *
  121. *Exit:
  122. *
  123. *Exceptions:
  124. *
  125. *******************************************************************************/
  126. _CRTIMP2 _Collvec __cdecl _Getcoll()
  127. {
  128. _Collvec coll;
  129. #ifdef _MT
  130. int local_lock_flag;
  131. #endif
  132. _lock_locale( local_lock_flag )
  133. coll._Hand = ___lc_handle_func()[LC_COLLATE];
  134. coll._Page = ___lc_collate_cp_func();
  135. _unlock_locale( local_lock_flag )
  136. return (coll);
  137. }