2021-01-02 04:24:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-12-03 20:57:05 +00:00
|
|
|
#include <cstdio>
|
2021-01-02 04:24:29 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
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
|
|
|
|
{
|
|
|
|
public:
|
2021-07-28 02:16:03 +00:00
|
|
|
static bool Exists(const fs::path& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
|
|
|
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
|
|
|
{
|
|
|
|
std::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;
|
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
|
|
|
{
|
|
|
|
std::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);
|
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;
|
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
|
|
|
{
|
|
|
|
std::ofstream file(filePath, std::ios::binary);
|
|
|
|
file.write((char*)data.data(), data.size());
|
|
|
|
};
|
|
|
|
|
2021-10-17 11:32:09 +00:00
|
|
|
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data)
|
|
|
|
{
|
|
|
|
std::ofstream file(filePath, std::ios::binary);
|
|
|
|
file.write((char*)data.data(), data.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize)
|
|
|
|
{
|
|
|
|
std::ofstream file(filePath, std::ios::binary);
|
|
|
|
file.write((char*)data, dataSize);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
|
|
|
std::ofstream file(filePath, std::ios::out);
|
|
|
|
file.write(text.c_str(), text.size());
|
|
|
|
}
|
2021-01-09 00:38:28 +00:00
|
|
|
};
|