Browse Source

sys: change lol::file from a namespace to an actual type

main
Sam Hocevar 1 week ago
parent
commit
7c28dcc3e5
1 changed files with 28 additions and 21 deletions
  1. +28
    -21
      include/lol/private/sys/file.h

+ 28
- 21
include/lol/private/sys/file.h View File

@@ -20,29 +20,36 @@

#include <fstream> // std::ofstream

namespace lol::file
namespace lol
{

template<typename T, typename U = typename T::value_type>
static inline bool read(std::string const &path, T &data)
struct file
{
std::ifstream f(path, std::ios::in | std::ios::binary | std::ios::ate);
auto file_size = f.tellg(); // works because std::ios::ate
if (file_size < 0)
return false;
f.seekg(0, std::ios::beg);
data.resize((size_t(file_size) + sizeof(U) - 1) / sizeof(U));
f.read((char *)data.data(), file_size);
return !f.fail();
}

template<typename T, typename U = typename T::value_type>
static inline bool write(std::string const &path, T const &data)
{
std::ofstream f(path, std::ios::binary);
f.write((char const *)data.data(), data.size() * sizeof(U));
f.close();
return !f.fail();
}
template<typename T, typename U = typename T::value_type>
static inline bool read(std::string const &path, T &data)
{
std::ifstream f(path, std::ios::in | std::ios::binary | std::ios::ate);
auto file_size = f.tellg(); // works because std::ios::ate
if (file_size < 0)
return false;
f.seekg(0, std::ios::beg);
data.resize((size_t(file_size) + sizeof(U) - 1) / sizeof(U));
f.read((char *)data.data(), file_size);
return !f.fail();
}

template<typename T, typename U = typename T::value_type>
static inline bool write(std::string const &path, T const &data)
{
std::ofstream f(path, std::ios::binary);
f.write((char const *)data.data(), data.size() * sizeof(U));
f.close();
return !f.fail();
}

private:
file() = delete;
};


} // namespace lol::file

Loading…
Cancel
Save