* load/save stream support

This commit is contained in:
peter 1999-11-23 09:50:51 +00:00
parent 56343f0b08
commit 1c1ec9afc9

View File

@ -13,7 +13,7 @@
unit IniFiles; unit IniFiles;
{$mode delphi} {$mode objfpc}
{$H+} {$H+}
interface interface
@ -27,6 +27,7 @@ type
TIniFile = class(TObject) TIniFile = class(TObject)
private private
FFileName : string; FFileName : string;
FStream : TStream;
FFileBuffer : TStringList; FFileBuffer : TStringList;
function GetName(const line : string) : string; function GetName(const line : string) : string;
function GetValue(const line, name : string) : string; function GetValue(const line, name : string) : string;
@ -34,10 +35,15 @@ type
function IsSection(const line : string) : boolean; function IsSection(const line : string) : boolean;
function GetSectionIndex(const section : string) : integer; function GetSectionIndex(const section : string) : integer;
protected protected
procedure SetFileName(const fn:string);
procedure SetStream(s:TStream);
procedure LoadFromFile; procedure LoadFromFile;
procedure SaveToFile; procedure SaveToFile;
procedure LoadFromStream;
procedure SaveToStream;
public public
constructor Create(const theFileName : string); constructor Create(const theFileName : string);
constructor Create(s:TStream);
destructor Destroy; override; destructor Destroy; override;
procedure DeleteKey(const section, ident : string); procedure DeleteKey(const section, ident : string);
procedure EraseSection(const section : string); procedure EraseSection(const section : string);
@ -69,12 +75,22 @@ const
constructor TIniFile.Create(const theFileName : string); constructor TIniFile.Create(const theFileName : string);
begin begin
FFileName := theFileName; FFileName := theFileName;
FStream:=nil;
FFileBuffer := TStringList.Create; FFileBuffer := TStringList.Create;
if FileExists(fileName) then if FileExists(fileName) then
LoadFromFile; LoadFromFile;
end; end;
constructor TIniFile.Create(s:TStream);
begin
FFileName := '';
FStream:=s;
FFileBuffer := TStringList.Create;
LoadFromStream;
end;
destructor TIniFile.Destroy; destructor TIniFile.Destroy;
begin begin
FFileBuffer.Free; FFileBuffer.Free;
@ -141,16 +157,42 @@ begin
result := FFileBuffer.IndexOf(brackets[0] + section + brackets[1]); result := FFileBuffer.IndexOf(brackets[0] + section + brackets[1]);
end; end;
{ Load/Save }
procedure TIniFile.SetFileName(const fn:string);
begin
FFileName:=fn;
end;
procedure TIniFile.SetStream(s:TStream);
begin
FStream:=s;
end;
procedure TIniFile.LoadFromFile; procedure TIniFile.LoadFromFile;
begin begin
if FFileName<>'' then
FFileBuffer.LoadFromFile(FFileName); FFileBuffer.LoadFromFile(FFileName);
end; end;
procedure TIniFile.SaveToFile; procedure TIniFile.SaveToFile;
begin begin
if FFileName<>'' then
FFileBuffer.SaveToFile(FFileName); FFileBuffer.SaveToFile(FFileName);
end; end;
procedure TIniFile.LoadFromStream;
begin
if assigned(FStream) then
FFileBuffer.LoadFromStream(FStream);
end;
procedure TIniFile.SaveToStream;
begin
if assigned(FStream) then
FFileBuffer.SaveToStream(FStream);
end;
{ Read all Names of one Section } { Read all Names of one Section }
procedure TIniFile.ReadSection(const section : string; strings : TStrings); procedure TIniFile.ReadSection(const section : string; strings : TStrings);
@ -439,7 +481,10 @@ end.
{ {
$Log$ $Log$
Revision 1.4 1999-11-08 15:01:38 peter Revision 1.5 1999-11-23 09:50:51 peter
* load/save stream support
Revision 1.4 1999/11/08 15:01:38 peter
* fpcmake support * fpcmake support
Revision 1.3 1999/11/02 23:58:37 peter Revision 1.3 1999/11/02 23:58:37 peter