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.

169 lines
4.1 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. wvsprintfW(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. wvsprintfW(msgbuff, fmtstr, args);
  76. va_end(args);
  77. //---- add a CR/LF at end ----
  78. lstrcat(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. LPSTR szAnsiBuff = (LPSTR)alloca(n+1);
  115. if (! szAnsiBuff)
  116. return MakeError32(E_OUTOFMEMORY);
  117. if (! ReadFile(_hFile, szAnsiBuff, n, lpcb, NULL))
  118. hr = MakeErrorLast();
  119. else
  120. {
  121. USES_CONVERSION;
  122. szAnsiBuff[*lpcb] = 0; // zero terminate
  123. LPCWSTR pw = A2W(szAnsiBuff);
  124. memcpy(lpBuffer, pw, lstrlen(pw)); // no room for NULL
  125. }
  126. }
  127. else
  128. {
  129. if (! ReadFile(_hFile, lpBuffer, n, lpcb, NULL))
  130. hr = MakeErrorLast();
  131. }
  132. return hr;
  133. }
  134. };
  135. //---------------------------------------------------------------------------
  136. #endif // CFILE_H