1414
1515const
1616 cVersion = { $I version.inc} ;
17- cSeed: LongInt = 46668267 ; // '1BRC' in ASCII
18-
19- cShortOptHelp = ' h' ;
20- cLongOptHelp = ' help' ;
21- cShortOptVersion = ' v' ;
22- cLongOptVersion = ' version' ;
23- cShortOptInput = ' i:' ;
24- cLongOptInput = ' input-file:' ;
25- cShortOptOutput = ' o:' ;
26- cLongOptOutput = ' output-file:' ;
27- cShortOptNumner = ' n:' ;
28- cLongOptNumber = ' line-count:' ;
17+
18+ cShortOptHelp: Char = ' h' ;
19+ cLongOptHelp = ' help' ;
20+ cShortOptVersion: Char = ' v' ;
21+ cLongOptVersion = ' version' ;
22+ cShortOptInput: Char = ' i' ;
23+ cLongOptInput = ' input-file' ;
24+ cShortOptOutput: Char = ' o' ;
25+ cLongOptOutput = ' output-file' ;
26+ cShortOptNumner: Char = ' n' ;
27+ cLongOptNumber = ' line-count' ;
28+
29+ var
30+ inputFilename: String = ' ' ;
31+ outputFilename: String = ' ' ;
32+ lineCount: Int64 = 0 ;
2933
3034type
3135
3236 { TOneBRCGenerator }
3337
3438 TOneBRCGenerator = class (TCustomApplication)
3539 private
40+ FGenerator: TGenerator;
3641 protected
3742 procedure DoRun ; override;
3843 public
@@ -47,9 +52,10 @@ TOneBRCGenerator = class(TCustomApplication)
4752procedure TOneBRCGenerator.DoRun ;
4853var
4954 ErrorMsg: String;
55+ tmpLineCount: String;
5056begin
5157 // quick check parameters
52- ErrorMsg:= CheckOptions(Format(' %s%s%s%s%s ' ,[
58+ ErrorMsg:= CheckOptions(Format(' %s%s%s:%s:%s: ' ,[
5359 cShortOptHelp,
5460 cShortOptVersion,
5561 cShortOptInput,
@@ -59,32 +65,105 @@ procedure TOneBRCGenerator.DoRun;
5965 [
6066 cLongOptHelp,
6167 cLongOptVersion,
62- cLongOptInput,
63- cLongOptOutput,
64- cLongOptNumber
68+ cLongOptInput+ ' : ' ,
69+ cLongOptOutput+ ' : ' ,
70+ cLongOptNumber+ ' : '
6571 ]
6672 );
67- if ErrorMsg<>' ' then begin
73+ if ErrorMsg<>' ' then
74+ begin
6875 // ShowException(Exception.Create(ErrorMsg));
69- WriteLn(ErrorMsg);
76+ WriteLn(' ERROR: ' , ErrorMsg);
7077 Terminate;
7178 Exit;
7279 end ;
7380
7481 // parse parameters
75- if HasOption(cShortOptHelp, cLongOptHelp) then begin
82+ if HasOption(cShortOptHelp, cLongOptHelp) then
83+ begin
7684 WriteHelp;
7785 Terminate;
7886 Exit;
7987 end ;
8088
81- if HasOption(cShortOptVersion, cLongOptVersion) then begin
89+ if HasOption(cShortOptVersion, cLongOptVersion) then
90+ begin
8291 WriteLn(' generator v' , cVersion);
8392 Terminate;
8493 Exit;
8594 end ;
8695
87- { add your program here }
96+ if HasOption(cShortOptInput, cLongOptInput) then
97+ begin
98+ inputFilename:= GetOptionValue(
99+ cShortOptInput,
100+ cLongOptInput
101+ );
102+ end
103+ else
104+ begin
105+ WriteLn(' ERROR: Missing input file flag' );
106+ Terminate;
107+ Exit;
108+ end ;
109+
110+ if HasOption(cShortOptOutput, cLongOptOutput) then
111+ begin
112+ outputFilename:= GetOptionValue(
113+ cShortOptOutput,
114+ cLongOptOutput
115+ );
116+ end
117+ else
118+ begin
119+ WriteLn(' ERROR: Missing output file flag' );
120+ Terminate;
121+ Exit;
122+ end ;
123+
124+ if HasOption(cShortOptNumner, cLongOptNumber) then
125+ begin
126+ tmpLineCount:=GetOptionValue(
127+ cShortOptNumner,
128+ cLongOptNumber
129+ );
130+ tmpLineCount:= StringReplace(tmpLineCount, ' _' , ' ' , [rfReplaceAll]);
131+ if not TryStrToInt64(tmpLineCount, lineCount) then
132+ begin
133+ WriteLn(' ERROR: Invalid integer "' ,tmpLineCount,' "' );
134+ Terminate;
135+ Exit;
136+ end ;
137+ end
138+ else
139+ begin
140+ WriteLn(' ERROR: Missing line count flag' );
141+ Terminate;
142+ Exit;
143+ end ;
144+
145+
146+ inputFilename:= ExpandFileName(inputFilename);
147+ outputFilename:= ExpandFileName(outputFilename);
148+
149+ WriteLn(' Input File : ' , inputFilename);
150+ WriteLn(' Output File: ' , outputFilename);
151+ WriteLn(' Line Count : ' , FormatFLoat(' #' +DefaultFormatSettings.ThousandSeparator+' ##0' , lineCount));
152+ WriteLn;
153+
154+ FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount);
155+ try
156+ try
157+ FGenerator.Generate;
158+ except
159+ on E: Exception do
160+ begin
161+ WriteLn(' ERROR: ' , E.Message);
162+ end ;
163+ end ;
164+ finally
165+ FGenerator.Free;
166+ end ;
88167
89168 // stop program loop
90169 Terminate;
0 commit comments