|
|
@ -5,6 +5,7 @@ |
|
|
|
#define LIBPEPADAPTER_STD_UTILS_HXX |
|
|
|
|
|
|
|
#include <sstream> |
|
|
|
#include <fstream> |
|
|
|
|
|
|
|
namespace pEp { |
|
|
|
namespace Utils { |
|
|
@ -17,6 +18,35 @@ namespace pEp { |
|
|
|
} |
|
|
|
return ss.str(); |
|
|
|
} |
|
|
|
|
|
|
|
template<typename T> |
|
|
|
std::vector<T> file_read_bin(const std::string &filename) |
|
|
|
{ |
|
|
|
std::vector<T> 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<T>(ifs), std::istream_iterator<T>() }; |
|
|
|
} else { |
|
|
|
throw std::runtime_error("File does not exist: '" + filename + "'"); |
|
|
|
} |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
template<typename T> |
|
|
|
void file_write_bin(const std::string &filename, std::vector<T> &data) |
|
|
|
{ |
|
|
|
std::fstream f(filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); |
|
|
|
f.write(data.data(), static_cast<std::streamsize>(data.size())); |
|
|
|
if (f.bad()) { |
|
|
|
throw std::runtime_error("failed to write file: '" + filename + "'"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} // namespace Utils
|
|
|
|
} // namespace pEp
|
|
|
|
#endif // LIBPEPADAPTER_STD_UTILS_HXX
|