90 lines
2.0 KiB
C++
90 lines
2.0 KiB
C++
#include "util.h"
|
|
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
|
|
namespace m2pack
|
|
{
|
|
|
|
[[noreturn]] void fail(const std::string& message)
|
|
{
|
|
throw std::runtime_error(message);
|
|
}
|
|
|
|
std::vector<std::uint8_t> read_file(const std::string& path)
|
|
{
|
|
std::ifstream input(path, std::ios::binary);
|
|
if (!input)
|
|
{
|
|
fail("Failed to open file for reading: " + path);
|
|
}
|
|
|
|
input.seekg(0, std::ios::end);
|
|
const auto size = static_cast<std::size_t>(input.tellg());
|
|
input.seekg(0, std::ios::beg);
|
|
|
|
std::vector<std::uint8_t> bytes(size);
|
|
if (!bytes.empty())
|
|
{
|
|
input.read(reinterpret_cast<char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
|
|
}
|
|
|
|
if (!input.good() && !input.eof())
|
|
{
|
|
fail("Failed to read file: " + path);
|
|
}
|
|
|
|
return bytes;
|
|
}
|
|
|
|
void write_file(const std::string& path, const std::vector<std::uint8_t>& data)
|
|
{
|
|
std::filesystem::create_directories(std::filesystem::path(path).parent_path());
|
|
std::ofstream output(path, std::ios::binary);
|
|
if (!output)
|
|
{
|
|
fail("Failed to open file for writing: " + path);
|
|
}
|
|
|
|
if (!data.empty())
|
|
{
|
|
output.write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
|
|
}
|
|
|
|
if (!output.good())
|
|
{
|
|
fail("Failed to write file: " + path);
|
|
}
|
|
}
|
|
|
|
std::string json_escape(std::string_view value)
|
|
{
|
|
std::string out;
|
|
out.reserve(value.size() + 8);
|
|
|
|
for (const char ch : value)
|
|
{
|
|
switch (ch)
|
|
{
|
|
case '\\': out += "\\\\"; break;
|
|
case '"': out += "\\\""; break;
|
|
case '\n': out += "\\n"; break;
|
|
case '\r': out += "\\r"; break;
|
|
case '\t': out += "\\t"; break;
|
|
default: out += ch; break;
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
void write_stdout(const std::vector<std::uint8_t>& data)
|
|
{
|
|
std::cout.write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
|
|
}
|
|
|
|
} // namespace m2pack
|