A33
This commit is contained in:
parent
cee65a7621
commit
fe7efba2c4
534
mdl/m_fileio.pas
534
mdl/m_fileio.pas
|
@ -1,16 +1,7 @@
|
|||
{
|
||||
Mystic Software Development Library
|
||||
===========================================================================
|
||||
File | M_FILEIO.PAS
|
||||
Desc | File IO related functions
|
||||
Created | August 22, 2002
|
||||
---------------------------------------------------------------------------
|
||||
}
|
||||
Unit m_FileIO;
|
||||
|
||||
{$I M_OPS.PAS}
|
||||
|
||||
Unit m_FileIO;
|
||||
|
||||
Interface
|
||||
|
||||
{ FILE ACCESS FUNCTIONS }
|
||||
|
@ -63,53 +54,48 @@ Const
|
|||
fmRWDR = 50;
|
||||
fmRWDW = 34;
|
||||
|
||||
{ BUFFERED FILE IO CLASS DEFINITION }
|
||||
{ STREAMING CLASS OPTIONS }
|
||||
|
||||
Const
|
||||
TMaxBufferSize = 64 * 1024 - 1; // Maximum of 64KB buffer for IO class
|
||||
MaxFileBufferSize = 64 * 1024;
|
||||
|
||||
Type
|
||||
TBufFileOpenType = (
|
||||
TFileBufferOpenType = (
|
||||
fmOpen,
|
||||
fmOpenCreate,
|
||||
fmCreate
|
||||
);
|
||||
|
||||
PBufFileBuffer = ^TBufFileBuffer;
|
||||
TBufFileBuffer = Array[0..TMaxBufferSize] of Byte;
|
||||
PFileBufferRec = ^TFileBufferRec;
|
||||
TFileBufferRec = Array[0..MaxFileBufferSize - 1] of Char;
|
||||
|
||||
TBufFile = Class
|
||||
Private
|
||||
BufFile : File;
|
||||
Buffer : PBufFileBuffer;
|
||||
Opened : Boolean;
|
||||
BufDirty : Boolean;
|
||||
BufFilePos : Longint;
|
||||
RecordSize : LongInt;
|
||||
BufSize : LongInt;
|
||||
BufPos : LongInt;
|
||||
BufTop : LongInt;
|
||||
TFileBuffer = Class
|
||||
BufSize : LongInt;
|
||||
Buffer : PFileBufferRec;
|
||||
BufRead : LongInt;
|
||||
BufStart : LongInt;
|
||||
BufEnd : LongInt;
|
||||
BufPos : LongInt;
|
||||
InFile : File;
|
||||
BufEOF : Boolean;
|
||||
BufDirty : Boolean;
|
||||
IsOpened : Boolean;
|
||||
|
||||
Procedure FillBuffer;
|
||||
Procedure FlushBuffer;
|
||||
Public
|
||||
IOResult : Integer;
|
||||
|
||||
Constructor Create (BS: Word);
|
||||
Constructor Create (BufferSize: LongInt);
|
||||
Destructor Destroy; Override;
|
||||
Function Open (FN: String; OM: TBufFileOpenType; FM: Byte; RS: Word) : Boolean;
|
||||
Procedure Close;
|
||||
Procedure Reset;
|
||||
Function EOF : Boolean;
|
||||
|
||||
Function OpenStream (FN: String; OpenType: TFileBufferOpenType; OpenMode: Byte) : Boolean;
|
||||
Procedure CloseStream;
|
||||
Function Read : Char;
|
||||
Procedure BlockRead (Var Buf; Size: LongInt; Var Count: LongInt); Overload;
|
||||
Procedure BlockRead (Var Buf; Size: LongInt); Overload;
|
||||
Procedure BlockWrite (Var Buf; Size: LongInt);
|
||||
Procedure Seek (FP : LongInt);
|
||||
Function FilePos : LongInt;
|
||||
Function FileSize : LongInt;
|
||||
Procedure Seek (Pos : LongInt);
|
||||
Procedure Read (Var V);
|
||||
Procedure Write (Var V);
|
||||
Procedure BlockRead (Var V; Count: LongInt; Var Result: LongInt);
|
||||
Procedure BlockWrite (Var V; Count: LongInt; Var Result: LongInt);
|
||||
Procedure RecordInsert (RecNum: LongInt);
|
||||
Procedure RecordDelete (RecNum: LongInt);
|
||||
Function EOF : Boolean;
|
||||
Procedure FillBuffer;
|
||||
Procedure FlushBuffer;
|
||||
End;
|
||||
|
||||
Implementation
|
||||
|
@ -502,287 +488,6 @@ Begin
|
|||
Result := ((Attr And Directory) = Directory);
|
||||
End;
|
||||
|
||||
{ BEGIN BUFFERED FILE IO CLASS HERE ======================================= }
|
||||
|
||||
Constructor TBufFile.Create (BS: Word);
|
||||
Begin
|
||||
Inherited Create;
|
||||
|
||||
Opened := False;
|
||||
BufDirty := False;
|
||||
BufFilePos := 0;
|
||||
RecordSize := 0;
|
||||
BufSize := BS;
|
||||
BufPos := 0;
|
||||
BufTop := 0;
|
||||
|
||||
If BufSize > TMaxBufferSize + 1 Then Fail;
|
||||
|
||||
GetMem (Buffer, BufSize);
|
||||
|
||||
If Buffer = Nil Then Fail;
|
||||
End;
|
||||
|
||||
Destructor TBufFile.Destroy;
|
||||
Begin
|
||||
If Opened Then Close;
|
||||
|
||||
If Buffer <> Nil Then FreeMem (Buffer, BufSize);
|
||||
|
||||
Inherited Destroy;
|
||||
End;
|
||||
|
||||
Function TBufFile.Open (FN: String; OM: TBufFileOpenType; FM: Byte; RS: Word) : Boolean;
|
||||
Begin
|
||||
If Opened Then Close;
|
||||
|
||||
Result := False;
|
||||
RecordSize := RS;
|
||||
BufFilePos := 0;
|
||||
BufPos := 0;
|
||||
BufTop := 0;
|
||||
|
||||
System.Assign (BufFile, FN);
|
||||
|
||||
If System.IoResult <> 0 Then Exit;
|
||||
|
||||
System.FileMode := FM;
|
||||
|
||||
Case OM of
|
||||
fmOpen : Begin
|
||||
{$I-} System.Reset(BufFile, 1); {$I+}
|
||||
If System.IoResult <> 0 Then Exit;
|
||||
FillBuffer;
|
||||
End;
|
||||
fmOpenCreate: Begin
|
||||
{$I-} System.Reset(BufFile, 1); {$I+}
|
||||
If System.IoResult <> 0 Then Begin
|
||||
{$I-} System.ReWrite(BufFile, 1); {$I+}
|
||||
If System.IoResult <> 0 Then Exit;
|
||||
End Else
|
||||
FillBuffer;
|
||||
End;
|
||||
fmCreate : Begin
|
||||
{$I-} System.ReWrite(BufFile, 1); {$I+}
|
||||
If IoResult <> 0 Then Exit;
|
||||
End;
|
||||
End;
|
||||
|
||||
Result := True;
|
||||
Opened := True;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.Close;
|
||||
Begin
|
||||
If BufDirty Then FlushBuffer;
|
||||
|
||||
System.Close(BufFile);
|
||||
|
||||
IOResult := System.IoResult;
|
||||
Opened := False;
|
||||
End;
|
||||
|
||||
Function TBufFile.EOF : Boolean;
|
||||
Begin
|
||||
Result := FilePos >= FileSize;
|
||||
End;
|
||||
|
||||
Function TBufFile.FileSize : Longint;
|
||||
Begin
|
||||
Result := System.FileSize(BufFile) DIV RecordSize;
|
||||
End;
|
||||
|
||||
Function TBufFile.FilePos : Longint;
|
||||
Begin
|
||||
Result := (BufFilePos + BufPos) DIV RecordSize;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.Reset;
|
||||
Begin
|
||||
If BufDirty Then FlushBuffer;
|
||||
|
||||
System.Seek(BufFile, 0);
|
||||
|
||||
BufFilePos := 0;
|
||||
BufPos := 0;
|
||||
|
||||
FillBuffer;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.Seek (Pos: Longint);
|
||||
Begin
|
||||
Pos := Pos * RecordSize;
|
||||
|
||||
If (Pos >= BufFilePos + BufSize) or (Pos < BufFilePos) Then Begin
|
||||
If BufDirty Then FlushBuffer;
|
||||
|
||||
System.Seek(BufFile, Pos);
|
||||
|
||||
BufFilePos := Pos;
|
||||
BufPos := 0;
|
||||
|
||||
FillBuffer;
|
||||
End Else
|
||||
BufPos := Pos - BufFilePos;
|
||||
|
||||
IoResult := System.IoResult;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.Read (Var V);
|
||||
Var
|
||||
Offset : Word;
|
||||
Begin
|
||||
If BufPos + RecordSize > BufTop Then Begin
|
||||
Offset := BufSize - BufPos;
|
||||
|
||||
Move(Buffer^[BufPos], V, Offset);
|
||||
|
||||
Inc(BufFilePos, BufSize);
|
||||
|
||||
BufPos:= 0;
|
||||
|
||||
FillBuffer;
|
||||
|
||||
Move(Buffer^[BufPos], TBufFileBuffer(V)[Offset], RecordSize - Offset);
|
||||
|
||||
BufPos:= BufPos + RecordSize - Offset;
|
||||
End Else Begin
|
||||
Move(Buffer^[BufPos], V, RecordSize);
|
||||
Inc(BufPos, RecordSize);
|
||||
End;
|
||||
|
||||
IoResult := System.IoResult;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.BlockRead (Var V; Count: LongInt; Var Result: LongInt);
|
||||
Begin
|
||||
Result := 0;
|
||||
|
||||
While (Result < Count) and (IoResult = 0) And Not EOF Do Begin
|
||||
Read (TBufFileBuffer(V)[Result * RecordSize]);
|
||||
Inc (Result);
|
||||
End;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.Write (Var V);
|
||||
Var
|
||||
Offset : Word;
|
||||
Begin
|
||||
BufDirty := True;
|
||||
|
||||
If BufPos + RecordSize > BufSize Then Begin
|
||||
Offset := BufSize - BufPos;
|
||||
|
||||
If Offset > 0 Then
|
||||
Move(V, Buffer^[BufPos], Offset);
|
||||
|
||||
BufTop := BufSize;
|
||||
|
||||
FlushBuffer;
|
||||
|
||||
Inc(BufFilePos, BufSize);
|
||||
|
||||
BufPos:= 0;
|
||||
|
||||
FillBuffer;
|
||||
|
||||
Move (TBufFileBuffer(V)[Offset], Buffer^[BufPos], RecordSize - Offset);
|
||||
|
||||
BufPos := BufPos + RecordSize - Offset;
|
||||
End Else Begin
|
||||
Move (V, Buffer^[BufPos], RecordSize);
|
||||
Inc (BufPos, RecordSize);
|
||||
End;
|
||||
|
||||
If BufTop < BufPos Then BufTop := BufPos;
|
||||
|
||||
IoResult := System.IoResult;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.BlockWrite (Var V; Count: LongInt; Var Result: LongInt);
|
||||
Begin
|
||||
Result := 0;
|
||||
|
||||
While (Result < Count) And (IoResult = 0) Do Begin
|
||||
Write (TBufFileBuffer(V)[Result * RecordSize]);
|
||||
Inc (Result);
|
||||
End;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.FillBuffer;
|
||||
Begin
|
||||
System.Seek (BufFile, BufFilePos);
|
||||
System.BlockRead (BufFile, Buffer^, BufSize, BufTop);
|
||||
|
||||
IoResult := System.IoResult;
|
||||
|
||||
If IoResult = 0 Then BufDirty := False;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.FlushBuffer;
|
||||
Begin
|
||||
System.Seek (BufFile, BufFilePos);
|
||||
System.BlockWrite (BufFile, Buffer^, BufTop, BufTop);
|
||||
|
||||
IoResult := System.IoResult;
|
||||
|
||||
// BufPos := 0;
|
||||
End;
|
||||
|
||||
Procedure TBufFile.RecordInsert (RecNum: LongInt);
|
||||
Var
|
||||
TempBuf : PBufFileBuffer;
|
||||
Count : LongInt;
|
||||
Begin
|
||||
If (RecNum < 1) or (RecNum > FileSize + 1) Then Exit;
|
||||
|
||||
GetMem (TempBuf, RecordSize);
|
||||
Dec (RecNum);
|
||||
|
||||
Reset;
|
||||
|
||||
For Count := FileSize - 1 DownTo RecNum Do Begin
|
||||
System.Seek (BufFile, Count * RecordSize);
|
||||
System.BlockRead (BufFile, TempBuf^, RecordSize);
|
||||
System.BlockWrite (BufFile, TempBuf^, RecordSize);
|
||||
End;
|
||||
|
||||
Seek (RecNum);
|
||||
|
||||
FillBuffer;
|
||||
|
||||
FreeMem (TempBuf, RecordSize);
|
||||
End;
|
||||
|
||||
Procedure TBufFile.RecordDelete (RecNum: LongInt);
|
||||
Var
|
||||
TempBuf : PBufFileBuffer;
|
||||
Count : LongInt;
|
||||
Begin
|
||||
If (RecNum < 1) or (RecNum > FileSize) Then Exit;
|
||||
|
||||
GetMem (TempBuf, RecordSize);
|
||||
Dec (RecNum);
|
||||
|
||||
Reset;
|
||||
|
||||
For Count := RecNum To FileSize - 2 Do Begin
|
||||
System.Seek (BufFile, Succ(Count) * RecordSize);
|
||||
System.BlockRead (BufFile, TempBuf^, RecordSize);
|
||||
System.Seek (BufFile, Count * RecordSize);
|
||||
System.BlockWrite (BufFile, TempBuf^, RecordSize);
|
||||
End;
|
||||
|
||||
System.Seek (BufFile, Pred(FileSize) * RecordSize);
|
||||
System.Truncate (BufFile);
|
||||
|
||||
Seek (RecNum);
|
||||
|
||||
FillBuffer;
|
||||
|
||||
FreeMem (TempBuf, RecordSize);
|
||||
End;
|
||||
|
||||
Function FileFind (FN: String) : String;
|
||||
Var
|
||||
Dir : SearchRec;
|
||||
|
@ -847,4 +552,185 @@ Begin
|
|||
Result := FN + '.' + NewExt;
|
||||
End;
|
||||
|
||||
{ FILE STREAMING FUNCTIONS }
|
||||
|
||||
Constructor TFileBuffer.Create (BufferSize: LongInt);
|
||||
Begin
|
||||
Inherited Create;
|
||||
|
||||
BufSize := BufferSize;
|
||||
BufStart := 0;
|
||||
BufEnd := 0;
|
||||
BufPos := 0;
|
||||
BufEOF := False;
|
||||
BufRead := 0;
|
||||
Buffer := NIL;
|
||||
BufDirty := False;
|
||||
IsOpened := False;
|
||||
End;
|
||||
|
||||
Destructor TFileBuffer.Destroy;
|
||||
Begin
|
||||
If IsOpened Then CloseStream;
|
||||
End;
|
||||
|
||||
Function TFileBuffer.OpenStream (FN: String; OpenType: TFileBufferOpenType; OpenMode: Byte) : Boolean;
|
||||
Begin
|
||||
Result := False;
|
||||
|
||||
If IsOpened Then CloseStream;
|
||||
|
||||
Assign (InFile, FN);
|
||||
|
||||
Case OpenType of
|
||||
fmOpen : If Not ioReset (InFile, 1, OpenMode) Then Exit;
|
||||
fmOpenCreate : If Not ioReset (InFile, 1, OpenMode) Then
|
||||
If Not FileExist(FN) Then Begin
|
||||
If Not ioReWrite (InFile, 1, OpenMode) Then Exit;
|
||||
End Else
|
||||
Exit;
|
||||
fmCreate : If Not ioReWrite (InFile, 1, OpenMode) Then Exit;
|
||||
End;
|
||||
|
||||
GetMem (Buffer, BufSize);
|
||||
|
||||
FillBuffer;
|
||||
|
||||
BufDirty := False;
|
||||
IsOpened := True;
|
||||
Result := True;
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.CloseStream;
|
||||
Begin
|
||||
If IsOpened Then Begin
|
||||
If BufDirty Then FlushBuffer;
|
||||
|
||||
System.Close (InFile);
|
||||
End;
|
||||
|
||||
If Assigned(Buffer) Then Begin
|
||||
FreeMem (Buffer, BufSize);
|
||||
|
||||
Buffer := NIL;
|
||||
End;
|
||||
|
||||
IsOpened := False;
|
||||
End;
|
||||
|
||||
Function TFileBuffer.FilePos : LongInt;
|
||||
Begin
|
||||
FilePos := BufStart + BufPos;
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.FillBuffer;
|
||||
Var
|
||||
Start : LongInt;
|
||||
Begin
|
||||
Start := System.FilePos(InFile);
|
||||
|
||||
System.BlockRead (InFile, Buffer^[0], BufSize, BufRead);
|
||||
|
||||
BufStart := Start;
|
||||
BufEnd := Start + BufRead;
|
||||
BufPos := 0;
|
||||
BufEOF := System.EOF(InFile);
|
||||
End;
|
||||
|
||||
Function TFileBuffer.Read : Char;
|
||||
Begin
|
||||
If BufPos >= BufSize Then FillBuffer;
|
||||
|
||||
Read := Buffer^[BufPos];
|
||||
|
||||
Inc (BufPos);
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.BlockWrite (Var Buf; Size: LongInt);
|
||||
Var
|
||||
Offset : LongInt;
|
||||
Begin
|
||||
If BufPos + Size > BufSize Then Begin
|
||||
Offset := BufSize - BufPos;
|
||||
|
||||
If Offset > 0 Then
|
||||
Move(Buf, Buffer^[BufPos], Offset);
|
||||
|
||||
FlushBuffer;
|
||||
FillBuffer;
|
||||
|
||||
Move (TFileBufferRec(Buf)[Offset], Buffer^[BufPos], Size - Offset);
|
||||
|
||||
BufPos := BufPos + Size - Offset;
|
||||
End Else Begin
|
||||
Move (Buf, Buffer^[BufPos], Size);
|
||||
Inc (BufPos, Size);
|
||||
End;
|
||||
|
||||
If BufPos > BufEnd Then BufEnd := BufPos;
|
||||
If BufPos > BufRead Then BufRead := BufPos;
|
||||
|
||||
BufDirty := True;
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.BlockRead (Var Buf; Size: LongInt);
|
||||
Var
|
||||
Res : LongInt;
|
||||
Begin
|
||||
BlockRead(Buf, Size, Res);
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.BlockRead (Var Buf; Size: LongInt; Var Count: LongInt);
|
||||
Begin
|
||||
If BufPos + Size >= BufRead Then Begin
|
||||
If BufDirty Then FlushBuffer;
|
||||
|
||||
If Size > BufSize Then Size := BufSize;
|
||||
|
||||
System.Seek(InFile, BufStart + BufPos);
|
||||
|
||||
FillBuffer;
|
||||
|
||||
If BufRead < Size Then Size := BufRead;
|
||||
End;
|
||||
|
||||
Move (Buffer^[BufPos], Buf, Size);
|
||||
Inc (BufPos, Size);
|
||||
|
||||
Count := Size;
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.Seek (FP : LongInt);
|
||||
Begin
|
||||
If (FP >= BufStart) and (FP < BufEnd) Then
|
||||
BufPos := (BufEnd - (BufEnd - FP)) - BufStart
|
||||
Else Begin
|
||||
If BufDirty Then FlushBuffer;
|
||||
|
||||
System.Seek(InFile, FP);
|
||||
|
||||
FillBuffer;
|
||||
End;
|
||||
End;
|
||||
|
||||
Function TFileBuffer.EOF : Boolean;
|
||||
Begin
|
||||
EOF := (BufStart + BufPos >= BufEnd) and BufEOF;
|
||||
End;
|
||||
|
||||
Function TFileBuffer.FileSize : LongInt;
|
||||
Begin
|
||||
FileSize := System.FileSize(InFile);
|
||||
End;
|
||||
|
||||
Procedure TFileBuffer.FlushBuffer;
|
||||
Var
|
||||
Res : LongInt;
|
||||
Begin
|
||||
System.Seek (InFile, BufStart);
|
||||
System.BlockWrite (InFile, Buffer^, BufRead, Res);
|
||||
|
||||
BufDirty := False;
|
||||
End;
|
||||
|
||||
End.
|
||||
|
|
|
@ -196,7 +196,7 @@ Begin
|
|||
Repeat Until Server <> NIL; // Synchronize with server class
|
||||
Repeat Until ServerStatus <> NIL; // Syncronize with status class
|
||||
|
||||
Server.WaitInit(Port);
|
||||
Server.WaitInit('0.0.0.0', Port);
|
||||
|
||||
If Terminated Then Exit;
|
||||
|
||||
|
|
Loading…
Reference in New Issue