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.

205 lines
5.4 KiB

  1. //#include <string.h>
  2. //#include <tchar.h>
  3. #include <stdio.h>
  4. #include "setedit.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 ;
  45. DWORD nAmtWritten ;
  46. bSuccess = WriteFile (hFile, lpMemory, nAmtToWrite, &nAmtWritten, NULL) ;
  47. return (bSuccess && (nAmtWritten == nAmtToWrite)) ;
  48. } // FileWrite
  49. #if 0
  50. HANDLE FileHandleOpen (LPTSTR lpszFilePath)
  51. { // FileHandleOpen
  52. return ((HANDLE) CreateFile (lpszFilePath,
  53. GENERIC_READ |
  54. GENERIC_WRITE,
  55. FILE_SHARE_READ |
  56. FILE_SHARE_WRITE,
  57. NULL,
  58. OPEN_EXISTING,
  59. 0,
  60. NULL)) ;
  61. } // FileHandleOpen
  62. HANDLE FileHandleCreate (LPTSTR lpszFilePath)
  63. { // FileHandleCreate
  64. return ((HANDLE) CreateFile (lpszFilePath,
  65. GENERIC_READ | GENERIC_WRITE,
  66. FILE_SHARE_READ,
  67. NULL,
  68. CREATE_ALWAYS,
  69. FILE_ATTRIBUTE_NORMAL,
  70. NULL)) ;
  71. } // FileHandleCreate
  72. long FileSeekEnd (HANDLE hFile,
  73. long lAmtToMove)
  74. { // FileSeekEnd
  75. return (SetFilePointer (hFile, lAmtToMove, NULL, FILE_END)) ;
  76. } // FileSeekEnd
  77. long FileSeekBegin (HANDLE hFile,
  78. long lAmtToMove)
  79. { // FileSeekBegin
  80. return (SetFilePointer (hFile, lAmtToMove, NULL, FILE_BEGIN)) ;
  81. } // FileSeekBegin
  82. long FileSeekCurrent (HANDLE hFile,
  83. long lAmtToMove)
  84. { // FileSeekCurrent
  85. return (SetFilePointer (hFile, lAmtToMove, NULL, FILE_CURRENT)) ;
  86. } // FileSeekCurrent
  87. long FileTell (HANDLE hFile)
  88. { // FileTell
  89. return (SetFilePointer (hFile, 0, NULL, FILE_CURRENT)) ;
  90. } // FileTell
  91. #endif
  92. LPMEMORY FileMap (HANDLE hFile, HANDLE *phMapHandle)
  93. /*
  94. To Do: Error reporting!!
  95. */
  96. { // FileMap
  97. HANDLE hMapping ;
  98. *phMapHandle = 0 ;
  99. hMapping = CreateFileMapping (hFile, NULL, PAGE_READONLY,
  100. 0, 0, NULL) ;
  101. if (!hMapping)
  102. return (NULL) ;
  103. *phMapHandle = hMapping ;
  104. return (MapViewOfFile (hMapping, FILE_MAP_READ, 0, 0, 0)) ;
  105. } // FileMap
  106. BOOL FileUnMap (LPVOID pBase, HANDLE hMapping)
  107. /*
  108. To Do: Error reporting!!
  109. */
  110. { // FileUnMap
  111. UnmapViewOfFile(pBase) ;
  112. CloseHandle (hMapping) ;
  113. return (TRUE) ;
  114. } // FileUnMap
  115. void FileNameExtension (LPTSTR lpszSpec,
  116. LPTSTR lpszFileNameExtension)
  117. /*
  118. Effect: Return the name and extension portion only of lpszSpec
  119. int lpszFileNameExtension.
  120. Assert: lpszFileNameExtension is large enough to hold a name,
  121. delimiter, extension, and terminating null character.
  122. */
  123. { // FileNameExtension
  124. LPTSTR lpszDelimiter ;
  125. lpszDelimiter = _tcsrchr ((LPCTSTR)lpszSpec, (TCHAR)DIRECTORY_DELIMITER) ;
  126. if (!lpszDelimiter)
  127. lpszDelimiter = _tcsrchr ((LPCTSTR)lpszSpec, (TCHAR)DRIVE_DELIMITER) ;
  128. lstrcpy (lpszFileNameExtension,
  129. lpszDelimiter ? ++lpszDelimiter : lpszSpec) ;
  130. } // FileNameExtension
  131. void FileDriveDirectory (LPTSTR lpszFileSpec,
  132. LPTSTR lpszDirectory)
  133. /*
  134. Effect: Extract the drive and directory from the file
  135. specification lpszFileSpec, and return the it in
  136. lpszDirectory.
  137. Internals: Copy the the whole spec to lpszDirectory. Use lstrrchr
  138. to find the *last* directory delimiter ('\') and
  139. truncate the string right after that.
  140. Note: This function assumes that the specification lpszFileSpec
  141. is fairly complete, in that it contains both a directory
  142. and a file name.
  143. */
  144. { // FileDriveDirectory
  145. LPTSTR lpszDelimiter ;
  146. lstrcpy (lpszDirectory, lpszFileSpec) ;
  147. lpszDelimiter = _tcsrchr ((LPCTSTR)lpszDirectory, (TCHAR)DIRECTORY_DELIMITER) ;
  148. if (lpszDelimiter)
  149. *(++lpszDelimiter) = TEXT('\0') ;
  150. } // FileDriveDirectory
  151. 
  152.