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.

187 lines
4.7 KiB

  1. /***
  2. *_toupper.c - convert character to uppercase
  3. *
  4. * Copyright (c) 1996-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines _Toupper()
  8. *
  9. *Revision History:.
  10. * 01-XX-96 PJP Created from toupper.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. * 03-17-97 RDK Added error flag to __crtLCMapStringA.
  14. * 05-17-99 PML Remove all Macintosh support.
  15. * 01-29-01 GB Added _func function version of data variable used in
  16. * msvcprt.lib to work with STATIC_CPPLIB
  17. * 03-12-01 PML Use supplied locale to check case VS7#190902
  18. * 04-03-01 PML Reverse lead/trail bytes in composed char (vs7#232853)
  19. * 04-26-02 GB Fixed problem with operator precedence. problem was
  20. * !ploc->_Table[c]&_LOWER
  21. * 04-29-02 GB Added try-finally arounds lock-unlock.
  22. *
  23. *******************************************************************************/
  24. #include <cruntime.h>
  25. #include <ctype.h>
  26. #include <stddef.h>
  27. #include <xlocinfo.h>
  28. #include <locale.h>
  29. #include <setlocal.h>
  30. #include <mtdll.h>
  31. #include <awint.h>
  32. /* remove macro definitions of _toupper() and toupper()
  33. */
  34. #undef _toupper
  35. #undef toupper
  36. /***
  37. *int _toupper(c) - convert character to uppercase
  38. *
  39. *Purpose:
  40. * _toupper() is a version of toupper with a locale argument.
  41. *
  42. *Entry:
  43. * c - int value of character to be converted
  44. * const _Ctypevec * = pointer to locale info
  45. *
  46. *Exit:
  47. * returns int value of uppercase representation of c
  48. *
  49. *Exceptions:
  50. *
  51. *******************************************************************************/
  52. #ifdef _MT
  53. int __cdecl _Toupper_lk (
  54. int c,
  55. const _Ctypevec *ploc
  56. );
  57. #endif
  58. _CRTIMP2 int __cdecl _Toupper (
  59. int c,
  60. const _Ctypevec *ploc
  61. )
  62. {
  63. #ifdef _MT
  64. LCID handle;
  65. int local_lock_flag;
  66. if (ploc == 0)
  67. handle = ___lc_handle_func()[LC_CTYPE];
  68. else
  69. handle = ploc->_Hand;
  70. if (handle == _CLOCALEHANDLE)
  71. {
  72. if ( (c >= 'a') && (c <= 'z') )
  73. c = c - ('a' - 'A');
  74. return c;
  75. }
  76. _lock_locale( local_lock_flag )
  77. __TRY
  78. c = _Toupper_lk(c, ploc);
  79. __FINALLY
  80. _unlock_locale( local_lock_flag )
  81. __END_TRY_FINALLY
  82. return c;
  83. }
  84. /***
  85. *int _toupper_lk(c) - convert character to uppercase
  86. *
  87. *Purpose:
  88. * Multi-thread function only! Non-locking version of toupper.
  89. *
  90. *Entry:
  91. *
  92. *Exit:
  93. *
  94. *Exceptions:
  95. *
  96. *******************************************************************************/
  97. int __cdecl _Toupper_lk (
  98. int c,
  99. const _Ctypevec *ploc
  100. )
  101. {
  102. #endif /* _MT */
  103. int size;
  104. unsigned char inbuffer[3];
  105. unsigned char outbuffer[3];
  106. LCID handle;
  107. UINT codepage;
  108. if (ploc == 0)
  109. {
  110. handle = ___lc_handle_func()[LC_CTYPE];
  111. codepage = ___lc_codepage_func();
  112. }
  113. else
  114. {
  115. handle = ploc->_Hand;
  116. codepage = ploc->_Page;
  117. }
  118. if (handle == _CLOCALEHANDLE)
  119. {
  120. if ( (c >= 'a') && (c <= 'z') )
  121. c = c - ('a' - 'A');
  122. return c;
  123. }
  124. /* if checking case of c does not require API call, do it */
  125. if ((unsigned)c < 256)
  126. {
  127. if (ploc == 0)
  128. {
  129. if (!islower(c))
  130. {
  131. return c;
  132. }
  133. }
  134. else
  135. {
  136. if (!(ploc->_Table[c] & _LOWER))
  137. {
  138. return c;
  139. }
  140. }
  141. }
  142. /* convert int c to multibyte string */
  143. if (_cpp_isleadbyte(c >> 8 & 0xff))
  144. {
  145. inbuffer[0] = (c >> 8 & 0xff);
  146. inbuffer[1] = (unsigned char)c;
  147. inbuffer[2] = 0;
  148. size = 2;
  149. } else {
  150. inbuffer[0] = (unsigned char)c;
  151. inbuffer[1] = 0;
  152. size = 1;
  153. }
  154. /* convert wide char to uppercase */
  155. if (0 == (size = __crtLCMapStringA(handle, LCMAP_UPPERCASE,
  156. inbuffer, size, outbuffer, 3, codepage, TRUE)))
  157. {
  158. return c;
  159. }
  160. /* construct integer return value */
  161. if (size == 1)
  162. return ((int)outbuffer[0]);
  163. else
  164. return ((int)outbuffer[1] | ((int)outbuffer[0] << 8));
  165. }