первый
This commit is contained in:
244
lib/stdTools.h
Normal file
244
lib/stdTools.h
Normal file
@ -0,0 +1,244 @@
|
||||
//---------------------------------------------------------------------------
|
||||
#ifndef stdToolsH
|
||||
#define stdToolsH
|
||||
//---------------------------------------------------------------------------
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <cerrno>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
#if defined(WIN32) || defined(_WINDOWS) || defined(_BORLAND) || defined(__BORLANDC__)
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#include <iconv.h>
|
||||
#endif
|
||||
//---------------------------------------------------------------------------
|
||||
/*class converter
|
||||
{
|
||||
public:
|
||||
converter(const std::string& in_encode, const std::string& out_encode, bool ignore_error = false, size_t buf_size = 1024) : ignore_error_(ignore_error), buf_size_(buf_size)
|
||||
{
|
||||
if (buf_size == 0)
|
||||
{
|
||||
throw std::runtime_error("buffer size must be greater than zero");
|
||||
}
|
||||
|
||||
iconv_t conv = ::iconv_open(out_encode.c_str(), in_encode.c_str());
|
||||
if (conv == (iconv_t)-1)
|
||||
{
|
||||
if (errno == EINVAL)
|
||||
throw std::runtime_error("not supported from " + in_encode + " to " + out_encode);
|
||||
else
|
||||
throw std::runtime_error("unknown error");
|
||||
}
|
||||
iconv_ = conv;
|
||||
}
|
||||
|
||||
~converter()
|
||||
{
|
||||
iconv_close(iconv_);
|
||||
}
|
||||
|
||||
void convert(const std::string& input, std::string& output) const
|
||||
{
|
||||
// copy the string to a buffer as iconv function requires a non-const char pointer
|
||||
std::vector<char> in_buf(input.begin(), input.end());
|
||||
char* src_ptr = &in_buf[0];
|
||||
size_t src_size = input.size();
|
||||
|
||||
std::vector<char> buf(buf_size_);
|
||||
std::string dst;
|
||||
while (0 < src_size)
|
||||
{
|
||||
char* dst_ptr = &buf[0];
|
||||
size_t dst_size = buf.size();
|
||||
size_t res = ::iconv(iconv_, &src_ptr, &src_size, &dst_ptr, &dst_size);
|
||||
if (res == (size_t)-1)
|
||||
{
|
||||
if (errno == E2BIG)
|
||||
{
|
||||
// ignore this error
|
||||
}
|
||||
else if (ignore_error_)
|
||||
{
|
||||
// skip character
|
||||
++src_ptr;
|
||||
--src_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
check_convert_error();
|
||||
}
|
||||
}
|
||||
dst.append(&buf[0], buf.size() - dst_size);
|
||||
}
|
||||
dst.swap(output);
|
||||
}
|
||||
|
||||
private:
|
||||
void check_convert_error() const
|
||||
{
|
||||
switch (errno)
|
||||
{
|
||||
case EILSEQ:
|
||||
throw std::runtime_error("invalid multibyte chars");
|
||||
case EINVAL:
|
||||
throw std::runtime_error("invalid multibyte chars");
|
||||
default:
|
||||
throw std::runtime_error("unknown error");
|
||||
}
|
||||
}
|
||||
|
||||
iconv_t iconv_;
|
||||
bool ignore_error_;
|
||||
const size_t buf_size_;
|
||||
};*/
|
||||
//---------------------------------------------------------------------------
|
||||
//template <typename T>
|
||||
//std::string toStdStr(T val);
|
||||
|
||||
namespace Utility
|
||||
{
|
||||
char separator();
|
||||
std::string escape_json(const std::string& input);
|
||||
|
||||
void strcat2( char * destptr, const char * srcptr );
|
||||
int strLen2(char * str);
|
||||
|
||||
std::string sp3();
|
||||
std::string base64_encode(const std::string &in);
|
||||
std::string base64_decode(const std::string &in);
|
||||
|
||||
bool seekToLEBOM(FILE* fp);
|
||||
std::wstring readUTF16LEString(FILE* fp);
|
||||
std::string readString(FILE* fp,char ch);
|
||||
int readFile(FILE* fp,char* data,int len);
|
||||
std::string readFileToString(std::string fName);
|
||||
bool deleteFile(std::string fileName);
|
||||
bool deleteOldFiles(std::string path,int days);
|
||||
void getFiles(std::list<std::string>& fileList, std::string directory);
|
||||
long getFileDateModiff(std::string file);
|
||||
|
||||
bool logrotate(std::string fileName, std::string thread, std::string level, std::string data, bool cout=false, int size=10);
|
||||
bool logrotateW(std::wstring fileName, std::wstring data);
|
||||
bool logrotateUTF8(std::wstring fileName, std::wstring data);
|
||||
|
||||
bool fileExists(std::string name);
|
||||
bool dirExists(std::string path);
|
||||
bool createFolder(std::string directory,mode_t mode=0776); //Создать директорию
|
||||
long getFileSize(std::string filename);
|
||||
|
||||
|
||||
//std::string FloatToStdStr(float val);
|
||||
std::string UIntToStdStr(unsigned int val);
|
||||
std::string IntToStdStr(int val); //Вырезает из строки не цифры
|
||||
std::string FloatToStdStr(double val, int digits = -1,char sep = 0);
|
||||
std::wstring FloatToStdWStr(double val,int digits = -1);
|
||||
template <typename T>
|
||||
std::wstring toStdWStr(T val);
|
||||
std::wstring IntToStdWStr(int val);
|
||||
|
||||
template <typename T>
|
||||
std::string toStringHex(T val);
|
||||
std::string toStringHex2(int val);
|
||||
std::string toHexString(unsigned char* mas,int len);
|
||||
bool hexStringToArray(std::string val, char* mas);
|
||||
unsigned int hexStdStrToUInt(std::string& str);
|
||||
unsigned int hexStringToInt(std::string val, bool ord = true);
|
||||
uint16_t hexString2ToUint(std::string &str, int pos, bool cut = false);
|
||||
uint32_t hexString4ToUint(std::string &str, int pos, bool cut = false);
|
||||
uint64_t hexString6ToUint(std::string &str, int pos, bool cut = false);
|
||||
|
||||
template<typename T>
|
||||
T fromString(const std::string& s);
|
||||
int StdStrToInt(std::string& str, bool cutInt = false, int def = 0);
|
||||
int StdStrToUInt(std::string& str, bool cutInt = false);
|
||||
float StdStrToFloat(std::string& str);
|
||||
double StdStrToDouble(std::string& str,double defval = 0);
|
||||
double StdWStrToDouble(std::wstring str, double defval = 0);
|
||||
|
||||
template<typename T>
|
||||
T fromWString(const std::wstring& s);
|
||||
int StdWStrToInt(const std::wstring& str, bool cutInt = false);
|
||||
int StdWStrToUInt(const std::wstring& str, bool cutInt = false);
|
||||
|
||||
//Конвертация строк
|
||||
std::string CodeBSD(std::string str); ///<Закодировать число в двоично десятичный формат
|
||||
std::string CodeBSD(int val,int len = 0); ///<Закодировать число в двоично десятичный формат
|
||||
int BSDToInt(char chr); //Двоично десятичный символ в int
|
||||
int BSDToInt(std::string str, size_t start = 0, size_t len = 0); //Двоично десятичную строку в int
|
||||
std::string DecodeBSD(char chr); //1 символ в BSD число
|
||||
std::string DecodeBSD(std::string str, size_t start = 0, size_t len = 0); //Раскодироваьт из двоично десятичного формата
|
||||
|
||||
std::string lowerCaseENG(std::string str); //Только для англ языка
|
||||
//std::wstring StringToWString(const std::string& in, const std::locale &loc = std::locale("")); // = std::locale("") //Конвертация из одно байтного в мульти и обратно
|
||||
std::wstring StringToWString(const std::string& in, const std::locale &loc);
|
||||
//std::string WStringToString(const std::wstring &s, const std::locale &loc = std::locale(""), char default_char = '?'); // = std::locale("")
|
||||
std::string WStringToString(const std::wstring &wstr, const std::locale &loc, char default_char = '?'); // = std::locale("")
|
||||
//std::wstring s2ws(const std::string& s); //В c++ buildere это работало а StringToWString нет
|
||||
std::string ws2s(const std::wstring& s);
|
||||
//char * CodeCharSet(char* in, const char* in_set, const char* out_set);
|
||||
std::string iConvert(const std::string& instr,const char* fromCode,const char* toCode); //Через iconf
|
||||
std::string convUTF16ToUTF8(const std::wstring& widestring); //Конвертация из UTF16LE в UTF8
|
||||
std::wstring convUTF8ToUTF16(const std::string& str); //Конвертация из UTF8 в UTF16LE
|
||||
std::string UnicodeToDOS886(std::wstring str); //Конвертим из Unicode в DOS 866 кодировку
|
||||
std::wstring DOS886ToUnicode(std::string str); //Конвертим из DOS 866 в Unicode кодировку
|
||||
|
||||
void printToPos(std::wstring& line, std::wstring data, char align); //Запись в право, лево, центр строки line
|
||||
void replaseChars(std::string& str,char oldCh,char newCh); //Заменить 1 символ другим
|
||||
// std::string replace(std::string text, std::string s, std::string d);
|
||||
// std::string getExePath();
|
||||
|
||||
std::wstring UpperCase(const std::wstring& str); ///<К верхнему регистру строку (долго работает)
|
||||
|
||||
std::string BeforeLast(std::string str,char ch);
|
||||
std::string BeforeFirst(std::string str,const unsigned char ch);
|
||||
std::string BeforeFirst(std::string str,const char ch);
|
||||
std::wstring BeforeWLast(std::wstring str,wchar_t ch);
|
||||
std::wstring BeforeWFirst(std::wstring str,const wchar_t ch);
|
||||
std::string AfterFirst(std::string str,const char ch);
|
||||
std::wstring AfterWFirst(std::wstring str,const wchar_t ch);
|
||||
std::string AfterLast(std::string str,const char ch);
|
||||
std::string CutAfterFirst(std::string& str,std::string br);
|
||||
std::string CutAfterLast(std::string& str,const unsigned char ch); //Если не найдёт нужный символ то вернёт всю строку
|
||||
std::string CutBeforeFirst(std::string& str,const char ch);
|
||||
std::string CutBeforeFirst(std::string& str,const std::string br);
|
||||
std::wstring CutBeforeWFirst(std::wstring& str,const wchar_t ch);
|
||||
std::string replaceStrings(std::string sors,std::string find,std::string repl);
|
||||
std::wstring replaceStrings(std::wstring sors,std::wstring find,std::wstring repl);
|
||||
|
||||
|
||||
void replaceAll(std::wstring& str, const std::wstring from, const std::wstring to);
|
||||
void replaceAll(std::string& str, const std::string from, const std::string to);
|
||||
std::string trim(const std::string & s); //Удалить пробелы в начале и конце строки
|
||||
void Trim(std::string& s);
|
||||
void TrimW(std::wstring& s);
|
||||
void TrimLeft(std::string& s);
|
||||
void TrimRight(std::string& s);
|
||||
void TrimWLeft(std::wstring& s);
|
||||
void TrimWRight(std::wstring& s);
|
||||
std::string printToPos(std::string str,int pos, std::string txt);
|
||||
|
||||
std::vector<std::wstring> split(const std::wstring& str, wchar_t delimiter);
|
||||
|
||||
std::string add0(std::string str); //Добавить 0 в начало строоки если 1 символ для даты нужно
|
||||
|
||||
std::string getStrDate();
|
||||
std::wstring getWStrDate();
|
||||
std::string getDateTime();
|
||||
unsigned int getTime1(std::string date); //Преобразую строку вида 02-10-18 20:51:40 в unix time
|
||||
|
||||
std::string getExePathA(bool add = true); //Путь к DLL либо к EXE
|
||||
std::wstring getExePathW(bool add = true); //Путь к DLL
|
||||
std::string getAppPath(); //Путь к дирректории приложений (если линукс то к томайней директории пользователя)
|
||||
void killProcessByName(const wchar_t *filename);
|
||||
|
||||
|
||||
std::string intToString(int val);
|
||||
std::string uintToString(unsigned int val);
|
||||
std::string ullintToString(unsigned long long int val);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
#endif
|
||||
Reference in New Issue
Block a user