Add read_file

This commit is contained in:
josh committed 2026-11-02 20:19:20 -05:00
1 parent e13608b287
commit 1475434c4c
3 files changed
+45 -1

No files matched your search

+7 -1
View File
@@ -1,6 +1,12 @@
#include <fart/Parser.h>
#include <fart/read_file.h>
Scene * load_scene(const string_view & input_file)
Scene * Parser::load_scene(const string_view & input_file)
{
opt<std::vector<uint8_t>> input = read_file(input_file);
if (!input)
{
return nullptr;
}
return nullptr;
}
+32
View File
@@ -0,0 +1,32 @@
#include <fart/read_file.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
opt<std::vector<uint8_t>> read_file(string_view fname)
{
string path(fname);
FILE * fh = fopen(path.c_str(), "rb");
if (fh == nullptr)
{
fprintf(stderr, "Error opening input file '%s': %s\n", path.c_str(), strerror(errno));
return std::nullopt;
}
std::vector<uint8_t> buffer;
uint8_t chunk[4096];
size_t n;
while ((n = fread(chunk, 1u, sizeof(chunk), fh)) > 0u)
{
buffer.insert(buffer.end(), chunk, chunk + n);
}
if (ferror(fh))
{
fprintf(stderr, "Error reading input file '%s': %s\n", path.c_str(), strerror(errno));
fclose(fh);
return std::nullopt;
}
fclose(fh);
return buffer;
}
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
#include <vector>
opt<std::vector<uint8_t>> read_file(string_view fname);