From 0d72ea5367bff1b37cbc54db291c1bdedfc90297 Mon Sep 17 00:00:00 2001 From: heck Date: Sun, 25 Apr 2021 01:32:48 +0200 Subject: [PATCH] Add pEp::Utils - collection of helper functions and stuff... Separated out from pEp::Test::Utils --- src/utils.cc | 200 ++++++++++++++++++++++++++++++++++++++ src/utils.hh | 44 +++++++++ src/utils.hxx | 22 +++++ test/framework/utils.cc | 204 --------------------------------------- test/framework/utils.hh | 38 +------- test/framework/utils.hxx | 24 ----- 6 files changed, 269 insertions(+), 263 deletions(-) create mode 100644 src/utils.cc create mode 100644 src/utils.hh create mode 100644 src/utils.hxx delete mode 100644 test/framework/utils.cc delete mode 100644 test/framework/utils.hxx diff --git a/src/utils.cc b/src/utils.cc new file mode 100644 index 0000000..bf58274 --- /dev/null +++ b/src/utils.cc @@ -0,0 +1,200 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#include "utils.hh" + +#include +#include +#include + +#include + +#include "Adapter.hh" +#include "adapter_group.h" + +using namespace std; +using namespace pEp; + +namespace pEp { + namespace Utils { + + string to_string(const ::pEp_identity *const ident, bool full, int indent) + { + stringstream builder; + if (ident != nullptr) { + if (full) { + builder << endl; + builder << std::string(indent, '\t') << "{" << endl; + indent++; + builder << std::string(indent, '\t') + << "address: " << (ident->address != nullptr ? ident->address : "NULL") + << endl; + builder << std::string(indent, '\t') + << "user_id: " << (ident->user_id != nullptr ? ident->user_id : "NULL") + << endl; + builder << std::string(indent, '\t') << "username: " + << (ident->username != nullptr ? ident->username : "NULL") << endl; + builder << std::string(indent, '\t') + << "fpr: " << (ident->fpr != nullptr ? ident->fpr : "NULL") << endl; + builder << std::string(indent, '\t') << "comm_type: " << ident->comm_type << endl; + builder << std::string(indent, '\t') + << "lang: " << static_cast(ident->lang) << endl; + builder << std::string(indent, '\t') << "me: " << ident->me << endl; + builder << std::string(indent, '\t') << "major_ver: " << ident->major_ver << endl; + builder << std::string(indent, '\t') << "minor_ver: " << ident->minor_ver << endl; + builder << std::string(indent, '\t') << "enc_format: " << ident->enc_format + << endl; + builder << std::string(indent, '\t') << "flags: " << ident->flags << endl; + indent--; + builder << std::string(indent, '\t') << "}"; + } else { + builder << "{ " << (ident->address != nullptr ? ident->address : "NULL") << "/" + << (ident->user_id != nullptr ? ident->user_id : "NULL") << "/" + << (ident->username != nullptr ? ident->username : "NULL") << "/" + << (ident->fpr != nullptr ? ident->fpr : "NULL") << " }"; + } + } else { + builder << "NULL"; + } + + return builder.str(); + } + + std::string to_string(const ::identity_list *const idl, bool full, int indent) + { + stringstream builder; + if (idl != nullptr) { + builder << endl; + builder << std::string(indent, '\t') << "[" << endl; + indent++; + for (const ::identity_list *curr = idl; curr != nullptr; curr = curr->next) { + builder << to_string(curr->ident, full, indent) << endl; + } + indent--; + builder << std::string(indent, '\t') << "]"; + } else { + builder << "NULL"; + } + + return builder.str(); + } + + string to_string(const ::pEp_member *const member, bool full, int indent) + { + stringstream builder; + if (member != nullptr) { + builder << std::string(indent, '\t') << "{" << endl; + indent++; + builder << std::string(indent, '\t') + << "ident: " << to_string(member->ident, full, indent) << endl; + builder << std::string(indent, '\t') << "joined: " << member->joined << endl; + indent--; + builder << std::string(indent, '\t') << "}"; + } else { + builder << "NULL"; + } + + return builder.str(); + } + + string to_string(const ::member_list *const mbl, bool full, int indent) + { + stringstream builder; + if (mbl != nullptr) { + builder << endl; + builder << std::string(indent, '\t') << "[" << endl; + indent++; + for (const member_list *curr_member = mbl; curr_member != nullptr; + curr_member = curr_member->next) { + builder << to_string(curr_member->member, full, indent) << endl; + } + indent--; + builder << std::string(indent, '\t') << "]"; + } else { + builder << "NULL"; + } + + return builder.str(); + } + + string to_string(const ::pEp_group *const group, bool full, int indent) + { + stringstream builder; + if (group != nullptr) { + builder << endl; + builder << std::string(indent, '\t') << "{" << endl; + indent++; + builder << std::string(indent, '\t') + << "group_identity: " << to_string(group->group_identity, full, indent) + << endl; + builder << std::string(indent, '\t') + << "manager: " << to_string(group->manager, full, indent) << endl; + builder << std::string(indent, '\t') << "active: " << group->active << endl; + builder << std::string(indent, '\t') + << "members: " << to_string(group->members, full, indent) << endl; + indent--; + builder << std::string(indent, '\t') << "]"; + } else { + builder << "NULL"; + } + + return builder.str(); + } + + string nested_exception_to_string(const exception &e, int level, string src) + { + src += string(level, ' ') + "exception: " + e.what() + "\n"; + try { + rethrow_if_nested(e); + } catch (const exception &e) { + src = nested_exception_to_string(e, level + 1, src); + } catch (...) { + } + return src; + } + + + // void print_exception(const exception &e, int level) + // { + // cerr << string(level, ' ') << "exception: " << e.what() << endl; + // try { + // rethrow_if_nested(e); + // } catch (const exception &e) { + // print_exception(e, level + 1); + // } catch (...) { + // } + // } + + + // File utils + ofstream file_create(const string &filename) + { + ofstream outfile{ filename }; + return outfile; + } + + bool file_exists(const string &filename) + { + + ifstream ifile(filename.c_str()); + return (bool)ifile; + } + + void file_delete(const string &filename) + { + int status = remove(filename.c_str()); + if (status) { + runtime_error e{ string("file_delete(\"" + filename + "\") - " + strerror(errno)) }; + throw(e); + } + } + + void file_ensure_not_existing(const string &path) + { + while (file_exists(path)) { + file_delete(path); + } + } + + } // namespace Utils +} // namespace pEp diff --git a/src/utils.hh b/src/utils.hh new file mode 100644 index 0000000..ba02a13 --- /dev/null +++ b/src/utils.hh @@ -0,0 +1,44 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_UTILS_HH +#define LIBPEPADAPTER_UTILS_HH + +#include "pEpLog.hh" +#include +#include +#include +#include +#include + +namespace pEp { + namespace Utils { + // pEpEngine datatypes to string + std::string to_string(const ::pEp_identity *const ident, bool full = true, int indent = 0); + std::string to_string(const ::identity_list *const idl, bool full = true, int indent = 0); + std::string to_string(const ::pEp_member *const member, bool full = true, int indent = 0); + std::string to_string(const ::member_list *const mbl, bool full = true, int indent = 0); + std::string to_string(const ::pEp_group *const group, bool full = true, int indent = 0); + + // C++/STL data types to string + template + std::string to_string(const std::vector &v); + + // exception utils + std::string nested_exception_to_string( + const std::exception &e, + int level = 0, + std::string src = ""); + void print_exception(const std::exception &e, int level = 0); + + // file utils + std::ofstream file_create(const std::string &filename); + bool file_exists(const std::string &filename); + void file_delete(const std::string &filename); + void file_ensure_not_existing(const std::string &path); + } // namespace Utils +} // namespace pEp + +#include "utils.hxx" + +#endif // LIBPEPADAPTER_UTILS_HH \ No newline at end of file diff --git a/src/utils.hxx b/src/utils.hxx new file mode 100644 index 0000000..0faff79 --- /dev/null +++ b/src/utils.hxx @@ -0,0 +1,22 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_UTILS_HH +#define LIBPEPADAPTER_UTILS_HH + +#include + +namespace pEp { + namespace Utils { + template + std::string to_string(std::vector v) + { + std::stringstream ss; + for (const T& elem : v) { + ss << elem << std::endl; + } + return ss.str(); + } + } // namespace Utils +} // namespace pEp +#endif // LIBPEPADAPTER_UTILS_HXX \ No newline at end of file diff --git a/test/framework/utils.cc b/test/framework/utils.cc deleted file mode 100644 index 29d3bc9..0000000 --- a/test/framework/utils.cc +++ /dev/null @@ -1,204 +0,0 @@ -// This file is under GNU General Public License 3.0 -// see LICENSE.txt - -#include "utils.hh" - -#include -#include -#include - -#include - -#include "../../src/Adapter.hh" -#include "../../src/adapter_group.h" - -using namespace std; -using namespace pEp; - -namespace pEp { - namespace Test { - namespace Utils { - - string to_string(const ::pEp_identity *const ident, bool full, int indent) - { - stringstream builder; - if (ident != nullptr) { - if (full) { - builder << endl; - builder << std::string(indent, '\t') << "{" << endl; - indent++; - builder << std::string(indent, '\t') << "address: " - << (ident->address != nullptr ? ident->address : "NULL") << endl; - builder << std::string(indent, '\t') << "user_id: " - << (ident->user_id != nullptr ? ident->user_id : "NULL") << endl; - builder << std::string(indent, '\t') << "username: " - << (ident->username != nullptr ? ident->username : "NULL") << endl; - builder << std::string(indent, '\t') - << "fpr: " << (ident->fpr != nullptr ? ident->fpr : "NULL") << endl; - builder << std::string(indent, '\t') << "comm_type: " << ident->comm_type - << endl; - builder << std::string(indent, '\t') - << "lang: " << static_cast(ident->lang) << endl; - builder << std::string(indent, '\t') << "me: " << ident->me << endl; - builder << std::string(indent, '\t') << "major_ver: " << ident->major_ver - << endl; - builder << std::string(indent, '\t') << "minor_ver: " << ident->minor_ver - << endl; - builder << std::string(indent, '\t') << "enc_format: " << ident->enc_format - << endl; - builder << std::string(indent, '\t') << "flags: " << ident->flags << endl; - indent--; - builder << std::string(indent, '\t') << "}"; - } else { - builder << "{ " << (ident->address != nullptr ? ident->address : "NULL") - << "/" << (ident->user_id != nullptr ? ident->user_id : "NULL") - << "/" << (ident->username != nullptr ? ident->username : "NULL") - << "/" << (ident->fpr != nullptr ? ident->fpr : "NULL") << " }"; - } - } else { - builder << "NULL"; - } - - return builder.str(); - } - - std::string to_string(const ::identity_list *const idl, bool full, int indent) - { - stringstream builder; - if (idl != nullptr) { - builder << endl; - builder << std::string(indent, '\t') << "[" << endl; - indent++; - for (const ::identity_list *curr = idl; curr != nullptr; curr = curr->next) { - builder << to_string(curr->ident, full, indent) << endl; - } - indent--; - builder << std::string(indent, '\t') << "]"; - } else { - builder << "NULL"; - } - - return builder.str(); - } - - string to_string(const ::pEp_member *const member, bool full, int indent) - { - stringstream builder; - if (member != nullptr) { - builder << std::string(indent, '\t') << "{" << endl; - indent++; - builder << std::string(indent, '\t') - << "ident: " << to_string(member->ident, full, indent) << endl; - builder << std::string(indent, '\t') << "joined: " << member->joined << endl; - indent--; - builder << std::string(indent, '\t') << "}"; - } else { - builder << "NULL"; - } - - return builder.str(); - } - - string to_string(const ::member_list *const mbl, bool full, int indent) - { - stringstream builder; - if (mbl != nullptr) { - builder << endl; - builder << std::string(indent, '\t') << "[" << endl; - indent++; - for (const member_list *curr_member = mbl; curr_member != nullptr; - curr_member = curr_member->next) { - builder << to_string(curr_member->member, full, indent) << endl; - } - indent--; - builder << std::string(indent, '\t') << "]"; - } else { - builder << "NULL"; - } - - return builder.str(); - } - - string to_string(const ::pEp_group *const group, bool full, int indent) - { - stringstream builder; - if (group != nullptr) { - builder << endl; - builder << std::string(indent, '\t') << "{" << endl; - indent++; - builder << std::string(indent, '\t') - << "group_identity: " << to_string(group->group_identity, full, indent) - << endl; - builder << std::string(indent, '\t') - << "manager: " << to_string(group->manager, full, indent) << endl; - builder << std::string(indent, '\t') << "active: " << group->active << endl; - builder << std::string(indent, '\t') - << "members: " << to_string(group->members, full, indent) << endl; - indent--; - builder << std::string(indent, '\t') << "]"; - } else { - builder << "NULL"; - } - - return builder.str(); - } - - string nested_exception_to_string(const exception &e, int level, string src) - { - src += string(level, ' ') + "exception: " + e.what() + "\n"; - try { - rethrow_if_nested(e); - } catch (const exception &e) { - src = nested_exception_to_string(e, level + 1, src); - } catch (...) { - } - return src; - } - - - // void print_exception(const exception &e, int level) - // { - // cerr << string(level, ' ') << "exception: " << e.what() << endl; - // try { - // rethrow_if_nested(e); - // } catch (const exception &e) { - // print_exception(e, level + 1); - // } catch (...) { - // } - // } - - - // File utils - ofstream file_create(const string &filename) - { - ofstream outfile{ filename }; - return outfile; - } - - bool file_exists(const string &filename) - { - - ifstream ifile(filename.c_str()); - return (bool)ifile; - } - - void file_delete(const string &filename) - { - int status = remove(filename.c_str()); - if (status) { - runtime_error e{ string( - "file_delete(\"" + filename + "\") - " + strerror(errno)) }; - throw(e); - } - } - - void file_ensure_not_existing(const string &path) - { - while (file_exists(path)) { - file_delete(path); - } - } - - } // namespace Utils - } // namespace Test -} // namespace pEp diff --git a/test/framework/utils.hh b/test/framework/utils.hh index bf5847c..3c45607 100644 --- a/test/framework/utils.hh +++ b/test/framework/utils.hh @@ -1,8 +1,8 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#ifndef LIBPEPADAPTER_UTILS_HH -#define LIBPEPADAPTER_UTILS_HH +#ifndef LIBPEPADAPTER_TEST_UTILS_HH +#define LIBPEPADAPTER_TEST_UTILS_HH #include "../../src/pEpLog.hh" #include @@ -70,36 +70,4 @@ // ------------------------------------------------------------------------------------------------ -namespace pEp { - namespace Test { - namespace Utils { - // pEpEngine datatypes to string - std::string to_string(const ::pEp_identity *const ident, bool full = true, int indent = 0); - std::string to_string(const ::identity_list *const idl, bool full = true, int indent = 0); - std::string to_string(const ::pEp_member *const member, bool full = true, int indent = 0); - std::string to_string(const ::member_list *const mbl, bool full = true, int indent = 0); - std::string to_string(const ::pEp_group *const group, bool full = true, int indent = 0); - - // C++/STL data types to string - template - std::string to_string(const std::vector &v); - - // exception utils - std::string nested_exception_to_string( - const std::exception &e, - int level = 0, - std::string src = ""); - void print_exception(const std::exception &e, int level = 0); - - // file utils - std::ofstream file_create(const std::string &filename); - bool file_exists(const std::string &filename); - void file_delete(const std::string &filename); - void file_ensure_not_existing(const std::string &path); - } // namespace Utils - } // namespace Test -} // namespace pEp - -#include "utils.hxx" - -#endif // LIBPEPADAPTER_UTILS_HH \ No newline at end of file +#endif // LIBPEPADAPTER_TEST_UTILS_HH \ No newline at end of file diff --git a/test/framework/utils.hxx b/test/framework/utils.hxx deleted file mode 100644 index 508281c..0000000 --- a/test/framework/utils.hxx +++ /dev/null @@ -1,24 +0,0 @@ -// This file is under GNU General Public License 3.0 -// see LICENSE.txt - -#ifndef LIBPEPADAPTER_UTILS_HXX -#define LIBPEPADAPTER_UTILS_HXX - -#include - -namespace pEp { - namespace Test { - namespace Utils { - template - std::string to_string(std::vector v) - { - std::stringstream ss; - for (const T& elem : v) { - ss << elem << std::endl; - } - return ss.str(); - } - } - } // namespace Test -} // namespace pEp -#endif // LIBPEPADAPTER_UTILS_HXX \ No newline at end of file