1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-11-14 05:19:36 +00:00
oot/tools/ZAPD/ZAPDUtils/Utils/File.h
Dragorn421 4e55168eaa
Update ZAPD (#1569)
* git subrepo pull --force tools/ZAPD

subrepo:
  subdir:   "tools/ZAPD"
  merged:   "094e79734"
upstream:
  origin:   "https://github.com/zeldaret/ZAPD.git"
  branch:   "master"
  commit:   "094e79734"
git-subrepo:
  version:  "0.4.6"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "110b9eb"

* Add EnumData.xml where some names are now externalized

* Remove legacy typedefs for zapd, no longer needed!
2023-10-24 21:36:10 -04:00

97 lines
2.3 KiB
C++

#pragma once
#ifdef USE_BOOST_FS
#include <boost/filesystem/fstream.hpp>
#else
#include <fstream>
#endif
#include <string>
#include <vector>
#include "Directory.h"
#include "Utils/StringHelper.h"
class File
{
#ifdef USE_BOOST_FS
typedef fs::ifstream ifstream;
typedef fs::ofstream ofstream;
#else
typedef std::ifstream ifstream;
typedef std::ofstream ofstream;
#endif
public:
static bool Exists(const fs::path& filePath)
{
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)
{
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];
file.read(data, fileSize);
std::vector<uint8_t> result = std::vector<uint8_t>(data, data + fileSize);
delete[] data;
file.close();
return result;
};
static std::string ReadAllText(const fs::path& filePath)
{
ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
if (!file.is_open())
return "";
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;
file.close();
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)
{
ofstream file(filePath, std::ios::binary);
file.write((char*)data.data(), data.size());
file.close();
};
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data)
{
ofstream file(filePath, std::ios::binary);
file.write((char*)data.data(), data.size());
file.close();
};
static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize)
{
ofstream file(filePath, std::ios::binary);
file.write((char*)data, dataSize);
file.close();
};
static void WriteAllText(const fs::path& filePath, const std::string& text)
{
ofstream file(filePath, std::ios::out);
file.write(text.c_str(), text.size());
file.close();
}
};