-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.VersionNumberParser.pas
More file actions
308 lines (257 loc) · 6.84 KB
/
Copy pathMaxLogic.VersionNumberParser.pas
File metadata and controls
308 lines (257 loc) · 6.84 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
unit MaxLogic.VersionNumberParser;
interface
uses
sysUtils, classes;
Type
TVersion = record
private
function GetBuild: Integer;
function GetHasBuild: Boolean;
function GetHasMajor: Boolean;
function GetHasMinor: Boolean;
function GetHasRelease: Boolean;
function GetMajor: Integer;
function GetMinor: Integer;
function GetRelease: Integer;
function GetText: String;
procedure SetBuild(const Value: Integer);
procedure SetMajor(const Value: Integer);
procedure SetMinor(const Value: Integer);
procedure SetRelease(const Value: Integer);
procedure SetText(const Value: String);
function GetFullText: String;
public
Data: array [0 .. 3] of Integer;
// returns true if HasMajor and HasMinor, Release and Build are optional
Function Valid: Boolean; overload;
// checks any amount of version parts. 0=Major, 1=Major+Minor, 2=Major+Minor+Release and 3=all
Function Valid(aCount: Integer): Boolean; overload;
// sets all values to -1
Procedure Clear;
Function ParsePostgresVersionString(const aVersionString: String): Boolean;
Function IsGreaterOrEqualThen(const v: TVersion): Boolean;
Function IsLessThen(const v: TVersion): Boolean;
property Text: String read GetText write SetText;
{ property: FullText
as text, but it always outputs all 4 parts of the version string. in other words all -1 will be treated as 0
that helps if we want to store the version in a db or as a part of the file name.
so we will evade the situation where we sometimes have the version like "1.0" and sometimes as "1.0.0" or "1.0.0.0"
sometimes have the version like "1.0" and sometimes as "1.0.0" or "1.0.0.0" }
property FullText: String read GetFullText;
// instead of accessing the arraym you can access the date like this
property Major: Integer read GetMajor write SetMajor;
property Minor: Integer read GetMinor write SetMinor;
property Release: Integer read GetRelease write SetRelease;
property Build: Integer read GetBuild write SetBuild;
// -1 means no valid entry exists
property HasMajor: Boolean read GetHasMajor;
property HasMinor: Boolean read GetHasMinor;
property HasRelease: Boolean read GetHasRelease;
property HasBuild: Boolean read GetHasBuild;
end;
/// <summary>
/// This class helps to use version ranges.
/// it is simply a minVersion-maxVersion notation, so separated by a "-"
/// like: 1.1.9-5.1
/// </summary>
TVersionRange = record
private
function GetText: String;
procedure SetText(const Value: String);
public
MinVersion: TVersion;
MaxVersion: TVersion;
Function InRange(const aVersion: TVersion): Boolean;
Property Text: String read GetText write SetText;
end;
implementation
uses
strUtils;
{ TVersion }
procedure TVersion.Clear;
begin
end;
function TVersion.GetBuild: Integer;
begin
Result := Data[3]
end;
function TVersion.GetFullText: String;
var
x: Integer;
sData: array of String;
begin
setLength(sData, length(Data));
for x := 0 to length(Data) - 1 do
begin
if Data[x] < 0 then
sData[x] := '0'
else
sData[x] := IntToStr(Data[x]);
end;
Result := String.Join('.', sData);
end;
function TVersion.GetHasBuild: Boolean;
begin
Result := GetBuild <> -1;
end;
function TVersion.GetHasMajor: Boolean;
begin
Result := Major <> -1;
end;
function TVersion.GetHasMinor: Boolean;
begin
Result := Minor <> -1;
end;
function TVersion.GetHasRelease: Boolean;
begin
Result := Release <> -1;
end;
function TVersion.GetMajor: Integer;
begin
Result := Data[0];;
end;
function TVersion.GetMinor: Integer;
begin
Result := Data[1];
end;
function TVersion.GetRelease: Integer;
begin
Result := Data[2];
end;
function TVersion.GetText: String;
var
x: Integer;
begin
Result := '';
for x := 3 downto 0 do
begin
if (Data[x] <> -1) or (Result <> '') then
begin
if Result <> '' then
Result := '.' + Result;
if Data[x] = -1 then
Result := '0' + Result
else
Result := Data[x].ToString + Result;
end;
end;
end;
function TVersion.IsGreaterOrEqualThen(const v: TVersion): Boolean;
var
x: Integer;
begin
for x := 0 to 3 do
// -1 means null/nil/invalid, so we can not really test with anything
if (Data[x] <> -1) and (v.Data[x] <> -1) then
begin
if Data[x] > v.Data[x] then
Exit(True)
else if Data[x] < v.Data[x] then
Exit(False);
end;
Result := True; // all are equal
end;
function TVersion.IsLessThen(const v: TVersion): Boolean;
var
x: Integer;
begin
for x := 0 to 3 do
// -1 means null/nil/invalid, so we can not really test with anything
if (Data[x] <> -1) and (v.Data[x] <> -1) then
begin
if Data[x] < v.Data[x] then
Exit(True)
else if Data[x] > v.Data[x] then
Exit(False);
end;
Result := False; // all are equal
end;
function TVersion.ParsePostgresVersionString(
const aVersionString: String): Boolean;
var
ar: TArray<String>;
begin
Clear;
Result := False;
// looks like this:
// "PostgreSQL 9.3.5, compiled by Visual C++ build 1600, 32-bit"
ar := aVersionString.Split([' ', ','], TStringSplitOptions.ExcludeEmpty);
if length(ar) >= 2 then
begin
self.Text := ar[1];
Result := self.Valid;
end;
end;
procedure TVersion.SetBuild(const Value: Integer);
begin
Data[3] := Value;
end;
procedure TVersion.SetMajor(const Value: Integer);
begin
Data[0] := Value;
end;
procedure TVersion.SetMinor(const Value: Integer);
begin
Data[1] := Value;
end;
procedure TVersion.SetRelease(const Value: Integer);
begin
Data[2] := Value;
end;
procedure TVersion.SetText(const Value: String);
var
ar: TArray<String>;
x: Integer;
begin
ar := Value.Split(['.', ' '], TStringSplitOptions.ExcludeEmpty);
setLength(ar, 4);
for x := 0 to 3 do
Data[x] := strToIntDef(ar[x], -1);
end;
function TVersion.Valid(aCount: Integer): Boolean;
var
x: Integer;
begin
Result := True;
if aCount < 0 then
aCount := 0
else if aCount > 3 then
aCount := 3;
for x := 0 to aCount do
if Data[x] = -1 then
Exit(False);
end;
function TVersion.Valid: Boolean;
begin
Result := HasMajor and HasMinor;
end;
{ TVersionRange }
function TVersionRange.GetText: String;
begin
if MinVersion.Valid then
Result := MinVersion.Text
else
Result := '*';
if MaxVersion.Valid then
Result := Result + '-' + MaxVersion.Text;
end;
function TVersionRange.InRange(const aVersion: TVersion): Boolean;
begin
Result := True;
if MinVersion.Valid then
if aVersion.IsLessThen(MinVersion) then
Exit(False);
if MaxVersion.Valid then
if MaxVersion.IsLessThen(aVersion) then
Exit(False);
end;
procedure TVersionRange.SetText(const Value: String);
var
ar: TArray<String>;
begin
ar := Value.Split(['-'], 2);
setLength(ar, 2);
MinVersion.Text := ar[0];
MaxVersion.Text := ar[1];
end;
end.