Subversion Repositories userdetect2

Rev

Rev 84 | Rev 87 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
84 daniel-mar 1
unit UD2_Parsing;
2
 
3
interface
4
 
5
uses
6
  Windows, SysUtils;
7
 
8
// TODO: use this for better object oriented programming
9
 
85 daniel-mar 10
// TODO : WideStrings  idName: WideString;   ???
11
 
12
const
13
  // Prefixes for UD2_RunCmd()
14
  UD2_RUN_IN_OWN_DIRECTORY_PREFIX = '$RIOD$';
15
  UD2_RUN_AS_ADMIN                = '$ADMIN$';
16
 
84 daniel-mar 17
type
18
  TUD2Command = record
19
    executable: string;
20
    runAsAdmin: boolean;
21
    runInOwnDirectory: boolean;
22
    windowMode: integer;
23
  end;
24
  TUD2CommandArray = array of TUD2Command;
25
 
26
  TUD2TDFCondition = record
27
    idMethodName: string;
28
    idStr: string;
29
    dynamicDataUsed: Boolean;
30
    dynamicData: string;
31
    caseSensitive: boolean;
32
  end;
33
  TUD2TDFConditionArray = array of TUD2TDFCondition;
34
 
35
  TUD2TDFEntry = record
36
    ids: TUD2TDFConditionArray;
37
    commands: TUD2CommandArray;
38
  end;
39
  TUD2TDFEntryArray = array of TUD2TDFEntry;
40
 
85 daniel-mar 41
// Split
42
function UD2P_ParseConditions(idTerm: string): TUD2TDFConditionArray;
43
function UD2P_ParseTdfLine(idTermAndCmd: string; var entry: TUD2TDFEntry): boolean;
44
function UD2P_DecodeCommand(line: string): TUD2Command;
45
function UD2P_DecodeCondition(idName: string; var cond: TUD2TDFCondition): boolean;
46
 
47
// Merge
48
function UD2_CondToStr(cond: TUD2TDFCondition): string;
49
function UD2_CondsToStr(conds: TUD2TDFConditionArray): string;
50
 
84 daniel-mar 51
implementation
52
 
53
uses
54
  UD2_Utils;
55
 
85 daniel-mar 56
function UD2_CondsToStr(conds: TUD2TDFConditionArray): string;
57
var
58
  i: integer;
84 daniel-mar 59
begin
85 daniel-mar 60
  result := '';
61
  for i := Low(conds) to High(conds) do
62
  begin
63
    if result <> '' then result := result + '&&';
64
    result := result + UD2_CondToStr(conds[i]);
65
  end;
66
end;
84 daniel-mar 67
 
85 daniel-mar 68
function UD2_CondToStr(cond: TUD2TDFCondition): string;
69
begin
70
  if cond.dynamicDataUsed then
71
  begin
72
    result := cond.idMethodName+'('+cond.dynamicData+'):'+cond.idStr;
73
  end
74
  else
75
  begin
76
    result := cond.idMethodName+':'+cond.idStr;
77
  end;
78
end;
84 daniel-mar 79
 
85 daniel-mar 80
function UD2P_DecodeCommand(line: string): TUD2Command;
81
begin
82
  result.runAsAdmin := Pos(UD2_RUN_AS_ADMIN, line) >= 1;
83
  if result.runAsAdmin then
84
  begin
85
    line := StringReplace(line, UD2_RUN_AS_ADMIN, '', [rfReplaceAll]);
86
  end;
84 daniel-mar 87
 
85 daniel-mar 88
  result.runInOwnDirectory := Pos(UD2_RUN_IN_OWN_DIRECTORY_PREFIX, line) >= 1;
89
  if result.runInOwnDirectory then
90
  begin
91
    line := StringReplace(line, UD2_RUN_IN_OWN_DIRECTORY_PREFIX, '', [rfReplaceAll]);
92
  end;
84 daniel-mar 93
 
85 daniel-mar 94
  result.executable := line;
95
  result.WindowMode := SW_NORMAL;  // TODO (future): make it configurable
84 daniel-mar 96
end;
97
 
85 daniel-mar 98
function UD2P_DecodeCondition(idName: string; var cond: TUD2TDFCondition): boolean;
99
const
100
  CASE_SENSITIVE_FLAG = '$CASESENSITIVE$';
101
var
102
  a, b: TArrayOfString;
84 daniel-mar 103
begin
85 daniel-mar 104
  result := false;
105
 
106
  cond.caseSensitive := Pos(CASE_SENSITIVE_FLAG, idName) >= 1;
107
  if cond.caseSensitive then
84 daniel-mar 108
  begin
85 daniel-mar 109
    idName := StringReplace(idName, CASE_SENSITIVE_FLAG, '', [rfReplaceAll]);
110
  end;
84 daniel-mar 111
 
85 daniel-mar 112
  /// --- Start Dynamic Extension
113
  // xxxxxx ( xxxxx ):  xxxxxxxxxxxx
114
  // xxxxx  ( xx:xx ):  xxxxx:xxx(x)
115
  // xxxxxxxxxxxx    :  xxxxx(xxx)xx
116
 
117
  SetLength(a, 0);
118
  a := SplitString('(', idName);
119
  if (Length(a) >= 2) and (Pos(':', a[0]) = 0) then
84 daniel-mar 120
  begin
85 daniel-mar 121
    SetLength(b, 0);
122
    b := SplitString('):', a[1]);
123
    if Length(b) >= 2 then
124
    begin
125
      cond.idMethodName    := a[0];
126
      cond.idStr           := b[1];
127
      cond.dynamicDataUsed := true;
128
      cond.dynamicData     := b[0];
129
      result := true;
130
      Exit;
131
    end;
132
  end;
133
  /// --- End Dynamic Extension
84 daniel-mar 134
 
85 daniel-mar 135
  if not result then
136
  begin
137
    a := SplitString(':', idName);
138
    if Length(a) >= 2 then
139
    begin
140
      cond.idMethodName    := a[0];
141
      cond.idStr           := a[1];
142
      cond.dynamicDataUsed := false;
143
      cond.dynamicData     := '';
144
      result := true;
145
      Exit;
146
    end;
147
  end;
148
end;
84 daniel-mar 149
 
85 daniel-mar 150
function UD2P_ParseConditions(idTerm: string): TUD2TDFConditionArray;
151
var
152
  cond: TUD2TDFCondition;
153
  x: TArrayOfString;
154
  i, l: integer;
155
  idName: string;
156
begin
157
  SetLength(x, 0);
158
  x := SplitString('&&', idTerm);
159
  SetLength(result, 0);
160
  for i := Low(x) to High(x) do
161
  begin
162
    idName := x[i];
163
    if UD2P_DecodeCondition(idName, cond) then
164
    begin
165
      l := Length(result);
166
      SetLength(result, l+1);
167
      result[l] := cond;
168
    end;
169
  end;
170
end;
84 daniel-mar 171
 
85 daniel-mar 172
function UD2P_ParseTdfLine(idTermAndCmd: string; var entry: TUD2TDFEntry): boolean;
173
var
174
  nameVal: TArrayOfString;
175
  idTerm, cmd: string;
176
begin
177
  result := false;
178
 
179
  // Split conditions and command
180
  nameVal := SplitString('=', idTermAndCmd); // TODO: problem... "=" could be inside dynamicData...
181
  if Length(nameVal) < 2 then exit;
182
  idTerm := nameVal[0];
183
  cmd    := nameVal[1];
184
 
185
  // Decode conditions
186
  entry.ids := UD2P_ParseConditions(idTerm);
187
 
188
  // Decode command
189
  SetLength(entry.commands, 1);
190
  entry.commands[0] := UD2P_DecodeCommand(cmd);
191
 
84 daniel-mar 192
  result := true;
193
end;
194
 
195
end.
196