#include #include #include #include #include #include #include #include #include #include #include #include #include namespace xrpl { std::string getFileContents( boost::system::error_code& ec, boost::filesystem::path const& sourcePath, std::optional maxSize) { using namespace boost::filesystem; using namespace boost::system::errc; path const fullPath{canonical(sourcePath, ec)}; if (ec) return {}; if (maxSize && (file_size(fullPath, ec) > *maxSize || ec)) { if (!ec) ec = make_error_code(file_too_large); return {}; } std::ifstream fileStream(fullPath.string(), std::ios::in); if (!fileStream) { ec = make_error_code(static_cast(errno)); return {}; } std::string result{ std::istreambuf_iterator{fileStream}, std::istreambuf_iterator{}}; if (fileStream.bad()) { ec = make_error_code(static_cast(errno)); return {}; } return result; } void writeFileContents( boost::system::error_code& ec, boost::filesystem::path const& destPath, std::string const& contents) { using namespace boost::filesystem; using namespace boost::system::errc; std::ofstream fileStream(destPath.string(), std::ios::out | std::ios::trunc); if (!fileStream) { ec = make_error_code(static_cast(errno)); return; } fileStream << contents; if (fileStream.bad()) { ec = make_error_code(static_cast(errno)); return; } } } // namespace xrpl