1 | unit UContext;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UDevice;
|
---|
9 |
|
---|
10 | type
|
---|
11 | { TContextFile }
|
---|
12 |
|
---|
13 | TContextFile = class(TContext)
|
---|
14 | RootDir: string;
|
---|
15 | function CommandPrint(Sender: TObject; Command: TCommand): string;
|
---|
16 | function GetCommands: TCommands; override;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | { TContextRoot }
|
---|
20 |
|
---|
21 | TContextRoot = class(TContext)
|
---|
22 | function CommandImport(Sender: TObject; Command: TCommand): string;
|
---|
23 | function GetCommands: TCommands; override;
|
---|
24 | end;
|
---|
25 |
|
---|
26 |
|
---|
27 | implementation
|
---|
28 |
|
---|
29 | uses
|
---|
30 | UTerminal;
|
---|
31 |
|
---|
32 | function GetFiles(DirName: string): TStrings;
|
---|
33 | var
|
---|
34 | Path: string;
|
---|
35 | SR: TSearchRec;
|
---|
36 | begin
|
---|
37 | Path := ExtractFileDir(DirName);
|
---|
38 | Result := TStringList.Create;
|
---|
39 | if FindFirst(Path + '*.*', faArchive, SR) = 0 then begin
|
---|
40 | repeat
|
---|
41 | Result.Add(SR.Name);
|
---|
42 | until FindNext(SR) <> 0;
|
---|
43 | FindClose(SR);
|
---|
44 | end;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | { TContextRoot }
|
---|
48 |
|
---|
49 | function TContextRoot.CommandImport(Sender: TObject; Command: TCommand): string;
|
---|
50 | var
|
---|
51 | Lines: TStringList;
|
---|
52 | FileName: string;
|
---|
53 | I: Integer;
|
---|
54 | Line: string;
|
---|
55 | Index: Integer;
|
---|
56 | CombinedLine: string;
|
---|
57 | InitialContext: TContext;
|
---|
58 | begin
|
---|
59 | Result := '';
|
---|
60 | InitialContext := TTerminal(Sender).Context;
|
---|
61 | FileName := TTerminal(Sender).CommandLine;
|
---|
62 | if FileExists(FileName) then begin
|
---|
63 | Lines := TStringList.Create;
|
---|
64 | Lines.LoadFromFile(FileName);
|
---|
65 | CombinedLine := '';
|
---|
66 | for I := 0 to Lines.Count - 1 do begin
|
---|
67 | Line := Lines[I];
|
---|
68 | Index := Pos('#', Line);
|
---|
69 | if Index > 0 then
|
---|
70 | Line := Trim(Copy(Line, 1, Index - 1));
|
---|
71 | if Line <> '' then begin
|
---|
72 | if Line[Length(Line)] = '\' then begin
|
---|
73 | CombinedLine := CombinedLine + Trim(Copy(Line, 1, Length(Line) - 1));
|
---|
74 | Continue;
|
---|
75 | end else CombinedLine := CombinedLine + Trim(Line);
|
---|
76 | TTerminal(Sender).Write(LineEnding + CombinedLine);
|
---|
77 | TTerminal(Sender).ExecuteCommand(CombinedLine);
|
---|
78 | CombinedLine := '';
|
---|
79 | end;
|
---|
80 | end;
|
---|
81 | Lines.Free;
|
---|
82 | end else TTerminal(Sender).Write('File not found');
|
---|
83 | TTerminal(Sender).Context := InitialContext;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | function TContextRoot.GetCommands: TCommands;
|
---|
87 | begin
|
---|
88 | Result := inherited;
|
---|
89 | with Result.AddNew('import', '') do
|
---|
90 | Execute := CommandImport;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | { TContextFile }
|
---|
94 |
|
---|
95 | function TContextFile.CommandPrint(Sender: TObject; Command: TCommand): string;
|
---|
96 | var
|
---|
97 | I: Integer;
|
---|
98 | Files: TStrings;
|
---|
99 | begin
|
---|
100 | Files := GetFiles(RootDir);
|
---|
101 | Result := '';
|
---|
102 | for I := 0 to Files.Count - 1 do begin
|
---|
103 | Result := Result + Files[I];
|
---|
104 | if I < Files.Count - 1 then
|
---|
105 | Result := Result + LineEnding;
|
---|
106 | end;
|
---|
107 | Files.Free;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | function TContextFile.GetCommands: TCommands;
|
---|
111 | begin
|
---|
112 | Result := inherited;
|
---|
113 | with Result.AddNew('print', 'Print values of item properties') do
|
---|
114 | Execute := CommandPrint;
|
---|
115 | end;
|
---|
116 |
|
---|
117 |
|
---|
118 | end.
|
---|
119 |
|
---|