Skip to content

Commit 99b0d3a

Browse files
authored
Merge pull request #120 from paweld/gus
added a parameter to change the end of the line `b` or `line-ending`
2 parents 08ae8d2 + e71a1ac commit 99b0d3a

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

generator/Common/generate.common.pas

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ TGenerator = class(TObject)
2222
FOutPutFile: String;
2323
FLineCount: Int64;
2424
FStationNames: TStringList;
25+
FLineEnding: String;
2526

2627
procedure BuildStationNames;
2728
function GenerateProgressBar(APBPosition, APBMax, APBWIdth: Integer;
2829
AFileSize: Int64; ATimeElapsed: TDateTime): String;
2930
function Rng1brc(Range: Integer): Integer;
3031
public
31-
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean = False);
32+
constructor Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean; ALineEnding: String);
3233
destructor Destroy; override;
3334

3435
procedure Generate;
@@ -57,17 +58,17 @@ implementation
5758
stationsCapacity = 50000;
5859
chunkBatch = 10000;
5960
chunkCapacity = 20 * 1024 * 1024;
60-
lineEnding = #13#10;
6161
lineBreak = #13;
6262

6363
{ TGenerator }
6464

65-
constructor TGenerator.Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean);
65+
constructor TGenerator.Create(AInputFile, AOutputFile: String; ALineCount: Int64; AOnly400Stations: Boolean; ALineEnding: String);
6666
begin
6767
FInputFile := AInputFile;
6868
FOutPutFile := AOutputFile;
6969
FLineCount := ALineCount;
7070
FOnly400Stations := AOnly400Stations;
71+
FLineEnding := ALineEnding;
7172

7273
FStationNames := TStringList.Create;
7374
FStationNames.Capacity := stationsCapacity;
@@ -193,6 +194,10 @@ procedure TGenerator.Generate;
193194
// Build list of station names
194195
BuildStationNames;
195196

197+
//initialize line ending if not corrected
198+
if FLineEnding <> #13#10 then
199+
FLineEnding := #10;
200+
196201
outputFileStream := TFileStream.Create(FOutPutFile, fmCreate);
197202

198203
progressBatch := floor(FLineCount * (linesPercent / 100));
@@ -228,7 +233,7 @@ procedure TGenerator.Generate;
228233
for index := 0 to High(temperatureArray) do
229234
begin
230235
randomTempFinal := FormatFloat('0"."0', index - 999);
231-
temperatureArray[index] := randomTempFinal + lineEnding;
236+
temperatureArray[index] := randomTempFinal + FLineEnding;
232237
LenTemperatureArray[index] := Length(temperatureArray[index]);
233238
end;
234239

generator/Common/generate.console.pas

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ interface
2424
cLongOptNumber = 'line-count';
2525
cShortOptStations: Char = '4';
2626
cLongOptStations = '400stations';
27+
cShortOptLineEnd: Char = 'b';
28+
cLongOptLineEnd = 'line-ending';
2729
{$IFNDEF FPC}
28-
cShortOptions: array of char = ['h', 'v', 'i', 'o', 'n', '4'];
30+
cShortOptions: array of char = ['h', 'v', 'i', 'o', 'n', '4', 'b'];
2931
cLongOptions: array of string = ['help', 'version', 'input-file', 'output-file',
30-
'line-count', '400stations'];
32+
'line-count', '400stations', 'line-ending'];
3133
{$ENDIF}
3234

3335
resourcestring
@@ -42,12 +44,14 @@ interface
4244
rsInputFile = 'Input Filename: "%s"';
4345
rsOutputFile = 'Output Filename: "%s"';
4446
rsLineCount = 'Line Count: %.n';
47+
rsInvalidLineEnd = 'Line ending should be a CRLF or LF';
4548

4649
var
4750
inputFilename: String = '';
4851
outputFilename: String = '';
4952
lineCount: Integer = 0;
5053
only400Stations: Boolean = False;
54+
lineEnding: String = '';
5155

5256
procedure WriteHelp;
5357

@@ -68,6 +72,7 @@ procedure WriteHelp;
6872
WriteLn(' -o|--output-file <filename> The file that will contain the generated lines');
6973
WriteLn(' -n|--line-count <number> The amount of lines to be generated ( Can use 1_000_000_000 )');
7074
WriteLn(' -4|--400stations Only 400 weather stations in output file');
75+
WriteLn(' -b|--line-ending Line ending: LF (default) or CRLF');
7176
end;
7277

7378
end.

generator/Delphi/src/generator.dpr

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ begin
4242
WriteLn(Format(rsLineCount, [Double(lineCount)]));
4343
WriteLn;
4444

45-
FGenerator := TGenerator.Create(inputFilename, outputFilename, lineCount, only400Stations);
45+
FGenerator := TGenerator.Create(inputFilename, outputFilename, lineCount, only400Stations, lineEnding);
4646
try
4747
try
4848
FGenerator.generate;
@@ -100,7 +100,7 @@ end;
100100
function TOneBRCGenerator.ParseConsoleParams: boolean;
101101
var
102102
I, J, invalid, valid: Integer;
103-
tmpLineCount: String;
103+
tmpLineCount, tmpLineEnd: String;
104104
ParamOK: Boolean;
105105
SkipNext: Boolean;
106106
begin
@@ -241,6 +241,27 @@ begin
241241
end;
242242

243243
only400Stations := (FParams.IndexOf(cShortOptStations) >= 0) or (FParams.IndexOf(cLongOptStations) >= 0);
244+
245+
// check line ending
246+
J := FParams.IndexOfName(cShortOptLineEnd);
247+
if J = -1 then
248+
J := FParams.IndexOfName(cLongOptLineEnd);
249+
if J >= 0 then
250+
begin
251+
tmpLineCount := FParams.ValueFromIndex[J];
252+
253+
if (UpperCase(tmpLineEnd) <> 'CRLF') and (UpperCase(tmpLineEnd) <> 'LF') then
254+
begin
255+
WriteLn(rsInvalidLineEnd);
256+
inc(invalid);
257+
end
258+
else
259+
begin
260+
if UpperCase(tmpLineEnd) = 'CRLF' then
261+
lineEnding := #13#10;
262+
inc(valid);
263+
end;
264+
end;
244265

245266
writeln(only400Stations);
246267
writeln(fparams.Text);

generator/Lazarus/src/generator.lpr

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,26 @@ TOneBRCGenerator = class(TCustomApplication)
3434
procedure TOneBRCGenerator.DoRun;
3535
var
3636
ErrorMsg: String;
37-
tmpLineCount: String;
37+
tmpLineCount, tmpLineEnd: String;
3838
begin
3939
// quick check parameters
40-
ErrorMsg:= CheckOptions(Format('%s%s%s:%s:%s:%s',[
40+
ErrorMsg:= CheckOptions(Format('%s%s%s:%s:%s:%s:%s',[
4141
cShortOptHelp,
4242
cShortOptVersion,
4343
cShortOptInput,
4444
cShortOptOutput,
4545
cShortOptNumber,
46-
cShortOptStations
46+
cShortOptStations,
47+
cShortOptLineEnd
4748
]),
4849
[
4950
cLongOptHelp,
5051
cLongOptVersion,
5152
cLongOptInput+':',
5253
cLongOptOutput+':',
5354
cLongOptNumber+':',
54-
cLongOptStations+':'
55+
cLongOptStations+':',
56+
cLongOptLineEnd+':'
5557
]
5658
);
5759
if ErrorMsg<>'' then
@@ -134,6 +136,22 @@ procedure TOneBRCGenerator.DoRun;
134136

135137
only400stations := HasOption(cShortOptStations, cLongOptStations);
136138

139+
if HasOption(cShortOptLineEnd, cLongOptLineEnd) then
140+
begin
141+
tmpLineEnd:=GetOptionValue(
142+
cShortOptLineEnd,
143+
cLongOptLineEnd
144+
);
145+
if (UpperCase(tmpLineEnd) <> 'CRLF') and (UpperCase(tmpLineEnd) <> 'LF') then
146+
begin
147+
WriteLn(rsInvalidLineEnd);
148+
Terminate;
149+
Exit;
150+
end
151+
else if UpperCase(tmpLineEnd) = 'CRLF' then
152+
lineEnding := #13#10;
153+
end;
154+
137155
inputFilename:= ExpandFileName(inputFilename);
138156
outputFilename:= ExpandFileName(outputFilename);
139157

@@ -142,7 +160,7 @@ procedure TOneBRCGenerator.DoRun;
142160
WriteLn(Format(rsLineCount, [ Double(lineCount) ]));
143161
WriteLn;
144162

145-
FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount, only400stations);
163+
FGenerator:= TGenerator.Create(inputFilename, outputFilename, lineCount, only400stations, lineEnding);
146164
try
147165
try
148166
FGenerator.Generate;

0 commit comments

Comments
 (0)