-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbaseline.console.pas
133 lines (115 loc) · 2.84 KB
/
baseline.console.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
unit Baseline.Console;
{$IFDEF FPC}
{$mode ObjFPC}{$H+}
{$ENDIF}
interface
uses
Classes
, SysUtils
;
const
{$IFDEF FPC}
cShortOptHelp: Char = 'h';
cLongOptHelp = 'help';
cShortOptVersion: Char = 'v';
cLongOptVersion = 'version';
cShortOptInput: Char = 'i';
cLongOptInput = 'input-file';
{$ELSE}
cOptionHelp: array of string = ['-h', '--help'];
cOptionVersion: array of string = ['-v', '--version'];
cOptionInput: array of string = ['-i', '--input-file'];
{$ENDIF}
{$I version.inc}
resourcestring
rsAppTitle = 'One Billion Row Challenge Baseline';
rsGeneratorVersion = 'baseline v%s';
rsErrorMessage = 'ERROR: %s';
rsMissingInputFlag = 'Missing input file flag.';
rsNoInputFile = 'File "%s" not found.';
var
inputFilename: String = '';
procedure WriteHelp;
{$IFNDEF FPC}
function ParseCmdLineParams(out aInputFile: string): Boolean;
{$ENDIF}
implementation
{$IFNDEF FPC}
uses
System.IOUtils
;
{$ENDIF}
procedure WriteHelp;
begin
WriteLn('Generates the output for the challenge');
WriteLn;
WriteLn('USAGE');
WriteLn(' baseline <flags>');
WriteLn;
WriteLn('FLAGS');
WriteLn(' -h|--help Writes this help message and exits');
WriteLn(' -v|--version Writes the version and exits');
WriteLn(' -i|--input-file <filename> The file containing the Weather Stations');
end;
{$IFNDEF FPC}
function ArrayContains(const aArray: array of string; const aValue: string): Boolean;
var
iValue: string;
begin
Result := False;
for iValue in aArray do
begin
if aValue.ToLower = iValue then
begin
Result := True;
break;
end;
end;
end;
function ParseCmdLineParams(out aInputFile: string): Boolean;
var
I: Integer;
begin
Result := False;
aInputFile := '';
// 0 is the exe path, so we start at 1
{$IFNDEF LINUX}
for I := 1 to ParamCount do
{$ELSE}
for I := 1 to ParamCount + 1 do
{$ENDIF}
begin
if ArrayContains(cOptionHelp, ParamStr(I)) then
begin
WriteHelp;
Exit;
end
else
if ArrayContains(cOptionVersion, ParamStr(I)) then
begin
WriteLn(Format(rsGeneratorVersion, [ cVersion ]));
exit;
end
else
if ArrayContains(cOptionInput, ParamStr(I)) then
begin
// must be followed by the user's specified input file
if (I+1) <= ParamCount then
aInputFile := ExpandFileName (ParamStr (I+1));
if (aInputFile = '') or (not TFile.Exists (aInputFile)) then
begin
WriteLn(Format(rsErrorMessage, [ Format(rsNoInputFile, [aInputFile]) ]));
Exit;
end
else
begin
Result := True;
Exit;
end;
end
else
WriteLn(Format(rsErrorMessage, [ rsMissingInputFlag ]));
end;
end;
{$ENDIF}
end.