diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-17 10:42:02 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-17 10:42:02 +0300 |
commit | b42c52475d6e86f6ae0ed01f502bd0b611974578 (patch) | |
tree | 30f4777c39f7d07aed6a85209a03bc8af44a415d /utils/helpers/file.hpp | |
parent | fix compiler warnings (diff) | |
download | aes-tools-b42c52475d6e86f6ae0ed01f502bd0b611974578.tar.gz aes-tools-b42c52475d6e86f6ae0ed01f502bd0b611974578.zip |
utils: code dedupe
Diffstat (limited to 'utils/helpers/file.hpp')
-rw-r--r-- | utils/helpers/file.hpp | 18 |
1 files changed, 15 insertions, 3 deletions
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 <cassert> #include <cstddef> #include <fstream> #include <iterator> +#include <limits> #include <string> #include <vector> @@ -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<std::streamoff>(ifs.tellg()); + assert(size <= static_cast<std::streamoff>(std::numeric_limits<std::size_t>::max())); + return static_cast<std::size_t>(size); } inline std::vector<char> read_file(const std::string& path) @@ -38,11 +42,19 @@ namespace file inline void write_file( const std::string& path, - const std::vector<unsigned char>& 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<const char*>(src.data()), src.size()); + ofs.write(reinterpret_cast<const char*>(buffer), size); + } + + inline void write_file( + const std::string& path, + const std::vector<unsigned char>& src) + { + write_file(path, src.data(), src.size()); } } |