qstdlib.cpp

来自「BCB下的高效开发包。包含大并发的网络库」· C++ 代码 · 共 689 行 · 第 1/2 页

CPP
689
字号
                                     0, 0, 0, 0, 0,10,11,12,13,14,
                                     15,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,10,11,12,
                                    13,14,15, 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, 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, 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,
                                     0, 0, 0, 0, 0, 0 };
//---------------------------------------------------------------------------
std::string HexToBin(const std::string& HexStr)
{

        std::string en = HexStr;
        if (en.size() / 2 * 2 != en.size ())//处理奇数问题
            en = "0" + en;
        std::string ret;
        for(unsigned int i = 0; i < en.size (); i += 2) {
            ret += (char)(_hex_d16[en[i]] * 16 + _hex_d16[en[i+1]]);
        }
        return ret;
}
//---------------------------------------------------------------------------
std::string BinToHex(const std::string& BinData)
{
        const char C16[] = "0123456789ABCDEF";
        std::string ret;
        for(unsigned int i = 0; i < BinData.size() ; i++) {
            ret += C16[(BinData[i] & 0xF0) >> 0x4];
            ret += C16[BinData[i] & 0x0F];
        }
        return ret;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//内部函数 CompareRule

bool _CompareFooCalc(const char* lpcszSrc,const char* lpcszRule) //数学上使用的原算法
{
    int lpOld[1024];
    int lpLine[1024];
    int imax=strlen(lpcszRule),jmax=strlen(lpcszSrc);
    if (imax >= 1024 || jmax >= 1024) //长度不能超过1024
        return false;
    int i,j;

    char lpszSrc[1024];
    char lpszRule[1024];
    strcpy(lpszSrc,lpcszSrc);
    strcpy(lpszRule,lpcszRule);
    strlwr(lpszSrc);
    strlwr(lpszRule);

        //初始化
    for(i=0;i<jmax+1;i++)
        lpLine[i]=0;
    for(i=1;i<jmax+1;i++)
        lpOld[i]=0;
    lpOld[0]=1;
    lpOld[1]=1;

    //1才是开始
    for(i=1;i<imax+1;i++) {
        for(j=1;j<jmax+1;j++) {
            lpLine[j]=0;
            if(lpszRule[i-1]==lpszSrc[j-1] && lpOld[j-1]==1) {
                lpLine[j]=1;
            }
            if(lpszRule[i-1]=='?' && lpOld[j-1]==1) {
                lpLine[j]=1;
            }
            if(lpszRule[i-1]=='*') {
                if(lpLine[j-1]==1)
                    lpLine[j]=1;
                if(lpOld[j]==1)
                    lpLine[j]=1;
            }
        }
        for(j=0;j<jmax+1;j++) {
            lpOld[j]=lpLine[j];
        }
    }
    return (lpLine[jmax]==1);
}
bool _CompareFooPath(const char* lpcszSrc,const char* lpcszRule)//保留\的文件路径匹配
{
    int lpOld[1024];
    int lpLine[1024];
    int imax=strlen(lpcszRule),jmax=strlen(lpcszSrc);
    if (imax >= 1024 || jmax >= 1024) //长度不能超过1024
        return false;

    int i,j;

    char lpszSrc[1024];
    char lpszRule[1024];
    strcpy(lpszSrc,lpcszSrc);
    strcpy(lpszRule,lpcszRule);
    strlwr(lpszSrc);
    strlwr(lpszRule);

        //初始化
    for(i=0;i<jmax+1;i++)
        lpLine[i]=0;
    for(i=1;i<jmax+1;i++)
        lpOld[i]=0;
    lpOld[0]=1;
    lpOld[1]=1;

    //1才是开始
    for(i=1;i<imax+1;i++) {
        for(j=1;j<jmax+1;j++) {
            lpLine[j]=0;
            if(lpszRule[i-1]==lpszSrc[j-1] && lpOld[j-1]==1) {
                lpLine[j]=1;
            }
            if(lpszRule[i-1]=='?' && lpOld[j-1]==1) {
                lpLine[j]=1;
            }
            if(lpszRule[i-1]=='*') {
                if(lpLine[j-1]==1 && lpszSrc[j-1]!='\\')
                    lpLine[j]=1;
                if(lpOld[j]==1)
                    lpLine[j]=1;
            }
        }
        for(j=0;j<jmax+1;j++) {
            lpOld[j]=lpLine[j];
        }
    }
    return (lpLine[jmax]==1);
}
bool _CompareFooDeepPath(const char* lpcszSrc,const char* lpcszRule)//使用**表示多层文件夹的匹配
{
    int lpOld[1024];
    int lpLine[1024];
    int imax=strlen(lpcszRule),jmax=strlen(lpcszSrc);
    if (imax >= 1024 || jmax >= 1024) //长度不能超过1024
        return false;
    int i,j;

    char lpszSrc[1024];
    char lpszRule[1024];
    strcpy(lpszSrc,lpcszSrc);
    strcpy(lpszRule,lpcszRule);
    strlwr(lpszSrc);
    strlwr(lpszRule);

        //初始化
    for(i=0;i<jmax+1;i++)
        lpLine[i]=0;
    for(i=1;i<jmax+1;i++)
        lpOld[i]=0;
    lpOld[0]=1;
    lpOld[1]=1;

    //1才是开始
    for(i=1;i<imax+1;i++) {
        for(j=1;j<jmax+1;j++) {
            lpLine[j]=0;
            if(lpszRule[i-1]==lpszSrc[j-1] && lpOld[j-1]==1) {
                lpLine[j]=1;
            }
            if(lpszRule[i-1]=='?' && lpOld[j-1]==1) {
                lpLine[j]=1;
            }
            if(lpszRule[i-1]=='*') {
                if(lpLine[j-1]==1) {
                    if(lpszSrc[j-1]!='\\') {
                        lpLine[j]=1;
                    }else if (i<imax+1) {
                        if(lpszRule[i-1+1]=='*')
                            lpLine[j]=1;
                    }
                }
                if(lpOld[j]==1)
                    lpLine[j]=1;
            }
        }
        for(j=0;j<jmax+1;j++) {
            lpOld[j]=lpLine[j];
        }
    }
    return (lpLine[jmax]==1);
}
//---------------------------------------------------------------------------
//外部函数 CompareRule
bool CheckPathRule(const std::string& Src,const std::string& Rule)
{   return _CompareFooPath(Src.c_str(),Rule.c_str());
}
bool CheckDeepPathRule(const std::string& Src,const std::string& Rule)
{   return _CompareFooDeepPath(Src.c_str(),Rule.c_str());
}
bool CheckStr(const std::string& Src,const std::string& Rule)
{   return _CompareFooCalc(Src.c_str(),Rule.c_str());
}
//---------------------------------------------------------------------------
//检测多个匹配
bool CheckStrs(const std::string& Addr, const std::vector<std::string>& Rules)
{
        for (unsigned int i = 0; i < Rules.size(); ++i) {
                if (CheckStr(Addr, Rules[i]))
                        return true;
        }
        return false;
}
//---------------------------------------------------------------------------
// 规则提取
//---------------------------------------------------------------------------
bool IsStrHead(const std::string& Src, int Index, const std::string& Head)
{
        if (Src.size() < Head.size() + Index)
                return false;

        std::string::const_iterator iter = Head.begin();
        const std::string::const_iterator iend = Head.end();
        std::string::const_iterator Src_iter = Src.begin();
        Src_iter += Index;

        while(iter != iend){
                if (*iter != *Src_iter)
                        return false;
                ++ iter;
                ++ Src_iter;
        }
        return true;
}
//---------------------------------------------------------------------------
int CheckStrHeads(const std::string& Src, int Index, const std::vector<std::string>& Heads)
{
        for (unsigned int i = 0; i < Heads.size(); ++i) {
                if (IsStrHead(Src, Index, Heads[i]))
                        return Heads[i].size();
        }
        return 0;
}
//---------------------------------------------------------------------------
std::string StrTake(int Index, const std::string& Src, const std::string& Starts
    , const std::string Ends, int* pEndIndex, std::string* pStart, std::string* pEnd)
{
        if ((int)Src.size() <= Index) {
                if (pEndIndex)
                        *pEndIndex = 0;
                return "";
        }

        const std::vector<std::string> StartV = StrSeg(Starts, "|");
        const std::vector<std::string> EndV   = StrSeg(Ends, "|");

        int StartIndex = -1;
        int StartLen = 0;
        for (unsigned int i = Index; i < Src.size(); ++i) {
                int HeadLen = CheckStrHeads(Src, i, StartV);
                if (HeadLen != 0) {
                        StartIndex = i + HeadLen;
                        StartLen = HeadLen;
                        i = StartIndex - 1;
                        continue;
                }



                int EndLen = CheckStrHeads(Src, i, EndV);
                if (EndLen == 0)
                        continue;
                if (StartIndex < 0)
                        continue;//还没有找到开始

                std::string Ret = Src.substr(StartIndex, i - StartIndex);

                if (pEndIndex) {
                        *pEndIndex = i + EndLen;
                }
                if (pStart) {
                        *pStart = Src.substr(StartIndex - StartLen, StartLen);
                }
                if (pEnd) {
                        *pEnd = Src.substr(i, EndLen);
                }
                return Ret;

        }

        /*
        int StartIndex = 0;
        for (unsigned int i = Index; i < Src.size(); ++i) {
                int HeadLen = CheckStrHeads(Src, i, StartV);
                if (HeadLen != 0) {
                        StartIndex = i + HeadLen;
                        break;
                }
        }
        if (StartIndex == 0) {
                if (pEndIndex)
                        *pEndIndex = Src.size();
                return "";
        }

        std::string MyStart;
        //去重复开头
        for (unsigned int i = StartIndex; i < Src.size();) {
                int HeadLen = CheckStrHeads(Src, i, StartV);
                if (HeadLen == 0) {
                        StartIndex = i;
                        MyStart = Src.substr(i, HeadLen);
                        break;
                }
                i += HeadLen;
        }

        std::string MyEnd;
        //开始寻找末尾
        for (unsigned int i = StartIndex; i < Src.size(); ++i) {
                int EndLen = CheckStrHeads(Src, i, EndV);
                if (EndLen == 0)
                        continue;
                std::string Ret = Src.substr(StartIndex, i - StartIndex);
                MyEnd = Src.substr(i, EndLen);
                if (pEndIndex)
                        *pEndIndex = i + EndLen;

                if (pStart)
                        *pStart = MyStart;
                if (pEnd)
                        *pEnd = MyEnd;
                return Ret;
        }
        */
        if (pEndIndex)
                *pEndIndex = Src.size();
        return "";
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?