54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
#include <fart/Engine.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
Engine::Engine(const RenderParams & params)
|
|
: params(params)
|
|
{
|
|
}
|
|
|
|
int Engine::run(string_view input_file, opt<string_view> output_file)
|
|
{
|
|
string output_path;
|
|
if (output_file)
|
|
{
|
|
output_path = *output_file;
|
|
}
|
|
else
|
|
{
|
|
string_view base = input_file;
|
|
if (base.ends_with(".fart"))
|
|
{
|
|
base.remove_suffix(5u);
|
|
}
|
|
output_path = string(base) + ".png";
|
|
}
|
|
|
|
image_writer.reset(ImageWriter::build(output_path));
|
|
if (!image_writer)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
/* Verify the output file can be written before rendering. */
|
|
FILE * fh = fopen(output_path.c_str(), "wb");
|
|
if (fh == nullptr)
|
|
{
|
|
fprintf(stderr, "Error opening output file '%s': %s\n", output_path.c_str(), strerror(errno));
|
|
return 1;
|
|
}
|
|
if (fclose(fh) != 0)
|
|
{
|
|
fprintf(stderr, "Error closing output file '%s': %s\n", output_path.c_str(), strerror(errno));
|
|
return 1;
|
|
}
|
|
if (remove(output_path.c_str()) != 0)
|
|
{
|
|
fprintf(stderr, "Error removing output file '%s': %s\n", output_path.c_str(), strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|