2021-01-02 04:24:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "StringHelper.h"
|
|
|
|
|
|
|
|
class File
|
|
|
|
{
|
|
|
|
public:
|
2021-01-09 00:38:28 +00:00
|
|
|
static bool Exists(const std::string& 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
|
|
|
|
|
|
|
static std::vector<uint8_t> ReadAllBytes(const std::string& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
|
|
|
int fileSize = (int)file.tellg();
|
|
|
|
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);
|
|
|
|
delete data;
|
|
|
|
|
|
|
|
return result;
|
2021-01-02 04:24:29 +00:00
|
|
|
};
|
|
|
|
|
2021-01-09 00:38:28 +00:00
|
|
|
static std::string ReadAllText(const std::string& filePath)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
|
|
|
int fileSize = (int)file.tellg();
|
|
|
|
file.seekg(0);
|
|
|
|
char* data = new char[fileSize+1];
|
|
|
|
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);
|
|
|
|
delete data;
|
|
|
|
|
|
|
|
return str;
|
2021-01-02 04:24:29 +00:00
|
|
|
};
|
|
|
|
|
2021-01-09 00:38:28 +00:00
|
|
|
static std::vector<std::string> ReadAllLines(const std::string& 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-01-09 00:38:28 +00:00
|
|
|
static void WriteAllBytes(const std::string& 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-01-09 00:38:28 +00:00
|
|
|
static void WriteAllText(const std::string& 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
|
|
|
};
|