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.

153 lines
3.5 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. SimpleFp.cpp
  5. Abstract:
  6. simple file pointer, to replace msvcrt.dll
  7. Author:
  8. Xiaoyu Wu(xiaoyuw) July 2000
  9. Revision History:
  10. --*/
  11. #include "stdinc.h"
  12. #if SXS_PRECOMPILED_MANIFESTS_ENABLED
  13. #include "simplefp.h"
  14. #include "fusiontrace.h"
  15. #include "csxspreservelasterror.h"
  16. #include "util.h"
  17. // NTRAID#NTBUG9 - 591005 - 2002/03/30 - mgrier - Operations which may fail may not be in a constructor
  18. CSimpleFileStream::CSimpleFileStream(PCWSTR pFileName)
  19. {
  20. BOOL fSuccess = FALSE;
  21. FN_TRACE_WIN32(fSuccess);
  22. if (!pFileName)
  23. {
  24. // duplicate it so we can close it like normal
  25. // NTRAID#NTBUG9 - 591005 - 2002/03/30 - mgrier - Missing error check from GetStdHandle
  26. HANDLE hFile = ::GetStdHandle(STD_ERROR_HANDLE);
  27. IFW32FALSE_ORIGINATE_AND_EXIT(::DuplicateHandle(::GetCurrentProcess(), hFile, ::GetCurrentProcess(), &m_hFile, 0, FALSE, DUPLICATE_SAME_ACCESS));
  28. }
  29. else
  30. {
  31. IFCOMFAILED_EXIT(this->fopen(pFileName));
  32. }
  33. fSuccess = FALSE;
  34. Exit:
  35. ;
  36. }
  37. CSimpleFileStream::~CSimpleFileStream()
  38. {
  39. if ( m_hFile != INVALID_HANDLE_VALUE) // if it is GetStdHandle, Could I close the handle?
  40. {
  41. CSxsPreserveLastError ple;
  42. this->fclose();
  43. ple.Restore();
  44. }
  45. }
  46. HRESULT
  47. CSimpleFileStream::fopen(
  48. PCWSTR pFileName
  49. )
  50. {
  51. FN_PROLOG_HR
  52. if (m_hFile != INVALID_HANDLE_VALUE)
  53. IFCOMFAILED_EXIT(this->fclose());
  54. INTERNAL_ERROR_CHECK(m_hFile == INVALID_HANDLE_VALUE);
  55. IFW32INVALIDHANDLE_ORIGINATE_AND_EXIT(
  56. m_hFile = ::CreateFileW(
  57. pFileName,
  58. GENERIC_WRITE,
  59. 0,
  60. NULL,
  61. CREATE_ALWAYS,
  62. 0,
  63. NULL));
  64. FN_EPILOG
  65. }
  66. HRESULT
  67. CSimpleFileStream::fclose()
  68. {
  69. FN_PROLOG_HR
  70. HANDLE hFile;
  71. INTERNAL_ERROR_CHECK(m_hFile != INVALID_HANDLE_VALUE);
  72. hFile = m_hFile;
  73. m_hFile = INVALID_HANDLE_VALUE;
  74. IFW32FALSE_ORIGINATE_AND_EXIT(::CloseHandle(hFile));
  75. FN_EPILOG
  76. }
  77. HRESULT
  78. CSimpleFileStream::fprintf(
  79. const char *format,
  80. ...
  81. )
  82. {
  83. FN_PROLOG_HR
  84. va_list ap;
  85. char rgchBuffer[2048];
  86. int cchIn = 0;
  87. DWORD cchWritten = 0;
  88. INTERNAL_ERROR_CHECK(m_hFile != INVALID_HANDLE_VALUE);
  89. va_start(ap, format);
  90. cchIn = _vsnprintf(rgchBuffer, NUMBER_OF(rgchBuffer) - 1, format, ap);
  91. rgchBuffer[NUMBER_OF(rgchBuffer) - 1] = 0;
  92. va_end(ap);
  93. // NTRAID#NTBUG9 - 591005 - 2002/03/30 - mgrier - this should be an origination and we should
  94. // probably use errno to generate a more useful error code.
  95. if (cchIn < 0)
  96. IFCOMFAILED_EXIT(E_UNEXPECTED);
  97. IFW32FALSE_ORIGINATE_AND_EXIT(::WriteFile(m_hFile, rgchBuffer, cchIn, &cchWritten, NULL));
  98. FN_EPILOG
  99. }
  100. HRESULT CSimpleFileStream::fwrite(const VOID* pData, SIZE_T itemsize, SIZE_T itemcount)
  101. {
  102. FN_PROLOG_HR
  103. SIZE_T count = 0;
  104. DWORD ByteWritten = 0;
  105. INTERNAL_ERROR_CHECK(m_hFile != INVALID_HANDLE_VALUE);
  106. count = itemsize * itemcount;
  107. while (count > ULONG_MAX)
  108. {
  109. IFW32FALSE_ORIGINATE_AND_EXIT(::WriteFile(m_hFile, pData, ULONG_MAX, &ByteWritten, NULL));
  110. count -= ULONG_MAX;
  111. pData = (const void *) (((ULONG_PTR) pData) + ULONG_MAX);
  112. }
  113. if (count != 0)
  114. {
  115. IFW32FALSE_ORIGINATE_AND_EXIT(::WriteFile(m_hFile, pData, static_cast<DWORD>(count), &ByteWritten, NULL));
  116. }
  117. FN_EPILOG
  118. }
  119. #endif