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.

129 lines
2.2 KiB

  1. /***********************************************************************
  2. * Microsoft (R) Windows (R) Resource Compiler
  3. *
  4. * Copyright (c) Microsoft Corporation. All rights reserved.
  5. *
  6. * File Comments:
  7. *
  8. *
  9. ***********************************************************************/
  10. #include "rc.h"
  11. #include "output.h"
  12. bool fStdOutConsole;
  13. bool FIsConsole(FILE *fd)
  14. {
  15. int fh = _fileno(fd);
  16. HANDLE hFile = (HANDLE) _get_osfhandle(fh);
  17. DWORD dwType = GetFileType(hFile);
  18. if (dwType != FILE_TYPE_CHAR) {
  19. return false;
  20. }
  21. switch (fh) {
  22. case 0 :
  23. hFile = GetStdHandle(STD_INPUT_HANDLE);
  24. break;
  25. case 1 :
  26. hFile = GetStdHandle(STD_OUTPUT_HANDLE);
  27. break;
  28. case 2 :
  29. hFile = GetStdHandle(STD_ERROR_HANDLE);
  30. break;
  31. }
  32. DWORD dwMode;
  33. if (!GetConsoleMode(hFile, &dwMode)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. int ConsoleVprintf(const wchar_t *szFormat, va_list valist)
  39. {
  40. // UNDONE: This exists only because the CRT lacks a va_list cwprintf variant
  41. int cch = _vscwprintf(szFormat, valist);
  42. wchar_t *sz = (wchar_t *) malloc((cch + 1) * sizeof(wchar_t));
  43. if (sz) {
  44. vswprintf(sz, szFormat, valist);
  45. _cputws(sz);
  46. free(sz);
  47. }
  48. return cch;
  49. }
  50. void OutputInit()
  51. {
  52. if (FIsConsole(stdout)) {
  53. fStdOutConsole = true;
  54. }
  55. }
  56. int StdOutFlush()
  57. {
  58. if (fStdOutConsole) {
  59. return 0;
  60. }
  61. return fflush(stdout);
  62. }
  63. int __cdecl StdOutPrintf(const wchar_t *szFormat, ...)
  64. {
  65. va_list valist;
  66. va_start(valist, szFormat);
  67. int ret = StdOutVprintf(szFormat, valist);
  68. va_end(valist);
  69. return ret;
  70. }
  71. int StdOutPutc(wchar_t ch)
  72. {
  73. if (fStdOutConsole) {
  74. return _putwch(ch);
  75. }
  76. return fputwc(ch, stdout);
  77. }
  78. int StdOutPuts(const wchar_t *sz)
  79. {
  80. if (fStdOutConsole) {
  81. return _cputws(sz);
  82. }
  83. return fputws(sz, stdout);
  84. }
  85. int StdOutVprintf(const wchar_t *szFormat, va_list valist)
  86. {
  87. if (fStdOutConsole) {
  88. return ConsoleVprintf(szFormat, valist);
  89. }
  90. return vfwprintf(stdout, szFormat, valist);
  91. }