Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.477 Beiträge
Delphi 12 Athens
|
Re: Stringmanipulation
21. Mai 2010, 11:21
Delphi-Quellcode:
type
TCustomConfig = class(TObject)
protected
procedure ReadValues(AValueList: TStrings); virtual; abstract;
public
procedure LoadFromStream(AStream: TStream);
end;
TConfig = class(TCustomConfig)
private
FVar1: string;
FVar2: string;
FVar3: string;
protected
procedure ReadValues(AValueList: TStrings); override;
public
property Var1: string read FVar1 write FVar1;
property Var2: string read FVar2 write FVar2;
property Var3: string read FVar3 write FVar3;
end;
implementation
procedure TCustomConfig.LoadFromStream(AStream: TStream);
var
cfg, row, col: TStringList;
i: Integer;
begin
col := nil;
row := nil;
cfg := TStringList.Create;
try
cfg.LineBreak := ';';
cfg.LoadFromStream(AStream);
{1.Zeile}
if cfg.Count >= 1 then
begin
row := TStringList.Create;
row.QuoteChar := '"';
row.Delimiter := ':';
row.StrictDelimiter := True;
row.DelimitedText := cfg[0];
{die ersten beiden Spalten ignorieren}
if row.Count >= 2 then
begin
col := TStringList.Create;
col.LineBreak := '
';
col.NameValueSeparator := ':';
col.Text := row[2];
{Leerzeichen entfernen}
for i := 0 to col.Count - 1 do
begin
col[i] := Trim(col.Names[i]) + col.NameValueSeparator +
Trim(col.ValueFromIndex[i]);
end;
{Werte übernehmen}
ReadValues(col);
end;
end;
finally
col.Free;
row.Free;
cfg.Free;
end;
end;
procedure TConfig.ReadValues(AValueList: TStrings);
begin
FVar1 := AValueList.Values['Var1'];
FVar2 := AValueList.Values['Var2'];
FVar3 := AValueList.Values['Var3'];
end;
Delphi-Quellcode:
const
TextConfig = ' s:85:"Var1 : 3235019924
Var2 : Test1234
Var3 : Test123456
";';
CRLF = Char(13) + Char(10);
procedure TForm1.TestClick(Sender: TObject);
var
Config: TConfig;
Stream: TMemoryStream;
begin
Stream := TMemoryStream.Create;
Stream.WriteBuffer(TextConfig, Length(TextConfig));
Stream.Seek(0, soFromBeginning);
Config := TConfig.Create;
Config.LoadFromStream(Stream);
MessageBox( Handle,
PChar(Config.Var1 + CRLF +
Config.Var2 + CRLF +
Config.Var3),
' Config',
mb_Ok);
Config.Free;
Stream.Free;
end;
|
|
Zitat
|