From 56f24c5e7babf69011271bc4518f3a2c29637c54 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 8 Jun 2021 20:04:29 +0200 Subject: [PATCH] std_utils: add random_string() random_char() / dir_ensure() / file_read() / dir_recreate() --- src/std_utils.cc | 65 +++++++++++++++++++++++++++++++++++++++++++++--- src/std_utils.hh | 22 ++++++++++++++-- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/std_utils.cc b/src/std_utils.cc index b78c385..89e308d 100644 --- a/src/std_utils.cc +++ b/src/std_utils.cc @@ -9,6 +9,8 @@ #include #include #include +#include +#include using namespace std; using namespace pEp; @@ -110,6 +112,18 @@ namespace pEp { return outfile; } + std::string file_read(const std::string &filename) + { + auto ss = ostringstream{}; + ifstream input_file(filename); + if (!input_file.is_open()) { + runtime_error e{ "Could not open the file: " + filename }; + exit(EXIT_FAILURE); + } + ss << input_file.rdbuf(); + return ss.str(); + } + void path_ensure_not_existing(const string &path) { while (path_exists(path)) { @@ -125,6 +139,22 @@ namespace pEp { } } + + void dir_ensure(const std::string &path) + { + if (!Utils::path_exists(path)) { + Utils::dir_create(path); + } + } + + void dir_recreate(const std::string &path) + { + if (Utils::path_exists(path)) { + Utils::path_delete_all(path); + } + Utils::dir_create(path); + } + vector dir_list_all(const std::string &path, const bool incl_dot_and_dotdot) { vector ret; @@ -166,7 +196,10 @@ namespace pEp { { 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); }), + remove_if( + ret.begin(), + ret.end(), + [dirname](string elem) { return !path_is_dir(dirname + "/" + elem); }), ret.end()); return ret; @@ -176,7 +209,10 @@ namespace pEp { { vector ret = dir_list_all(dirname); ret.erase( - remove_if(ret.begin(), ret.end(), [](string elem) { return !path_is_dir(elem); }), + remove_if( + ret.begin(), + ret.end(), + [dirname](string elem) { return path_is_dir(dirname + "/" + elem); }), ret.end()); return ret; } @@ -191,7 +227,7 @@ namespace pEp { return ret; } - string to_termcol(const Color& col) + string to_termcol(const Color &col) { switch (col) { case Color::RESET: @@ -216,5 +252,28 @@ namespace pEp { return "\033[0m"; } } + + void sleep_millis(int milis) { + std::chrono::milliseconds timespan(milis); + std::this_thread::sleep_for(timespan); + } + + unsigned char random_char(unsigned char min, unsigned char max) + { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dis(static_cast(min), static_cast(max)); + return static_cast(dis(gen)); + } + + std::string random_string(unsigned char min, unsigned char max, int len) + { + std::stringstream ret; + for (int i = 0; i < len; i++) { + ret << random_char(97, 122); + } + return ret.str(); + } + } // namespace Utils } // namespace pEp diff --git a/src/std_utils.hh b/src/std_utils.hh index 35775f0..91c3848 100644 --- a/src/std_utils.hh +++ b/src/std_utils.hh @@ -24,22 +24,32 @@ namespace pEp { std::string src = ""); void print_exception(const std::exception &e, int level = 0); - // file utils + // File utils + // ---------- + // path (file&dir) 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); + void path_ensure_not_existing(const std::string &path); + // file std::ofstream file_create(const std::string &filename); - void path_ensure_not_existing(const std::string &path); + std::string file_read(const std::string &filename); + // dir void dir_create(const std::string &dirname, const mode_t mode = 0775); + void dir_ensure(const std::string& path); + void dir_recreate(const std::string& path); + 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); //String formatting @@ -60,6 +70,14 @@ namespace pEp { std::string to_termcol(const Color& col); + + // Time + void sleep_millis(int milis); + + // Random + unsigned char random_char(unsigned char min, unsigned char max); + std::string random_string(unsigned char min, unsigned char max, int len); + } // namespace Utils } // namespace pEp