mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-14 10:37:17 -07:00
git subrepo clone https://github.com/HarbourMasters/ZAPDTR.git
subrepo: subdir: "ZAPDTR" merged: "a53a53ea4" upstream: origin: "https://github.com/HarbourMasters/ZAPDTR.git" branch: "master" commit: "a53a53ea4" git-subrepo: version: "0.4.1" origin: "???" commit: "???"
This commit is contained in:
parent
f52a2a6406
commit
5dda5762ba
217 changed files with 47152 additions and 0 deletions
87
ZAPDTR/ZAPDUtils/Utils/File.h
Normal file
87
ZAPDTR/ZAPDUtils/Utils/File.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Path.h"
|
||||
#include "Utils/StringHelper.h"
|
||||
#include "Utils/Directory.h"
|
||||
|
||||
|
||||
class File
|
||||
{
|
||||
public:
|
||||
static bool Exists(const fs::path& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
return file.good();
|
||||
}
|
||||
|
||||
static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
|
||||
if (!file)
|
||||
return std::vector<uint8_t>();
|
||||
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = new char[fileSize];
|
||||
file.read(data, fileSize);
|
||||
std::vector<uint8_t> result = std::vector<uint8_t>(data, data + fileSize);
|
||||
delete[] data;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
static std::string ReadAllText(const fs::path& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = new char[fileSize + 1];
|
||||
memset(data, 0, fileSize + 1);
|
||||
file.read(data, fileSize);
|
||||
std::string str = std::string((const char*)data);
|
||||
delete[] data;
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
static std::vector<std::string> ReadAllLines(const fs::path& filePath)
|
||||
{
|
||||
std::string text = ReadAllText(filePath);
|
||||
std::vector<std::string> lines = StringHelper::Split(text, "\n");
|
||||
|
||||
return lines;
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data)
|
||||
{
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data)
|
||||
{
|
||||
if (!Directory::Exists(Path::GetDirectoryName(filePath)))
|
||||
Directory::MakeDirectory(Path::GetDirectoryName(filePath).string());
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
static void WriteAllText(const fs::path& filePath, const std::string& text)
|
||||
{
|
||||
std::ofstream file(filePath, std::ios::out);
|
||||
file.write(text.c_str(), text.size());
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue