{ $Id: fMapExample.pas 499 2002-04-18 04:19:07Z jamesk $} unit fMapExample; interface uses SysUtils, Types, Classes, Variants, QGraphics, QControls, QForms, QDialogs, QStdCtrls, QButtons; type TfrmExample = class(TForm) bbReadOnly: TBitBtn; bbReadWrite: TBitBtn; bbTerminateProgram: TBitBtn; procedure bbTerminateProgramClick(Sender: TObject); procedure bbReadOnlyClick(Sender: TObject); procedure bbReadWriteClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmExample: TfrmExample; implementation {$R *.xfm} uses MemoryMapper; procedure TfrmExample.bbTerminateProgramClick(Sender: TObject); begin Application.Terminate; end; procedure TfrmExample.bbReadOnlyClick(Sender: TObject); const FILE_NAME='DeleteMe'; // Name of test file NUM_TESTS=100; // Perform this number of tests. TEST_STRING='0123456789ABCDEF'; var fhTest :TextFile; map :ImmMemoryMap; pMap :PChar; N :Integer; idx :Integer; i :Integer; begin // Cache value for convenience N := Length(TEST_STRING); // Create the test file to map. AssignFile(fhTest,FILE_NAME); Rewrite(fhTest); Write(fhTest,TEST_STRING); CloseFile(fhTest); // Obtain a read-only memory map. map := CommMemoryMap.CreateInstance(FILE_NAME); if not map.IsValid then raise EInOutError.Create('UNEXPECTED ERROR: File mapping failed.'); // Convert the raw mapped region to something useful (PChar) pMap := map.Data; // Give the mapping a work-out. Randomly chose a character in the file // and compare it with the original data. for i := 1 to NUM_TESTS do begin idx := Random(N); if pMap[idx]<>TEST_STRING[Succ(idx)] then raise Exception.Create('UNEXPECTED ERROR: Memory map test failed!!!!'); end; MessageDlg('Test complete.',mtInformation,[mbOK],0); end; procedure TfrmExample.bbReadWriteClick(Sender: TObject); const FILE_NAME='DeleteMe'; // Name of test file NUM_TESTS=100; // Perform this number of tests. TEST_STRING='0123456789ABCDEF'; var fhTest :TextFile; map :ImmMemoryMap; pMap :PChar; N :Integer; idx :Integer; i :Integer; c :Char; begin // Cache value for convenience N := Length(TEST_STRING); // Create the test file to map. AssignFile(fhTest,'DeleteMe'); Rewrite(fhTest); Write(fhTest,TEST_STRING); CloseFile(fhTest); // Obtain a read/write memory map. map := CommMemoryMap.CreateInstance(FILE_NAME,False); if not map.IsValid then raise EInOutError.Create('UNEXPECTED ERROR: File mapping failed.'); // Convert the raw mapped region to something useful (PChar) pMap := map.Data; // Reverse the characters in the file. // Note that we're using normal memory access operations!!! for i := 0 to Pred(N div 2) do begin c := pMap[i]; pMap[i] := pMap[Pred(N)-i]; pMap[Pred(N)-i] := c; end; // Give the mapping a work-out. Randomly chose a character in the file // and compare it with the original data. for i := 1 to NUM_TESTS do begin idx := Random(N); if pMap[idx]<>TEST_STRING[N-idx] then raise Exception.Create('UNEXPECTED ERROR: Memory map test failed!!!!'); end; MessageDlg('Test complete.',mtInformation,[mbOK],0); end; initialization Randomize; end.