Browse Source

Split utils.cc and add std_utils.cc

LIB-11
heck 1 year ago
parent
commit
a015331c53
9 changed files with 272 additions and 129 deletions
  1. +1
    -0
      src/grp_driver_dummy.cc
  2. +186
    -0
      src/std_utils.cc
  3. +50
    -0
      src/std_utils.hh
  4. +4
    -4
      src/std_utils.hxx
  5. +1
    -76
      src/utils.cc
  6. +1
    -23
      src/utils.hh
  7. +4
    -3
      test/framework/utils.hh
  8. +4
    -3
      test/test_listmanager_dummy.cc
  9. +21
    -20
      test/test_pEpSQLite.cc

+ 1
- 0
src/grp_driver_dummy.cc View File

@ -1,6 +1,7 @@
#include "grp_driver_dummy.hh"
#include "pEpLog.hh"
#include "utils.hh"
#include "std_utils.hh"
#include <pEp/message_api.h>
#include "listmanager_dummy.hh"


+ 186
- 0
src/std_utils.cc View File

@ -0,0 +1,186 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "std_utils.hh"
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cerrno>
#include <dirent.h>
#include <sys/stat.h>
#include <algorithm>
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<string> 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<string> dir_list_all(const std::string &path, const bool incl_dot_and_dotdot)
{
vector<string> 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<string> dir_list_dirs(const string &dirname, const bool incl_dot_and_dotdot)
{
vector<string> 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<string> dir_list_files(const string &dirname)
{
vector<string> 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

+ 50
- 0
src/std_utils.hh View File

@ -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 <string>
#include <exception>
#include <vector>
namespace pEp {
namespace Utils {
// C-types helpers
bool is_c_str_empty(const char *str);
// C++/STL data types to string
template<typename T>
std::string to_string(const std::vector<T> &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<std::string> dir_list_all(
const std::string &path,
const bool incl_dot_and_dotdot = false);
std::vector<std::string> dir_list_dirs(
const std::string &dirname,
const bool incl_dot_and_dotdot = false);
std::vector<std::string> dir_list_files(const std::string &dirname);
} // namespace Utils
} // namespace pEp
#include "std_utils.hxx"
#endif // LIBPEPADAPTER_STD_UTILS_HH

src/utils.hxx → src/std_utils.hxx View File


+ 1
- 76
src/utils.cc View File

@ -2,15 +2,8 @@
// see LICENSE.txt
#include "utils.hh"
#include <iostream>
#include <iostream>
#include <fstream>
#include <pEp/identity_list.h>
#include "Adapter.hh"
#include "group_manager_api.h"
#include <iostream>
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

+ 1
- 23
src/utils.hh View File

@ -5,21 +5,18 @@
#define LIBPEPADAPTER_UTILS_HH
#include "pEpLog.hh"
#include <string>
#include <pEp/message.h>
#include <pEp/identity_list.h>
#include <pEp/group.h>
#include <exception>
#include <vector>
#include <string>
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<typename T>
std::string to_string(const std::vector<T> &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

+ 4
- 3
test/framework/utils.hh View File

@ -6,10 +6,9 @@
#include "../../src/pEpLog.hh"
#include <string>
#include <pEp/message.h>
#include <pEp/identity_list.h>
#include <pEp/group.h>
#include <exception>
#include <chrono>
#include <thread>
// ------------------------------------------------------------------------------------------------
@ -70,4 +69,6 @@
// ------------------------------------------------------------------------------------------------
void sleep_millis(int milis);
#endif // LIBPEPADAPTER_TEST_UTILS_HH

+ 4
- 3
test/test_listmanager_dummy.cc View File

@ -1,5 +1,6 @@
#include "../src/listmanager_dummy.hh"
#include "../src/utils.hh"
#include "../src/std_utils.hh"
#include "framework/utils.hh"
#include <iostream>
#include <exception>
@ -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");


+ 21
- 20
test/test_pEpSQLite.cc View File

@ -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 <fstream>
@ -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;
}

Loading…
Cancel
Save