Leaked source code of windows server 2003
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.

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