|
| 1 | +unit FileReader; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + System.Classes, System.SysUtils, System.Types, |
| 7 | + Winapi.Windows; |
| 8 | + |
| 9 | +type |
| 10 | + TInputReader = class |
| 11 | + class procedure MMF(const AFilename: string; var aOut: TArray<AnsiChar>); |
| 12 | + end; |
| 13 | + |
| 14 | +implementation |
| 15 | + |
| 16 | + |
| 17 | +class procedure TInputReader.MMF (const AFilename: string; var aOut: TArray<AnsiChar>); |
| 18 | +// procedure found online while searching for memory-mappped files |
| 19 | + |
| 20 | +// PROBLEM-1: GetFileSize was returning ~3.9B, which is incorrect. |
| 21 | +// GetFileSizeEx was correctly returning 16B instead. |
| 22 | +// PROBLEM-2: CreateFileMapping only accepts Cardinals, which are not enough to fit 16B, |
| 23 | +// thus RangeCheck error. |
| 24 | +// One way would be to call CreateFileMapping in chunks of acceptable size, |
| 25 | +// but a chunk of 3.9B was taking ~1.7 seconds to load. |
| 26 | +// so 4 times that was pretty slow, especially that it is a single-threaded operation |
| 27 | +var |
| 28 | + hFile: THandle; |
| 29 | + hFileMap: THandle; |
| 30 | + hiSize: Int64; |
| 31 | + loSize: Int64; |
| 32 | + view: pointer; |
| 33 | +begin |
| 34 | + if AFilename = '' then |
| 35 | + Exit; |
| 36 | + if not FileExists(AFilename) then |
| 37 | + Exit; |
| 38 | + {Open the file} |
| 39 | + hFile := CreateFile( |
| 40 | + PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil, |
| 41 | + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 |
| 42 | + ); |
| 43 | + if hFile <> INVALID_HANDLE_VALUE then |
| 44 | + begin |
| 45 | + loSize := GetFileSize(hFile, @hiSize); |
| 46 | + {File was opened successfully, now map it:} |
| 47 | + hFileMap := CreateFileMapping( |
| 48 | + hFile, nil, PAGE_READONLY, hiSize, loSize, 'TextForString' |
| 49 | + ); |
| 50 | + if (hFileMap <> 0) then |
| 51 | + begin |
| 52 | + if (GetLastError() = ERROR_ALREADY_EXISTS) then |
| 53 | + begin |
| 54 | + Writeln('Mapping already exists - not created.'); |
| 55 | + CloseHandle(hFileMap) |
| 56 | + end |
| 57 | + else |
| 58 | + begin |
| 59 | + view := nil; |
| 60 | + try |
| 61 | + {File mapped successfully, now map a view of the file into the |
| 62 | + address space:} |
| 63 | + view := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0); |
| 64 | + if (view <> nil) then |
| 65 | + begin {View mapped successfully} |
| 66 | + {Close file handle - as long is view is open it will persist} |
| 67 | + CloseHandle(hFile); |
| 68 | + SetLength(aOut, loSize); |
| 69 | + Move(view^, aOut[0], loSize); |
| 70 | + end |
| 71 | + else |
| 72 | + WriteLn('Unable to map view of file.'); |
| 73 | + finally |
| 74 | + UnmapViewOfFile(view); {Close view} |
| 75 | + CloseHandle(hFileMap); {Close mapping} |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + else |
| 80 | + begin |
| 81 | + WriteLn('Unable to create file mapping.'); |
| 82 | + end; |
| 83 | + end |
| 84 | + else |
| 85 | + begin |
| 86 | + WriteLn('Unable to open file.'); |
| 87 | + end; |
| 88 | +end; |
| 89 | + |
| 90 | +end. |
0 commit comments