light/modules/engine/src/utils/file_manager.cpp

64 lines
1.2 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/utils/file_manager.hpp>
2025-07-06 16:52:50 +03:30
#include <utility>
2025-07-05 13:28:41 +03:30
namespace Light {
2025-07-05 16:07:51 +03:30
BasicFileHandle::BasicFileHandle(
2025-07-05 13:28:41 +03:30
uint8_t *data,
uint32_t size,
2025-07-06 16:52:50 +03:30
std::string path,
std::string name,
std::string extension
2025-07-05 13:28:41 +03:30
)
: m_data(data)
, m_size(size)
2025-07-06 16:52:50 +03:30
, m_path(std::move(path))
, m_name(std::move(name))
, m_extension(std::move(extension))
2025-07-05 13:28:41 +03:30
{
}
2025-07-05 16:07:51 +03:30
void BasicFileHandle::release()
2025-07-05 13:28:41 +03:30
{
delete m_data;
m_data = nullptr;
m_size = 0ull;
2025-07-05 13:28:41 +03:30
}
2025-07-06 14:02:50 +03:30
auto FileManager::read_text_file(const std::string &path) -> BasicFileHandle
2025-07-05 13:28:41 +03:30
{
// parse path info
2025-07-06 14:02:50 +03:30
auto name = path.substr(0, path.find('.') + -1);
auto extension = path.substr(path.find('.') + 1);
2025-07-05 13:28:41 +03:30
// open file
2025-07-06 14:02:50 +03:30
auto file = std::ifstream { path.c_str(), std::ios_base::in | std::ios_base::binary };
2025-07-05 13:28:41 +03:30
// check
if (!file)
{
2025-07-06 16:30:38 +03:30
log_wrn("Failed to load text file: {}", path);
2025-07-05 13:28:41 +03:30
file.close();
2025-07-06 16:30:38 +03:30
return nullptr;
2025-07-05 13:28:41 +03:30
}
// fetch file size
file.seekg(0, std::ios::end);
2025-07-06 14:02:50 +03:30
auto size = file.tellg();
2025-07-05 13:28:41 +03:30
file.seekg(0, std::ios::beg);
if (!size)
2025-07-06 16:30:38 +03:30
{
log_wrn("Empty text file: {}", path);
}
2025-07-05 13:28:41 +03:30
// read file
2025-07-06 14:02:50 +03:30
auto *data = new uint8_t[size];
2025-07-05 13:28:41 +03:30
file.read(reinterpret_cast<char *>(data), size);
file.close();
2025-07-06 14:02:50 +03:30
return { data, static_cast<unsigned int>(size), path, name, extension };
2025-07-05 13:28:41 +03:30
}
} // namespace Light