#include <regex>
#include <vector>
#include <fstream>
#include <iostream>
#include <streambuf>
#include <filesystem>
#include <unordered_map>
#ifdef _MSC_VER
#include <stdlib.h>
#define MAX_DIR_LEN _MAX_PATH
#else
#include <limits.h>
#define MAX_DIR_LEN PATH_MAX
#endif
#define __DEF_APP_DIR__ char APP_DIR[MAX_DIR_LEN] = { 0 };
#define __DEF_APP_DIR_INIT__(X) {char*P=NULL;strncpy(APP_DIR,X,sizeof(APP_DIR));P=APP_DIR;while(*P!='\0'){if(*P=='\\')*P='\\';P++;}P=strrchr(APP_DIR,'\\');if(P!=NULL)*P='\0';}
#define __EXT_APP_DIR__ extern char APP_DIR[MAX_DIR_LEN];
__EXT_APP_DIR__
#define SINGLETON_INSTANCE(C) static C * GetInstance(){static C instance##C;return &instance##C;}
#define FILE_READER(F, M) std::string((std::istreambuf_iterator<char>(std::ifstream(F, M | std::ios::in).rdbuf())), std::istreambuf_iterator<char>())
#define FILE_WRITER(D, S, F, M) std::ofstream(F, M | std::ios::out).write((D), (S))
__inline static bool string_regex_valid(const std::string& data, const std::string& pattern)
{
return std::regex_match(data, std::regex(pattern));
}
__inline static size_t string_regex_replace_all(std::string& result, std::string& data, const std::string& replace, const std::string& pattern, std::regex_constants::match_flag_type matchflagtype = std::regex_constants::match_default)
{
try
{
data = std::regex_replace(data, std::regex(pattern), replace, matchflagtype);
}
catch (const std::exception& e)
{
result = e.what();
}
return data.length();
}
__inline static size_t string_regex_find(std::string& result, std::vector<std::vector<std::string>>& svv, std::string& data, const std::string& pattern)
{
std::smatch smatch;
std::string::const_iterator ite = data.end();
std::string::const_iterator itb = data.begin();
try
{
//如果匹配成功
while (std::regex_search(itb, ite, smatch, std::regex(pattern)))
{
bool empty = true;
for (auto it : smatch)
{
if (empty)
{
empty = false;
svv.push_back({});
}
else
{
svv.rbegin()->emplace_back(std::string(it.first, it.second));
}
}
if (!empty)
{
itb = smatch.suffix().first;
}
}
}
catch (const std::exception& e)
{
result = e.what();
}
return svv.size();
}
__inline static std::string::size_type string_reader(std::string& result, const std::string& strData,
const std::string& strStart, const std::string& strFinal, std::string::size_type stPos = 0,
bool bTakeStart = false, bool bTakeFinal = false)
{
std::string::size_type stRetPos = std::string::npos;
std::string::size_type stStartPos = stPos;
std::string::size_type stFinalPos = std::string::npos;
stStartPos = strData.find(strStart, stStartPos);
if (stStartPos != std::string::npos)
{
stRetPos = stFinalPos = strData.find(strFinal, stStartPos + strStart.length());
if (stFinalPos != std::string::npos)
{
if (!bTakeStart)
{
stStartPos += strStart.length();
}
if (bTakeFinal)
{
stFinalPos += strFinal.length();
}
result = strData.substr(stStartPos, stFinalPos - stStartPos);
}
}
return stRetPos;
}
__inline static std::string string_reader(const std::string& strData,
const std::string& strStart, const std::string& strFinal,
bool bTakeStart = false, bool bTakeFinal = false)
{
std::string strRet = ("");
std::string::size_type stStartPos = std::string::npos;
std::string::size_type stFinalPos = std::string::npos;
stStartPos = strData.find(strStart);
if (stStartPos != std::string::npos)
{
stFinalPos = strData.find(strFinal, stStartPos + strStart.length());
if (stFinalPos != std::string::npos)
{
if (!bTakeStart)
{
stStartPos += strStart.length();
}
if (bTakeFinal)
{
stFinalPos += strFinal.length();
}
strRet = strData.substr(stStartPos, stFinalPos - stStartPos);
}
}
return strRet;
}
__inline static
const std::string& string_replace_all_after_pos(std::string& strData, const std::string& strDst, const std::string& strSrc, std::string::size_type stPos = 0)
{
while ((stPos = strData.find(strSrc, stPos)) != std::string::npos)
{
strData.replace(stPos, strSrc.length(), strDst);
stPos += strDst.length();
}
return strData;
}
__inline static
const std::string& string_replace_all(std::string& strData, const std::string& strDst, const std::string& strSrc)
{
size_t pos = 0;
while ((pos = strData.find(strSrc)) != std::string::npos)
{
strData.replace(pos, strSrc.length(), strDst);
}
return strData;
}
__inline static size_t string_split_to_vector(std::vector<std::string>& sv, const std::string& strData, const std::string& strSplitter, std::string::size_type stPos = 0)
{
std::string strTmp = ("");
std::string::size_type stIdx = 0;
std::string::size_type stSize = strData.length();
while ((stPos = strData.find(strSplitter, stIdx)) != std::string::npos)
{
strTmp = strData.substr(stIdx, stPos - stIdx);
if (!strTmp.length())
{
strTmp = ("");
}
sv.push_back(strTmp);
stIdx = stPos + strSplitter.length();
}
if (stIdx < stSize)
{
strTmp = strData.substr(stIdx, stSize - stIdx);
if (!strTmp.length())
{
strTmp = ("");
}
sv.push_back(strTmp);
}
return sv.size();
}
__inline static
int enum_file(std::unordered_map<std::string, std::string>& file_list, const std::string& root)
{
std::error_code ec;
for (auto& item : std::filesystem::recursive_directory_iterator(root, std::filesystem::directory_options::skip_permission_denied | std::filesystem::directory_options::follow_directory_symlink, ec))
{
try
{
if (item.is_regular_file())
{
std::string _p = item.path().string();
file_list.insert(std::unordered_map<std::string, std::string>::value_type(_p, _p));
}
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
return 0;
}
class CRandomString {
public:
CRandomString() { srand(time(nullptr)); }
virtual ~CRandomString() {}
private:
char _digit()
{
return static_cast<char>('0' + rand() % ('9' - '0' + 1));
}
char _lower()
{
return static_cast<char>('a' + rand() % ('z' - 'a' + 1));
}
char _upper()
{
return static_cast<char>('A' + rand() % ('Z' - 'A' + 1));
}
public:
std::string make_random_string(int length) {
std::string str = ("");
while (length-- != 0)
{
switch (rand() % 3)
{
case 0:str.push_back(_digit()); break;
case 1:str.push_back(_lower()); break;
default:str.push_back(_upper()); break;
}
}
return (str);
}
public:
SINGLETON_INSTANCE(CRandomString)
};