// Copyright (c) 2016 Egor Tensin // 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 #include #include #include #include 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 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 src_buf; src_buf.reserve(size); src_buf.assign( std::istreambuf_iterator(ifs), std::istreambuf_iterator()); return src_buf; } inline void write_file( const std::string& path, const std::vector& src) { 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()); } }