Leaked source code of windows server 2003
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.

157 lines
4.6 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. * 04-29-02 GB Added try-finally arounds lock-unlock.
  23. *
  24. *******************************************************************************/
  25. #include <cruntime.h>
  26. #include <string.h>
  27. #include <xlocinfo.h> /* for _Collvec, _Strcoll */
  28. #include <windows.h>
  29. #include <stdlib.h>
  30. #include <malloc.h>
  31. #include <locale.h>
  32. #include <setlocal.h>
  33. #include <mtdll.h>
  34. #include <errno.h>
  35. #include <awint.h>
  36. /* Define _CRTIMP2 */
  37. #ifndef _CRTIMP2
  38. #ifdef CRTDLL2
  39. #define _CRTIMP2 __declspec(dllexport)
  40. #else /* ndef CRTDLL2 */
  41. #ifdef _DLL
  42. #define _CRTIMP2 __declspec(dllimport)
  43. #else /* ndef _DLL */
  44. #define _CRTIMP2
  45. #endif /* _DLL */
  46. #endif /* CRTDLL2 */
  47. #endif /* _CRTIMP2 */
  48. /***
  49. *int _Strcoll() - Collate locale strings
  50. *
  51. *Purpose:
  52. * Compare two strings using the locale LC_COLLATE information.
  53. * [ANSI].
  54. *
  55. * Non-C locale support available under _INTL switch.
  56. * In the C locale, strcoll() simply resolves to strcmp().
  57. *Entry:
  58. * const char *s1b = pointer to beginning of the first string
  59. * const char *s1e = pointer past end of the first string
  60. * const char *s2b = pointer to beginning of the second string
  61. * const char *s1e = pointer past end of the second string
  62. * const _Collvec *ploc = pointer to locale info
  63. *
  64. *Exit:
  65. * Less than 0 = first string less than second string
  66. * 0 = strings are equal
  67. * Greater than 0 = first string greater than second string
  68. *
  69. *Exceptions:
  70. * _NLSCMPERROR = error
  71. * errno = EINVAL
  72. *
  73. *******************************************************************************/
  74. _CRTIMP2 int __cdecl _Strcoll (
  75. const char *_string1,
  76. const char *_end1,
  77. const char *_string2,
  78. const char *_end2,
  79. const _Collvec *ploc
  80. )
  81. {
  82. int ret;
  83. LCID handle;
  84. #ifdef _MT
  85. int local_lock_flag;
  86. #endif
  87. int n1 = (int)(_end1 - _string1);
  88. int n2 = (int)(_end2 - _string2);
  89. _lock_locale( local_lock_flag )
  90. __TRY
  91. if (ploc == 0)
  92. handle = ___lc_handle_func()[LC_COLLATE];
  93. else
  94. handle = ploc->_Hand;
  95. if (handle == _CLOCALEHANDLE) {
  96. int ans;
  97. ans = memcmp(_string1, _string2, n1 < n2 ? n1 : n2);
  98. return ans != 0 || n1 == n2 ? ans : n1 < n2 ? -1 : +1;
  99. }
  100. if ( 0 == (ret = __crtCompareStringA( handle,
  101. 0,
  102. _string1,
  103. n1,
  104. _string2,
  105. n2,
  106. ___lc_collate_cp_func() )) )
  107. goto error_cleanup;
  108. __FINALLY
  109. _unlock_locale( local_lock_flag )
  110. __END_TRY_FINALLY
  111. return (ret - 2);
  112. error_cleanup:
  113. errno = EINVAL;
  114. return _NLSCMPERROR;
  115. }
  116. /***
  117. *_Collvec _Getcoll() - get collation info for current locale
  118. *
  119. *Purpose:
  120. *
  121. *Entry:
  122. *
  123. *Exit:
  124. *
  125. *Exceptions:
  126. *
  127. *******************************************************************************/
  128. _CRTIMP2 _Collvec __cdecl _Getcoll()
  129. {
  130. _Collvec coll;
  131. #ifdef _MT
  132. int local_lock_flag;
  133. #endif
  134. _lock_locale( local_lock_flag )
  135. __TRY
  136. coll._Hand = ___lc_handle_func()[LC_COLLATE];
  137. coll._Page = ___lc_collate_cp_func();
  138. __FINALLY
  139. _unlock_locale( local_lock_flag )
  140. __END_TRY_FINALLY
  141. return (coll);
  142. }