From b42c52475d6e86f6ae0ed01f502bd0b611974578 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 17 Oct 2016 10:42:02 +0300 Subject: utils: code dedupe --- utils/helpers/file.hpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'utils/helpers/file.hpp') diff --git a/utils/helpers/file.hpp b/utils/helpers/file.hpp index a327c35..febe98f 100644 --- a/utils/helpers/file.hpp +++ b/utils/helpers/file.hpp @@ -3,10 +3,12 @@ // For details, see https://github.com/egor-tensin/aes-tools. // Distributed under the MIT License. +#include #include #include #include +#include #include #include @@ -17,7 +19,9 @@ namespace file std::ifstream ifs; ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return ifs.tellg(); + const auto size = static_cast(ifs.tellg()); + assert(size <= static_cast(std::numeric_limits::max())); + return static_cast(size); } inline std::vector read_file(const std::string& path) @@ -38,11 +42,19 @@ namespace file inline void write_file( const std::string& path, - const std::vector& src) + const void* buffer, + const std::size_t size) { std::ofstream ofs; ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); ofs.open(path, std::ofstream::binary); - ofs.write(reinterpret_cast(src.data()), src.size()); + ofs.write(reinterpret_cast(buffer), size); + } + + inline void write_file( + const std::string& path, + const std::vector& src) + { + write_file(path, src.data(), src.size()); } } -- cgit v1.2.3