Initial Commit
This commit is contained in:
commit
c13016275b
41 changed files with 3596 additions and 0 deletions
30
headers/VUtils/Environment.h
Normal file
30
headers/VUtils/Environment.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace VUtils {
|
||||
class Environment {
|
||||
public:
|
||||
Environment();
|
||||
explicit Environment(const char *filename);
|
||||
void setFile(const char *filename);
|
||||
void setPrefix(std::string prefix);
|
||||
void loadFile();
|
||||
std::string& getEnv(const std::string& name, std::string def);
|
||||
bool hasEnv(const std::string& name);
|
||||
int getAsInt(const std::string& name, int def);
|
||||
double getAsDouble(const std::string& name, double def);
|
||||
bool getAsBool(const std::string& name);
|
||||
bool saveFile();
|
||||
void set(const char* name, const char *value);
|
||||
void setNumber(const char* name, double value);
|
||||
protected:
|
||||
std::unordered_map<std::string, std::string> m_env;
|
||||
std::string m_prefix = "VENV_";
|
||||
std::string m_filename;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
24
headers/VUtils/FileHandler.h
Normal file
24
headers/VUtils/FileHandler.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace VUtils {
|
||||
class FileHandler {
|
||||
public:
|
||||
static bool fileExists(const std::string& fileName);
|
||||
static bool isDirectory(const std::string& fileName);
|
||||
static std::string readFile(const std::string& fileName);
|
||||
static bool writeFile(const std::string& fileName, const std::string& content);
|
||||
static int getFileID(const std::string& fileName);
|
||||
static void closeFD(int fd);
|
||||
static std::string getExtension(const std::string& fileName);
|
||||
static long getFileSize(int fd);
|
||||
static std::string getFileName(const std::basic_string<char>& name);
|
||||
static bool createDirectoryIfNotExist(const std::basic_string<char>& fileName);
|
||||
static char * getHomeDirectory();
|
||||
static std::string getFromHomeDir(const std::basic_string<char>& path);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
36
headers/VUtils/Logging.h
Normal file
36
headers/VUtils/Logging.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <VUtils/FileHandler.h>
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DEBUGLOG(message, mod) { Logging::debugMod(message, mod); }
|
||||
#define DBG(...) { Logging::debug(true,__FILE__, __FUNCTION__, __VA_ARGS__); }
|
||||
#define DBGWN(...) { Logging::debug(false,__FILE__, __FUNCTION__, __VA_ARGS__); }
|
||||
#else
|
||||
#define DEBUGLOG(message, mod)
|
||||
#define DBG(...)
|
||||
#define DBGWN(...)
|
||||
#endif
|
||||
#define ERR(...) { Logging::error(true,__FILE__, __FUNCTION__, __VA_ARGS__); }
|
||||
#define ERRWN(...) { Logging::error(false,__FILE__, __FUNCTION__, __VA_ARGS__); }
|
||||
#define LOG(...) { Logging::log(true,__FILE__, __FUNCTION__, __VA_ARGS__ ); }
|
||||
#define LOGWN(...) { Logging::log(false,__FILE__, __FUNCTION__, __VA_ARGS__ ); }
|
||||
#define WARN(...) { Logging::warn(true, __FILE__, __FUNCTION__, __VA_ARGS__ ); }
|
||||
#define WARNWN(...) { Logging::warn(false, __FILE__, __FUNCTION__, __VA_ARGS__ ); }
|
||||
|
||||
class Logging {
|
||||
public:
|
||||
enum class PrintType {
|
||||
ERROR = 0,
|
||||
LOG = 1,
|
||||
WARN = 2,
|
||||
DBG = 3
|
||||
};
|
||||
static void debug(bool newLine, const char *file, const char *func, ...) noexcept;
|
||||
static void log(bool newLine,const char *file, const char *func, ...) noexcept;
|
||||
static void warn(bool newLine,const char *file, const char *func, ...) noexcept;
|
||||
static void error(bool newLine,const char *file, const char *func, ...) noexcept;
|
||||
static std::string format(bool newLine,PrintType type, const char *log, const char *file, const char *func);
|
||||
static std::string getPrefix(PrintType type, const char *module);
|
||||
};
|
||||
26
headers/VUtils/Pool.h
Normal file
26
headers/VUtils/Pool.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
|
||||
namespace VUtils {
|
||||
struct PoolWorker {
|
||||
virtual void run() = 0;
|
||||
};
|
||||
|
||||
class Pool {
|
||||
public:
|
||||
explicit Pool(const char *name);
|
||||
~Pool();
|
||||
void setThreadCount(int count);
|
||||
void create(PoolWorker& worker);
|
||||
void joinFirstThread();
|
||||
protected:
|
||||
bool m_isCreated = false;
|
||||
int m_count = 1;
|
||||
const char *m_name = "Pool";
|
||||
PoolWorker *m_worker{};
|
||||
std::thread *m_threads{};
|
||||
void execute();
|
||||
};
|
||||
}
|
||||
90
headers/VUtils/Storage/SafeMap.h
Normal file
90
headers/VUtils/Storage/SafeMap.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
#pragma once
|
||||
#include <utility>
|
||||
#include <atomic>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <optional>
|
||||
#include <cassert>
|
||||
|
||||
namespace VUtils {
|
||||
template<typename T, typename S>
|
||||
struct SafeMap {
|
||||
explicit SafeMap(size_t maxSize = -1UL) : m_maxSize(maxSize), m_end(false) {};
|
||||
|
||||
bool add(const T &t, S &x);
|
||||
|
||||
bool add(T &&t, S &&x);
|
||||
|
||||
void remove(T t);
|
||||
|
||||
void clear();
|
||||
|
||||
bool has(T t);
|
||||
|
||||
S &get(T t);
|
||||
|
||||
int size();
|
||||
|
||||
private:
|
||||
std::unordered_map <T, S> m_map{};
|
||||
std::mutex m_mtx{};
|
||||
std::condition_variable m_cvFull{};
|
||||
const size_t m_maxSize{};
|
||||
std::atomic<bool> m_end{};
|
||||
};
|
||||
|
||||
template<typename T, typename S>
|
||||
bool SafeMap<T, S>::add(const T &t, S &x) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_map.size() == m_maxSize && !m_end) {
|
||||
return false;
|
||||
}
|
||||
assert(!m_end);
|
||||
m_map.emplace(t, std::move(x));
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
bool SafeMap<T, S>::add(T &&t, S &&x) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_map.size() == m_maxSize && !m_end)
|
||||
return false;
|
||||
assert(!m_end);
|
||||
m_map.push(std::move(t));
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
void SafeMap<T, S>::clear() {
|
||||
std::unique_lock <std::mutex> lck(m_mtx);
|
||||
std::unordered_map <T, S> empty;
|
||||
std::swap(m_map, empty);
|
||||
m_cvFull.notify_all();
|
||||
}
|
||||
template<typename T, typename S>
|
||||
void SafeMap<T, S>::remove(T t) {
|
||||
std::unique_lock <std::mutex> lck(m_mtx);
|
||||
if (m_map.empty() || m_end) return;
|
||||
if (m_map.contains(t)) {
|
||||
m_map.erase(t);
|
||||
}
|
||||
m_cvFull.notify_one();
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
int SafeMap<T, S>::size() {
|
||||
return m_map.size();
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
bool SafeMap<T, S>::has(T t) {
|
||||
std::unique_lock <std::mutex> lck(m_mtx);
|
||||
return m_map.contains(t);
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
S &SafeMap<T, S>::get(T t) {
|
||||
std::unique_lock <std::mutex> lck(m_mtx);
|
||||
return m_map[t];
|
||||
}
|
||||
}
|
||||
109
headers/VUtils/Storage/SafeQueue.h
Normal file
109
headers/VUtils/Storage/SafeQueue.h
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <atomic>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <optional>
|
||||
#include <cassert>
|
||||
namespace VUtils {
|
||||
template<typename T>
|
||||
struct SafeQueue {
|
||||
explicit SafeQueue(size_t maxSize = -1UL) : m_maxSize(maxSize), m_end(false) {};
|
||||
|
||||
void push(const T &t);
|
||||
|
||||
void push(T &&t);
|
||||
|
||||
void close();
|
||||
|
||||
void clear();
|
||||
|
||||
std::optional<T> pop();
|
||||
|
||||
std::optional<T> waitAndPop();
|
||||
|
||||
bool isClosed();
|
||||
|
||||
int size();
|
||||
|
||||
private:
|
||||
std::queue<T> m_que;
|
||||
std::mutex m_mtx;
|
||||
std::condition_variable m_cvEmpty, m_cvFull;
|
||||
const size_t m_maxSize;
|
||||
std::atomic<bool> m_end;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void SafeQueue<T>::push(const T &t) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_que.size() == m_maxSize && !m_end) {
|
||||
// we dont wait! we return false because queue is full...
|
||||
m_cvFull.wait(lck);
|
||||
}
|
||||
assert(!m_end);
|
||||
m_que.push(std::move(t));
|
||||
m_cvEmpty.notify_one();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SafeQueue<T>::push(T &&t) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_que.size() == m_maxSize && !m_end)
|
||||
m_cvFull.wait(lck);
|
||||
assert(!m_end);
|
||||
m_que.push(std::move(t));
|
||||
m_cvEmpty.notify_one();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SafeQueue<T>::close() {
|
||||
m_end = true;
|
||||
std::lock_guard<std::mutex> lck(m_mtx);
|
||||
m_cvEmpty.notify_all();
|
||||
m_cvFull.notify_all();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> SafeQueue<T>::pop() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
if (m_que.empty() || m_end) return {};
|
||||
T t = std::move(m_que.front());
|
||||
m_que.pop();
|
||||
m_cvFull.notify_one();
|
||||
return t;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> SafeQueue<T>::waitAndPop() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_que.empty() && !m_end)
|
||||
m_cvEmpty.wait(lck);
|
||||
if (m_que.empty() || m_end) return {};
|
||||
T t = std::move(m_que.front());
|
||||
m_que.pop();
|
||||
m_cvFull.notify_one();
|
||||
return t;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SafeQueue<T>::clear() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
std::queue<T> empty;
|
||||
std::swap(m_que, empty);
|
||||
m_cvEmpty.notify_all();
|
||||
m_cvFull.notify_all();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SafeQueue<T>::isClosed() {
|
||||
return m_end;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int SafeQueue<T>::size() {
|
||||
return m_que.size();
|
||||
}
|
||||
}
|
||||
|
||||
31
headers/VUtils/StringUtils.h
Normal file
31
headers/VUtils/StringUtils.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace VUtils {
|
||||
class StringUtils {
|
||||
public:
|
||||
static void leftTrim(std::string &s);
|
||||
|
||||
static void rightTrim(std::string &s);
|
||||
|
||||
static void trim(std::string &s);
|
||||
|
||||
static std::string leftTrimCopy(std::string s);
|
||||
|
||||
static std::string rightTrimCopy(std::string s);
|
||||
|
||||
static std::string trimCopy(std::string s);
|
||||
|
||||
static std::vector<std::string> split(const std::string &s, const std::string &delimiter);
|
||||
|
||||
static std::string urlDecode(const std::string &url);
|
||||
|
||||
static std::string urlEncode(const std::string &url);
|
||||
|
||||
static std::string join(const std::vector<std::string>& vector, const std::string& delimiter);
|
||||
|
||||
static bool hasNullByte(int size, const char string[1024]);
|
||||
};
|
||||
}// namespace VUtils
|
||||
Loading…
Add table
Add a link
Reference in a new issue