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.

167 lines
3.0 KiB

  1. #include "ulib.hxx"
  2. #include "fileio.hxx"
  3. #include "diskedit.h"
  4. extern "C" {
  5. #include <stdio.h>
  6. }
  7. STATIC TCHAR FilePath[MAX_PATH];
  8. BOOLEAN
  9. FILE_IO::Setup(
  10. IN PMEM Mem,
  11. IN PLOG_IO_DP_DRIVE Drive,
  12. IN HANDLE Application,
  13. IN HWND WindowHandle,
  14. OUT PBOOLEAN Error
  15. )
  16. {
  17. FARPROC proc;
  18. proc = MakeProcInstance((FARPROC) ReadTheFile, Application);
  19. if (!DialogBox((HINSTANCE)Application, TEXT("ReadFileBox"),
  20. WindowHandle, (DLGPROC) proc)) {
  21. *Error = FALSE;
  22. return FALSE;
  23. }
  24. FreeProcInstance(proc);
  25. *Error = TRUE;
  26. _file_handle = CreateFile(FilePath, GENERIC_READ | GENERIC_WRITE,
  27. FILE_SHARE_READ, NULL, OPEN_ALWAYS,
  28. FILE_ATTRIBUTE_NORMAL, NULL);
  29. if (_file_handle == INVALID_HANDLE_VALUE) {
  30. return FALSE;
  31. }
  32. if (!_buffer_size) {
  33. _buffer_size = GetFileSize(_file_handle, NULL);
  34. }
  35. if (_buffer_size == (ULONG) -1) {
  36. return FALSE;
  37. }
  38. _buffer = Mem->Acquire(_buffer_size, Drive->QueryAlignmentMask());
  39. if (!_buffer) {
  40. return FALSE;
  41. }
  42. swprintf(_header_text, TEXT("%s"), FilePath);
  43. return TRUE;
  44. }
  45. BOOLEAN
  46. FILE_IO::Read(
  47. OUT PULONG pError
  48. )
  49. {
  50. DWORD bytes_read;
  51. *pError = 0;
  52. if (_file_handle == INVALID_HANDLE_VALUE) {
  53. return FALSE;
  54. }
  55. if (SetFilePointer(_file_handle, 0, NULL, FILE_BEGIN) == (DWORD) -1) {
  56. return FALSE;
  57. }
  58. if (!ReadFile(_file_handle, _buffer, _buffer_size, &bytes_read, NULL) ||
  59. bytes_read != _buffer_size) {
  60. return FALSE;
  61. }
  62. return TRUE;
  63. }
  64. BOOLEAN
  65. FILE_IO::Write(
  66. )
  67. {
  68. DWORD bytes_written;
  69. if (_file_handle == INVALID_HANDLE_VALUE) {
  70. return FALSE;
  71. }
  72. if (SetFilePointer(_file_handle, 0, NULL, FILE_BEGIN) == (DWORD) -1) {
  73. return FALSE;
  74. }
  75. if (!WriteFile(_file_handle, _buffer, _buffer_size, &bytes_written, NULL) ||
  76. bytes_written != _buffer_size) {
  77. return FALSE;
  78. }
  79. if (!SetEndOfFile(_file_handle)) {
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. PVOID
  85. FILE_IO::GetBuf(
  86. OUT PULONG Size
  87. )
  88. {
  89. if (Size) {
  90. *Size = _buffer_size;
  91. }
  92. return _buffer;
  93. }
  94. PTCHAR
  95. FILE_IO::GetHeaderText(
  96. )
  97. {
  98. return _header_text;
  99. }
  100. BOOL
  101. ReadTheFile(
  102. IN HWND hDlg,
  103. IN UINT message,
  104. IN UINT wParam,
  105. IN LONG lParam
  106. )
  107. {
  108. switch (message) {
  109. case WM_INITDIALOG:
  110. return TRUE;
  111. case WM_COMMAND:
  112. if (LOWORD(wParam) == IDCANCEL) {
  113. EndDialog(hDlg, FALSE);
  114. return TRUE;
  115. }
  116. if (LOWORD(wParam) == IDOK) {
  117. INT n;
  118. n = GetDlgItemText(hDlg, IDTEXT, FilePath, sizeof(FilePath)/sizeof(TCHAR));
  119. FilePath[n] = 0;
  120. EndDialog(hDlg, TRUE);
  121. return TRUE;
  122. }
  123. break;
  124. }
  125. return FALSE;
  126. }