std_utils: add random_string() random_char() / dir_ensure() / file_read() / dir_recreate()

LIB-11
heck 2021-06-08 20:04:29 +02:00
parent 9849a98124
commit 56f24c5e7b
2 changed files with 83 additions and 6 deletions

View File

@ -9,6 +9,8 @@
#include <dirent.h>
#include <sys/stat.h>
#include <algorithm>
#include <thread>
#include <random>
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<string> dir_list_all(const std::string &path, const bool incl_dot_and_dotdot)
{
vector<string> ret;
@ -166,7 +196,10 @@ namespace pEp {
{
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); }),
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<string> 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<short> dis(static_cast<short>(min), static_cast<short>(max));
return static_cast<unsigned char>(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

View File

@ -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);
std::ofstream file_create(const std::string &filename);
void path_ensure_not_existing(const std::string &path);
// file
std::ofstream file_create(const std::string &filename);
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<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);
//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