From 6fd8e5ee43dd0f87eb4e57b86e84770fcd95db25 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Thu, 17 Aug 2023 00:35:58 -0600 Subject: Added StormLib, nlohmann, BinaryTools and CLI11 --- lib/binarytools/FileHelper.h | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 lib/binarytools/FileHelper.h (limited to 'lib/binarytools/FileHelper.h') diff --git a/lib/binarytools/FileHelper.h b/lib/binarytools/FileHelper.h new file mode 100644 index 0000000..bb65886 --- /dev/null +++ b/lib/binarytools/FileHelper.h @@ -0,0 +1,99 @@ +#pragma once + +#include +#include +#include +#include +#include +#include "PathHelper.h" +#include "Utils/StringHelper.h" +#include "Utils/Directory.h" + +namespace LUS { +class FileHelper { + 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 ReadAllBytes(const fs::path& filePath) { + std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate); + + if (!file) { + return std::vector(); + } + + int32_t fileSize = (int32_t)file.tellg(); + file.seekg(0); + char* data = nullptr; + std::vector result; + + try { + data = new char[fileSize]; + file.read(data, fileSize); + result = std::vector(data, data + fileSize); + } catch (const std::exception& e) { + delete[] data; + throw e; + } + + 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 = nullptr; + std::string str; + + try { + data = new char[fileSize + 1]; + memset(data, 0, fileSize + 1); + file.read(data, fileSize); + str = std::string((const char*)data); + } catch (const std::exception& e) { + delete[] data; + throw e; + } + + delete[] data; + + return str; + }; + + static std::vector ReadAllLines(const fs::path& filePath) { + std::string text = ReadAllText(filePath); + std::vector lines = StringHelper::Split(text, "\n"); + + return lines; + }; + + static void WriteAllBytes(const fs::path& filePath, const std::vector& 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& data) { + if (!Directory::Exists(PathHelper::GetDirectoryName(filePath))) { + Directory::MakeDirectory(PathHelper::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()); + } +}; +} // namespace LUS \ No newline at end of file -- cgit v1.2.3