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.

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