Subversion Repositories userdetect2

Rev

Rev 85 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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