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.

164 lines
5.1 KiB

  1. /***
  2. *wcslwr.c - routine to map upper-case characters in a wchar_t string
  3. * to lower-case
  4. *
  5. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  6. *
  7. *Purpose:
  8. * Converts all the upper case characters in a wchar_t string
  9. * to lower case, in place.
  10. *
  11. *Revision History:
  12. * 09-09-91 ETC Created from strlwr.c.
  13. * 04-06-92 KRS Make work without _INTL also.
  14. * 08-19-92 KRS Activate NLS support.
  15. * 08-22-92 SRW Allow INTL definition to be conditional for building ntcrt.lib
  16. * 09-02-92 SRW Get _INTL definition via ..\crt32.def
  17. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  18. * 06-02-93 SRW ignore _INTL if _NTSUBSET_ defined.
  19. * 09-15-93 CFW Use ANSI conformant "__" names.
  20. * 09-16-93 GJF Merged NT SDK and Cuda versions.
  21. * 09-22-93 CFW Use __crtxxx internal NLS API wrapper.
  22. * 11-09-93 CFW Add code page for __crtxxx().
  23. * 02-07-94 CFW POSIXify.
  24. * 09-06-94 CFW Remove _INTL switch.
  25. * 10-25-94 GJF Sped up C locale.
  26. * 01-10-95 CFW Debug CRT allocs.
  27. * 09-26-95 GJF New locking macro, and scheme, for functions which
  28. * reference the locale.
  29. * 08-12-98 GJF Revised multithread support based on threadlocinfo
  30. * struct. Also, use _alloca instead of _malloc_crt if
  31. * possible.
  32. * 12-10-99 GB Added support for recovery from stack overflow around
  33. * _alloca().
  34. *
  35. *******************************************************************************/
  36. #ifndef _POSIX_
  37. #include <cruntime.h>
  38. #include <string.h>
  39. #include <malloc.h>
  40. #include <locale.h>
  41. #include <ctype.h>
  42. #include <setlocal.h>
  43. #include <mtdll.h>
  44. #include <awint.h>
  45. #include <dbgint.h>
  46. /***
  47. *wchar_t *_wcslwr(string) - map upper-case characters in a string to lower-case
  48. *
  49. *Purpose:
  50. * wcslwr converts upper-case characters in a null-terminated wchar_t
  51. * string to their lower-case equivalents. The result may be longer or
  52. * shorter than the original string. Assumes enough space in string
  53. * to hold the result.
  54. *
  55. *Entry:
  56. * wchar_t *wsrc - wchar_t string to change to lower case
  57. *
  58. *Exit:
  59. * input string address
  60. *
  61. *Exceptions:
  62. * on an error, the original string is unaltered
  63. *
  64. *******************************************************************************/
  65. wchar_t * __cdecl _wcslwr (
  66. wchar_t * wsrc
  67. )
  68. {
  69. #ifndef _NTSUBSET_
  70. wchar_t *p; /* traverses string for C locale conversion */
  71. wchar_t *wdst; /* wide version of string in alternate case */
  72. int dstlen; /* len of wdst string, wide chars, with null */
  73. int malloc_flag = 0;
  74. #ifdef _MT
  75. pthreadlocinfo ptloci = _getptd()->ptlocinfo;
  76. if ( ptloci != __ptlocinfo )
  77. ptloci = __updatetlocinfo();
  78. if ( ptloci->lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  79. #else
  80. if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  81. #endif
  82. for ( p = wsrc ; *p ; p++ )
  83. if ( (*p >= (wchar_t)L'A') && (*p <= (wchar_t)L'Z') )
  84. *p -= L'A' - L'a';
  85. return(wsrc);
  86. } /* C locale */
  87. /* Inquire size of wdst string */
  88. #ifdef _MT
  89. if ( (dstlen = __crtLCMapStringW( ptloci->lc_handle[LC_CTYPE],
  90. #else
  91. if ( (dstlen = __crtLCMapStringW( __lc_handle[LC_CTYPE],
  92. #endif
  93. LCMAP_LOWERCASE,
  94. wsrc,
  95. -1,
  96. NULL,
  97. 0,
  98. #ifdef _MT
  99. ptloci->lc_codepage )) == 0 )
  100. #else
  101. __lc_codepage )) == 0 )
  102. #endif
  103. return(wsrc);
  104. /* Allocate space for wdst */
  105. __try {
  106. wdst = (wchar_t *)_alloca(dstlen * sizeof(wchar_t));
  107. }
  108. __except( EXCEPTION_EXECUTE_HANDLER ) {
  109. _resetstkoflw();
  110. wdst = NULL;
  111. }
  112. if ( wdst == NULL ) {
  113. wdst = (wchar_t *)_malloc_crt(dstlen * sizeof(wchar_t));
  114. malloc_flag++;
  115. }
  116. /* Map wrc string to wide-character wdst string in alternate case */
  117. if ( (wdst != NULL) &&
  118. #ifdef _MT
  119. (__crtLCMapStringW( ptloci->lc_handle[LC_CTYPE],
  120. #else
  121. (__crtLCMapStringW( __lc_handle[LC_CTYPE],
  122. #endif
  123. LCMAP_LOWERCASE,
  124. wsrc,
  125. -1,
  126. wdst,
  127. dstlen,
  128. #ifdef _MT
  129. ptloci->lc_codepage ) != 0) )
  130. #else
  131. __lc_codepage ) != 0) )
  132. #endif
  133. /* Copy wdst string to user string */
  134. wcscpy(wsrc, wdst);
  135. if ( malloc_flag )
  136. _free_crt(wdst);
  137. #else
  138. wchar_t * p;
  139. for (p=wsrc; *p; ++p)
  140. {
  141. if (L'A' <= *p && *p <= L'Z')
  142. *p += (wchar_t)L'a' - (wchar_t)L'A';
  143. }
  144. #endif
  145. return(wsrc);
  146. }
  147. #endif /* _POSIX_ */