Here is a quick parser if you wish to parse multiple lines from a text file with that structure.
Code:
unit quickparse;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;
const DumpList = 'stopped Alerter Alerter'#13 +
'stopped Apache Apache'#13 +
'stopped Apache2 Apache2';
type
PServiceDumpData = ^TServiceDumpData;
TServiceDumpData = record
Status : String[255];
Process1 : String[255];
Process2 : String[255];
end;
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
DumpStrings : TStringList;
DumpString : String;
Delimiter : Char;
LastPos : Integer;
i : Integer;
c : Integer;
ColCount : Integer;
ServiceDumpData : PServiceDumpData;
begin
Delimiter := ' ';
DumpStrings := TStringList.Create;
DumpStrings.Clear;
DumpStrings.Text := DumpList;
ListBox1.Items.BeginUpdate;
ListBox1.Items.Clear;
for i := 0 to DumpStrings.Count - 1 do
begin
ColCount := 0; // reset the column count
LastPos := 0; // default last position
New(ServiceDumpData); // create a new record to hold the data
DumpString := DumpStrings[i]; // assign the dumpstring
for c := 1 to Length(DumpString) do // iterate through the string till we find a delimiter
begin
if (DumpString[c] = Delimiter) or (c= Length(DumpString)) then // is this a delimiter or Eos?
begin
Inc(ColCount); // let's now parse for columns
case ColCount of
1 : ServiceDumpData.Status := Copy(DumpString, LastPos + 1, c - LastPos); // since it's the first delimiter it's column 1
2 : ServiceDumpData.Process1 := Copy(DumpString, LastPos + 1, c - LastPos); // and so on and so on
3 : ServiceDumpData.Process2 := Copy(DumpString, LastPos + 1, c - LastPos); // ......
end; // we can remove the old column from the string;
LastPos := c; // set the last pos as a reference
end;
end;
ListBox1.Items.AddObject(ServiceDumpData.Status + #9 +
ServiceDumpData.Process1 + #9 +
ServiceDumpData.Process2, TObject(ServiceDumpData));
end;
ListBox1.Items.EndUpdate;
DumpStrings.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.TabWidth := 64; // gives the listbox a multi-column effect when used with tabs (^I or #9 ASCII character)
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if TListBox(Sender).ItemIndex = -1 then
Exit;
ShowMessage(PServiceDumpData(TListBox(Sender).Items.Objects[TListBox(Sender).ItemIndex])^.Status);
end;
end.
The use of records is very efficient. You can use classes, but the point is that since you said you dumped it to a text file, you can use the LoadFromFile method of a TString descentend to get the data from the file, given that the dump follows that 3 column format and get it parsed. Because you will be using records or classes you can attach it to the ListBox items and recall it in the future when needed; as shown in the ListBox.OnClick event handler.