Initial Commit
This commit is contained in:
commit
c13016275b
41 changed files with 3596 additions and 0 deletions
123
src/VUtils/Environment.cpp
Normal file
123
src/VUtils/Environment.cpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#include <VUtils/Environment.h>
|
||||
#include <VUtils/FileHandler.h>
|
||||
#include <VUtils/StringUtils.h>
|
||||
#include <VUtils/Logging.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace VUtils {
|
||||
Environment::Environment(const char *filename) : m_filename(filename) {
|
||||
m_env[""] = "";
|
||||
}
|
||||
Environment::Environment() {
|
||||
m_env[""] = "";
|
||||
}
|
||||
|
||||
void Environment::loadFile() {
|
||||
DBG("Load ENV-File: %s", m_filename.c_str());
|
||||
if (!FileHandler::fileExists(m_filename)) {
|
||||
WARN("Cannot Load Env-File %s! File not found", m_filename.c_str());
|
||||
return;
|
||||
}
|
||||
auto content = StringUtils::trimCopy(FileHandler::readFile(m_filename));
|
||||
auto lines = StringUtils::split(content, "\n");
|
||||
for (auto &line : lines) {
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
auto split = StringUtils::split(line, "=");
|
||||
if (split.size() >= 2) {
|
||||
m_env[StringUtils::trimCopy(split[0])] = StringUtils::trimCopy(split[1]);
|
||||
} else {
|
||||
m_env[StringUtils::trimCopy(split[0])] = "true";
|
||||
}
|
||||
}
|
||||
DBG("Found %d Elements for Environment File %s", m_env.size(), m_filename.c_str());
|
||||
}
|
||||
|
||||
std::string &Environment::getEnv(const std::string &name, std::string def) {
|
||||
if (m_env.contains(name)) {
|
||||
return m_env[name];
|
||||
}
|
||||
auto *val = std::getenv(std::string(m_prefix + name).c_str());
|
||||
if (val) {
|
||||
m_env[name] = std::string(val);
|
||||
return m_env[name];
|
||||
}
|
||||
if (def.empty()) {
|
||||
return m_env[""];
|
||||
}
|
||||
m_env[name] = std::move(def);
|
||||
return m_env[name];
|
||||
}
|
||||
|
||||
bool Environment::hasEnv(const std::string &name) {
|
||||
return m_env.contains(name) || std::getenv(name.c_str()) != nullptr;
|
||||
}
|
||||
|
||||
int Environment::getAsInt(const std::string &name, int def) {
|
||||
return (int) getAsDouble(name, def);
|
||||
}
|
||||
|
||||
double Environment::getAsDouble(const std::string &name, double def) {
|
||||
char *end;
|
||||
auto *v = getEnv(name, "").c_str();
|
||||
double val = (int) std::strtod(v, &end);
|
||||
if (end == v) {
|
||||
setNumber(name.c_str(), def);
|
||||
return def;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
bool Environment::getAsBool(const std::string &name) {
|
||||
return getEnv(name, "false") == "true";
|
||||
}
|
||||
|
||||
void Environment::setPrefix(std::string prefix) {
|
||||
m_prefix = std::move(prefix);
|
||||
}
|
||||
|
||||
bool Environment::saveFile() {
|
||||
// override file if not exists!
|
||||
std::stringstream stream{};
|
||||
stream << std::setprecision(4);
|
||||
for (auto &element : m_env) {
|
||||
if (element.first.empty())
|
||||
continue;
|
||||
stream << element.first << "=" << element.second << "\n";
|
||||
}
|
||||
if (!FileHandler::createDirectoryIfNotExist(m_filename)) {
|
||||
ERR("Directory not exists or cannot create for: %s", m_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
if (!FileHandler::writeFile(m_filename, stream.str())) {
|
||||
WARN("Cannot Save Env-File %s! Write failed", m_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
DBG("Saved file to: %s", m_filename.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void Environment::setFile(const char *filename) {
|
||||
m_filename = filename;
|
||||
}
|
||||
|
||||
void Environment::set(const char *name, const char *value) {
|
||||
m_env[name] = value;
|
||||
}
|
||||
|
||||
// Small hack that set numbers to max precision
|
||||
void Environment::setNumber(const char *name, double value) {
|
||||
int newValue = (int) value;
|
||||
std::ostringstream out;
|
||||
|
||||
if (value != newValue) {
|
||||
out.precision(4);
|
||||
} else {
|
||||
out.precision(0);
|
||||
}
|
||||
out << std::fixed << value;
|
||||
m_env[name] = out.str();
|
||||
}
|
||||
}
|
||||
91
src/VUtils/FileHandler.cpp
Normal file
91
src/VUtils/FileHandler.cpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <VUtils/FileHandler.h>
|
||||
#include <VUtils/Logging.h>
|
||||
#include <pwd.h>
|
||||
|
||||
namespace VUtils {
|
||||
|
||||
bool FileHandler::fileExists(const std::string &fileName) {
|
||||
return std::filesystem::exists(fileName);
|
||||
}
|
||||
|
||||
std::string FileHandler::readFile(const std::string &fileName) {
|
||||
std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||
std::ifstream::pos_type fileSize = ifs.tellg();
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<char> bytes(fileSize);
|
||||
ifs.read(bytes.data(), fileSize);
|
||||
|
||||
return std::string(bytes.data(), fileSize);
|
||||
}
|
||||
|
||||
bool FileHandler::writeFile(const std::string &fileName, const std::string &content) {
|
||||
try {
|
||||
std::ofstream ofs(fileName);
|
||||
ofs << content;
|
||||
ofs.close();
|
||||
} catch (std::exception &e) {
|
||||
ERR("Save Failed: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool FileHandler::isDirectory(const std::string &fileName) {
|
||||
return std::filesystem::is_directory(fileName);
|
||||
}
|
||||
|
||||
std::string FileHandler::getExtension(const std::string &fileName) {
|
||||
auto ext = std::filesystem::path(fileName).extension().string();
|
||||
if (ext.empty()) {
|
||||
return ext;
|
||||
}
|
||||
// remove first dot!
|
||||
return ext.erase(0, 1);
|
||||
}
|
||||
|
||||
int FileHandler::getFileID(const std::string &fileName) {
|
||||
return open(fileName.c_str(), O_RDONLY);
|
||||
}
|
||||
|
||||
long FileHandler::getFileSize(int fd) {
|
||||
struct stat stat_buf;
|
||||
int rc = fstat(fd, &stat_buf);
|
||||
return rc == 0 ? stat_buf.st_size : -1;
|
||||
}
|
||||
|
||||
void FileHandler::closeFD(int fd) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
std::string FileHandler::getFileName(const std::basic_string<char> &name) {
|
||||
auto p = std::filesystem::path(name);
|
||||
return p.filename().replace_extension("");
|
||||
}
|
||||
|
||||
bool FileHandler::createDirectoryIfNotExist(const std::basic_string<char> &fileName) {
|
||||
auto p = std::filesystem::path(fileName);
|
||||
if (!p.has_parent_path()) {
|
||||
return false;
|
||||
}
|
||||
if (FileHandler::isDirectory(p.parent_path())) {
|
||||
return true;
|
||||
}
|
||||
return std::filesystem::create_directories(p.parent_path());
|
||||
}
|
||||
char *FileHandler::getHomeDirectory() {
|
||||
char *homedir;
|
||||
if ((homedir = getenv("HOME")) == nullptr) {
|
||||
homedir = getpwuid(getuid())->pw_dir;
|
||||
}
|
||||
return homedir;
|
||||
}
|
||||
std::string FileHandler::getFromHomeDir(const std::basic_string<char> &path) {
|
||||
return std::string(getHomeDirectory() + path);
|
||||
}
|
||||
}
|
||||
68
src/VUtils/Logging.cpp
Normal file
68
src/VUtils/Logging.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include <VUtils/Logging.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
|
||||
#ifndef ERROR_COLOR
|
||||
#define ERROR_COLOR 31
|
||||
#endif
|
||||
#ifndef INFO_COLOR
|
||||
#define INFO_COLOR 34
|
||||
#endif
|
||||
#ifndef DEBUG_COLOR
|
||||
#define DEBUG_COLOR 32
|
||||
#endif
|
||||
#ifndef WARN_COLOR
|
||||
#define WARN_COLOR 33
|
||||
#endif
|
||||
|
||||
void Logging::debug(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::DBG, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logging::log(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::LOG, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logging::error(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::ERROR, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logging::warn(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::WARN, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
std::string Logging::format(bool newLine, PrintType type, const char *log, const char *file, const char *func) {
|
||||
auto pre = getPrefix(type, std::string(VUtils::FileHandler::getFileName(file) + "::" + func).c_str());
|
||||
pre += log;
|
||||
pre += "\033[0m";
|
||||
if (newLine) pre += "\n";
|
||||
return pre;
|
||||
}
|
||||
std::string Logging::getPrefix(PrintType type, const char *s) {
|
||||
switch (type) {
|
||||
case PrintType::ERROR:
|
||||
return "\033[1;" + std::to_string(ERROR_COLOR) + "m[ERROR][" + std::string(s) + "] >> ";
|
||||
case PrintType::DBG:
|
||||
return "\033[1;" + std::to_string(DEBUG_COLOR) + "m[DEBUG][" + std::string(s) + "] >> ";
|
||||
case PrintType::WARN:
|
||||
return "\033[1;" + std::to_string(WARN_COLOR) + "m[WARN][" + std::string(s) + "] >> ";
|
||||
default:
|
||||
return "\033[1;" + std::to_string(INFO_COLOR) + "m[LOG][" + std::string(s) + "] >> ";
|
||||
}
|
||||
}
|
||||
39
src/VUtils/Pool.cpp
Normal file
39
src/VUtils/Pool.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <VUtils/Pool.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
namespace VUtils {
|
||||
Pool::Pool(const char *name) : m_name(name) {
|
||||
}
|
||||
|
||||
Pool::~Pool() {
|
||||
delete[] m_threads;
|
||||
}
|
||||
|
||||
void Pool::setThreadCount(int count) {
|
||||
if (m_isCreated) return;
|
||||
m_count = count == -1 ? std::thread::hardware_concurrency() : count;
|
||||
}
|
||||
|
||||
void Pool::joinFirstThread() {
|
||||
if (!m_isCreated) return;
|
||||
if (m_threads[0].joinable()) {
|
||||
m_threads[0].join();
|
||||
}
|
||||
}
|
||||
|
||||
void Pool::create(PoolWorker &worker) {
|
||||
if (m_isCreated) return;
|
||||
m_isCreated = true;
|
||||
m_worker = &worker;
|
||||
m_threads = new std::thread[m_count];
|
||||
for (int i = 0; i < m_count; ++i) {
|
||||
m_threads[i] = std::thread(&Pool::execute, this);
|
||||
}
|
||||
DBG("Created %d Threads for ThreadPool %s", m_count, m_name)
|
||||
}
|
||||
|
||||
void Pool::execute() {
|
||||
m_worker->run();
|
||||
}
|
||||
}
|
||||
|
||||
106
src/VUtils/StringUtils.cpp
Normal file
106
src/VUtils/StringUtils.cpp
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#include <VUtils/StringUtils.h>
|
||||
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace VUtils {
|
||||
void StringUtils::leftTrim(std::string &s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace))));
|
||||
}
|
||||
|
||||
void StringUtils::rightTrim(std::string &s) {
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace)))
|
||||
.base(),
|
||||
s.end());
|
||||
}
|
||||
|
||||
void StringUtils::trim(std::string &s) {
|
||||
leftTrim(s);
|
||||
rightTrim(s);
|
||||
}
|
||||
|
||||
std::string StringUtils::leftTrimCopy(std::string s) {
|
||||
leftTrim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string StringUtils::rightTrimCopy(std::string s) {
|
||||
rightTrim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string StringUtils::trimCopy(std::string s) {
|
||||
trim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> StringUtils::split(const std::string &s, const std::string &delimiter) {
|
||||
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
|
||||
std::string token;
|
||||
std::vector<std::string> res;
|
||||
|
||||
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
|
||||
token = s.substr(pos_start, pos_end - pos_start);
|
||||
pos_start = pos_end + delim_len;
|
||||
res.push_back(token);
|
||||
}
|
||||
|
||||
res.push_back(s.substr(pos_start));
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string StringUtils::urlDecode(const std::string &val) {
|
||||
std::string ret;
|
||||
char ch;
|
||||
int i, ii;
|
||||
for (i = 0; i < val.length(); i++) {
|
||||
if (int(val[ i ]) == 37) {
|
||||
sscanf(val.substr(i + 1, 2).c_str(), "%x", &ii);
|
||||
ch = static_cast<char>(ii);
|
||||
ret += ch;
|
||||
i = i + 2;
|
||||
} else {
|
||||
ret += val[ i ];
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
std::string StringUtils::urlEncode(const std::string &value) {
|
||||
std::ostringstream escaped;
|
||||
escaped.fill('0');
|
||||
escaped << std::hex;
|
||||
|
||||
for (char c : value) {
|
||||
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||
escaped << c;
|
||||
continue;
|
||||
}
|
||||
escaped << std::uppercase;
|
||||
escaped << '%' << std::setw(2) << int((unsigned char) c);
|
||||
escaped << std::nouppercase;
|
||||
}
|
||||
|
||||
return escaped.str();
|
||||
}
|
||||
std::string StringUtils::join(const std::vector<std::string> &vector, const std::string &delimiter) {
|
||||
std::stringstream string;
|
||||
for (int i = 0; i < vector.size(); ++i) {
|
||||
if (i != 0)
|
||||
string << delimiter;
|
||||
string << vector[ i ];
|
||||
|
||||
}
|
||||
return string.str();
|
||||
}
|
||||
bool StringUtils::hasNullByte(int size, const char *string) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (string[ i ] == '\0') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}// namespace VUtils
|
||||
Loading…
Add table
Add a link
Reference in a new issue