-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCtl.pas
158 lines (142 loc) · 4.75 KB
/
Ctl.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
unit Ctl;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils, IOUtils, PropSet, Log;
Type
TControlFile = Type TPropertySet;
TCtlFileHelper = record helper for TControlFile
public
Constructor Create(ControlFileName: String);
Function Read(ControlFilename: String): Boolean;
Function InpFileName(const Name: string; out FileName: String): Boolean; overload;
Function InpFileName(const Name: string; Optional: Boolean = false): String; overload;
Function InpProperties(const Name: string; Optional: Boolean = false): TPropertySet;
Function OutpFileName(const Name: string; out FileName: String): Boolean; overload;
Function OutpFileName(const Name: string; Optional: Boolean = false): String; overload;
Function OutpProperties(const Name: string; Optional: Boolean = false): TPropertySet;
end;
Var
CtlFile: TControlFile;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Constructor TCtlFileHelper.Create(ControlFilename: String);
begin
Read(ControlFilename);
end;
Function TCtlFileHelper.Read(ControlFilename: String): Boolean;
begin
ControlFileName := ExpandFileName(ControlFileName);
if FileExists(ControlFileName) then
begin
Result := true;
Self.NameValueSeparator := ':';
Self.PropertiesSeparator := ';';
Self.BaseDirectory := ExtractFileDir(ControlFileName);
Self.AsStrings := TFile.ReadAllLines(ControlFileName);
Self.Lock;
end else
begin
Result := false;
writeln('Control file does not exist');
end;
end;
Function TCtlFileHelper.InpFileName(const Name: string; out FileName: String): Boolean;
begin
FileName := ToFileName(Name);
if FileName <> '' then
if FileExists(FileName) then
begin
Result := true;
if LogFile <> nil then LogFile.InputFile(Name,FileName);
end else
raise Exception.Create('File does not exist (' + Name + ')')
else
Result := false;
end;
Function TCtlFileHelper.InpFileName(const Name: string; Optional: Boolean = false): String;
begin
Result := ToFileName(Name);
if Result <> '' then
if FileExists(Result) then
begin
if LogFile <> nil then LogFile.InputFile(Name,Result);
end else
raise Exception.Create('File does not exist (' + Name + ')')
else
if not Optional then
raise Exception.Create('Missing file name (' + Name + ')')
end;
Function TCtlFileHelper.InpProperties(const Name: string; Optional: Boolean = false): TPropertySet;
Var
Value: String;
begin
if Contains(Name,Value) and (Value <> '') then
begin
Result := TPropertySet.Create('=',';',true);
Result.AsString := Value;
if Result.Contains('file',Value) then
begin
Value := TPropertySet.BaseDirectory.AbsolutePath(Value);
if FileExists(Value) then
begin
if LogFile <> nil then LogFile.InputFile(Name,Value);
end else
raise Exception.Create('File does not exist (' + Name + ')')
end else
raise Exception.Create('Missing file property (' + Name + ')')
end else
begin
if not Optional then
raise Exception.Create('Missing properties (' + Name + ')')
end;
end;
Function TCtlFileHelper.OutpFileName(const Name: string; out FileName: String): Boolean;
begin
FileName := ToFileName(Name);
if FileName <> '' then
begin
Result := true;
if LogFile <> nil then LogFile.OutputFile(Name,FileName);
end else
Result := false;
end;
Function TCtlFileHelper.OutpFileName(const Name: string; Optional: Boolean = false): String;
begin
Result := ToFileName(Name);
if Result <> '' then
begin
if LogFile <> nil then LogFile.OutputFile(Name,Result);
end else
if not Optional then
raise Exception.Create('Missing file name (' + Name + ')')
end;
Function TCtlFileHelper.OutpProperties(const Name: string; Optional: Boolean = false): TPropertySet;
Var
Value: String;
begin
if Contains(Name,Value) and (Value <> '') then
begin
Result := TPropertySet.Create('=',';',true);
Result.AsString := Value;
if Result.Contains('file',Value) then
begin
Value := TPropertySet.BaseDirectory.AbsolutePath(Value);
if LogFile <> nil then LogFile.OutputFile(Name,Value);
end else
raise Exception.Create('Missing file property (' + Name + ')')
end else
begin
if not Optional then
raise Exception.Create('Missing properties (' + Name + ')')
end;
end;
end.