2021-01-02 04:24:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-10-25 01:36:10 +00:00
|
|
|
#ifdef USE_BOOST_FS
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
|
|
|
#else
|
2021-01-02 04:24:29 +00:00
|
|
|
#include <fstream>
|
2023-10-25 01:36:10 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-20 16:02:12 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-07-28 02:16:03 +00:00
|
|
|
#include "Directory.h"
|
2021-10-17 11:32:09 +00:00
|
|
|
#include "Utils/StringHelper.h"
|
2021-01-02 04:24:29 +00:00
|
|
|
|
|
|
|
class File
|
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
#ifdef USE_BOOST_FS
|
|
|
|
typedef fs::ifstream ifstream;
|
|
|
|
typedef fs::ofstream ofstream;
|
|
|
|
#else
|
|
|
|
typedef std::ifstream ifstream;
|
|
|
|
typedef std::ofstream ofstream;
|
|
|
|
#endif
|
|
|
|
|
2021-01-02 04:24:29 +00:00
|
|
|
public:
|
2021-07-28 02:16:03 +00:00
|
|
|
static bool Exists(const fs::path& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
2021-01-02 04:24:29 +00:00
|
|
|
return file.good();
|
|
|
|
}
|
2021-01-09 00:38:28 +00:00
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
2021-04-30 21:23:22 +00:00
|
|
|
int32_t fileSize = (int32_t)file.tellg();
|
2021-01-02 04:24:29 +00:00
|
|
|
file.seekg(0);
|
|
|
|
char* data = new char[fileSize];
|
|
|
|
file.read(data, fileSize);
|
2021-01-09 00:38:28 +00:00
|
|
|
std::vector<uint8_t> result = std::vector<uint8_t>(data, data + fileSize);
|
2021-03-20 16:02:12 +00:00
|
|
|
delete[] data;
|
2023-10-25 01:36:10 +00:00
|
|
|
file.close();
|
2021-01-09 00:38:28 +00:00
|
|
|
|
|
|
|
return result;
|
2021-01-02 04:24:29 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
static std::string ReadAllText(const fs::path& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
2022-12-08 18:15:18 +00:00
|
|
|
if (!file.is_open())
|
|
|
|
return "";
|
2021-04-30 21:23:22 +00:00
|
|
|
int32_t fileSize = (int32_t)file.tellg();
|
2021-01-02 04:24:29 +00:00
|
|
|
file.seekg(0);
|
2021-03-20 16:02:12 +00:00
|
|
|
char* data = new char[fileSize + 1];
|
2021-01-02 04:24:29 +00:00
|
|
|
memset(data, 0, fileSize + 1);
|
|
|
|
file.read(data, fileSize);
|
2021-01-09 00:38:28 +00:00
|
|
|
std::string str = std::string((const char*)data);
|
2021-03-20 16:02:12 +00:00
|
|
|
delete[] data;
|
2023-10-25 01:36:10 +00:00
|
|
|
file.close();
|
2021-01-09 00:38:28 +00:00
|
|
|
|
|
|
|
return str;
|
2021-01-02 04:24:29 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
static std::vector<std::string> ReadAllLines(const fs::path& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
std::string text = ReadAllText(filePath);
|
|
|
|
std::vector<std::string> lines = StringHelper::Split(text, "\n");
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
};
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ofstream file(filePath, std::ios::binary);
|
2021-01-02 04:24:29 +00:00
|
|
|
file.write((char*)data.data(), data.size());
|
2023-10-25 01:36:10 +00:00
|
|
|
file.close();
|
2021-01-02 04:24:29 +00:00
|
|
|
};
|
|
|
|
|
2021-10-17 11:32:09 +00:00
|
|
|
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data)
|
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ofstream file(filePath, std::ios::binary);
|
2021-10-17 11:32:09 +00:00
|
|
|
file.write((char*)data.data(), data.size());
|
2023-10-25 01:36:10 +00:00
|
|
|
file.close();
|
2021-10-17 11:32:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize)
|
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ofstream file(filePath, std::ios::binary);
|
2021-10-17 11:32:09 +00:00
|
|
|
file.write((char*)data, dataSize);
|
2023-10-25 01:36:10 +00:00
|
|
|
file.close();
|
2021-10-17 11:32:09 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
static void WriteAllText(const fs::path& filePath, const std::string& text)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
2023-10-25 01:36:10 +00:00
|
|
|
ofstream file(filePath, std::ios::out);
|
2021-01-02 04:24:29 +00:00
|
|
|
file.write(text.c_str(), text.size());
|
2023-10-25 01:36:10 +00:00
|
|
|
file.close();
|
2021-01-02 04:24:29 +00:00
|
|
|
}
|
2021-01-09 00:38:28 +00:00
|
|
|
};
|