C++11 library providing functionality common to all adapters.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2 KiB

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #ifndef LIBPEPADAPTER_STD_UTILS_HXX
  4. #define LIBPEPADAPTER_STD_UTILS_HXX
  5. #include <sstream>
  6. #include <fstream>
  7. #include <iterator>
  8. namespace pEp {
  9. namespace Utils {
  10. template<typename T>
  11. std::string to_string(const std::vector<T>& v)
  12. {
  13. std::stringstream ss;
  14. for (const T& elem : v) {
  15. ss << elem << std::endl;
  16. }
  17. return ss.str();
  18. }
  19. template<typename T>
  20. std::vector<T> file_read_bin(const std::string &filename)
  21. {
  22. std::vector<T> ret{};
  23. if (pEp::Utils::path_exists(filename)) {
  24. std::ifstream ifs(filename, std::ios_base::binary);
  25. ifs.unsetf(std::ios_base::skipws);
  26. if (ifs.bad()) {
  27. throw std::runtime_error("failed to read file: '" + filename + "'");
  28. }
  29. ret = { std::istream_iterator<T>(ifs), std::istream_iterator<T>() };
  30. } else {
  31. throw std::runtime_error("File does not exist: '" + filename + "'");
  32. }
  33. return ret;
  34. }
  35. template<typename T>
  36. void file_write_bin(const std::string &filename, std::vector<T> &data)
  37. {
  38. std::fstream f(filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
  39. f.write(data.data(), static_cast<std::streamsize>(data.size()));
  40. if (f.bad()) {
  41. throw std::runtime_error("failed to write file: '" + filename + "'");
  42. }
  43. }
  44. template<class T>
  45. std::string bin2hex(const T &bin)
  46. {
  47. std::string ret{};
  48. std::stringstream ss{};
  49. for (const auto &i : bin) {
  50. ss << std::hex << std::setfill('0') << std::setw(2) << (int)i;
  51. }
  52. ret = ss.str();
  53. return ret;
  54. }
  55. template<class T>
  56. T hex2bin(const std::string &hex_str)
  57. {
  58. T ret{};
  59. if ((hex_str.size() % 2) != 0) {
  60. throw std::runtime_error("hex2bin: Invalid hex string: must be even length");
  61. }
  62. for (int i = 0; i < hex_str.size(); i += 2) {
  63. std::ostringstream val_hex{};
  64. val_hex << hex_str.at(i);
  65. val_hex << hex_str.at(i + 1);
  66. int val_int;
  67. std::istringstream conv_ss{ val_hex.str() };
  68. conv_ss >> std::hex >> val_int;
  69. if (conv_ss.fail()) {
  70. throw std::runtime_error("hex2bin: invalid hex string" + hex_str);
  71. }
  72. ret.push_back(static_cast<typename T::value_type>(val_int));
  73. }
  74. return ret;
  75. // alternative way
  76. // std::string extract;
  77. // for (std::string::const_iterator pos = hex_str.begin(); pos < hex_str.end(); pos += 2) {
  78. // extract.assign(pos, pos + 2);
  79. // ret.push_back(std::stoi(extract, nullptr, 16));
  80. // }
  81. // return ret;
  82. }
  83. } // namespace Utils
  84. } // namespace pEp
  85. #endif // LIBPEPADAPTER_STD_UTILS_HXX