* SectionExists now Delphi compatible

git-svn-id: trunk@7378 -
This commit is contained in:
michael 2007-05-17 15:53:02 +00:00
parent 720ad93659
commit cc9bc424e4

View File

@ -81,6 +81,7 @@ type
FName: string; FName: string;
FKeyList: TIniFileKeyList; FKeyList: TIniFileKeyList;
public public
Function Empty : Boolean;
constructor Create(AName: string); constructor Create(AName: string);
destructor Destroy; override; destructor Destroy; override;
property Name: string read FName; property Name: string read FName;
@ -143,6 +144,8 @@ type
FCacheUpdates: Boolean; FCacheUpdates: Boolean;
FDirty : Boolean; FDirty : Boolean;
procedure FillSectionList(AStrings: TStrings); procedure FillSectionList(AStrings: TStrings);
Procedure DeleteSection(ASection : TIniFileSection);
Procedure MaybeDeleteSection(ASection : TIniFileSection);
protected protected
procedure MaybeUpdateFile; procedure MaybeUpdateFile;
property Dirty : Boolean Read FDirty; property Dirty : Boolean Read FDirty;
@ -255,6 +258,22 @@ begin
inherited Clear; inherited Clear;
end; end;
Function TIniFileSection.Empty : Boolean;
Var
I : Integer;
begin
Result:=True;
I:=0;
While Result and (I<KeyList.Count) do
begin
result:=IsComment(KeyList[i].Ident);
Inc(i);
end;
end;
{ TIniFileSection } { TIniFileSection }
constructor TIniFileSection.Create(AName: string); constructor TIniFileSection.Create(AName: string);
@ -332,8 +351,13 @@ begin
end; end;
function TCustomIniFile.SectionExists(const Section: string): Boolean; function TCustomIniFile.SectionExists(const Section: string): Boolean;
Var
S : TIniFileSection;
begin begin
Result := (FSectionList.SectionByName(Section,CaseSensitive) <> nil); S:=FSectionList.SectionByName(Section,CaseSensitive);
Result:=Assigned(S) and Not S.Empty;
end; end;
function TCustomIniFile.ReadInteger(const Section, Ident: string; Default: Longint): Longint; function TCustomIniFile.ReadInteger(const Section, Ident: string; Default: Longint): Longint;
@ -765,6 +789,20 @@ begin
end; end;
end; end;
procedure TIniFile.DeleteSection(ASection : TIniFileSection);
begin
FSectionList.Delete(FSectionList.IndexOf(ASection));
ASection.Free;
end;
Procedure TIniFile.MaybeDeleteSection(ASection : TIniFileSection);
begin
If Asection.Empty then
DeleteSection(ASection);
end;
procedure TIniFile.EraseSection(const Section: string); procedure TIniFile.EraseSection(const Section: string);
var var
oSection: TIniFileSection; oSection: TIniFileSection;
@ -773,8 +811,7 @@ begin
if oSection <> nil then begin if oSection <> nil then begin
{ It is needed so UpdateFile doesn't find a defunct section } { It is needed so UpdateFile doesn't find a defunct section }
{ and cause the program to crash } { and cause the program to crash }
FSectionList.Delete(FSectionList.IndexOf(oSection)); DeleteSection(OSection);
oSection.Free;
MaybeUpdateFile; MaybeUpdateFile;
end; end;
end; end;
@ -784,15 +821,18 @@ var
oSection: TIniFileSection; oSection: TIniFileSection;
oKey: TIniFileKey; oKey: TIniFileKey;
begin begin
oSection := FSectionList.SectionByName(Section,CaseSensitive); oSection := FSectionList.SectionByName(Section,CaseSensitive);
if oSection <> nil then begin if oSection <> nil then
oKey := oSection.KeyList.KeyByName(Ident,CaseSensitive); begin
if oKey <> nil then begin oKey := oSection.KeyList.KeyByName(Ident,CaseSensitive);
oSection.KeyList.Delete(oSection.KeyList.IndexOf(oKey)); if oKey <> nil then
oKey.Free; begin
MaybeUpdateFile; oSection.KeyList.Delete(oSection.KeyList.IndexOf(oKey));
end; oKey.Free;
end; MaybeDeleteSection(oSection);
MaybeUpdateFile;
end;
end;
end; end;
procedure TIniFile.UpdateFile; procedure TIniFile.UpdateFile;