Skip to content

Commit 334922f

Browse files
authored
Merge pull request #73 from georges-hatem/delphi-baseline
baseline implementation in Delphi
2 parents cdcf5e6 + 3ae181d commit 334922f

6 files changed

Lines changed: 1383 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
unit Baseline.Console;
2+
3+
interface
4+
5+
uses
6+
System.Classes, SysUtils;
7+
8+
const
9+
cOptionHelp: array of string = ['-h', '--help'];
10+
cOptionVersion: array of string = ['-v', '--version'];
11+
cOptionInput: array of string = ['-i', '--input-file'];
12+
13+
14+
resourcestring
15+
rsAppTitle = 'One Billion Row Challenge Baseline';
16+
rsGeneratorVersion = 'baseline v%s';
17+
rsErrorMessage = 'ERROR: %s';
18+
rsMissingInputFlag = 'Missing input file flag.';
19+
rsNoInputFile = 'Input file does not exist.';
20+
21+
function ParseCmdLineParams (out aInputFile: string): Boolean;
22+
23+
24+
implementation
25+
26+
uses
27+
System.IOUtils;
28+
29+
{$I version.inc}
30+
31+
procedure WriteHelp;
32+
begin
33+
WriteLn('Generates the output for the challenge');
34+
WriteLn;
35+
WriteLn('USAGE');
36+
WriteLn(' baseline <flags>');
37+
WriteLn;
38+
WriteLn('FLAGS');
39+
WriteLn(' -h|--help Writes this help message and exits');
40+
WriteLn(' -v|--version Writes the version and exits');
41+
WriteLn(' -i|--input-file <filename> The file containing the Weather Stations');
42+
end;
43+
44+
function ArrayContains (const aArray: array of string; const aValue: string): Boolean;
45+
var
46+
iValue: string;
47+
begin
48+
Result := False;
49+
for iValue in aArray do begin
50+
if aValue.ToLower = iValue then begin
51+
Result := True;
52+
break;
53+
end;
54+
end;
55+
end;
56+
57+
function ParseCmdLineParams (out aInputFile: string): Boolean;
58+
var
59+
I: Integer;
60+
begin
61+
Result := False;
62+
aInputFile := '';
63+
64+
// 0 is the exe path, so we start at 1
65+
for I := 1 to ParamCount do begin
66+
if ArrayContains (cOptionHelp, ParamStr(I)) then begin
67+
WriteHelp;
68+
exit;
69+
end
70+
else if ArrayContains (cOptionVersion, ParamStr(I)) then begin
71+
WriteLn(Format(rsGeneratorVersion, [ cVersion ]));
72+
exit;
73+
end
74+
else if ArrayContains (cOptionInput, ParamStr(I)) then begin
75+
// must be followed by the user's specified input file
76+
if (I+1) <= ParamCount then
77+
aInputFile := ExpandFileName (ParamStr (I+1));
78+
if not TFile.Exists (aInputFile) then
79+
WriteLn(Format(rsErrorMessage, [ rsNoInputFile ]))
80+
else begin
81+
Result := True;
82+
exit;
83+
end;
84+
end
85+
else
86+
WriteLn(Format(rsErrorMessage, [ rsMissingInputFlag ]));
87+
end;
88+
end;
89+
90+
end.
91+
92+

baseline/Delphi/src/Baseline.dpr

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
program Baseline;
2+
3+
{$APPTYPE CONSOLE}
4+
5+
{$R *.res}
6+
7+
uses
8+
System.SysUtils,
9+
Baseline.Console in 'Baseline.Console.pas',
10+
OneBRC in 'OneBRC.pas';
11+
12+
var
13+
vInputFilePath: string;
14+
vBaseline: TBaseline;
15+
begin
16+
try
17+
if ParseCmdLineParams (vInputFilePath) then begin
18+
vBaseline := TBaseline.Create (vInputFilePath);
19+
try
20+
vBaseline.Generate;
21+
finally
22+
vBaseline.Free;
23+
end;
24+
end;
25+
26+
except
27+
on E: Exception do
28+
Writeln(E.ClassName, ': ', E.Message);
29+
end;
30+
end.

0 commit comments

Comments
 (0)