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.

183 lines
6.0 KiB

  1. /***
  2. *strlwr.c - routine to map upper-case characters in a string to lower-case
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Converts all the upper case characters in a string to lower case,
  8. * in place.
  9. *
  10. *Revision History:
  11. * 05-31-89 JCR C version created.
  12. * 02-27-90 GJF Fixed calling type, #include <cruntime.h>, fixed
  13. * copyright.
  14. * 10-02-90 GJF New-style function declarator.
  15. * 01-18-91 GJF ANSI naming.
  16. * 09-18-91 ETC Locale support under _INTL switch.
  17. * 12-08-91 ETC Updated nlsapi; added multithread.
  18. * 08-19-92 KRS Activated NLS support.
  19. * 08-22-92 SRW Allow INTL definition to be conditional for building
  20. * ntcrt.lib
  21. * 09-02-92 SRW Get _INTL definition via ..\crt32.def
  22. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  23. * 06-01-93 CFW Simplify "C" locale test.
  24. * 06-02-93 SRW ignore _INTL if _NTSUBSET_ defined.
  25. * 09-15-93 CFW Use ANSI conformant "__" names.
  26. * 09-16-93 GJF Merged NT SDK and Cuda versions.
  27. * 09-22-93 CFW Use __crtxxx internal NLS API wrapper.
  28. * 11-09-93 CFW Add code page for __crtxxx().
  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. * 01-10-95 CFW Debug CRT allocs.
  33. * 09-26-95 GJF New locking macro, and scheme, for functions which
  34. * reference the locale.
  35. * 03-17-97 RDK Added error flag to __crtLCMapStringA.
  36. * 08-11-98 GJF Revised multithread support based on threadlocinfo
  37. * struct. Also, use _alloca instead of _malloc_crt.
  38. * 05-17-99 PML Remove all Macintosh support.
  39. * 12-10-99 GB Added support for recovery from stack overflow around
  40. * _alloca().
  41. * 05-01-00 BWT Fix Posix.
  42. * 03-13-01 PML Fix memory leak if _alloca failed (vs7#224860)
  43. *
  44. *******************************************************************************/
  45. #include <cruntime.h>
  46. #include <string.h>
  47. #include <malloc.h>
  48. #include <locale.h>
  49. #include <setlocal.h>
  50. #include <limits.h> /* for INT_MAX */
  51. #include <mtdll.h>
  52. #include <awint.h>
  53. #include <dbgint.h>
  54. /***
  55. *char *_strlwr(string) - map upper-case characters in a string to lower-case
  56. *
  57. *Purpose:
  58. * _strlwr() converts upper-case characters in a null-terminated string
  59. * to their lower-case equivalents. Conversion is done in place and
  60. * characters other than upper-case letters are not modified.
  61. *
  62. * In the C locale, this function modifies only 7-bit ASCII characters
  63. * in the range 0x41 through 0x5A ('A' through 'Z').
  64. *
  65. * If the locale is not the 'C' locale, LCMapString() is used to do
  66. * the work. Assumes enough space in the string to hold result.
  67. *
  68. *Entry:
  69. * char *string - string to change to lower case
  70. *
  71. *Exit:
  72. * input string address
  73. *
  74. *Exceptions:
  75. * The original string is returned unchanged on any error.
  76. *
  77. *******************************************************************************/
  78. char * __cdecl _strlwr (
  79. char * string
  80. )
  81. {
  82. #if !defined(_NTSUBSET_) && !defined(_POSIX_)
  83. int dstlen; /* len of dst string, with null */
  84. unsigned char *dst; /* destination string */
  85. int malloc_flag = 0;
  86. #ifdef _MT
  87. pthreadlocinfo ptloci = _getptd()->ptlocinfo;
  88. if ( ptloci != __ptlocinfo )
  89. ptloci = __updatetlocinfo();
  90. if ( ptloci->lc_handle[LC_CTYPE] == _CLOCALEHANDLE )
  91. #else
  92. if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE )
  93. #endif
  94. {
  95. char *cp; /* traverses string for C locale conversion */
  96. for ( cp = string ; *cp ; ++cp )
  97. if ( ('A' <= *cp) && (*cp <= 'Z') )
  98. *cp -= 'A' - 'a';
  99. return(string);
  100. } /* C locale */
  101. /* Inquire size of dst string */
  102. #ifdef _MT
  103. if ( 0 == (dstlen = __crtLCMapStringA( ptloci->lc_handle[LC_CTYPE],
  104. #else
  105. if ( 0 == (dstlen = __crtLCMapStringA( __lc_handle[LC_CTYPE],
  106. #endif
  107. LCMAP_LOWERCASE,
  108. string,
  109. -1,
  110. NULL,
  111. 0,
  112. #ifdef _MT
  113. ptloci->lc_codepage,
  114. #else
  115. __lc_codepage,
  116. #endif
  117. TRUE )) )
  118. return(string);
  119. /* Allocate space for dst */
  120. __try {
  121. dst = (unsigned char *)_alloca(dstlen * sizeof(unsigned char));
  122. }
  123. __except( EXCEPTION_EXECUTE_HANDLER )
  124. {
  125. _resetstkoflw();
  126. dst = NULL;
  127. }
  128. if ( dst == NULL ) {
  129. dst = (unsigned char *)_malloc_crt(dstlen * sizeof(unsigned char));
  130. malloc_flag++;
  131. }
  132. /* Map src string to dst string in alternate case */
  133. if ( (dst != NULL) &&
  134. #ifdef _MT
  135. (__crtLCMapStringA( ptloci->lc_handle[LC_CTYPE],
  136. #else
  137. (__crtLCMapStringA( __lc_handle[LC_CTYPE],
  138. #endif
  139. LCMAP_LOWERCASE,
  140. string,
  141. -1,
  142. dst,
  143. dstlen,
  144. #ifdef _MT
  145. ptloci->lc_codepage,
  146. #else
  147. __lc_codepage,
  148. #endif
  149. TRUE ) != 0) )
  150. /* copy dst string to return string */
  151. strcpy(string, dst);
  152. if ( malloc_flag )
  153. _free_crt(dst);
  154. return(string);
  155. #else
  156. char * cp;
  157. for (cp=string; *cp; ++cp)
  158. {
  159. if ('A' <= *cp && *cp <= 'Z')
  160. *cp += 'a' - 'A';
  161. }
  162. return(string);
  163. #endif
  164. }