home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 May
/
Pcwk0597.iso
/
borland
/
cb
/
setup
/
cbuilder
/
data.z
/
MIFILES.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-02-28
|
7KB
|
274 lines
//---------------------------------------------------------------------------
// Borland C++Builder
// Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
//---------------------------------------------------------------------------
// Mifiles.pas
//
// VCL Class Browser
//---------------------------------------------------------------------------
unit MIFiles;
interface
uses SysUtils, Classes;
type
TMemIniFile = class(TObject)
private
FFileName: string;
FSections: TStringList;
function AddSection(const Section: string): TStrings;
public
constructor Create(const FileName: string);
destructor Destroy; override;
procedure Clear;
procedure DeleteKey(const Section, Ident: String);
procedure EraseSection(const Section: string);
procedure GetStrings(List: TStrings);
function ReadBool(const Section, Ident: string;
Default: Boolean): Boolean;
function ReadInteger(const Section, Ident: string;
Default: Longint): Longint;
procedure ReadSection(const Section: string; Strings: TStrings);
procedure ReadSections(Strings: TStrings);
procedure ReadSectionValues(const Section: string; Strings: TStrings);
function ReadString(const Section, Ident, Default: string): string;
procedure SetStrings(List: TStrings);
procedure UpdateFile;
procedure WriteBool(const Section, Ident: string; Value: Boolean);
procedure WriteInteger(const Section, Ident: string; Value: Longint);
procedure WriteString(const Section, Ident, Value: String);
property FileName: string read FFileName;
end;
implementation
constructor TMemIniFile.Create(const FileName: string);
var
List: TStringList;
begin
FFileName := FileName;
FSections := TStringList.Create;
if (FileName <> '') and FileExists(FileName) then
begin
List := TStringList.Create;
try
List.LoadFromFile(FileName);
SetStrings(List);
finally
List.Free;
end;
end;
end;
destructor TMemIniFile.Destroy;
begin
if FSections <> nil then Clear;
FSections.Free;
end;
function TMemIniFile.AddSection(const Section: string): TStrings;
begin
Result := TStringList.Create;
try
FSections.AddObject(Section, Result);
except
Result.Free;
end;
end;
procedure TMemIniFile.Clear;
var
I: Integer;
begin
for I := 0 to FSections.Count - 1 do
TStrings(FSections.Objects[I]).Free;
end;
procedure TMemIniFile.DeleteKey(const Section, Ident: String);
var
I, J: Integer;
Strings: TStrings;
begin
I := FSections.IndexOf(Section);
if I >= 0 then
begin
Strings := TStrings(FSections.Objects[I]);
J := Strings.IndexOfName(Ident);
if J >= 0 then Strings.Delete(J);
end;
end;
procedure TMemIniFile.EraseSection(const Section: string);
var
I: Integer;
begin
I := FSections.IndexOf(Section);
if I >= 0 then
begin
TStrings(FSections.Objects[I]).Free;
FSections.Delete(I);
end;
end;
procedure TMemIniFile.GetStrings(List: TStrings);
var
I, J: Integer;
Strings: TStrings;
begin
List.BeginUpdate;
try
for I := 0 to FSections.Count - 1 do
begin
List.Add('[' + FSections[I] + ']');
Strings := TStrings(FSections.Objects[I]);
for J := 0 to Strings.Count - 1 do List.Add(Strings[J]);
List.Add('');
end;
finally
List.EndUpdate;
end;
end;
function TMemIniFile.ReadBool(const Section, Ident: string;
Default: Boolean): Boolean;
begin
Result := ReadInteger(Section, Ident, Ord(Default)) <> 0;
end;
function TMemIniFile.ReadInteger(const Section, Ident: string;
Default: Longint): Longint;
var
IntStr: string;
begin
IntStr := ReadString(Section, Ident, '');
if (Length(IntStr) > 2) and (IntStr[1] = '0') and
((IntStr[2] = 'X') or (IntStr[2] = 'x')) then
IntStr := '$' + Copy(IntStr, 3, Maxint);
Result := StrToIntDef(IntStr, Default);
end;
procedure TMemIniFile.ReadSection(const Section: string;
Strings: TStrings);
var
I, J: Integer;
SectionStrings: TStrings;
begin
Strings.BeginUpdate;
try
Strings.Clear;
I := FSections.IndexOf(Section);
if I >= 0 then
begin
SectionStrings := TStrings(FSections.Objects[I]);
for J := 0 to SectionStrings.Count - 1 do
Strings.Add(SectionStrings.Names[J]);
end;
finally
Strings.EndUpdate;
end;
end;
procedure TMemIniFile.ReadSections(Strings: TStrings);
begin
Strings.Assign(FSections);
end;
procedure TMemIniFile.ReadSectionValues(const Section: string;
Strings: TStrings);
var
I: Integer;
begin
Strings.BeginUpdate;
try
Strings.Clear;
I := FSections.IndexOf(Section);
if I >= 0 then Strings.Assign(TStrings(FSections.Objects[I]));
finally
Strings.EndUpdate;
end;
end;
function TMemIniFile.ReadString(const Section, Ident,
Default: string): string;
var
I: Integer;
Strings: TStrings;
begin
I := FSections.IndexOf(Section);
if I >= 0 then
begin
Strings := TStrings(FSections.Objects[I]);
I := Strings.IndexOfName(Ident);
if I >= 0 then
begin
Result := Copy(Strings[I], Length(Ident) + 2, Maxint);
Exit;
end;
end;
Result := Default;
end;
procedure TMemIniFile.SetStrings(List: TStrings);
var
I: Integer;
S: string;
Strings: TStrings;
begin
Clear;
Strings := nil;
for I := 0 to List.Count - 1 do
begin
S := List[I];
if (S <> '') and (S[1] <> ';') then
if (S[1] = '[') and (S[Length(S)] = ']') then
Strings := AddSection(Copy(S, 2, Length(S) - 2))
else
if Strings <> nil then Strings.Add(S);
end;
end;
procedure TMemIniFile.UpdateFile;
var
List: TStringList;
begin
List := TStringList.Create;
try
GetStrings(List);
List.SaveToFile(FFileName);
finally
List.Free;
end;
end;
procedure TMemIniFile.WriteBool(const Section, Ident: string;
Value: Boolean);
const
Values: array[Boolean] of string = ('0', '1');
begin
WriteString(Section, Ident, Values[Value]);
end;
procedure TMemIniFile.WriteInteger(const Section, Ident: string;
Value: Longint);
begin
WriteString(Section, Ident, IntToStr(Value));
end;
procedure TMemIniFile.WriteString(const Section, Ident, Value: String);
var
I: Integer;
S: string;
Strings: TStrings;
begin
I := FSections.IndexOf(Section);
if I >= 0 then
Strings := TStrings(FSections.Objects[I]) else
Strings := AddSection(Section);
S := Ident + '=' + Value;
I := Strings.IndexOfName(Ident);
if I >= 0 then Strings[I] := S else Strings.Add(S);
end;
end.