diff --git a/src/std_utils.cc b/src/std_utils.cc index 5102fdd..d32c8d3 100644 --- a/src/std_utils.cc +++ b/src/std_utils.cc @@ -158,32 +158,6 @@ namespace pEp { return ss.str(); } - std::vector file_read_bin(const std::string &filename) - { - std::vector ret{}; - if (pEp::Utils::path_exists(filename)) { - std::ifstream ifs(filename, std::ios_base::binary); - ifs.unsetf(std::ios_base::skipws); - - if (ifs.bad()) { - throw std::runtime_error("failed to read file: '" + filename + "'"); - } - ret = { std::istream_iterator(ifs), std::istream_iterator() }; - } else { - throw std::runtime_error("File does not exist: '" + filename + "'"); - } - return ret; - } - - void file_write_bin(const std::string &filename, std::vector &data) - { - std::fstream f(filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); - f.write(data.data(), static_cast(data.size())); - if (f.bad()) { - throw std::runtime_error("failed to write file: '" + filename + "'"); - } - } - #ifndef WIN32 void path_ensure_not_existing(const string &path) { diff --git a/src/std_utils.hh b/src/std_utils.hh index 301f063..c2033b6 100644 --- a/src/std_utils.hh +++ b/src/std_utils.hh @@ -41,8 +41,12 @@ namespace pEp { // file std::ofstream file_create(const std::string &filename); std::string file_read(const std::string &filename); - std::vector file_read_bin(const std::string& filename); - void file_write_bin(const std::string& filename, std::vector& data); + + template + std::vector file_read_bin(const std::string& filename); + + template + void file_write_bin(const std::string& filename, std::vector& data); // dir #ifndef WIN32 diff --git a/src/std_utils.hxx b/src/std_utils.hxx index a54b160..a1db8dd 100644 --- a/src/std_utils.hxx +++ b/src/std_utils.hxx @@ -5,6 +5,7 @@ #define LIBPEPADAPTER_STD_UTILS_HXX #include +#include namespace pEp { namespace Utils { @@ -17,6 +18,35 @@ namespace pEp { } return ss.str(); } + + template + std::vector file_read_bin(const std::string &filename) + { + std::vector ret{}; + if (pEp::Utils::path_exists(filename)) { + std::ifstream ifs(filename, std::ios_base::binary); + ifs.unsetf(std::ios_base::skipws); + + if (ifs.bad()) { + throw std::runtime_error("failed to read file: '" + filename + "'"); + } + ret = { std::istream_iterator(ifs), std::istream_iterator() }; + } else { + throw std::runtime_error("File does not exist: '" + filename + "'"); + } + return ret; + } + + template + void file_write_bin(const std::string &filename, std::vector &data) + { + std::fstream f(filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); + f.write(data.data(), static_cast(data.size())); + if (f.bad()) { + throw std::runtime_error("failed to write file: '" + filename + "'"); + } + } + } // namespace Utils } // namespace pEp #endif // LIBPEPADAPTER_STD_UTILS_HXX \ No newline at end of file