From b5bf12e8cfe42834fd782918be4de8bcae61ce04 Mon Sep 17 00:00:00 2001 From: Egor Tensin <Egor.Tensin@gmail.com> Date: Sun, 22 Jan 2017 09:12:43 +0300 Subject: utils: bugfix --- utils/helpers/file.hpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'utils') 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) -- cgit v1.2.3