-
Notifications
You must be signed in to change notification settings - Fork 0
/
inimakeparser.pas
144 lines (122 loc) · 3.64 KB
/
inimakeparser.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
unit IniMakeParser;
{$mode ObjFPC}{$H+}
interface
procedure StartParse(const FileName, StartEntry: String);
implementation
uses IniFiles, Process, SysUtils, Classes;
const
Section_Rules = '&Rules';
Section_Include = '&Include';
KeyBeginning_Command = '@';
KeyBeginning_Rule = '$';
KeyBeginning_Include = '!';
function GetRule(Rule: String; I: TIniFile): String;
var counter, number: integer; SectionStrings: TStringList; Tempi: TIniFile;
begin
Result:=Rule;
if I.ValueExists(Section_Rules,Rule) then
begin
Result:=I.ReadString(Section_Rules,Rule,'');
exit;
end else
begin
if I.SectionExists(Section_Include) then
begin
SectionStrings:=TStringList.Create;
try
I.ReadSection(Section_Include,SectionStrings);
number:=1;
for counter:=0 to SectionStrings.Count-1 do
begin
if I.ValueExists(Section_Include,KeyBeginning_Include+Inttostr(number)) then
begin
if FileExists(I.ReadString(Section_Include,KeyBeginning_Include+Inttostr(number),'')) then
begin
TempI:=TIniFile.Create(I.ReadString(Section_Include,KeyBeginning_Include+Inttostr(number),''));
try
Result:=GetRule(Rule,TempI);
finally
TempI.Free;
end;
end;
end;
inc(number);
end;
finally
SectionStrings.Free;
end;
end;
end;
end;
function ParseExternalExecutionString(Input: String; I: TIniFile): String;
var position: integer;
begin
Result:=Input;
Input:=Input+' ';
position:=1;
while position<=length(input) do
begin
if input[position]=KeyBeginning_Rule then
begin
input:=StringReplace(input,input[position..Pos(' ',input,position)-1],GetRule(input[position..Pos(' ',input,position)-1],I),[rfReplaceAll]);
end;
inc(position);
end;
Result:=Input;
end;
procedure ParseEntry(const Entry: String; I: TIniFile);
var counter, number: Integer; SectionStrings: TStringList; P: TProcess;
begin
if I.SectionExists(Entry) then
begin
SectionStrings:=TStringList.Create;
try
I.ReadSection(Entry,SectionStrings);
number:=1;
for counter:=0 to SectionStrings.Count-1 do
begin
// Command Stuff
if I.ValueExists(Entry,KeyBeginning_Command+Inttostr(number)) then
begin
// if command is another block
if I.SectionExists(I.ReadString(Entry,KeyBeginning_Command+Inttostr(number),'')) then
begin
// Recursive Execution
ParseEntry(I.ReadString(Entry,KeyBeginning_Command+Inttostr(number),''),I);
end else
// Command is an Executable String
begin
P:=TProcess.Create(nil);
try
P.CommandLine:=ParseExternalExecutionString(I.ReadString(Entry,KeyBeginning_Command+Inttostr(number),''),I);
P.Options:=P.Options + [poWaitOnExit];
P.Execute;
finally
P.Free;
end;
end;
inc(number);
end else
begin
raise Exception.Create('Error ['+Entry+']: Command '+Inttostr(number)+' not found.');
end;
end;
finally
SectionStrings.Free;
end;
end else
begin
raise Exception.Create('Error ['+Entry+']: Does not exist.');
end;
end;
procedure StartParse(const FileName, StartEntry: String);
var I: TIniFile;
begin
I:=TIniFile.Create(FileName);
try
ParseEntry(StartEntry,I);
finally
I.Free;
end;
end;
end.