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.

249 lines
7.5 KiB

  1. #ifndef _FILEIO_H
  2. #define _FILEIO_H
  3. #define WIN32_MEAN_AND_LEAN
  4. #define WIN32_EXTRA_LEAN
  5. class CFileIO {
  6. public:
  7. CFileIO(LPCTSTR szFileName = NULL)
  8. {
  9. m_hFile = NULL;
  10. m_TotalFileSize = 0;
  11. Open(szFileName);
  12. }
  13. ~CFileIO()
  14. {
  15. Close();
  16. }
  17. ////////////////////////////////////////////////////////////////////
  18. //
  19. // Function Name: Open()
  20. // Purpose: open a given file, for reading and writing
  21. // Parameters:
  22. // LPCTSTR szFileName - File name to open
  23. //
  24. ////////////////////////////////////////////////////////////////////
  25. HRESULT Open(LPCTSTR szFileName, BOOL bCreate = FALSE){
  26. if(NULL != szFileName){
  27. DWORD dwOpen = OPEN_EXISTING;
  28. if(bCreate){
  29. dwOpen = OPEN_ALWAYS;
  30. }
  31. m_hFile = CreateFile(szFileName,
  32. GENERIC_READ|GENERIC_WRITE,
  33. FILE_SHARE_READ|FILE_SHARE_WRITE,
  34. NULL,
  35. dwOpen,
  36. FILE_ATTRIBUTE_NORMAL,
  37. NULL);
  38. BY_HANDLE_FILE_INFORMATION FileInfo;
  39. memset(&FileInfo,0,sizeof(BY_HANDLE_FILE_INFORMATION));
  40. GetFileInformationByHandle(m_hFile,&FileInfo);
  41. m_TotalFileSize = FileInfo.nFileSizeLow;
  42. }
  43. m_bReady = (INVALID_HANDLE_VALUE != m_hFile);
  44. if(m_bReady)
  45. return S_OK;
  46. return E_FAIL;
  47. }
  48. ////////////////////////////////////////////////////////////////////
  49. //
  50. // Function Name: FileSize()
  51. // Purpose: return the file size of the currently opened file
  52. // Parameters: none
  53. //
  54. ////////////////////////////////////////////////////////////////////
  55. LONG FileSize(){
  56. return m_TotalFileSize;
  57. }
  58. ////////////////////////////////////////////////////////////////////
  59. //
  60. // Function Name: EraseFile()
  61. // Purpose: erase file contents, by resetting the EOF
  62. // Parameters: none
  63. //
  64. ////////////////////////////////////////////////////////////////////
  65. VOID EraseFile(){
  66. SetEndOfFile(m_hFile);
  67. }
  68. ////////////////////////////////////////////////////////////////////
  69. //
  70. // Function Name: WriteEOL()
  71. // Purpose: write a carriage return, line feed to the file
  72. // Parameters: none
  73. //
  74. ////////////////////////////////////////////////////////////////////
  75. BOOL WriteEOL(){
  76. DWORD dwBytesWritten = 0;
  77. TCHAR EndOfLine[2];
  78. EndOfLine[0] = TEXT('\r');
  79. EndOfLine[1] = TEXT('\n');
  80. return WriteFile(EndOfLine,2,&dwBytesWritten,NULL);
  81. }
  82. ////////////////////////////////////////////////////////////////////
  83. //
  84. // Function Name: Close()
  85. // Purpose: close the opened file
  86. // Parameters: none
  87. //
  88. ////////////////////////////////////////////////////////////////////
  89. VOID Close(){
  90. if(NULL != m_hFile){
  91. CloseHandle(m_hFile);
  92. m_hFile = NULL;
  93. }
  94. }
  95. ////////////////////////////////////////////////////////////////////
  96. //
  97. // Function Name: ReadLine()
  98. // Purpose: read a text line from a text-base file
  99. // Parameters:
  100. // LPTSTR szTextLine - buffer for text line data
  101. // LONG lBufferSize - size of szTextLine buffer
  102. // PLONG pNumTCHARs - number of characters read (can be NULL)
  103. //
  104. ////////////////////////////////////////////////////////////////////
  105. BOOL ReadLine (LPTSTR szTextLine, LONG lBufferSize, PLONG pNumTCHARs){
  106. TCHAR ch = TEXT(' ');
  107. LONG index = 0;
  108. DWORD dwBytesRead = 1;
  109. DWORD dwTotalBytesRead = 0;
  110. memset(szTextLine,0,lBufferSize);
  111. while(ch != TEXT('\r') &&
  112. ch != TEXT('\n') &&
  113. dwBytesRead > 0) {
  114. if(!ReadFile(&ch,sizeof(TCHAR),&dwBytesRead,NULL)){
  115. return FALSE;
  116. }
  117. if(dwBytesRead > 0){
  118. szTextLine[index] = ch;
  119. dwTotalBytesRead += dwBytesRead;
  120. index++;
  121. }
  122. }
  123. //
  124. // rip to next line...
  125. //
  126. while(ch == TEXT('\r') && dwBytesRead > 0){
  127. ch = TEXT(' ');
  128. if(!ReadFile(&ch,sizeof(TCHAR),&dwBytesRead,NULL)) {
  129. return FALSE;
  130. }
  131. }
  132. if(NULL != pNumTCHARs){
  133. *pNumTCHARs = 0;
  134. *pNumTCHARs = (dwTotalBytesRead/sizeof(TCHAR));
  135. }
  136. if(dwTotalBytesRead > 0)
  137. return TRUE;
  138. return FALSE;
  139. }
  140. BOOL WriteLine(LPTSTR szTextLine){
  141. DWORD dwBytesWritten = 0;
  142. if(WriteFile((LPCVOID)szTextLine,(lstrlen(szTextLine) * sizeof(TCHAR)),&dwBytesWritten,NULL)){
  143. return WriteFile((LPCVOID)TEXT("\r\n"),(2* sizeof(TCHAR)),&dwBytesWritten,NULL);
  144. } else {
  145. return FALSE;
  146. }
  147. }
  148. ////////////////////////////////////////////////////////////////////
  149. //
  150. // Function Name: ReadFile()
  151. // Purpose: read data from a file
  152. // Parameters:
  153. // LPVOID lpBuffer - buffer to fill with data
  154. // DWORD nNumberOfBytesToRead - number of bytes to read
  155. // LPDWORD lpNumberOfBytesRead - number of bytes read
  156. // LPOVERLAPPED lpOverlapped - overlap
  157. //
  158. ////////////////////////////////////////////////////////////////////
  159. BOOL ReadFile(LPVOID lpBuffer,
  160. DWORD nNumberOfBytesToRead,
  161. LPDWORD lpNumberOfBytesRead,
  162. LPOVERLAPPED lpOverlapped){
  163. if(!m_bReady){
  164. return FALSE;
  165. }
  166. return ::ReadFile(m_hFile,lpBuffer,nNumberOfBytesToRead,
  167. lpNumberOfBytesRead,lpOverlapped);
  168. }
  169. ////////////////////////////////////////////////////////////////////
  170. //
  171. // Function Name: WriteFile()
  172. // Purpose: write data to a file
  173. // Parameters:
  174. // LPCVOID lpBuffer - buffer to write to a file
  175. // DWORD nNumberOfBytesToWrite - number of bytes to write
  176. // LPDWORD lpNumberOfBytesWritten - number of bytes written
  177. // LPOVERLAPPED lpOverlapped - overlap
  178. //
  179. ////////////////////////////////////////////////////////////////////
  180. BOOL WriteFile(LPCVOID lpBuffer,
  181. DWORD nNumberOfBytesToWrite,
  182. LPDWORD lpNumberOfBytesWritten,
  183. LPOVERLAPPED lpOverlapped){
  184. if(!m_bReady){
  185. return FALSE;
  186. }
  187. return ::WriteFile(m_hFile,lpBuffer,nNumberOfBytesToWrite,
  188. lpNumberOfBytesWritten,lpOverlapped);
  189. }
  190. ////////////////////////////////////////////////////////////////////
  191. //
  192. // Function Name: SeekFile()
  193. // Purpose: move current file pointer
  194. // Parameters:
  195. // DWORD dwBytesToSeek - number of bytes to move file pointer
  196. // DWORD dwMoveMethod - method of moving file pointer
  197. // [Valid dwMoveMethods]
  198. // FILE_BEGIN - from the beginning of the file
  199. // FILE_CURRENT - from the current file position
  200. // FILE_END - from the end of the file
  201. //
  202. ////////////////////////////////////////////////////////////////////
  203. BOOL SeekFile(DWORD dwBytesToSeek, DWORD dwMoveMethod){
  204. DWORD dwFilePointer = 0;
  205. dwFilePointer = SetFilePointer(m_hFile,dwBytesToSeek,NULL,dwMoveMethod);
  206. if (dwFilePointer == 0xFFFFFFFF)
  207. return FALSE;
  208. return TRUE;
  209. }
  210. private:
  211. HANDLE m_hFile; // file handle
  212. BOOL m_bReady; // cfileio class ready flag
  213. LONG m_TotalFileSize; // file size (bytes)
  214. protected:
  215. };
  216. #endif