diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-01-22 09:12:43 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-01-22 09:12:43 +0300 |
commit | b5bf12e8cfe42834fd782918be4de8bcae61ce04 (patch) | |
tree | 50cf4b270d22b4f8a5626ce2aba369d17f2e9a27 /utils/helpers | |
parent | test: fix Pylint warnings (diff) | |
download | aes-tools-b5bf12e8cfe42834fd782918be4de8bcae61ce04.tar.gz aes-tools-b5bf12e8cfe42834fd782918be4de8bcae61ce04.zip |
utils: bugfix
Diffstat (limited to '')
-rw-r--r-- | utils/helpers/file.hpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/utils/helpers/file.hpp b/utils/helpers/file.hpp index febe98f..e767bbc 100644 --- a/utils/helpers/file.hpp +++ b/utils/helpers/file.hpp @@ -10,18 +10,24 @@ #include <iterator> #include <limits> #include <string> +#include <type_traits> #include <vector> namespace file { + inline std::size_t cast_to_size_t(std::streamoff size) + { + assert(size >= 0); + assert(static_cast<std::make_unsigned<std::streamoff>::type>(size) <= std::numeric_limits<std::size_t>::max()); + return static_cast<std::size_t>(size); + } + 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); - 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); + return cast_to_size_t(ifs.tellg()); } inline std::vector<char> read_file(const std::string& path) |