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.

188 lines
4.0 KiB

  1. /***
  2. *putwch.c - write a wide character to console
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _putwch() - writes a wide character to a console
  8. *
  9. *Revision History:
  10. * 02-11-00 GB Module Created.
  11. * 04-25-00 GB Made putwch more robust in using WriteConsoleW
  12. * 05-17-00 GB Use ERROR_CALL_NOT_IMPLEMENTED for existance of W API
  13. * 11-22-00 PML Wide-char *putwc* functions take a wchar_t, not wint_t.
  14. *
  15. *******************************************************************************/
  16. #ifndef _POSIX_
  17. #include <stdlib.h>
  18. #include <conio.h>
  19. #include <io.h>
  20. #include <errno.h>
  21. #include <cruntime.h>
  22. #include <stdio.h>
  23. #include <dbgint.h>
  24. #include <file2.h>
  25. #include <internal.h>
  26. #include <mtdll.h>
  27. #include <limits.h>
  28. /*
  29. * declaration for console handle
  30. */
  31. extern intptr_t _confh;
  32. /***
  33. *wint_t _putwch(ch) - write a wide character to a console
  34. *
  35. *Purpose:
  36. * Writes a wide character to a console.
  37. *
  38. *Entry:
  39. * wchar_t ch - wide character to write
  40. *
  41. *Exit:
  42. * returns the wide character if successful
  43. * returns WEOF if fails
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48. #ifdef _MT
  49. wint_t _CRTIMP __cdecl _putwch (
  50. wchar_t ch
  51. )
  52. {
  53. REG2 wint_t retval;
  54. _mlock(_CONIO_LOCK);
  55. __try {
  56. retval = _putwch_lk(ch);
  57. }
  58. __finally {
  59. _munlock(_CONIO_LOCK);
  60. }
  61. return(retval);
  62. }
  63. /***
  64. *_putwch_lk() - _putwch() core routine (locked version)
  65. *
  66. *Purpose:
  67. * Core _putwch() routine; assumes stream is already locked.
  68. *
  69. * [See _putwch() above for more info.]
  70. *
  71. *Entry: [See _putwch()]
  72. *
  73. *Exit: [See _putwch()]
  74. *
  75. *Exceptions:
  76. *
  77. *******************************************************************************/
  78. wint_t __cdecl _putwch_lk (
  79. wchar_t ch
  80. )
  81. {
  82. #else
  83. wint_t _CRTIMP __cdecl _putwch (
  84. wchar_t ch
  85. )
  86. {
  87. #endif
  88. int size, i, num_written;
  89. static int use_w = 2;
  90. char mbc[MB_LEN_MAX +1];
  91. if ( use_w)
  92. {
  93. if (_confh == -2)
  94. __initconout();
  95. /* write character to console file handle */
  96. if (_confh == -1)
  97. return WEOF;
  98. else if ( !WriteConsoleW( (HANDLE)_confh,
  99. (LPVOID)&ch,
  100. 1,
  101. &num_written,
  102. NULL )
  103. )
  104. {
  105. if ( use_w == 2 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
  106. use_w = 0;
  107. else
  108. return WEOF;
  109. } else
  110. use_w = 1;
  111. }
  112. if ( use_w == 0)
  113. {
  114. size = WideCharToMultiByte(
  115. GetConsoleOutputCP(),
  116. 0,
  117. (LPWSTR)&ch, 1,
  118. mbc,
  119. MB_LEN_MAX,
  120. NULL,
  121. NULL
  122. );
  123. for ( i = 0; i < size; i++)
  124. {
  125. if (_putch_lk(mbc[i]) == EOF)
  126. return WEOF;
  127. }
  128. }
  129. return ch;
  130. }
  131. /***
  132. * _cputws() - _cputws() writes a wide char string to console.
  133. *
  134. * Purpose:
  135. * Writes a wide char string to console.
  136. *
  137. * Entry:
  138. * str: pointer to string
  139. * Exit:
  140. * returns 0 if sucessful. Nonzero if unsucessful
  141. *
  142. *******************************************************************************/
  143. int _CRTIMP __cdecl _cputws(
  144. const wchar_t *str
  145. )
  146. {
  147. size_t len;
  148. int retval = 0;
  149. len = wcslen(str);
  150. #ifdef _MT
  151. _mlock(_CONIO_LOCK);
  152. __try {
  153. #endif
  154. while(len--)
  155. {
  156. if ( _putwch_lk(*str++) == WEOF)
  157. {
  158. retval = -1;
  159. break;
  160. }
  161. }
  162. #ifdef _MT
  163. }
  164. __finally {
  165. _munlock(_CONIO_LOCK);
  166. }
  167. #endif
  168. return retval;
  169. }
  170. #endif /* _POSIX_ */