Added error handling for missing data files

This commit is contained in:
Stefan Müller 2024-02-21 20:59:21 +01:00
parent 3f4a64b6e0
commit 824ec0e29b
3 changed files with 27 additions and 5 deletions

View File

@ -6,7 +6,7 @@ This is a single command line application for all puzzles written in [FreePascal
## Puzzle Input
This project does not contain the puzzle or example inputs as per the [copyright notice of Advent of Code](https://adventofcode.com/about).
This project does not contain the puzzle or example inputs as per the [copyright notice of Advent of Code](https://adventofcode.com/about). In order to run the compiled application, the puzzle inputs have to be downloaded from the [Advent of Code 2023](https://adventofcode.com/2023/) puzzle pages, and placed as text files into the `bin\data` directory. The application will output an error with details, if it cannot find an input file.
## Day 1: Trebuchet?!

View File

@ -69,6 +69,7 @@ type
procedure ProcessData(const ASolver: ISolver);
procedure Run(const ASolver: ISolver);
procedure RunAndFree(const ASolver: ISolver);
function GetDataFileName(const ASolver: ISolver): string;
end;
implementation
@ -105,7 +106,7 @@ var
begin
ASolver.Init;
AssignFile(data, ConcatPaths([FRelativeDataPath, ASolver.DataFileName]));
AssignFile(data, GetDataFileName(ASolver));
try
reset(data);
while (not EOF(data)) do
@ -121,12 +122,22 @@ begin
end;
procedure TSolverEngine.Run(const ASolver: ISolver);
var
fileName: string;
begin
WriteLn;
WriteLn('--- ', ASolver.PuzzleName, ' ---');
fileName := GetDataFileName(ASolver);
if FileExists(fileName) then
begin
ProcessData(ASolver);
WriteLn('Part 1: ', ASolver.ResultPart1);
WriteLn('Part 2: ', ASolver.ResultPart2);
end
else begin
WriteLn('Cannot find puzzle input file ''', ExpandFileName(fileName), '''.');
WriteLn('Please download the file content from https://adventofcode.com/2023/');
end;
end;
procedure TSolverEngine.RunAndFree(const ASolver: ISolver);
@ -135,5 +146,10 @@ begin
ASolver.Free;
end;
function TSolverEngine.GetDataFileName(const ASolver: ISolver): string;
begin
Result := ConcatPaths([FRelativeDataPath, ASolver.DataFileName]);
end;
end.

View File

@ -72,9 +72,15 @@ end;
{ TEngineBaseTest }
procedure TEngineBaseTest.Setup;
var
fileName: string;
begin
inherited Setup;
FEngine := TSolverEngine.Create(GetDataPath);
fileName := FEngine.GetDataFileName(FSolver);
AssertTrue('Cannot find puzzle input file ''' + ExpandFileName(fileName) + '''. '
+ 'Please download the file from https://adventofcode.com/2023/',
FileExists(fileName));
FEngine.ProcessData(FSolver);
end;