📄 qstdlib.cpp
字号:
#include "QStdlib.h"
#include "../QFile/QFile.h"
#include "../QSystem/QSystem.h"
#include "Windows.h"
#include <stdio.h>
#include <string.h>
//------------------------------------------------------------------------------
void MsgBox(const std::string& Msg)
{
::MessageBox(NULL, Msg.c_str(), "MSG", 0);
}
//------------------------------------------------------------------------------
void MsgBox(const std::string& Msg, const std::string &Title)
{
::MessageBox(NULL, Msg.c_str(), Title.c_str(), 0);
}
//------------------------------------------------------------------------------
std::string GetAppPath(void)
{
return GetPathDir(GetAppExe());
}
//------------------------------------------------------------------------------
std::string GetAppExe(void)
{
static std::string Ret = "";
if (Ret.empty()) {
char szCPath[MAX_PATH];
if(!GetModuleFileName(NULL,szCPath,sizeof(szCPath)))
return Ret;
Ret = szCPath;
}
return Ret;
}
//---------------------------------------------------------------------------
std::string GetPathDir(const std::string& FileName)
{
for(int i = FileName.length()-1; i>=0; i--) {
if('\\' == FileName[i])
return FileName.substr(0, i + 1);
}
return "";
}
//---------------------------------------------------------------------------
std::string GetPathFile(const std::string& FileName)
{
for(int i = FileName.length()-1; i>=0; i--) {
if('\\' == FileName[i])
return FileName.substr(i+1, FileName.length() - i - 1);
}
return FileName;
}
//---------------------------------------------------------------------------
std::string LongToStr(long LongValue)
{
char Var[48];
sprintf(Var, "%ld", LongValue);
return std::string(Var);
}
//---------------------------------------------------------------------------
std::string CutLeftStr(const std::string& Src, const std::string& ToCut)
{
if (ToCut.empty())
return Src;
std::string ret = Src;
while(ret.size() >= ToCut.size()) {
if (ret.substr(0, ToCut.size()) != ToCut)
break;
ret.erase(0,ToCut.size());
}
return ret;
}
//---------------------------------------------------------------------------
std::string CutRightStr(const std::string& Src, const std::string& ToCut)
{
if (ToCut.empty())
return Src;
std::string ret = Src;
while(ret.size() >= ToCut.size()) {
if (ret.substr(ret.size() - ToCut.size(), ToCut.size()) != ToCut)
break;
ret.erase(ret.size() - ToCut.size(), ToCut.size());
}
return ret;
}
//---------------------------------------------------------------------------
std::string CutLeftRightStr(const std::string& Src, const std::string& ToCut)
{
if (ToCut.empty())
return Src;
return CutRightStr(CutLeftStr(Src, ToCut), ToCut);
}
//---------------------------------------------------------------------------
std::string StrReplace(const std::string& Src,const std::string& Cut,const std::string& Set)
{
std::string Ret;
int Begin = 0;
while(true) {
int End = Src.find(Cut, Begin);
if(std::string::npos == End) {
Ret += Src.substr(Begin, End - Begin);
break;
}else{
Ret += Src.substr(Begin, End - Begin) + Set;
}
Begin = End + Cut.size();
}
return Ret;
}
//---------------------------------------------------------------------------
std::string StrReplaceUL(const std::string& Src,const std::string& Cut,const std::string& Set)
{
std::string Ret;
std::string SrcLow = StrLow(Src);
std::string CutLow = StrLow(Cut);
int Begin = 0;
while(true) {
int End = SrcLow.find(CutLow, Begin);
if(std::string::npos == End) {
Ret += Src.substr(Begin, End - Begin);
break;
}else{
Ret += Src.substr(Begin, End - Begin) + Set;
}
Begin = End + Cut.size();
}
return Ret;
}
//---------------------------------------------------------------------------
std::string ScanKeyValue(const std::string& Text, const std::string& KeyName
, const std::string& MoveInSegs, const std::string EndCutSegs)
{
std::string Ret;
const std::string KeyNameLow = StrLow(KeyName);
const std::vector<std::string> HeaderLines = StrSeg(StrLow(Text), KeyNameLow);
const std::vector<std::string> MoveIn = StrSeg(StrLow(MoveInSegs), "|");
const std::vector<std::string> EndCut = StrSeg(StrLow(EndCutSegs), "|");
if (HeaderLines.size() == 1)
return "";
for(unsigned int i = 1; i < HeaderLines.size(); ++i) {
std::string Line = HeaderLines[i];
for(unsigned int iter = 0; iter < MoveIn.size(); ++iter) {
Line = CutLeftStr(Line, MoveIn[iter]);
}
for(unsigned int iter = 0; iter < EndCut.size(); ++iter) {
Line = GetNext(Line, EndCut[iter]);
}
Ret += Line;
Ret += "\r\n";
}
return Ret;
}
//---------------------------------------------------------------------------
std::string StrLow(const std::string& Str)
{
char* buf = new char[Str.size() + 1];
memcpy(buf, Str.c_str(), Str.size() + 1);//包含最后\0
strlwr(buf);
std::string Ret(buf, Str.size());
delete[] buf;
return Ret;
}
//---------------------------------------------------------------------------
std::string StrUp(const std::string& Str)
{
char* buf = new char[Str.size() + 1];
memcpy(buf, Str.c_str(), Str.size() + 1);//包含最后\0
strupr(buf);
std::string Ret(buf, Str.size());
delete[] buf;
return Ret;
}
//---------------------------------------------------------------------------
std::string GetNext(std::string &Data,const std::string& Boundary)
{
int Loc = Data.find(Boundary);
std::string Ret;
if(Loc == std::string::npos) {//没有找到
Ret = Data;
Data.clear();
return Ret;
}else{
Ret=Data.substr(0,Loc);
Data.erase(0,Loc+Boundary.size());
return Ret;
}
}
//---------------------------------------------------------------------------
std::string GetRightStr(const std::string& Str, unsigned int Count)
{
if (Count > Str.size())
return Str;
if (0 == Count)
return "";
return Str.substr(Str.size() - Count, Count);
}
//===========================================================================
long QStrToLong(const std::string& Value)
{
return atol(Value.c_str());
}
//---------------------------------------------------------------------------
__int64 QStrToInt64(const std::string& Value)
{
return _atoi64(Value.c_str());
}
//---------------------------------------------------------------------------
int QStrToInt(const std::string& Value)
{
return atoi(Value.c_str());
}
//---------------------------------------------------------------------------
double QStrToDouble(const std::string& Value)
{
return atof(Value.c_str());
}
//---------------------------------------------------------------------------
std::string QInt64ToStr(__int64 Value)
{
//__i64toa:These functions can return up to 33 bytes
char var[36];
_i64toa(Value, var, 10);
return std::string(var);
}
//---------------------------------------------------------------------------
std::string QIntToStr(int Value)
{
char var[36];
itoa(Value, var, 10);
return std::string(var);
}
//---------------------------------------------------------------------------
std::string QLongToStr(long Value)
{
char var[36];
ltoa(Value, var, 10);
return std::string(var);
}
//---------------------------------------------------------------------------
std::string QDoubleToStr(double Value)
{
char var[64];
sprintf(var,"%lf",Value);
return std::string(var);
}
//---------------------------------------------------------------------------
std::string GetKey(const std::string& SrcStr,const std::string& Key)
{
std::string Start = "<" + Key + ">";
std::string End = "</" + Key + ">";
int StartPos=SrcStr.find(Start);
if(std::string::npos == StartPos)
return "";
int EndPos=SrcStr.rfind(End);
if(std::string::npos == EndPos)
return "";
if (StartPos < 0 || EndPos < 0)
return "";
if ((UINT)StartPos + Start.size() > (UINT)EndPos)
return "";
return SrcStr.substr(StartPos + Start.size(), EndPos - StartPos - Start.size());
}
//---------------------------------------------------------------------------
std::string MakeKey(const std::string& Key,const std::string& Value)
{
return "<" + Key + ">" + Value + "</" + Key + ">";
}
//---------------------------------------------------------------------------
unsigned int GetRealTickDifference(unsigned int StartTime, unsigned int EndTime)
{ //正常情况:跨日
return (EndTime>=StartTime)?(EndTime-StartTime):(0xFFFFFFFF-StartTime+EndTime);
}
//---------------------------------------------------------------------------
unsigned int GetTick(void)
{
return GetTickCount();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
std::vector<std::string> StrSeg(const std::string& s,const std::string& Seg)
{
std::vector<std::string> r;
unsigned int begin = 0, end = 0;
const unsigned int Seg_size=Seg.size();
const unsigned int s_size=s.size();
while(begin < s_size && std::string::npos != (int)end) {
end = s.find(Seg,begin);
r.push_back(s.substr(begin, end - begin));
begin = (Seg_size + end);
}
return r;
}
//---------------------------------------------------------------------------
std::string XorEncode(const std::string& Src, const std::string& PassWord)
{
std::string en(Src.size(), '\0');
unsigned int PassLen = PassWord.size();
unsigned int SrcLen = Src.size();
for(unsigned int i = 0; i < SrcLen; ++i) {
en[i] = Src[i] ^ PassWord[i % PassLen];
}
return en;
}
//---------------------------------------------------------------------------
std::wstring AnsiToUnicode(const std::string& Ansi)
{
std::wstring ws(Ansi.size(), L'\0');
MultiByteToWideChar(CP_ACP, 0, Ansi.c_str(), -1, &ws[0], ws.size());
return ws.c_str();
}
//---------------------------------------------------------------------------
std::string UnicodeToAnsi(const std::wstring& Unicode)
{
std::string as(Unicode.size()*2, '\0');
WideCharToMultiByte(CP_OEMCP, WC_COMPOSITECHECK, Unicode.c_str(), -1, &as[0], as.size(), NULL, NULL);
return as.c_str();
}
//---------------------------------------------------------------------------
std::string GBKToUtf8(const std::string& GBK)
{
const std::wstring wide = AnsiToUnicode(GBK);
std::string utf8s(wide.size() * 4, '\0');
WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, &utf8s[0], utf8s.size(), NULL, NULL);
return utf8s.c_str();
}
//---------------------------------------------------------------------------
std::string Utf8ToGBK(const std::string& Uft8)
{
std::wstring wide(Uft8.size() * 4, L'\0');
MultiByteToWideChar(CP_UTF8, 0, Uft8.c_str(), -1, &wide[0], wide.size());
return UnicodeToAnsi(wide).c_str();
}
//---------------------------------------------------------------------------
const unsigned char _hex_d16[256] = { //用于HEX转换
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 0, 0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -