diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-17 08:05:40 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-17 08:05:40 +0300 |
commit | 7100d6af6d22ade0914fa5f8275401e37ca9610f (patch) | |
tree | 60fc298154ef4a71fdfde2b15162a1c5ec5478d0 /utils/helpers | |
parent | fix compiler warnings (diff) | |
download | aes-tools-7100d6af6d22ade0914fa5f8275401e37ca9610f.tar.gz aes-tools-7100d6af6d22ade0914fa5f8275401e37ca9610f.zip |
utils: code dedupe
Diffstat (limited to '')
-rw-r--r-- | utils/helpers/file.hpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/utils/helpers/file.hpp b/utils/helpers/file.hpp new file mode 100644 index 0000000..a327c35 --- /dev/null +++ b/utils/helpers/file.hpp @@ -0,0 +1,48 @@ +// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com> +// This file is part of the "AES tools" project. +// For details, see https://github.com/egor-tensin/aes-tools. +// Distributed under the MIT License. + +#include <cstddef> + +#include <fstream> +#include <iterator> +#include <string> +#include <vector> + +namespace file +{ + inline std::size_t get_file_size(const std::string& path) + { + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary | std::ifstream::ate); + return ifs.tellg(); + } + + inline std::vector<char> read_file(const std::string& path) + { + const auto size = get_file_size(path); + + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary); + + std::vector<char> src_buf; + src_buf.reserve(size); + src_buf.assign( + std::istreambuf_iterator<char>(ifs), + std::istreambuf_iterator<char>()); + return src_buf; + } + + inline void write_file( + const std::string& path, + const std::vector<unsigned char>& src) + { + 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()); + } +} |