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