2005-08-26 12:25:39 +02:00
|
|
|
{
|
|
|
|
:: Implements the IX2ConfigSource for INI files.
|
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtConfigIni;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
2005-10-18 11:25:05 +02:00
|
|
|
Classes,
|
|
|
|
|
|
|
|
X2UtConfig,
|
|
|
|
X2UtConfigBase;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TX2IniConfigSource = class(TX2StreamConfigSource)
|
|
|
|
protected
|
2005-10-18 11:25:05 +02:00
|
|
|
function CreateConfig(const AName: String): TX2BaseConfig; override;
|
|
|
|
procedure LoadFromStream(const AStream: TStream); override;
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
procedure IniSection(Sender: TObject; Section: String);
|
2005-10-18 11:25:05 +02:00
|
|
|
procedure SaveValue(Sender: IX2Config; const Name: String;
|
|
|
|
const Data: Pointer);
|
2005-08-26 12:25:39 +02:00
|
|
|
public
|
2005-10-18 11:25:05 +02:00
|
|
|
procedure Save(const AStream: TStream); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TX2IniConfig = class(TX2BaseConfig)
|
|
|
|
protected
|
|
|
|
procedure IniValue(Sender: TObject; Name, Value: String);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
2005-10-18 11:25:05 +02:00
|
|
|
Variants,
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
X2UtIniParser;
|
|
|
|
|
2005-10-18 11:25:05 +02:00
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2IniConfigSource
|
2005-08-26 12:25:39 +02:00
|
|
|
========================================}
|
2005-10-18 11:25:05 +02:00
|
|
|
function TX2IniConfigSource.CreateConfig(const AName: String): TX2BaseConfig;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2005-10-18 11:25:05 +02:00
|
|
|
Result := TX2IniConfig.Create(AName, Self);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2IniConfigSource.LoadFromStream(const AStream: TStream);
|
|
|
|
begin
|
|
|
|
with TX2IniParser.Create() do
|
|
|
|
try
|
|
|
|
OnSection := IniSection;
|
|
|
|
Execute(AStream);
|
|
|
|
finally
|
|
|
|
Free();
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2IniConfigSource.Save(const AStream: TStream);
|
|
|
|
procedure WriteLine(const ALine: String);
|
|
|
|
var
|
|
|
|
sLine: String;
|
|
|
|
|
|
|
|
begin
|
|
|
|
sLine := ALine + #13#10;
|
|
|
|
AStream.WriteBuffer(PChar(sLine)^, Length(sLine));
|
|
|
|
end;
|
|
|
|
|
|
|
|
begin
|
|
|
|
ConfigItems.First();
|
|
|
|
while ConfigItems.Next() do
|
|
|
|
begin
|
|
|
|
WriteLine('[' + ConfigItems.CurrentValue.Name + ']');
|
|
|
|
ConfigItems.CurrentValue.Iterate(SaveValue, Pointer(AStream));
|
|
|
|
WriteLine('');
|
|
|
|
end;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2IniConfigSource.IniSection(Sender: TObject; Section: String);
|
|
|
|
begin
|
2005-10-18 11:25:05 +02:00
|
|
|
(Sender as TX2IniParser).OnValue := (GetConfig(Section) as TX2IniConfig).IniValue;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2005-10-18 11:25:05 +02:00
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2IniConfig
|
|
|
|
========================================}
|
|
|
|
procedure TX2IniConfig.IniValue(Sender: TObject; Name, Value: String);
|
|
|
|
begin
|
|
|
|
InternalWrite(Name, Value, False);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2IniConfigSource.SaveValue(Sender: IX2Config;
|
|
|
|
const Name: String;
|
|
|
|
const Data: Pointer);
|
|
|
|
var
|
|
|
|
sLine: String;
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2005-10-18 11:25:05 +02:00
|
|
|
sLine := Name + '=' + VarAsType(Sender.Read(Name), varString) + #13#10;
|
|
|
|
TStream(Data).WriteBuffer(PChar(sLine)^, Length(sLine));
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|