Files
FEX-Emu--FEX/External/FEXCore/Source/Utils/FileLoading.cpp
T
Ryan Houdek 35d126e12d Utils: Temporarily duplicate LoadFile
The std::vector implementation is going to be short-lived while code
churn happens.

Just duplicate it while we smash through the world.
2023-03-15 12:04:46 -07:00

86 lines
1.6 KiB
C++

#include <FEXCore/fextl/vector.h>
#include <string>
#include <vector>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <span>
#include <unistd.h>
namespace FEXCore::FileLoading {
// TODO: Delete once all uses of std::vector LoadFile is removed.
bool LoadFile(std::vector<char> &Data, const std::string &Filepath, size_t FixedSize) {
int FD = open(Filepath.c_str(), O_RDONLY);
if (FD == -1) {
return false;
}
size_t FileSize{};
if (FixedSize == 0) {
struct stat buf;
if (fstat(FD, &buf) != 0) {
close(FD);
return false;
}
FileSize = buf.st_size;
}
else {
FileSize = FixedSize;
}
ssize_t Read = -1;
if (FileSize > 0) {
Data.resize(FileSize);
Read = pread(FD, &Data.at(0), FileSize, 0);
}
close(FD);
return Read == FileSize;
}
bool LoadFile(fextl::vector<char> &Data, const std::string &Filepath, size_t FixedSize) {
int FD = open(Filepath.c_str(), O_RDONLY);
if (FD == -1) {
return false;
}
size_t FileSize{};
if (FixedSize == 0) {
struct stat buf;
if (fstat(FD, &buf) != 0) {
close(FD);
return false;
}
FileSize = buf.st_size;
}
else {
FileSize = FixedSize;
}
ssize_t Read = -1;
if (FileSize > 0) {
Data.resize(FileSize);
Read = pread(FD, &Data.at(0), FileSize, 0);
}
close(FD);
return Read == FileSize;
}
ssize_t LoadFileToBuffer(const std::string &Filepath, std::span<char> Buffer) {
int FD = open(Filepath.c_str(), O_RDONLY);
if (FD == -1) {
return -1;
}
ssize_t Read = pread(FD, Buffer.data(), Buffer.size(), 0);
close(FD);
return Read;
}
}