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.

223 lines
4.8 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. // File: PRINTFOA.C
  3. //
  4. //============================================================================
  5. // Include Files:
  6. //===============
  7. #include <printfoa.h>
  8. // Extern Data:
  9. //=============
  10. // Global Data:
  11. //=============
  12. // Function Prototypes:
  13. //=====================
  14. // Code:
  15. //======
  16. #undef printf
  17. #undef wprintf
  18. // Function: int ANSI2OEM_Printf(const char *format, ...)
  19. //=======================================================
  20. //
  21. // Desc: Takes ANSI code page characters and prints them out
  22. // in OEM code page
  23. //
  24. // Input:
  25. //
  26. // Return:
  27. //
  28. // Misc:
  29. //
  30. //=======================================================
  31. int ANSI2OEM_Printf(const char *format, ...)
  32. {
  33. va_list arg_marker;
  34. char buffer[256];
  35. WCHAR uniBuffer[256];
  36. // sprintf the buffer
  37. va_start(arg_marker, format);
  38. vsprintf(buffer, format, arg_marker);
  39. va_end(arg_marker);
  40. if (GetACP() == GetOEMCP()) {
  41. // In case of Far East, ACP and OEMCP are equal, then return.
  42. return printf(buffer);
  43. }
  44. // clear it out
  45. memset(uniBuffer, 0, sizeof(uniBuffer));
  46. // convert it to unicode
  47. MultiByteToWideChar(CP_ACP, 0, buffer, strlen(buffer), uniBuffer,
  48. sizeof(uniBuffer) / sizeof(WCHAR));
  49. // change the code page of the buffer
  50. CharToOemW(uniBuffer, buffer);
  51. // do the actual printf
  52. return printf(buffer);
  53. }
  54. // end - int ANSI2OEM_Printf(const char *format, ...)
  55. // Function: int ANSI2OEM_Wprintf(const wchar_t *format, ...)
  56. //=======================================================
  57. //
  58. // Desc: Takes ANSI code page characters and prints them out
  59. // in OEM code page
  60. //
  61. // Input:
  62. //
  63. // Return:
  64. //
  65. // Misc:
  66. //
  67. //=======================================================
  68. int ANSI2OEM_Wprintf(const wchar_t *format, ...)
  69. {
  70. va_list arg_marker;
  71. wchar_t buffer[256];
  72. char oemBuffer[256];
  73. // do the sprintf
  74. va_start(arg_marker, format);
  75. wvsprintf(buffer, format, arg_marker);
  76. va_end(arg_marker);
  77. if (GetACP() == GetOEMCP()) {
  78. // In case of Far East, ACP and OEMCP are equal, then return.
  79. return wprintf(buffer);
  80. }
  81. // clear the buffer
  82. memset(oemBuffer, 0, sizeof(oemBuffer));
  83. // change the code page of the buffer (this function outputs ascii)
  84. CharToOemW(buffer, oemBuffer);
  85. return printf(oemBuffer);
  86. }
  87. // end - int ANSI2OEM_Wprintf(const wchar_t *format, ...)
  88. // Function: void OEM2ANSIW(WCHAR *buffer, USHORT len)
  89. //=======================================================
  90. //
  91. // Desc: converts wide characters from the OEM code page to
  92. // ANSI
  93. //
  94. // Input:
  95. //
  96. // Return:
  97. //
  98. // Misc:
  99. //
  100. //=======================================================
  101. void OEM2ANSIW(WCHAR *buffer, USHORT len)
  102. {
  103. int BufferNeeded;
  104. char *temp = NULL;
  105. WCHAR *cvt;
  106. if (GetACP() == GetOEMCP()) {
  107. // In case of Far East, ACP and OEMCP are equal, then return.
  108. return;
  109. }
  110. // allocate a wide character buffer
  111. cvt = (WCHAR *) LocalAlloc( 0, (len+1) * sizeof(WCHAR) );
  112. if (cvt) {
  113. // determine the buffer size needed for the multi byte string
  114. BufferNeeded = WideCharToMultiByte(CP_OEMCP, 0, buffer, len, NULL, 0,
  115. NULL, NULL);
  116. // allocate the temporary buffer
  117. temp = (char *)LocalAlloc(0, BufferNeeded+1);
  118. if (temp) {
  119. // clear them out
  120. memset(temp, 0, BufferNeeded+1);
  121. memset(cvt, 0, (len + 1) * sizeof(WCHAR));
  122. // convert the incoming wide buffer to a multi byte buffer
  123. WideCharToMultiByte(CP_OEMCP, 0, buffer, len, temp, BufferNeeded+1,
  124. NULL, NULL);
  125. // convert the oem multi byte buffer to ansi (wide)
  126. OemToCharW(temp, cvt);
  127. // copy the buffer onto the orginal
  128. wcscpy(buffer, cvt);
  129. }
  130. }
  131. // clean up
  132. if (cvt)
  133. LocalFree(cvt);
  134. if (temp)
  135. LocalFree(temp);
  136. }
  137. // end - void OEM2ANSIW(WCHAR *buffer, USHORT len)
  138. // Function: void OEM2ANSIA(char *buffer, USHORT len)
  139. //=======================================================
  140. //
  141. // Desc: converts ascii characters from the OEM code page to
  142. // ANSI
  143. //
  144. // Input:
  145. //
  146. // Return:
  147. //
  148. // Misc:
  149. //
  150. //=======================================================
  151. void OEM2ANSIA(char *buffer, USHORT len)
  152. {
  153. WCHAR *temp;
  154. if (GetACP() == GetOEMCP()) {
  155. // In case of Far East, ACP and OEMCP are equal, then return.
  156. return;
  157. }
  158. temp = (WCHAR *) LocalAlloc(9, (len+1) * sizeof(WCHAR));
  159. if (temp) {
  160. // set the buffer
  161. memset(temp, 0, (len+1) * sizeof(WCHAR));
  162. // convert the oem multi byte buffer to ansi (wide)
  163. OemToCharW(buffer, temp);
  164. // convert from wide back to multi byte
  165. WideCharToMultiByte(CP_OEMCP, 0, temp, wcslen(temp), buffer, len+1, NULL, NULL);
  166. // clean up
  167. LocalFree(temp);
  168. }
  169. }
  170. // end - void OEM2ANSIA(char *buffer, USHORT len)
  171. // - end