{ This file is part of the Free Pascal Integrated Development Environment Copyright (c) 1998 by B‚rczi G bor Reading and writing .INI files See the file COPYING.FPC, included in this distribution, for details about the copyright. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. **********************************************************************} unit WINI; interface uses Objects; type PINIEntry = ^TINIEntry; TINIEntry = object(TObject) constructor Init(const ALine: string); constructor Init(const ATag,AValue,AComment: string); function GetText: string; function GetTag: string; function GetComment: string; function GetValue: string; procedure SetValue(const S: string); procedure SetComment(const S: string); destructor Done; virtual; private TagHash : Cardinal; Tag : PString; Value : PString; Comment : PString; Text : PString; Modified : boolean; procedure Split; end; PINISection = ^TINISection; TINISection = object(TObject) constructor Init(const AName: string); function GetName: string; function AddEntry(const S: string): PINIEntry; function AddEntry(const Tag,Value,Comment: string): PINIEntry; function SearchEntry(Tag: string): PINIEntry; virtual; procedure DeleteEntry(Tag: string); procedure ForEachEntry(EnumProc: pointer); virtual; destructor Done; virtual; private NameHash : Cardinal; Name : PString; Entries : PCollection; end; PINIFile = ^TINIFile; TINIFile = object(TObject) MakeNullEntries: boolean; constructor Init(const AFileName: string); function GetFileName: string; function Read: boolean; virtual; function Update: boolean; virtual; function IsModified: boolean; virtual; function SearchSection(Section: string): PINISection; virtual; function SearchEntry(const Section, Tag: string): PINIEntry; virtual; procedure ForEachSection(EnumProc: pointer); virtual; procedure ForEachEntry(const Section: string; EnumProc: pointer); virtual; function GetEntry(const Section, Tag, Default: string): string; virtual; procedure SetEntry(const Section, Tag, Value: string); virtual; procedure SetEntry(const Section, Tag, Value,Comment: string); virtual; function GetIntEntry(const Section, Tag: string; Default: longint): longint; virtual; procedure SetIntEntry(const Section, Tag: string; Value: longint); virtual; procedure DeleteSection(const Section: string); virtual; procedure DeleteEntry(const Section, Tag: string); destructor Done; virtual; private { ReadOnly: boolean;} Sections: PCollection; FileName: PString; end; const MainSectionName : string[40] = 'MainSection'; CommentChar : char = ';'; ValidStrDelimiters: set of char = ['''','"']; function EscapeIniText(S : string) : String; implementation uses WUtils; function EscapeIniText(S : string) : String; var delimiter : char; i: integer; begin delimiter:=#0; while delimiter < #255 do begin if (delimiter in ValidStrDelimiters) and (pos(delimiter,S)=0) then break; delimiter:=succ(delimiter); end; if delimiter=#255 then begin { we use " delimiter, but the text also contains double quotes, which need to be escaped, by doubling it } delimiter:='"'; for i:=length(s) downto 1 do if (s[i]=delimiter) then s:=copy(s,1,i-1)+delimiter+copy(s,i+1,length(s)); end; EscapeIniText:=delimiter+s+delimiter; end; {$IFOPT Q+} {$Q-} {$DEFINE REENABLE_Q} {$ENDIF} {$IFOPT R+} {$R-} {$DEFINE REENABLE_R} {$ENDIF} function CalcHash(const s: String): Cardinal; var i: integer; begin CalcHash := 0; for i := 1 to Length(s) do CalcHash := CalcHash shl 9 - CalcHash shl 4 + Ord(S[I]); end; {$IFDEF REENABLE_Q} {$Q+} {$ENDIF} {$IFDEF REENABLE_R} {$R+} {$ENDIF} constructor TINIEntry.Init(const ALine: string); begin inherited Init; Text:=NewStr(ALine); Split; end; constructor TINIEntry.Init(const ATag,AValue,AComment: string); begin inherited Init; Tag:=NewStr(ATag); Value:=NewStr(AValue); Comment:=NewStr(AComment); Text:=NewStr(GetText); end; function TINIEntry.GetText: string; var S,CoS: string; begin if Text=nil then begin CoS:=GetComment; S:=GetTag+'='+GetValue; if Trim(S)='=' then S:=CoS else if CoS<>'' then begin { if Value contains CommentChar, we need to add delimiters } if pos(CommentChar,S)>0 then S:=EscapeIniText(S); S:=S+' '+CommentChar+' '+CoS; end end else S:=Text^; GetText:=S; end; function TINIEntry.GetTag: string; begin GetTag:=GetStr(Tag); end; function TINIEntry.GetComment: string; begin GetComment:=GetStr(Comment); end; function TINIEntry.GetValue: string; begin GetValue:=GetStr(Value); end; procedure TINIEntry.SetValue(const S: string); begin if GetValue<>S then begin if Text<>nil then DisposeStr(Text); Text:=nil; if Value<>nil then DisposeStr(Value); Value:=NewStr(S); Modified:=true; end; end; procedure TINIEntry.SetComment(const S: string); begin if (GetComment<>S) then begin if Text<>nil then DisposeStr(Text); Text:=nil; if Comment<>nil then DisposeStr(Comment); Comment:=NewStr(S); Modified:=true; end; end; procedure TINIEntry.Split; var S,ValueS: string; P,P2,StartP: longint; { using byte for P2 lead to infinite loops PM } C: char; InString: boolean; Delimiter: char; begin S:=GetText; Delimiter:=#0; P:=Pos('=',S); P2:=Pos(CommentChar,S); if (P2<>0) and (P2
0 then
begin
Tag:=NewStr(copy(S,1,P-1));
TagHash:=CalcHash(UpcaseStr(Tag^));
P2:=P+1; InString:=false; ValueS:='';
StartP:=P2;
while (P2<=length(S)) do
begin
C:=S[P2];
if (P2=StartP) and (C in ValidStrDelimiters) then
begin
Delimiter:=C;
InString:=true;
end
else if (C=Delimiter) then
begin
{ Delimiter inside escaped Value are simply doubled }
if (P2+1