Skip to content

Commit

Permalink
Update FileUtils.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
baderouaich committed Jul 13, 2024
1 parent cbc3ee7 commit b7b2507
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Utility/FileUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace fs = std::experimental::filesystem;
#error compiler does not support std::filesystem
#endif

#if defined(ENIGMA_PLATFORM_WINDOWS)
#include <fcntl.h>
#include <io.h>
#endif

NS_ENIGMA_BEGIN
class FileUtils final {
ENIGMA_STATIC_CLASS(FileUtils);
Expand All @@ -25,6 +30,9 @@ class FileUtils final {
static bool Read(const fs::path& filename, std::vector<byte>& buffer) {
if (std::ifstream ifs{filename, std::ios::binary | std::ios::ate}) // ate: open at the end
{
#if defined(ENIGMA_PLATFORM_WINDOWS)
_setmode(_fileno(ifs), _O_BINARY);
#endif
const std::size_t file_size = static_cast<std::size_t>(ifs.tellg());
buffer.resize(file_size, '\000');
ifs.seekg(0, std::ios::beg);
Expand All @@ -40,6 +48,9 @@ class FileUtils final {
static bool ReadString(const fs::path& filename, std::string& buffer) {
if (std::ifstream ifs{filename, std::ios::binary | std::ios::ate}) // ate: open at the end
{
#if defined(ENIGMA_PLATFORM_WINDOWS)
_setmode(_fileno(ifs), _O_BINARY);
#endif
const std::size_t file_size = static_cast<std::size_t>(ifs.tellg());
buffer.resize(file_size, '\000');
ifs.seekg(0, std::ios::beg);
Expand All @@ -54,6 +65,9 @@ class FileUtils final {

static bool Write(const fs::path& filename, const std::vector<byte>& buffer) {
if (std::ofstream ofs{filename, std::ios::binary}) {
#if defined(ENIGMA_PLATFORM_WINDOWS)
_setmode(_fileno(ofs), _O_BINARY);
#endif
ofs.write(reinterpret_cast<const char *>(buffer.data()), buffer.size());
ofs.close();
return true;
Expand All @@ -65,6 +79,9 @@ class FileUtils final {

static bool WriteString(const fs::path& filename, const std::string& buffer) {
if (std::ofstream ofs{filename, std::ios::binary}) {
#if defined(ENIGMA_PLATFORM_WINDOWS)
_setmode(_fileno(ofs), _O_BINARY);
#endif
ofs.write(reinterpret_cast<const char *>(buffer.data()), buffer.size());
ofs.close();
return true;
Expand All @@ -77,8 +94,12 @@ class FileUtils final {
/*
* Reads a file chunk by chunk
*/

static void ReadChunks(const fs::path& filename, const std::size_t max_chunk_size, const std::function<bool(std::vector<byte>&&)>& callback) {
if (std::ifstream ifs{filename, std::ios::binary}) {
#if defined(ENIGMA_PLATFORM_WINDOWS)
_setmode(_fileno(ifs), _O_BINARY);
#endif
while (!ifs.eof()) {
std::vector<Enigma::byte> chunk(max_chunk_size, '\000');
ifs.read(reinterpret_cast<char *>(chunk.data()), chunk.size());
Expand Down

0 comments on commit b7b2507

Please sign in to comment.