home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 April
/
Chip_1997-04_cd.bin
/
prezent
/
cb
/
data.z
/
INIFILES.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-01-16
|
5KB
|
186 lines
{*******************************************************}
{ }
{ Delphi Visual Component Library }
{ }
{ Copyright (c) 1995,96 Borland International }
{ }
{*******************************************************}
unit IniFiles;
{$R-}
interface
uses Windows, Classes;
type
TIniFile = class(TObject)
private
FFileName: string;
public
constructor Create(const FileName: string);
function ReadString(const Section, Ident, Default: string): string;
procedure WriteString(const Section, Ident, Value: String);
function ReadInteger(const Section, Ident: string;
Default: Longint): Longint;
procedure WriteInteger(const Section, Ident: string; Value: Longint);
function ReadBool(const Section, Ident: string;
Default: Boolean): Boolean;
procedure WriteBool(const Section, Ident: string; Value: Boolean);
procedure ReadSection(const Section: string; Strings: TStrings);
procedure ReadSections(Strings: TStrings);
procedure ReadSectionValues(const Section: string; Strings: TStrings);
procedure EraseSection(const Section: string);
procedure DeleteKey(const Section, Ident: String);
property FileName: string read FFileName;
end;
implementation
uses SysUtils, Consts;
constructor TIniFile.Create(const FileName: string);
begin
FFileName := FileName;
end;
function TIniFile.ReadString(const Section, Ident, Default: string): string;
var
Buffer: array[0..1023] of Char;
begin
SetString(Result, Buffer, GetPrivateProfileString(PChar(Section),
PChar(Ident), PChar(Default), Buffer, SizeOf(Buffer), PChar(FFileName)));
end;
procedure TIniFile.WriteString(const Section, Ident, Value: string);
begin
if not WritePrivateProfileString(PChar(Section), PChar(Ident),
PChar(Value), PChar(FFileName)) then
raise Exception.CreateResFmt(SIniFileWriteError, [FileName]);
end;
function TIniFile.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 TIniFile.WriteInteger(const Section, Ident: string; Value: Longint);
begin
WriteString(Section, Ident, IntToStr(Value));
end;
function TIniFile.ReadBool(const Section, Ident: string;
Default: Boolean): Boolean;
begin
Result := ReadInteger(Section, Ident, Ord(Default)) <> 0;
end;
procedure TIniFile.WriteBool(const Section, Ident: string; Value: Boolean);
const
Values: array[Boolean] of string = ('0', '1');
begin
WriteString(Section, Ident, Values[Value]);
end;
procedure TIniFile.ReadSections(Strings: TStrings);
const
BufSize = 8192;
var
Buffer, P: PChar;
begin
GetMem(Buffer, BufSize);
try
Strings.BeginUpdate;
try
Strings.Clear;
if GetPrivateProfileString(nil, nil, nil, Buffer, BufSize,
PChar(FFileName)) <> 0 then
begin
P := Buffer;
while P^ <> #0 do
begin
Strings.Add(P);
Inc(P, StrLen(P) + 1);
end;
end;
finally
Strings.EndUpdate;
end;
finally
FreeMem(Buffer, BufSize);
end;
end;
procedure TIniFile.ReadSection(const Section: string; Strings: TStrings);
const
BufSize = 8192;
var
Buffer, P: PChar;
begin
GetMem(Buffer, BufSize);
try
Strings.BeginUpdate;
try
Strings.Clear;
if GetPrivateProfileString(PChar(Section), nil, nil, Buffer, BufSize,
PChar(FFileName)) <> 0 then
begin
P := Buffer;
while P^ <> #0 do
begin
Strings.Add(P);
Inc(P, StrLen(P) + 1);
end;
end;
finally
Strings.EndUpdate;
end;
finally
FreeMem(Buffer, BufSize);
end;
end;
procedure TIniFile.ReadSectionValues(const Section: string; Strings: TStrings);
var
KeyList: TStringList;
I: Integer;
begin
KeyList := TStringList.Create;
try
ReadSection(Section, KeyList);
Strings.BeginUpdate;
try
for I := 0 to KeyList.Count - 1 do
Strings.Values[KeyList[I]] := ReadString(Section, KeyList[I], '');
finally
Strings.EndUpdate;
end;
finally
KeyList.Free;
end;
end;
procedure TIniFile.EraseSection(const Section: string);
begin
if not WritePrivateProfileString(PChar(Section), nil, nil,
PChar(FFileName)) then
raise Exception.CreateResFmt(SIniFileWriteError, [FileName]);
end;
procedure TIniFile.DeleteKey(const Section, Ident: String);
begin
WritePrivateProfileString(PChar(Section), PChar(Ident), nil,
PChar(FFileName));
end;
end.