Orange/src/Orange/Core/Filesystem.cpp

24 lines
607 B
C++

#include <Orange/Core/FileSystem.h>
#include <stdio.h>
namespace orange::fs
{
Result<Buffer> OpenFileIntoBuffer(const char* path, bool text)
{
FILE* file = fopen(path, text ? "r" : "rb");
if (!file)
Result<Buffer>::Error(BasicErrorCode::NotFound);
fseek(file, 0, SEEK_END);
size_t size = size_t(ftell(file));
fseek(file, 0, SEEK_SET);
uint8_t* data = new uint8_t[size];
fread(data, size, 1, file);
fclose(file);
if (text) data[size++] = '\0';
return Result<Buffer>::Success(data, size);
}
}