Util: Add length() and raw() functions to File class
This commit is contained in:
+35
-20
@@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#include <cstdint> // int32_t
|
#include <cstdint> // int32_t
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream> // ifstream, ios, ofstream
|
#include <fstream> // std::ifstream, std::ofstream
|
||||||
#include <memory> // make_unique
|
#include <memory> // std::make_shared, std::shared_ptr
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
@@ -19,24 +19,7 @@ namespace ruc {
|
|||||||
File::File(std::string_view path)
|
File::File(std::string_view path)
|
||||||
: m_path(path)
|
: m_path(path)
|
||||||
{
|
{
|
||||||
// Create input stream object and open file
|
m_data = std::string(File::raw(path).get());
|
||||||
std::ifstream file(path.data(), std::ios::in);
|
|
||||||
VERIFY(file.is_open(), "failed to open file: '{}'", path);
|
|
||||||
|
|
||||||
// Get length of the file
|
|
||||||
file.seekg(0, std::ios::end);
|
|
||||||
int32_t size = file.tellg();
|
|
||||||
file.seekg(0, std::ios::beg);
|
|
||||||
VERIFY(size != -1, "failed to read file length: '{}'", path);
|
|
||||||
|
|
||||||
// Allocate memory filled with zeros
|
|
||||||
auto buffer = std::make_unique<char[]>(size);
|
|
||||||
|
|
||||||
// Fill buffer with file contents
|
|
||||||
file.read(buffer.get(), size);
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
m_data = std::string(buffer.get(), size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
File::~File()
|
File::~File()
|
||||||
@@ -54,6 +37,38 @@ File File::create(std::string_view path)
|
|||||||
return File(path);
|
return File(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int32_t File::length(std::string_view path, std::ifstream& file)
|
||||||
|
{
|
||||||
|
file.seekg(0, std::ios::end);
|
||||||
|
int32_t length = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
VERIFY(length != -1, "failed to read file length: '{}'", path);
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<char[]> File::raw(std::string_view path)
|
||||||
|
{
|
||||||
|
// Create input stream object and open file
|
||||||
|
std::ifstream file(path.data());
|
||||||
|
VERIFY(file.is_open(), "failed to open file: '{}'", path);
|
||||||
|
|
||||||
|
// Get length of the file
|
||||||
|
int32_t size = File::length(path, file);
|
||||||
|
|
||||||
|
// Allocate memory filled with zeros
|
||||||
|
auto buffer = std::make_shared<char[]>(size + 1);
|
||||||
|
|
||||||
|
// Fill buffer with file contents
|
||||||
|
file.read(buffer.get(), size);
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
// Null termination
|
||||||
|
buffer[size] = '\0';
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|
||||||
void File::clear()
|
void File::clear()
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint> // int32_t
|
||||||
|
#include <memory> // std::shared_ptr
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
@@ -17,6 +19,8 @@ public:
|
|||||||
virtual ~File();
|
virtual ~File();
|
||||||
|
|
||||||
static File create(std::string_view path);
|
static File create(std::string_view path);
|
||||||
|
static int32_t length(std::string_view path, std::ifstream& file);
|
||||||
|
static std::shared_ptr<char[]> raw(std::string_view path);
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
File& append(std::string_view data);
|
File& append(std::string_view data);
|
||||||
|
|||||||
@@ -331,8 +331,6 @@ struct Formatter<Specifier> : Formatter<std::nullptr_t> {
|
|||||||
|
|
||||||
} // namespace ruc::format
|
} // namespace ruc::format
|
||||||
|
|
||||||
using ruc::format::Formatter;
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
|
|||||||
Reference in New Issue
Block a user