source: trunk/UContext.pas

Last change on this file was 1, checked in by chronos, 4 years ago
  • Added: Initial version.
File size: 2.6 KB
Line 
1unit UContext;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, UDevice;
9
10type
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
27implementation
28
29uses
30 UTerminal;
31
32function GetFiles(DirName: string): TStrings;
33var
34 Path: string;
35 SR: TSearchRec;
36begin
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;
45end;
46
47{ TContextRoot }
48
49function TContextRoot.CommandImport(Sender: TObject; Command: TCommand): string;
50var
51 Lines: TStringList;
52 FileName: string;
53 I: Integer;
54 Line: string;
55 Index: Integer;
56 CombinedLine: string;
57 InitialContext: TContext;
58begin
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;
84end;
85
86function TContextRoot.GetCommands: TCommands;
87begin
88 Result := inherited;
89 with Result.AddNew('import', '') do
90 Execute := CommandImport;
91end;
92
93{ TContextFile }
94
95function TContextFile.CommandPrint(Sender: TObject; Command: TCommand): string;
96var
97 I: Integer;
98 Files: TStrings;
99begin
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;
108end;
109
110function TContextFile.GetCommands: TCommands;
111begin
112 Result := inherited;
113 with Result.AddNew('print', 'Print values of item properties') do
114 Execute := CommandPrint;
115end;
116
117
118end.
119
Note: See TracBrowser for help on using the repository browser.