From a015331c53c238543ff2786221d1fc6f1b1a60ee Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 29 Apr 2021 02:11:44 +0200 Subject: [PATCH] Split utils.cc and add std_utils.cc --- src/grp_driver_dummy.cc | 1 + src/std_utils.cc | 186 +++++++++++++++++++++++++++++++ src/std_utils.hh | 50 +++++++++ src/{utils.hxx => std_utils.hxx} | 8 +- src/utils.cc | 77 +------------ src/utils.hh | 24 +--- test/framework/utils.hh | 7 +- test/test_listmanager_dummy.cc | 7 +- test/test_pEpSQLite.cc | 41 +++---- 9 files changed, 272 insertions(+), 129 deletions(-) create mode 100644 src/std_utils.cc create mode 100644 src/std_utils.hh rename src/{utils.hxx => std_utils.hxx} (69%) diff --git a/src/grp_driver_dummy.cc b/src/grp_driver_dummy.cc index 6c15dd3..6c214ab 100644 --- a/src/grp_driver_dummy.cc +++ b/src/grp_driver_dummy.cc @@ -1,6 +1,7 @@ #include "grp_driver_dummy.hh" #include "pEpLog.hh" #include "utils.hh" +#include "std_utils.hh" #include #include "listmanager_dummy.hh" diff --git a/src/std_utils.cc b/src/std_utils.cc new file mode 100644 index 0000000..ba79af6 --- /dev/null +++ b/src/std_utils.cc @@ -0,0 +1,186 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#include "std_utils.hh" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace pEp; + +namespace pEp { + namespace Utils { + bool is_c_str_empty(const char *str) + { + if (str == nullptr) { + return true; + } + string tmp{ str }; + if (tmp.empty()) { + return true; + } + return false; + } + + 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 + bool path_exists(const string &filename) + { + ifstream ifile(filename.c_str()); + return (bool)ifile; + } + + bool path_is_dir(const string &path) + { + bool ret = false; + struct stat statbuf; + if (stat(path.c_str(), &statbuf) != 0) { + runtime_error e{ "path_is_dir(\"" + path + "\") - " + strerror(errno) }; + throw(e); + } + if (S_ISDIR(statbuf.st_mode)) { + ret = true; + } + return ret; + } + + void path_delete(const string &filename) + { + int status = remove(filename.c_str()); + if (status) { + runtime_error e{ string("path_delete(\"" + filename + "\") - " + strerror(errno)) }; + throw(e); + } + } + + void path_delete_all(const string &path) + { + try { + if (!path_is_dir(path)) { + path_delete(path); + } else { + vector dirlist = dir_list_all(path); + if (dirlist.empty()) { + path_delete(path); + } else { + for (const string &filename : dirlist) { + string newpath = path + "/" + filename; + path_delete_all(newpath); + } + path_delete(path); + } + } + } catch (...) { + runtime_error e{"path_delete_all(\""+path+"\")"}; + throw_with_nested(e); + } + } + + ofstream file_create(const string &filename) + { + ofstream outfile{ filename }; + return outfile; + } + + + void path_ensure_not_existing(const string &path) + { + while (path_exists(path)) { + path_delete(path); + } + } + + void dir_create(const string &dirname, const mode_t mode) + { + if (mkdir(dirname.c_str(), mode) != 0) { + runtime_error e{ string("dir_create(\"" + dirname + "\") - " + strerror(errno)) }; + throw(e); + } + } + + vector dir_list_all(const std::string &path, const bool incl_dot_and_dotdot) + { + vector ret; + if (!path_exists(path)) { + runtime_error e{ "dir_list_all(\"" + path + "\") - Error: does not exist" }; + throw(e); + } + + if (!path_is_dir(path)) { + runtime_error e{ "dir_list_all(\"" + path + "\") - Error: is not a directory" }; + throw(e); + } + + DIR *dirp = opendir(path.c_str()); + if (dirp == nullptr) { + runtime_error e{ "dir_list_all(\"" + path + "\") - Error opening dir" }; + throw e; + } + + struct dirent *dp; + while ((dp = readdir(dirp)) != NULL) { + ret.push_back(string(dp->d_name)); + } + + if (!incl_dot_and_dotdot) { + ret.erase( + remove_if( + ret.begin(), + ret.end(), + [](string elem) { return (elem == "." || elem == ".."); }), + ret.end()); + } + + closedir(dirp); + return ret; + } + + vector dir_list_dirs(const string &dirname, const bool incl_dot_and_dotdot) + { + vector ret = dir_list_all(dirname, incl_dot_and_dotdot); + ret.erase( + remove_if(ret.begin(), ret.end(), [](string elem) { return !path_is_dir(elem); }), + ret.end()); + + return ret; + } + + vector dir_list_files(const string &dirname) + { + vector ret = dir_list_all(dirname); + ret.erase( + remove_if(ret.begin(), ret.end(), [](string elem) { return !path_is_dir(elem); }), + ret.end()); + return ret; + } + + } // namespace Utils +} // namespace pEp diff --git a/src/std_utils.hh b/src/std_utils.hh new file mode 100644 index 0000000..22de58c --- /dev/null +++ b/src/std_utils.hh @@ -0,0 +1,50 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_STD_UTILS_HH +#define LIBPEPADAPTER_STD_UTILS_HH + +#include +#include +#include + +namespace pEp { + namespace Utils { + // C-types helpers + bool is_c_str_empty(const char *str); + + // 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 + + bool path_exists(const std::string &filename); + bool path_is_dir(const std::string &path); + void path_delete(const std::string &filename); + void path_delete_all(const std::string &path); + + std::ofstream file_create(const std::string &filename); + void path_ensure_not_existing(const std::string &path); + + void dir_create(const std::string &dirname, const mode_t mode = 0775); + std::vector dir_list_all( + const std::string &path, + const bool incl_dot_and_dotdot = false); + std::vector dir_list_dirs( + const std::string &dirname, + const bool incl_dot_and_dotdot = false); + std::vector dir_list_files(const std::string &dirname); + } // namespace Utils +} // namespace pEp + +#include "std_utils.hxx" + +#endif // LIBPEPADAPTER_STD_UTILS_HH \ No newline at end of file diff --git a/src/utils.hxx b/src/std_utils.hxx similarity index 69% rename from src/utils.hxx rename to src/std_utils.hxx index 0faff79..a54b160 100644 --- a/src/utils.hxx +++ b/src/std_utils.hxx @@ -1,15 +1,15 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#ifndef LIBPEPADAPTER_UTILS_HH -#define LIBPEPADAPTER_UTILS_HH +#ifndef LIBPEPADAPTER_STD_UTILS_HXX +#define LIBPEPADAPTER_STD_UTILS_HXX #include namespace pEp { namespace Utils { template - std::string to_string(std::vector v) + std::string to_string(const std::vector& v) { std::stringstream ss; for (const T& elem : v) { @@ -19,4 +19,4 @@ namespace pEp { } } // namespace Utils } // namespace pEp -#endif // LIBPEPADAPTER_UTILS_HXX \ No newline at end of file +#endif // LIBPEPADAPTER_STD_UTILS_HXX \ No newline at end of file diff --git a/src/utils.cc b/src/utils.cc index 6d3becf..73b091c 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -2,15 +2,8 @@ // see LICENSE.txt #include "utils.hh" - -#include -#include -#include - #include - -#include "Adapter.hh" -#include "group_manager_api.h" +#include using namespace std; using namespace pEp; @@ -28,18 +21,6 @@ namespace pEp { return ret; } - bool is_c_str_empty(const char *str) - { - if (str == nullptr) { - return true; - } - string tmp{ str }; - if (tmp.empty()) { - return true; - } - return false; - } - string to_string(const ::pEp_identity *const ident, bool full, int indent) { stringstream builder; @@ -162,61 +143,5 @@ namespace pEp { 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 index 037a327..107668f 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -5,21 +5,18 @@ #define LIBPEPADAPTER_UTILS_HH #include "pEpLog.hh" -#include #include #include #include #include #include +#include namespace pEp { namespace Utils { // C-types to C++ types std::vector<::pEp_identity *> to_cxx(const ::identity_list &idl); - // C-types helpers - bool is_c_str_empty(const char *str); - // 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); @@ -27,25 +24,6 @@ namespace pEp { 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/test/framework/utils.hh b/test/framework/utils.hh index 3c45607..ea0084f 100644 --- a/test/framework/utils.hh +++ b/test/framework/utils.hh @@ -6,10 +6,9 @@ #include "../../src/pEpLog.hh" #include -#include -#include -#include #include +#include +#include // ------------------------------------------------------------------------------------------------ @@ -70,4 +69,6 @@ // ------------------------------------------------------------------------------------------------ +void sleep_millis(int milis); + #endif // LIBPEPADAPTER_TEST_UTILS_HH \ No newline at end of file diff --git a/test/test_listmanager_dummy.cc b/test/test_listmanager_dummy.cc index 5c14284..6985166 100644 --- a/test/test_listmanager_dummy.cc +++ b/test/test_listmanager_dummy.cc @@ -1,5 +1,6 @@ #include "../src/listmanager_dummy.hh" #include "../src/utils.hh" +#include "../src/std_utils.hh" #include "framework/utils.hh" #include #include @@ -63,7 +64,7 @@ void recreate_apply_and_verify_model(ListManagerDummy& lmd, const model_test_lmd lmd.delete_db(); } catch (const exception& e) { } - assert(!file_exists(model.db_path)); + assert(!path_exists(model.db_path)); apply_model(lmd, model); verify_model(lmd, model); } @@ -124,7 +125,7 @@ int main(int argc, char* argv[]) { logH2("Test re-open db"); model_test_lmd model = create_default_model(); - assert(file_exists(model.db_path)); + assert(path_exists(model.db_path)); ListManagerDummy lmd(model.db_path); verify_model(lmd, model); @@ -155,7 +156,7 @@ int main(int argc, char* argv[]) logH2("Test delete_db"); lmd.delete_db(); - assert(!file_exists(model.db_path)); + assert(!path_exists(model.db_path)); } logH1("Testing ERROR conditions"); diff --git a/test/test_pEpSQLite.cc b/test/test_pEpSQLite.cc index d95d006..a68ccde 100644 --- a/test/test_pEpSQLite.cc +++ b/test/test_pEpSQLite.cc @@ -1,6 +1,7 @@ #include "test_pEpSQLite.hh" #include "../src/pEpSQLite.hh" #include "../src/utils.hh" +#include "../src/std_utils.hh" #include "framework/utils.hh" #include @@ -47,7 +48,7 @@ namespace pEp { { // TESTLOG("called"); string path = fixture_db_filename_new(); - file_ensure_not_existing(path); + path_ensure_not_existing(path); return path; } @@ -55,7 +56,7 @@ namespace pEp { { // TESTLOG("called"); string path = "existing.db"; - file_ensure_not_existing(path); + path_ensure_not_existing(path); return path; } @@ -63,7 +64,7 @@ namespace pEp { { // TESTLOG("called"); string path = fixture_db_filename_corrupt(); - file_ensure_not_existing(path); + path_ensure_not_existing(path); ofstream db_corrupt = file_create(path); db_corrupt << "G4rbage" << endl; db_corrupt.close(); @@ -200,9 +201,9 @@ namespace pEp { { TESTLOG("called"); pEpSQLite db = fixture_instance_of_new(); - assert(!file_exists(fixture_db_filename_new())); + assert(!path_exists(fixture_db_filename_new())); db.create_or_open_db(); - assert(file_exists(fixture_db_filename_new())); + assert(path_exists(fixture_db_filename_new())); return db; } @@ -211,7 +212,7 @@ namespace pEp { { TESTLOG("called"); pEpSQLite db = fixture_instance_of_existing_and_verified(); - assert(file_exists(fixture_db_filename_existing_and_verified())); + assert(path_exists(fixture_db_filename_existing_and_verified())); db.create_or_open_db(); return db; } @@ -220,11 +221,11 @@ namespace pEp { pEpSQLite test_createopen_db_bad() { TESTLOG("called"); - assert(!file_exists(fixture_db_filename_bad())); + assert(!path_exists(fixture_db_filename_bad())); pEpSQLite db = fixture_instance_of_bad(); - assert(!file_exists(fixture_db_filename_bad())); + assert(!path_exists(fixture_db_filename_bad())); ASSERT_EXCEPT(db.create_or_open_db()); - assert(!file_exists(fixture_db_filename_bad())); + assert(!path_exists(fixture_db_filename_bad())); return db; } @@ -233,9 +234,9 @@ namespace pEp { { TESTLOG("called"); pEpSQLite db = fixture_instance_of_corrupt(); - assert(file_exists(fixture_db_filename_corrupt())); + assert(path_exists(fixture_db_filename_corrupt())); db.create_or_open_db(); - assert(file_exists(fixture_db_filename_corrupt())); + assert(path_exists(fixture_db_filename_corrupt())); return db; } @@ -450,7 +451,7 @@ namespace pEp { TESTLOG("called"); pEpSQLite db = fixture_instance_of_new(); ASSERT_EXCEPT(db.delete_db()); - assert(!file_exists(fixture_db_filename_new())); + assert(!path_exists(fixture_db_filename_new())); return db; } @@ -460,7 +461,7 @@ namespace pEp { TESTLOG("called"); pEpSQLite db = fixture_instance_of_existing_and_verified(); ASSERT_EXCEPT(db.delete_db()); - assert(!file_exists(fixture_db_filename_existing_and_verified())); + assert(!path_exists(fixture_db_filename_existing_and_verified())); return db; } @@ -470,7 +471,7 @@ namespace pEp { TESTLOG("called"); pEpSQLite db = fixture_db_open_after_close(); db.delete_db(); - assert(!file_exists(fixture_db_filename_new())); + assert(!path_exists(fixture_db_filename_new())); return db; } @@ -480,7 +481,7 @@ namespace pEp { TESTLOG("called"); pEpSQLite db = fixture_db_open_of_existing_and_verified(); db.delete_db(); - assert(!file_exists(fixture_db_filename_existing_and_verified())); + assert(!path_exists(fixture_db_filename_existing_and_verified())); return db; } @@ -490,7 +491,7 @@ namespace pEp { TESTLOG("called"); pEpSQLite db = fixture_db_open_of_corrupt(); db.delete_db(); - assert(!file_exists(fixture_db_filename_corrupt())); + assert(!path_exists(fixture_db_filename_corrupt())); return db; } @@ -500,7 +501,7 @@ namespace pEp { TESTLOG("called"); pEpSQLite db = fixture_db_open_of_bad(); ASSERT_EXCEPT(db.delete_db()); - assert(!file_exists(fixture_db_filename_bad())); + assert(!path_exists(fixture_db_filename_bad())); return db; } @@ -633,8 +634,8 @@ int main(int argc, char* argv[]) test_is_open_after_delete_on_open(); test_is_open_after_delete_on_closed(); - file_ensure_not_existing(fixture_db_filename_new()); - file_ensure_not_existing(fixture_db_filename_corrupt()); - file_ensure_not_existing(fixture_db_filename_existing_and_verified()); + path_ensure_not_existing(fixture_db_filename_new()); + path_ensure_not_existing(fixture_db_filename_corrupt()); + path_ensure_not_existing(fixture_db_filename_existing_and_verified()); return 0; } \ No newline at end of file