Move file length check to separate function
This commit is contained in:
@@ -9,19 +9,16 @@ namespace Inferno {
|
||||
std::string File::read(const std::string &path)
|
||||
{
|
||||
// Create input stream object and open file
|
||||
std::ifstream file(path);
|
||||
ASSERT(file.is_open(), "File could not open: '{}'", path);
|
||||
std::ifstream ifstream(path);
|
||||
ASSERT(ifstream.is_open(), "File could not open '{}'", path);
|
||||
|
||||
// Check if file exists
|
||||
if (!file.is_open()) {
|
||||
if (!ifstream.is_open()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Get length of the file
|
||||
file.seekg(0, std::ios::end);
|
||||
int length = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
ASSERT(length != -1, "File could not determine length: '{}'", path);
|
||||
int32_t length = File::length(path, ifstream);
|
||||
|
||||
// Check for valid file length
|
||||
if (length == -1) {
|
||||
@@ -32,11 +29,21 @@ namespace Inferno {
|
||||
auto buffer = std::make_unique<char[]>(length);
|
||||
|
||||
// Fill buffer with file contents
|
||||
file.read(buffer.get(), length);
|
||||
file.close();
|
||||
ifstream.read(buffer.get(), length);
|
||||
ifstream.close();
|
||||
|
||||
// Create string from the buffer and return
|
||||
return std::string(buffer.get(), length);
|
||||
}
|
||||
|
||||
int32_t File::length(const std::string& path, std::ifstream& ifstream)
|
||||
{
|
||||
ifstream.seekg(0, std::ios::end);
|
||||
int32_t length = ifstream.tellg();
|
||||
ifstream.seekg(0, std::ios::beg);
|
||||
ASSERT(length != -1, "File could not determine length '{}'", path);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Inferno {
|
||||
class File {
|
||||
public:
|
||||
static std::string read(const std::string& path);
|
||||
static int32_t length(const std::string& path, std::ifstream& ifstream);
|
||||
|
||||
template<typename T>
|
||||
static void ioRead(T& t, const std::string& path)
|
||||
|
||||
Reference in New Issue
Block a user