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.

189 lines
4.9 KiB

  1. //---------------------------------------------------------------------------
  2. // cfile.h - file read/write (ansi/unicode) helper class
  3. //---------------------------------------------------------------------------
  4. #pragma once
  5. //---------------------------------------------------------------------------
  6. #ifndef CFILE_H
  7. #define CFILE_H
  8. //---------------------------------------------------------------------------
  9. class CSimpleFile
  10. {
  11. public:
  12. HANDLE _hFile;
  13. BOOL _fAnsi;
  14. CSimpleFile()
  15. {
  16. _hFile = INVALID_HANDLE_VALUE;
  17. _fAnsi = FALSE;
  18. }
  19. BOOL IsOpen()
  20. {
  21. return _hFile != INVALID_HANDLE_VALUE;
  22. }
  23. HRESULT Create(LPCWSTR lpsz, BOOL fAnsi=FALSE)
  24. {
  25. _fAnsi = fAnsi;
  26. _hFile = CreateFile(lpsz, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
  27. if (_hFile == INVALID_HANDLE_VALUE)
  28. return MakeErrorLast();
  29. return S_OK;
  30. }
  31. HRESULT Open(LPCWSTR lpsz, BOOL fAnsi=FALSE, BOOL fExclusive=FALSE)
  32. {
  33. _fAnsi = fAnsi;
  34. DWORD dwShare = 0;
  35. if (! fExclusive)
  36. dwShare = FILE_SHARE_READ;
  37. _hFile = CreateFile(lpsz, GENERIC_READ, dwShare, NULL, OPEN_EXISTING, 0, NULL);
  38. if (_hFile == INVALID_HANDLE_VALUE)
  39. return MakeErrorLast();
  40. return S_OK;
  41. }
  42. HRESULT Append(LPCWSTR lpsz, BOOL fAnsi=FALSE)
  43. {
  44. _fAnsi = fAnsi;
  45. _hFile = CreateFile(lpsz, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
  46. if (_hFile == INVALID_HANDLE_VALUE)
  47. return MakeErrorLast();
  48. // cannot rely on retval from SetFilePointer() so must use GetLastError()
  49. SetLastError(0);
  50. SetFilePointer(_hFile, 0, NULL, FILE_END);
  51. if (GetLastError() != NO_ERROR)
  52. return MakeErrorLast();
  53. return S_OK;
  54. }
  55. DWORD GetFileSize(DWORD* pdw = NULL)
  56. {
  57. return ::GetFileSize(_hFile, pdw);
  58. }
  59. HRESULT Printf(LPCWSTR fmtstr, ...)
  60. {
  61. va_list args;
  62. va_start(args, fmtstr);
  63. WCHAR msgbuff[2048];
  64. //---- format caller's string ----
  65. StringCchVPrintfW(msgbuff, ARRAYSIZE(msgbuff), fmtstr, args);
  66. va_end(args);
  67. return Write((void*)msgbuff, lstrlen(msgbuff)*sizeof(WCHAR));
  68. }
  69. HRESULT OutLine(LPCWSTR fmtstr, ...)
  70. {
  71. va_list args;
  72. va_start(args, fmtstr);
  73. WCHAR msgbuff[2048];
  74. //---- format caller's string ----
  75. StringCchVPrintfW(msgbuff, ARRAYSIZE(msgbuff), fmtstr, args);
  76. va_end(args);
  77. //---- add a CR/LF at end ----
  78. StringCchCatW(msgbuff, ARRAYSIZE(msgbuff), L"\r\n");
  79. return Write((void*)msgbuff, lstrlen(msgbuff)*sizeof(WCHAR));
  80. }
  81. ~CSimpleFile()
  82. {
  83. Close();
  84. }
  85. void Close()
  86. {
  87. if (_hFile != INVALID_HANDLE_VALUE)
  88. CloseHandle(_hFile);
  89. _hFile = INVALID_HANDLE_VALUE;
  90. }
  91. HRESULT Write(void* pv, DWORD dwBytes)
  92. {
  93. HRESULT hr = S_OK;
  94. DWORD dw;
  95. if (_fAnsi)
  96. {
  97. USES_CONVERSION;
  98. LPSTR p = W2A((LPCWSTR)pv);
  99. if (! WriteFile(_hFile, p, dwBytes/sizeof(WCHAR), &dw, NULL))
  100. hr = MakeErrorLast();
  101. }
  102. else
  103. {
  104. if (! WriteFile(_hFile, pv, dwBytes, &dw, NULL))
  105. hr = MakeErrorLast();
  106. }
  107. return hr;
  108. }
  109. HRESULT Read(void* lpBuffer, DWORD n, DWORD* lpcb)
  110. {
  111. HRESULT hr = S_OK;
  112. if (_fAnsi)
  113. {
  114. ASSERT(FALSE); // you don't want to be here. Here's why (exercise for the reader):
  115. #if 0
  116. LPSTR pszAnsiBuff = new CHAR[n+1];
  117. if (pszAnsiBuff)
  118. {
  119. if (ReadFile(_hFile, pszAnsiBuff, n, lpcb, NULL))
  120. {
  121. // Copy out as unicode.
  122. USES_CONVERSION;
  123. pszAnsiBuff[*lpcb] = 0; // zero terminate
  124. // THIS IS UNACCEPTABLE:
  125. #error I'm an idiot.
  126. LPCWSTR pwsz = A2W(pszAnsiBuff);
  127. // CALLER GETS SCREWED HERE (half of the contents of the file):
  128. CopyMemory(lpBuffer, pwsz, lstrlen(pwsz)); // no room for NULL
  129. }
  130. else
  131. {
  132. hr = MakeErrorLast();
  133. }
  134. delete [] pszAnsiBuff;
  135. }
  136. else
  137. {
  138. hr = MakeError32(E_OUTOFMEMORY);
  139. }
  140. #endif 0
  141. }
  142. else
  143. {
  144. if (! ReadFile(_hFile, lpBuffer, n, lpcb, NULL))
  145. hr = MakeErrorLast();
  146. }
  147. return hr;
  148. }
  149. };
  150. //---------------------------------------------------------------------------
  151. #endif // CFILE_H