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.

183 lines
6.4 KiB

  1. /***
  2. *strxfrm.c - Transform a string using locale information
  3. *
  4. * Copyright (c) 1988-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Transform a string using the locale information as set by
  8. * LC_COLLATE.
  9. *
  10. *Revision History:
  11. * 03-21-89 JCR Module created.
  12. * 06-20-89 JCR Removed _LOAD_DGROUP code
  13. * 02-27-90 GJF Fixed calling type, #include <cruntime.h>, fixed
  14. * copyright.
  15. * 10-02-90 GJF New-style function declarator.
  16. * 10-02-91 ETC Non-C locale support under _INTL switch.
  17. * 12-09-91 ETC Updated api; added multithread.
  18. * 12-18-91 ETC Don't convert output of LCMapString.
  19. * 08-18-92 KRS Activate NLS API. Fix behavior.
  20. * 09-02-92 SRW Get _INTL definition via ..\crt32.def
  21. * 12-11-92 SKS Need to handle count=0 in non-INTL code
  22. * 12-15-92 KRS Handle return value according to ANSI.
  23. * 01-18-93 CFW Removed unreferenced variable "dummy".
  24. * 03-10-93 CFW Remove UNDONE comment.
  25. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  26. * 09-15-93 CFW Use ANSI conformant "__" names.
  27. * 09-22-93 CFW Use __crtxxx internal NLS API wrapper.
  28. * 11-09-93 CFW Use LC_COLLATE code page for __crtxxx() conversion.
  29. * 09-06-94 CFW Remove _INTL switch.
  30. * 10-24-94 GJF Sped up C locale, multi-thread case.
  31. * 12-29-94 CFW Merge non-Win32.
  32. * 09-26-95 GJF New locking macro, and scheme, for functions which
  33. * reference the locale.
  34. * 10-11-95 BWT Fix NTSUBSET
  35. * 03-17-97 RDK Added error flag to __crtLCMapStringA.
  36. * 11-24-97 GJF Removed bogus codepage determination.
  37. * 01-12-98 GJF Use _lc_collate_cp codepage.
  38. * 07-16-98 GJF Revised multithread support based on threadlocinfo
  39. * struct.
  40. * 01-04-99 GJF Changes for 64-bit size_t.
  41. * 04-30-99 PML Minor cleanup as part of 64-bit merge.
  42. * 05-17-99 PML Remove all Macintosh support.
  43. *
  44. *******************************************************************************/
  45. #include <cruntime.h>
  46. #include <string.h>
  47. #include <windows.h>
  48. #include <stdlib.h>
  49. #include <limits.h>
  50. #include <malloc.h>
  51. #include <locale.h>
  52. #include <setlocal.h>
  53. #include <awint.h>
  54. #include <mtdll.h>
  55. /***
  56. *size_t strxfrm() - Transform a string using locale information
  57. *
  58. *Purpose:
  59. * Transform the string pointer to by _string2 and place the
  60. * resulting string into the array pointer to by _string1.
  61. * No more than _count characters are place into the
  62. * resulting string (including the null).
  63. *
  64. * The transformation is such that if strcmp() is applied to
  65. * the two transformed strings, the return value is equal to
  66. * the result of strcoll() applied to the two original strings.
  67. * Thus, the conversion must take the locale LC_COLLATE info
  68. * into account.
  69. * [ANSI]
  70. *
  71. * The value of the following expression is the size of the array
  72. * needed to hold the transformation of the source string:
  73. *
  74. * 1 + strxfrm(NULL,string,0)
  75. *
  76. * NOTE: Currently, the C libraries support the "C" locale only.
  77. * Thus, strxfrm() simply resolves to strncpy()/strlen().
  78. *
  79. *Entry:
  80. * char *_string1 = result string
  81. * const char *_string2 = source string
  82. * size_t _count = max chars to move
  83. *
  84. * [If _count is 0, _string1 is permitted to be NULL.]
  85. *
  86. *Exit:
  87. * Length of the transformed string (not including the terminating
  88. * null). If the value returned is >= _count, the contents of the
  89. * _string1 array are indeterminate.
  90. *
  91. *Exceptions:
  92. * Non-standard: if OM/API error, return INT_MAX.
  93. *
  94. *******************************************************************************/
  95. size_t __cdecl strxfrm (
  96. char *_string1,
  97. const char *_string2,
  98. size_t _count
  99. )
  100. {
  101. #ifdef _NTSUBSET_
  102. if (_string1)
  103. strncpy(_string1, _string2, _count);
  104. return strlen(_string2);
  105. #else
  106. int dstlen;
  107. int retval = INT_MAX; /* NON-ANSI: default if OM or API error */
  108. #ifdef _MT
  109. pthreadlocinfo ptloci;
  110. #endif
  111. if ( _count > INT_MAX )
  112. return (size_t)retval;
  113. #ifdef _MT
  114. ptloci = _getptd()->ptlocinfo;
  115. if ( ptloci != __ptlocinfo )
  116. ptloci = __updatetlocinfo();
  117. if ( (ptloci->lc_handle[LC_COLLATE] == _CLOCALEHANDLE) &&
  118. (ptloci->lc_collate_cp == _CLOCALECP) )
  119. #else
  120. if ( (__lc_handle[LC_COLLATE] == _CLOCALEHANDLE) &&
  121. (__lc_collate_cp == _CLOCALECP) )
  122. #endif
  123. {
  124. strncpy(_string1, _string2, _count);
  125. return strlen(_string2);
  126. }
  127. /* Inquire size of dst string in BYTES */
  128. #ifdef _MT
  129. if ( 0 == (dstlen = __crtLCMapStringA( ptloci->lc_handle[LC_COLLATE],
  130. #else
  131. if ( 0 == (dstlen = __crtLCMapStringA( __lc_handle[LC_COLLATE],
  132. #endif
  133. LCMAP_SORTKEY,
  134. _string2,
  135. -1,
  136. NULL,
  137. 0,
  138. #ifdef _MT
  139. ptloci->lc_collate_cp,
  140. #else
  141. __lc_collate_cp,
  142. #endif
  143. TRUE )) )
  144. goto error_cleanup;
  145. retval = dstlen;
  146. /* if not enough room, return amount needed */
  147. if ( dstlen > (int)_count )
  148. goto error_cleanup;
  149. /* Map src string to dst string */
  150. #ifdef _MT
  151. if ( 0 == __crtLCMapStringA( ptloci->lc_handle[LC_COLLATE],
  152. #else
  153. if ( 0 == __crtLCMapStringA( __lc_handle[LC_COLLATE],
  154. #endif
  155. LCMAP_SORTKEY,
  156. _string2,
  157. -1,
  158. _string1,
  159. (int)_count,
  160. #ifdef _MT
  161. ptloci->lc_collate_cp,
  162. #else
  163. __lc_collate_cp,
  164. #endif
  165. TRUE ) )
  166. retval = INT_MAX;
  167. error_cleanup:
  168. return (size_t)retval;
  169. #endif /* _NTSUBSET_ */
  170. }