1
0
mirror of https://github.com/moparisthebest/Simba synced 2025-01-30 23:00:18 -05:00

Files + Fixed form popup. ;)

git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@24 3f818213-9676-44b0-a9b4-5e4c4e03d09d
This commit is contained in:
Wizzup? 2009-09-04 05:57:40 +00:00
parent 71c7f9bf3d
commit fd960aa1aa
4 changed files with 525 additions and 20 deletions

View File

@ -6,7 +6,7 @@
<MainUnit Value="0"/>
<TargetFileExt Value=""/>
<UseXPManifest Value="True"/>
<ActiveEditorIndexAtStart Value="0"/>
<ActiveEditorIndexAtStart Value="3"/>
</General>
<VersionInfo>
<ProjectVersion Value=""/>
@ -30,12 +30,12 @@
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="42">
<Units Count="43">
<Unit0>
<Filename Value="project1.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="project1"/>
<CursorPos X="71" Y="6"/>
<CursorPos X="41" Y="17"/>
<TopLine Value="1"/>
<EditorIndex Value="0"/>
<UsageCount Value="55"/>
@ -167,7 +167,7 @@
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="TestUnit"/>
<CursorPos X="1" Y="1"/>
<CursorPos X="39" Y="66"/>
<TopLine Value="1"/>
<EditorIndex Value="1"/>
<UsageCount Value="21"/>
@ -299,8 +299,8 @@
<Filename Value="../../Units/MMLCore/client.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="Client"/>
<CursorPos X="38" Y="8"/>
<TopLine Value="1"/>
<CursorPos X="3" Y="36"/>
<TopLine Value="16"/>
<EditorIndex Value="3"/>
<UsageCount Value="20"/>
<Loaded Value="True"/>
@ -318,8 +318,8 @@
<Unit39>
<Filename Value="../../../cogat/Units/CogatUnits/compfiles.pas"/>
<UnitName Value="compFiles"/>
<CursorPos X="1" Y="1"/>
<TopLine Value="1"/>
<CursorPos X="55" Y="501"/>
<TopLine Value="501"/>
<EditorIndex Value="6"/>
<UsageCount Value="20"/>
<Loaded Value="True"/>
@ -328,8 +328,8 @@
<Filename Value="../../Units/MMLCore/files.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="files"/>
<CursorPos X="61" Y="9"/>
<TopLine Value="2"/>
<CursorPos X="21" Y="44"/>
<TopLine Value="31"/>
<EditorIndex Value="5"/>
<UsageCount Value="21"/>
<Loaded Value="True"/>
@ -344,8 +344,15 @@
<UsageCount Value="20"/>
<Loaded Value="True"/>
</Unit41>
<Unit42>
<Filename Value="../../../../Documents/lazarus/lcl/forms.pp"/>
<UnitName Value="Forms"/>
<CursorPos X="15" Y="1236"/>
<TopLine Value="981"/>
<UsageCount Value="10"/>
</Unit42>
</Units>
<JumpHistory Count="7" HistoryIndex="6">
<JumpHistory Count="26" HistoryIndex="25">
<Position1>
<Filename Value="testunit.pas"/>
<Caret Line="10" Column="7" TopLine="1"/>
@ -374,6 +381,82 @@
<Filename Value="testunit.pas"/>
<Caret Line="9" Column="32" TopLine="1"/>
</Position7>
<Position8>
<Filename Value="project1.lpr"/>
<Caret Line="17" Column="17" TopLine="1"/>
</Position8>
<Position9>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="13" Column="20" TopLine="7"/>
</Position9>
<Position10>
<Filename Value="../../../cogat/Units/CogatUnits/compfiles.pas"/>
<Caret Line="30" Column="22" TopLine="11"/>
</Position10>
<Position11>
<Filename Value="../../../cogat/Units/CogatUnits/compfiles.pas"/>
<Caret Line="31" Column="23" TopLine="11"/>
</Position11>
<Position12>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="439" Column="74" TopLine="11"/>
</Position12>
<Position13>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="43" Column="20" TopLine="28"/>
</Position13>
<Position14>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="55" Column="24" TopLine="40"/>
</Position14>
<Position15>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="36" Column="27" TopLine="31"/>
</Position15>
<Position16>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="41" Column="46" TopLine="31"/>
</Position16>
<Position17>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="463" Column="34" TopLine="448"/>
</Position17>
<Position18>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="34" Column="12" TopLine="30"/>
</Position18>
<Position19>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="46" Column="19" TopLine="31"/>
</Position19>
<Position20>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="72" Column="19" TopLine="50"/>
</Position20>
<Position21>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="37" Column="11" TopLine="21"/>
</Position21>
<Position22>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="38" Column="96" TopLine="28"/>
</Position22>
<Position23>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="417" Column="42" TopLine="402"/>
</Position23>
<Position24>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="449" Column="1" TopLine="431"/>
</Position24>
<Position25>
<Filename Value="../../Units/MMLCore/client.pas"/>
<Caret Line="37" Column="18" TopLine="12"/>
</Position25>
<Position26>
<Filename Value="../../Units/MMLCore/files.pas"/>
<Caret Line="20" Column="29" TopLine="11"/>
</Position26>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>

View File

@ -14,6 +14,7 @@ uses
begin
{$I project1.lrs}
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

View File

@ -5,7 +5,7 @@ unit Client;
interface
uses
Classes, SysUtils, MufasaTypes, Window, Input;
Classes, SysUtils, MufasaTypes, Window, Input, Files;
type
TClient = class(TObject)
@ -15,6 +15,7 @@ type
public
MWindow: TMWindow;
MInput: TMInput;
MFiles: TMFiles;
end;
@ -27,12 +28,15 @@ begin
MWindow := TMWindow.Create(Self);
MInput := TMInput.Create(Self);
MFiles := TMFiles.Create;
end;
destructor TClient.Destroy;
begin
MWindow.Destroy;
MFiles.Destroy;
MInput.Destroy;
MWindow.Destroy;
inherited;
end;

View File

@ -8,25 +8,442 @@ uses
Classes, SysUtils;
type
TMWindow = class(TObject)
constructor Create(Client: TObject);
destructor Destroy; override;
public
TMufasaFile = record
Path: String;
FS: TFileStream;
BytesRead, Mode: Integer;
end;
TMufasaFilesArray = Array Of TMufasaFile;
TMFiles = class(TObject)
constructor Create;
destructor Destroy; override;
public
function CreateFile(Path: string): Integer;
function OpenFile(Path: string; Shared: Boolean): Integer;
function RewriteFile(Path: string; Shared: Boolean): Integer;
procedure CloseFile(FileNum: Integer);
function EndOfFile(FileNum: Integer): Boolean;
function FileSizeMuf(FileNum: Integer): LongInt;
function ReadFileString(FileNum: Integer; var s: string; x: Integer): Boolean;
function WriteFileString(FileNum: Integer; s: string): Boolean;
Function SetFileCharPointer(FileNum, cChars, Origin: Integer): Integer;
function FilePointerPos(FileNum: Integer): Integer;
protected
MFiles: TMufasaFilesArray;
FreeSpots: Array Of Integer;
private
procedure FreeFileList;
function AddFileToManagedList(Path: string; FS: TFileStream; Mode: Integer): Integer;
end;
implementation
uses
{$IFDEF MSWINDOWS}Windows,{$ENDIF} IniFiles;
constructor TMWindow.Create(Client: TObject);
constructor TMFiles.Create;
begin
inherited Create;
SetLength(Self.MFiles, 0);
SetLength(Self.FreeSpots, 0);
end;
destructor TMWindow.Destroy;
procedure TMFiles.FreeFileList;
var
I : integer;
begin;
//WriteLn('Freeing all open Files.');
For I := 0 To High(MFiles) Do
If MFiles[i].FS <> nil Then
Begin
WriteLn('You forgot to free a file...');
Try
MFiles[I].FS.Free;
Except
WriteLn('FreeFileList - Exception when freeing');
End;
//FileClose(Files[i].Handle);
End;
SetLength(MFiles, 0);
SetLength(FreeSpots, 0);
//WriteLn('Done Freeing all Files');
end;
destructor TMFiles.Destroy;
begin
FreeFileList;
inherited;
end;
Function TMFiles.AddFileToManagedList(Path: String; FS: TFileStream; Mode: Integer): Integer;
Var
tFile: TMufasaFile;
Begin
tFile.Path := Path;
tFile.FS := FS;
tFile.Mode := Mode;
tFile.BytesRead := 0;
If Length(FreeSpots) > 0 Then
Begin
//WriteLn('There is a free spot: ' + IntToStr(FreeSpots[High(FreeSpots)]));
MFiles[FreeSpots[High(FreeSpots)]] := tFile;
Result := FreeSpots[High(FreeSpots)];
SetLength(FreeSpots, High(FreeSpots));
End Else
Begin
//WriteLn('No Free Spot. Increasing the size');
SetLength(MFiles, Length(MFiles) + 1);
//Writeln('Length of Files: ' + IntToStr(Length(Files)));
MFiles[High(MFiles)] := tFile;
Result := High(MFiles);
End;
End;
{/\
Copies content of the file into the result (if no errors occur).
/\}
Function TMFiles.SetFileCharPointer(FileNum, cChars, Origin: Integer): Integer;
Begin
If(FileNum < 0) or (FileNum >= Length(MFiles)) Then
Begin
WriteLn('Invalid File Num');
Result := -1;
Exit;
End;
{If Files[FileNum].Handle = -1 Then
Begin
WriteLn('SetFileCharPointer: Invalid Internal Handle');
Result := -1;
Exit;
End;}
case Origin of
fsFromBeginning:
If(cChars < 0) Then
Begin
Writeln('fsFromBeginning takes no negative cChars.');
Result := -1;
Exit;
End;
fsFromCurrent:
;
fsFromEnd:
If(cChars > 0) Then
Begin
Writeln('fsFromEnd takes no positive cChars.');
Result := -1;
Exit;
End;
else
Begin
WriteLn('Invalid Origin: ' + IntToStr(Origin));
Result := -1;
Exit;
End;
End;
Try
Result := MFiles[FileNum].FS.Seek(cChars, Origin);
Except
WriteLn('SetFileCharPointer - Exception Occured.');
End;
//Result := FileSeek(Files[FileNum].Handle, cChars, Origin);
End;
{/\
Opens a file for reading.
Returns the handle (index) to the File Array.
Returns -1 if unsuccesfull.
/\}
function TMFiles.CreateFile(Path: string): Integer;
Var
FS: TFileStream;
begin
Try
FS := TFileStream.Create(Path, fmCreate);
Except
Result := -1;
WriteLn('CreateFile - Exception. Could not create file. Returning -1');
Exit;
End;
Result := AddFileToManagedList(Path, FS, fmCreate);
end;
{/\
Opens a file for reading.
Returns the handle (index) to the File Array.
Returns -1 if unsuccesfull.
/\}
function TMFiles.OpenFile(Path: string; Shared: Boolean): Integer;
Var
FS: TFileStream;
fMode: Integer;
begin
If Shared Then
fMode := fmOpenRead or fmShareDenyNone
Else
fMode := fmOpenRead or fmShareExclusive;
Try
FS := TFileStream.Create(Path, fMode)
Except
Result := -1;
WriteLn('OpenFile - Exception. Could not create file. Returning -1');
Exit;
End;
Result := AddFileToManagedList(Path, FS, fMode);
{Result := FileOpen(Path, fmOpenRead);
If Result <> -1 Then
Begin
//WriteLn('File was successfully opened');
Result := AddFileToManagedList(Path, Result, fmOpenRead);
{If Result <> -1 Then
WriteLn('File was successfully added: ' + IntToStr(Result));}
End Else
Begin
WriteLn('Could not open file. Returning -1');
End; }
end;
{/\
Opens a file for writing.
Returns the handle (index) to the File Array.
Returns -1 if unsuccesfull.
/\}
function TMFiles.RewriteFile(Path: string; Shared: Boolean): Integer;
Var
FS: TFileStream;
fMode: Integer;
begin
If Shared Then
fMode := fmOpenReadWrite or fmShareDenyNone or fmCreate
Else
fMode := fmOpenReadWrite or fmShareDenyWrite or fmShareDenyRead or fmCreate;
Try
FS := TFileStream.Create(Path, fMode);
Except
Result := -1;
WriteLn('ReWriteFile - Exception. Could not create file. Returning -1');
Exit;
End;
Result := AddFileToManagedList(Path, FS, fMode);
{Result := FileOpen(Path, fmOpenReadWrite);
If Result <> -1 Then
Begin
//WriteLn('File was successfully opened.');
Result := AddFileToManagedList(Path, Result, fmOpenReadWrite);
{If Result <> -1 Then
WriteLn('File was successfully added: ' + IntToStr(Result)); }
End Else
Begin
WriteLn('Could not open file. Returning -1');
End; }
end;
{/\
Free's the given File at the given index.
/\}
procedure TMFiles.CloseFile(FileNum: Integer);
begin
//Writeln('Length of Files: ' + IntToStr(Length(Files)));
If (FileNum >= Length(MFiles)) or (FileNum < 0) Then
Begin
WriteLn('CloseFile. Invalid FileNum: ' + IntToStr(FileNum));
Exit;
End;
Try
MFiles[FileNum].FS.Free;
Except
WriteLn('CloseFile, exception when freeing the file...');
Exit;
End;
MFiles[FileNum].FS := nil;
SetLength(FreeSpots, Length(FreeSpots) + 1);
FreeSpots[High(FreeSpots)] := FileNum;
{If Files[FileNum].Handle = -1 Then
Begin
WriteLn('CloseFile: Invalid Internal Handle');
Exit;
End;
FileClose(Files[FileNum].Handle); }
end;
{/\
Returns true if the BytesRead of the given FileNum (Index) has been reached.
Also returns true if the FileNum is not valid.
/\}
function TMFiles.EndOfFile(FileNum: Integer): Boolean;
begin
If(FileNum < 0) or (FileNum >= Length(MFiles)) Then
Begin
WriteLn('Invalid File Num');
Result := True;
Exit;
End;
If MFiles[FileNum].FS = nil Then
Begin
WriteLn('EndOfFile: Invalid Internal Handle');
Result := True;
Exit;
End;
Result := FilePointerPos(FileNum) >= FileSizeMuf(FileNum);
end;
{/\
Returns the FileSize of the given index (FileNum)
/\}
function TMFiles.FileSizeMuf(FileNum: Integer): LongInt;
Var
tempPos: Integer;
begin
inherited;
If(FileNum < 0) or (FileNum >= Length(MFiles)) Then
Begin
WriteLn('Invalid File Num');
Result := -1;
Exit;
End;
If MFiles[FileNum].FS = nil Then
Begin
WriteLn('FileSize: Invalid Internal Handle');
Result := -1;
Exit;
End;
Result := MFiles[FileNum].FS.Size;
{
If Files[FileNum].Handle = -1 Then
Begin
WriteLn('FileSize: Invalid Internal Handle');
Result := -1;
Exit;
End;
// Get our current position.
tempPos := FileSeek(Files[FileNum].Handle, 0, fsFromCurrent);
// End of the file.
Result := FileSeek(Files[FileNum].Handle, 0, fsFromEnd);
// Reset the position.
FileSeek(Files[FileNum].Handle, tempPos, fsFromBeginning); }
end;
function TMFiles.FilePointerPos(FileNum: Integer): Integer;
begin
If(FileNum < 0) or (FileNum >= Length(MFiles)) Then
Begin
WriteLn('Invalid File Num');
Result := -1;
Exit;
End;
If MFiles[FileNum].FS = nil Then
Begin
WriteLn('FileSize: Invalid Internal Handle');
Result := -1;
Exit;
End;
try
Result := MFiles[FileNum].FS.Seek(0, fsFromCurrent);
Except
WriteLn('Exception in FilePointerPos');
End;
//Result := FileSeek(Files[FileNum].FS, 0, fsFromCurrent);
end;
{/\
Reads x numbers of characters from a file, and stores it into s.
/\}
function TMFiles.ReadFileString(FileNum: Integer; var s: string; x: Integer): Boolean;
begin
If(FileNum < 0) or (FileNum >= Length(MFiles)) Then
Begin
WriteLn('Invalid File Num');
Result := False;
Exit;
End;
If MFiles[FileNum].FS = nil Then
Begin
WriteLn('ReadFileString: Invalid Internal Handle');
Exit;
End;
SetLength(S, 0);
SetLength(S, X);
MFiles[FileNum].FS.Read(S[1], x);
{Files[FileNum].BytesRead := Files[FileNum].BytesRead + X;
FileRead(Files[FileNum].Handle, S[1], X);
SetLength(S, X); }
end;
{/\
Writes s in the given File.
/\}
function TMFiles.WriteFileString(FileNum: Integer; s: string): Boolean;
var
BytesRead : DWord;
begin
If(FileNum < 0) or (FileNum >= Length(MFiles)) Then
Begin
WriteLn('Invalid File Num');
Result := False;
Exit;
End;
If(MFiles[FileNum].FS = nil) Then
Begin
WriteLn('WriteFileString: Invalid Internal Handle');
Result := False;
Exit;
End;
{If((Files[FileNum].Mode and fmOpenWrite) = 0) Then
Begin
WriteLn('This file may not write');
Exit;
End; }
try
Result := MFiles[FileNum].FS.Write(S[1], Length(S)) <> 1;
except
WriteLn('Exception - WriteFileString.');
Result := False;
end;
{If(FileWrite(Files[FileNum].Handle, S[1], Length(S)) <> -1) Then
Result := True
Else
Result := False; }
end;
end.