home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 5
/
ctrom5b.zip
/
ctrom5b
/
PROGRAM
/
DELPHI
/
EASY_INI
/
EASYINI.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-04-27
|
8KB
|
270 lines
unit EasyINI;
interface
uses
SysUtils, WinProcs, Classes, Forms;
type
TFileLocation = (ApplicationPath, WindowsPath, PathInFileName);
TIniEditor = class(TComponent)
private
FFileName: string;
FFilePath: string;
FFileLocation: TFileLocation;
procedure SetFileName(strFileName: string);
procedure SetFileLocation(Location: TFileLocation);
procedure AppendFilePath(var FullPath: string);
public
constructor Create(AOwner: TComponent); override;
published
property FileName: string read FFileName write SetFileName;
property FileLocation: TFileLocation read FFileLocation write SetFileLocation;
procedure WriteBool(Section, Entry: string; Value: boolean);
procedure WriteChar(Section, Entry: string; Value: char);
procedure WriteInt(Section, Entry: string; Value: LongInt);
procedure WriteReal(Section, Entry: string; Value: Extended);
procedure WriteStr(Section, Entry, Value: string);
function ReadBool(Section, Entry: string; DefaultValue: boolean): boolean;
function ReadChar(Section, Entry: string; DefaultValue: char): char;
function ReadInt(Section, Entry: string; DefaultValue: LongInt): LongInt;
function ReadReal(Section, Entry: string; DefaultValue: Extended): Extended;
function ReadStr(Section, Entry, DefaultValue: string): string;
procedure EraseSection(Section: string);
procedure EraseEntry(Section, Entry: string);
end;
procedure Register;
implementation
constructor TIniEditor.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFileName := '';
FFileLocation := ApplicationPath;
end;
procedure TIniEditor.SetFileName(strFileName: string);
begin
if strFileName <> FFileName then
FFileName := strFileName;
end;
procedure TIniEditor.SetFileLocation(Location: TFileLocation);
var
i: integer;
szPath: PChar;
strPath: string;
begin
if Location <> FFileLocation then
FFileLocation := Location;
if FFileLocation = ApplicationPath then
begin
{Set path to the application. Data files will be in DATA directory below application}
strPath := Application.ExeName;
i := Length(strPath);
while (i >= 1) and (strPath[i] <> '\') do
dec(i);
FFilePath := Copy(strPath,1,i);
end
else
if FFileLocation = WindowsPath then
begin
szPath := StrAlloc(144);
GetWindowsDirectory(szPath, 144);
FFilePath := StrPas(szPath);
if FFilePath[Length(FFilePath)] <> '\' then
FFilePath := FFilePath + '\';
StrDispose(szPath);
end
else
FFilePath := '';
end;
function StrToBool(strVal: string): boolean;
begin
if (UpperCase(strVal) = 'TRUE') or (UpperCase(strVal) = 'YES') or (strVal = '1') then
Result := True
else
Result := False;
end;
function BoolToStr(bVal: boolean): string;
begin
if bVal then
Result := 'True'
else
Result := 'False';
end;
procedure TIniEditor.WriteBool(Section, Entry: string; Value: boolean);
begin
WriteStr(Section, Entry, BoolToStr(Value));
end;
function TIniEditor.ReadBool(Section, Entry: string; DefaultValue: boolean): boolean;
var
iniStrVal: string;
begin
iniStrVal := ReadStr(Section, Entry, BoolToStr(DefaultValue));
result := StrToBool(iniStrVal);
end;
procedure TIniEditor.WriteChar(Section, Entry: string; Value: char);
begin
WriteStr(Section, Entry, Value);
end;
function TIniEditor.ReadChar(Section, Entry: string; DefaultValue: char): char;
var
iniVal: string;
begin
iniVal := DefaultValue;
iniVal := ReadStr(Section, Entry, DefaultValue);
result := iniVal[1];
end;
procedure TIniEditor.WriteReal(Section, Entry: string; Value: Extended);
begin
WriteStr(Section, Entry, FormatFloat('',Value));
end;
procedure TIniEditor.WriteInt(Section, Entry: string; Value: LongInt);
begin
WriteStr(Section, Entry, IntToStr(Value));
end;
function TIniEditor.ReadReal(Section, Entry: string; DefaultValue: Extended): Extended;
var
StrValue: string;
strDefault: string;
begin
try
strDefault := FloatToStr(DefaultValue);
StrValue := ReadStr(Section, Entry, StrDefault);
result := StrToFloat(strValue);
except
result := DefaultValue;
end;
end;
function TIniEditor.ReadInt(Section, Entry: string; DefaultValue: LongInt): LongInt;
begin
try
result := Trunc(ReadReal(Section, Entry, DefaultValue));
except
result := DefaultValue;
end;
end;
procedure TIniEditor.EraseSection(Section: string);
begin
WriteStr(Section, '', '');
end;
procedure TIniEditor.EraseEntry(Section, Entry: string);
begin
WriteStr(Section, Entry, '');
end;
procedure TIniEditor.AppendFilePath(var FullPath: string);
begin
if (FFilePath = '') or (FFileName[2] = ':') then
FullPath := FFileName
else
if FFileName[1] = '\' then
FullPath := FFilePath + Copy(FFileName, 2, Length(FFileName)-1)
else
FullPath := FFilePath + FFileName;
if Pos('.',FullPath) = 0 then
FullPath := FullPath + '.INI';
end;
procedure TIniEditor.WriteStr(Section, Entry, Value: string);
var
szEntry, szFile, szSection, szValue: PChar;
Path: string;
begin
if (Section <> '') and (FFileName <> '') then
begin
{ Allocate some memory for PChars }
szSection := StrAlloc(256);
szEntry := StrAlloc(256);
szValue := StrAlloc(256);
szFile := StrAlloc(256);
try
{ Add path and filename }
AppendFilePath(Path);
{ Copy our properties into the PChars }
StrPCopy(szFile, Path);
StrPCopy(szSection, Section);
StrPCopy(szEntry, Entry);
if Value = '' then
begin
if Entry = '' then
{Erase Entry by passing NIL for its value.}
WritePrivateProfileString(szSection, NIL, NIL, szFile)
else
{Erase Entry by passing NIL for its value.}
WritePrivateProfileString(szSection, szEntry, NIL, szFile);
end
else
begin
StrPCopy(szValue, Value);
WritePrivateProfileString(szSection, szEntry, szValue, szFile);
end;
finally
StrDispose(szEntry);
StrDispose(szFile);
StrDispose(szSection);
StrDispose(szValue);
end;
end;
end;
function TIniEditor.ReadStr(Section, Entry, DefaultValue: string): string;
var
Path: string;
szSection, szEntry, szDefaultValue, szValue, szFile: PChar;
begin
if (Section <> '') and (Entry <> '') and (FFileName <> '') then
begin
{ Allocate some memory for PChars }
szSection := StrAlloc(256);
szEntry := StrAlloc(256);
szDefaultValue := StrAlloc(256);
szValue := StrAlloc(256);
szFile := StrAlloc(256);
try
{ Add path and filename }
AppendFilePath(Path);
{ Copy our properties into the PChars }
StrPCopy(szFile, Path);
StrPCopy(szSection, Section);
StrPCopy(szEntry, Entry);
StrPCopy(szDefaultValue, DefaultValue);
{ Read the profile string }
GetPrivateProfileString(szSection, szEntry, szDefaultValue, szValue, 256, szFile);
Result := StrPas(szValue);
finally
StrDispose(szSection);
StrDispose(szEntry);
StrDispose(szValue);
StrDispose(szDefaultValue);
StrDispose(szFile);
end;
end
else
begin
Result := DefaultValue;
end;
end;
procedure Register;
begin
RegisterComponents('Data Access', [TIniEditor]);
end;
end.