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.

228 lines
6.1 KiB

  1. //#include <string.h>
  2. //#include <tchar.h>
  3. #include <stdio.h>
  4. #include "perfmon.h"
  5. #include "fileutil.h"
  6. #include "utils.h"
  7. #include <string.h> // for strncpy
  8. #ifdef UNICODE
  9. #define _tcsrchr wcsrchr
  10. #else
  11. #define _tcsrchr strrchr
  12. #endif
  13. #define DRIVE_DELIMITER TEXT(':')
  14. #define DIRECTORY_DELIMITER TEXT('\\')
  15. #define EXTENSION_DELIMITER TEXT('.')
  16. #if 0
  17. VOID FileErrorMessageBox(HWND hWnd, LPTSTR lpszFileName, DWORD ErrorCode)
  18. {
  19. TCHAR szErrorMessage[FILE_ERROR_MESSAGE_SIZE] ;
  20. TCHAR szErrorMessageTemplate[FILE_ERROR_MESSAGE_SIZE] ;
  21. StringLoad (IDS_FILE_ERROR, szErrorMessageTemplate) ;
  22. TSPRINTF((LPTSTR)szErrorMessage,
  23. (LPTSTR)szErrorMessageTemplate,
  24. lpszFileName,
  25. ErrorCode) ;
  26. MessageBox (hWnd, (LPTSTR)szErrorMessage, NULL,
  27. MB_OK | MB_ICONSTOP | MB_TASKMODAL);
  28. return ;
  29. }
  30. #endif
  31. BOOL FileRead (HANDLE hFile,
  32. LPMEMORY lpMemory,
  33. DWORD nAmtToRead)
  34. { // FileRead
  35. BOOL bSuccess ;
  36. DWORD nAmtRead ;
  37. bSuccess = ReadFile (hFile, lpMemory, nAmtToRead, &nAmtRead, NULL) ;
  38. return (bSuccess && (nAmtRead == nAmtToRead)) ;
  39. } // FileRead
  40. BOOL FileWrite (HANDLE hFile,
  41. LPMEMORY lpMemory,
  42. DWORD nAmtToWrite)
  43. { // FileWrite
  44. BOOL bSuccess = FALSE;
  45. DWORD nAmtWritten = 0;
  46. DWORD dwFileSizeLow, dwFileSizeHigh;
  47. LONGLONG llResultSize;
  48. dwFileSizeLow = GetFileSize (hFile, &dwFileSizeHigh);
  49. // limit file size to 2GB
  50. if (dwFileSizeHigh > 0) {
  51. SetLastError (ERROR_WRITE_FAULT);
  52. bSuccess = FALSE;
  53. } else {
  54. // note that the error return of this function is 0xFFFFFFFF
  55. // since that is > the file size limit, this will be interpreted
  56. // as an error (a size error) so it's accounted for in the following
  57. // test.
  58. llResultSize = dwFileSizeLow + nAmtToWrite;
  59. if (llResultSize >= 0x80000000) {
  60. SetLastError (ERROR_WRITE_FAULT);
  61. bSuccess = FALSE;
  62. } else {
  63. // write buffer to file
  64. bSuccess = WriteFile (hFile, lpMemory, nAmtToWrite, &nAmtWritten, NULL) ;
  65. if (bSuccess) bSuccess = (nAmtWritten == nAmtToWrite ? TRUE : FALSE);
  66. }
  67. }
  68. return (bSuccess) ;
  69. } // FileWrite
  70. #if 0
  71. HANDLE FileHandleOpen (LPTSTR lpszFilePath)
  72. { // FileHandleOpen
  73. return ((HANDLE) CreateFile (lpszFilePath,
  74. GENERIC_READ |
  75. GENERIC_WRITE,
  76. FILE_SHARE_READ |
  77. FILE_SHARE_WRITE,
  78. NULL,
  79. OPEN_EXISTING,
  80. 0,
  81. NULL)) ;
  82. } // FileHandleOpen
  83. HANDLE FileHandleCreate (LPTSTR lpszFilePath)
  84. { // FileHandleCreate
  85. return ((HANDLE) CreateFile (lpszFilePath,
  86. GENERIC_READ | GENERIC_WRITE,
  87. FILE_SHARE_READ,
  88. NULL,
  89. CREATE_ALWAYS,
  90. FILE_ATTRIBUTE_NORMAL,
  91. NULL)) ;
  92. } // FileHandleCreate
  93. long FileSeekEnd (HANDLE hFile,
  94. long lAmtToMove)
  95. { // FileSeekEnd
  96. return (SetFilePointer (hFile, lAmtToMove, NULL, FILE_END)) ;
  97. } // FileSeekEnd
  98. long FileSeekBegin (HANDLE hFile,
  99. long lAmtToMove)
  100. { // FileSeekBegin
  101. return (SetFilePointer (hFile, lAmtToMove, NULL, FILE_BEGIN)) ;
  102. } // FileSeekBegin
  103. long FileSeekCurrent (HANDLE hFile,
  104. long lAmtToMove)
  105. { // FileSeekCurrent
  106. return (SetFilePointer (hFile, lAmtToMove, NULL, FILE_CURRENT)) ;
  107. } // FileSeekCurrent
  108. long FileTell (HANDLE hFile)
  109. { // FileTell
  110. return (SetFilePointer (hFile, 0, NULL, FILE_CURRENT)) ;
  111. } // FileTell
  112. #endif
  113. LPMEMORY FileMap (HANDLE hFile, HANDLE *phMapHandle)
  114. /*
  115. To Do: Error reporting!!
  116. */
  117. { // FileMap
  118. HANDLE hMapping ;
  119. *phMapHandle = 0 ;
  120. hMapping = CreateFileMapping (hFile, NULL, PAGE_READONLY,
  121. 0, 0, NULL) ;
  122. if (!hMapping)
  123. return (NULL) ;
  124. *phMapHandle = hMapping ;
  125. return (MapViewOfFile (hMapping, FILE_MAP_READ, 0, 0, 0)) ;
  126. } // FileMap
  127. BOOL FileUnMap (LPVOID pBase, HANDLE hMapping)
  128. /*
  129. To Do: Error reporting!!
  130. */
  131. { // FileUnMap
  132. UnmapViewOfFile(pBase) ;
  133. CloseHandle (hMapping) ;
  134. return (TRUE) ;
  135. } // FileUnMap
  136. void FileNameExtension (LPTSTR lpszSpec,
  137. LPTSTR lpszFileNameExtension)
  138. /*
  139. Effect: Return the name and extension portion only of lpszSpec
  140. int lpszFileNameExtension.
  141. Assert: lpszFileNameExtension is large enough to hold a name,
  142. delimiter, extension, and terminating null character.
  143. */
  144. { // FileNameExtension
  145. LPTSTR lpszDelimiter ;
  146. lpszDelimiter = _tcsrchr ((LPCTSTR)lpszSpec, (TCHAR)DIRECTORY_DELIMITER) ;
  147. if (!lpszDelimiter)
  148. lpszDelimiter = _tcsrchr ((LPCTSTR)lpszSpec, (TCHAR)DRIVE_DELIMITER) ;
  149. lstrcpy (lpszFileNameExtension,
  150. lpszDelimiter ? ++lpszDelimiter : lpszSpec) ;
  151. } // FileNameExtension
  152. void FileDriveDirectory (LPTSTR lpszFileSpec,
  153. LPTSTR lpszDirectory)
  154. /*
  155. Effect: Extract the drive and directory from the file
  156. specification lpszFileSpec, and return the it in
  157. lpszDirectory.
  158. Internals: Copy the the whole spec to lpszDirectory. Use lstrrchr
  159. to find the *last* directory delimiter ('\') and
  160. truncate the string right after that.
  161. Note: This function assumes that the specification lpszFileSpec
  162. is fairly complete, in that it contains both a directory
  163. and a file name.
  164. */
  165. { // FileDriveDirectory
  166. LPTSTR lpszDelimiter ;
  167. lstrcpy (lpszDirectory, lpszFileSpec) ;
  168. lpszDelimiter = _tcsrchr ((LPCTSTR)lpszDirectory, (TCHAR)DIRECTORY_DELIMITER) ;
  169. if (lpszDelimiter)
  170. *(++lpszDelimiter) = TEXT('\0') ;
  171. } // FileDriveDirectory
  172. 
  173.