Registriert seit: 10. Jun 2003
Ort: Berlin
9.580 Beiträge
Delphi 11 Alexandria
|
Re: StringGrid mit Daten füllen und farblich Darstellen
1. Feb 2009, 01:09
Ich habe wie angekündigt mich eben noch kurz drangesetzt, das Projekt liegt im Anhang. Ich habe das jetzt einfach nur schnell heruntergeschrieben und nicht besonders optimiert oder elegant geschrieben, funktionieren tut es aber.
Der Quelltext sieht so aus, ich denke der sollte auch ohne Kommentare verständlich sein:
Delphi-Quellcode:
type
TMyDataEntry = record
Index: Integer;
DateString, IdString, Line1, Line2: String;
end;
TMySimpleParser = class
private
Data: String;
DataPointer: Integer;
function OemToAnsi(const OemStr: string): string;
procedure SkipSpaces;
procedure SkipTrash;
function GetTextLength(UpToLineEnd: Boolean): Integer;
public
Entries: array of TMyDataEntry;
procedure Parse(Text: String);
end;
Delphi-Quellcode:
{ TMySimpleParser }
function TMySimpleParser.OemToAnsi(const OemStr: string): string;
begin
SetLength(Result, Length(OemStr));
if Length(Result) > 0 then
OemToCharBuff(PChar(OemStr), PChar(Result), Length(Result));
end;
procedure TMySimpleParser.SkipSpaces;
begin
while (DataPointer < Length(Data)) and (Data[DataPointer] = ' ') do
Inc(DataPointer);
end;
procedure TMySimpleParser.SkipTrash;
begin
while (DataPointer < Length(Data)) and (Data[DataPointer] in [#13, #10, #32]) do
Inc(DataPointer);
end;
function TMySimpleParser.GetTextLength(UpToLineEnd: Boolean): Integer;
function IsTerminatingChar(CharIndex: Integer): Boolean;
begin
if UpToLineEnd then
Result := Data[CharIndex] in [#13, #10]
else
Result := Data[CharIndex] in [#13, #10, #32];
end;
var
CurPointer: Integer;
begin
Result := -1;
CurPointer := DataPointer;
while Result < 0 do
begin
while (CurPointer < Length(Data)) and not IsTerminatingChar(CurPointer) do
Inc(CurPointer);
if (CurPointer + 1 < Length(Data)) and not IsTerminatingChar(CurPointer + 1) then
Inc(CurPointer)
else
Result := CurPointer - DataPointer;
if CurPointer = Length(Data) then
Result := CurPointer - DataPointer + 1;
end;
end;
procedure TMySimpleParser.Parse(Text: String);
var
CurEntry: ^TMyDataEntry;
TextLen: Integer;
begin
Data := Text;
DataPointer := 1;
SetLength(Entries, 0);
SkipTrash;
while DataPointer < Length(Data) do
begin
SetLength(Entries, Length(Entries) + 1);
CurEntry := @Entries[High(Entries)];
TextLen := GetTextLength(False);
CurEntry.Index := StrToInt(Copy(Data, DataPointer, TextLen));
DataPointer := DataPointer + TextLen;
SkipSpaces;
TextLen := GetTextLength(False);
CurEntry.DateString := Copy(Data, DataPointer, TextLen);
DataPointer := DataPointer + TextLen;
SkipSpaces;
if Data[DataPointer] <> #13 then
begin
TextLen := GetTextLength(False);
CurEntry.IdString := Copy(Data, DataPointer, TextLen);
DataPointer := DataPointer + TextLen;
end;
SkipTrash;
TextLen := GetTextLength(True);
CurEntry.Line1 := Copy(Data, DataPointer, TextLen);
DataPointer := DataPointer + TextLen;
SkipTrash;
TextLen := GetTextLength(True);
CurEntry.Line2 := OemToAnsi(Copy(Data, DataPointer, TextLen));
DataPointer := DataPointer + TextLen;
SkipTrash;
end;
end;
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
Parser: TMySimpleParser;
FileStream: TFileStream;
FileContents: TStringStream;
i: Integer;
begin
FileStream := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'test.txt', fmOpenRead);
FileContents := TStringStream.Create('');
FileContents.CopyFrom(FileStream, FileStream.Size);
FileStream.Free;
Parser := TMySimpleParser.Create;
Parser.Parse(FileContents.DataString);
FileContents.Free;
StringGrid1.RowCount := Length(Parser.Entries);
for i := 0 to High(Parser.Entries) do
begin
StringGrid1.Cells[0, i + 1] := IntToStr(Parser.Entries[i].Index);
StringGrid1.Cells[1, i + 1] := Parser.Entries[i].DateString;
StringGrid1.Cells[2, i + 1] := Parser.Entries[i].IdString;
StringGrid1.Cells[3, i + 1] := Parser.Entries[i].Line1;
StringGrid1.Cells[4, i + 1] := Parser.Entries[i].Line2;
end;
Parser.Free;
end;
Sebastian Jänicke Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
|