常用函数

class    CFunction
{

public:
        static    int    StrSafeCopy(CHAR *pszDestBuffer, UINT32 un32DestBuffLen, const CHAR *pszFromStr){
        unsigned int un32FromStrLen = (unsigned int)strlen(pszFromStr);
        unsigned int  un32CopyStrLen = un32FromStrLen;
        if (un32CopyStrLen > un32DestBuffLen){
            un32CopyStrLen = un32DestBuffLen;
        }
        memcpy(pszDestBuffer, pszFromStr, un32CopyStrLen);
        pszDestBuffer[un32DestBuffLen - 1] = 0;
        return eNormal;
    }

    static    bool    StrStartWith(const char *cpszStr, const char *cpszCmp){
        if (NULL == cpszStr || NULL == cpszCmp){
            return false;
        }
        unsigned int  un32StrLen = (unsigned int)strlen(cpszStr); 
        unsigned int  un32CmpLen = (unsigned int)strlen(cpszCmp); 
        if (un32StrLen < un32CmpLen){
            return false;
        }
        for (unsigned int i = 0; i < un32CmpLen; i++){
            if (cpszStr[i] != cpszCmp[i]){ 
                return false;
            }
        }
        return true;
    }

    static bool IfChangeYear(TIME_SECOND    tLastSec, TIME_SECOND tThisSec){
        UINT32        un32LastYear = (UINT32)(tLastSec / 86400 * 365);
        UINT32        un32ThisYear = (UINT32)(tThisSec / 86400 * 365);
        if (un32ThisYear != un32LastYear){
            return true;
        }
        return false;
    }

    static    bool    IfZeroGold(DOUBLE dThisGold){
        if (abs(dThisGold) > 0.001f){
            return false;
        }
        return true;
    }

    static    UINT32    GetTaskLineID(UINT32 un32TaskTempID){
        return un32TaskTempID / 1000;
    }

    static    UINT32    GetTaskStepID(UINT32 un32TaskTempID){
        return un32TaskTempID % 1000;
    }

    static    UINT32    MakeTaskID(UINT32 un32LineID, UINT32 un32StepID){
        return un32LineID * 1000 + un32StepID;
    }

    static    INT32    AbsDiff(INT32 n32Num1, INT32 n32Num2){
        if (n32Num1 > n32Num2){
            return n32Num1 - n32Num2;
        }
        else{
            return n32Num2 - n32Num1;
        }
    }

    static void IntToIP(INT32 n32IP, char apsz[64]){
        int n32StrLen = 0;
        apsz[0] = 0;
        n32StrLen = (INT32)strlen(apsz);
        assert(0 == n32StrLen);
        int nIdx = 0;
        BYTE bThisByte1 = (BYTE)((n32IP >> 24)&0xFF);
        BYTE bThisByte2 = (BYTE)((n32IP >> 16) & 0xFF);
        BYTE bThisByte3 = (BYTE)((n32IP >> 8) & 0xFF);
        BYTE bThisByte4 = (BYTE)(n32IP & 0xFF);
        sprintf_s(apsz, 64, "%d.%d.%d.%d", bThisByte4, bThisByte3, bThisByte2, bThisByte1);
    }

    static void ToUpper(char *cpszStr){
        UINT32 un32StrLen = (UINT32)strlen(cpszStr);
        for (UINT32 i = 0; i < un32StrLen; i++){
            if (97 <= cpszStr[i] && 122 >= cpszStr[i]){
                cpszStr[i] -= 32;
            }
        }
    }

    static void ToLower(char *cpszStr){
        UINT32 un32StrLen = (UINT32)strlen(cpszStr);
        for (UINT32 i = 0; i < un32StrLen; i++){
            if (65 <= cpszStr[i] && 90 >= cpszStr[i]){
                cpszStr[i] += 32;
            }
        }
    }

    static    void    GenerateRobotNickName(CHAR * aszNickName, INT32 nLen){    
        memset(aszNickName, 0, sizeof(aszNickName));

        const char *c_szRandCharList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        INT32    n32RandNumberCharNum = rand() % 10 + 6;
        INT32 n32RandIdx = 0;
        INT32 n32FillIdx = 0;
        for (INT32 i = 0; i < n32RandNumberCharNum; i++){
            if (n32FillIdx >= nLen-1)
            {
                break;
            }
            n32RandIdx = rand() % 62;
            aszNickName[n32FillIdx++] = c_szRandCharList[n32RandIdx];
        }
        aszNickName[n32FillIdx] = 0;
    }

    static vector<string>& splitEx(const std::string& src, const std::string &separate_character)
    {
        static vector<string> strs;
        strs.clear();

        int separate_characterLen = separate_character.size(); 
        int lastPosition = 0, index = -1;
        while (-1 != (index = src.find(separate_character, lastPosition)))
        {
            strs.push_back(src.substr(lastPosition, index - lastPosition));
            lastPosition = index + separate_characterLen;
        }
        string lastString = src.substr(lastPosition); 
        if (!lastString.empty())
            strs.push_back(lastString); 
        return strs;
    }

    static void splitEx(const std::string& str, const std::string &separate_character, vector<string> & strsVec)
    { 
         strsVec.clear();

        int separate_characterLen = separate_character.size(); 
        int lastPosition = 0, index = -1;
        while (-1 != (index = str.find(separate_character, lastPosition)))
        {
            strsVec.push_back(str.substr(lastPosition, index - lastPosition));
            lastPosition = index + separate_characterLen;
        }
        string lastString = str.substr(lastPosition); 
        if (!lastString.empty())
            strsVec.push_back(lastString); 
         
    }
    //replace
    static std::string replaceStr(std::string &str,const std::string& oldk, const std::string& newk)
    {
        while (true)   {
            string::size_type   pos(0);
            if ((pos = str.find(oldk)) != string::npos)
                str.replace(pos, oldk.length(), newk);
            else   break;
        }
        return   str;
    }
    //--------------windows------------------------
    static void CharToTchar(const char * _char, TCHAR * tchar)
    {
        int iLength;

        iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);
        MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);
    }

    static void TcharToChar(  TCHAR * tchar, char * _char)
    {
        int iLength = 0;
        iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);
        WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);
    }

    static void MyReplace(std::string & old )
    {
        INT32 npos = old.find_first_of("\\");
        while (npos != -1)
        {
            old.replace(npos, 1, "/");
            npos = old.find_first_of("\\");
        }
    }

    //获得进程运行路径和进程名
    static void GetProgramInfo(std::vector<std::string> &vec)
    {         
        TCHAR exeFullPath[MAX_PATH] = { 0 };
        CHAR  szFullPath[MAX_PATH] = { 0 };
        ::GetModuleFileName(NULL, exeFullPath, MAX_PATH);

        CFunction::TcharToChar(exeFullPath, szFullPath);

        std::string strPath = std::string(szFullPath);
        INT32  nLen = strPath.length();

        INT32 npos = strPath.find_last_of("\\", nLen);
        std::string fullPath = strPath.substr(0, npos);
        std::string progressName = strPath.substr(npos + 1, nLen);

        MyReplace(fullPath); 

        vec.push_back(fullPath);
        vec.push_back(progressName);
    }
    //
    static UINT32  GetProcessId()
    {
        return GetCurrentProcessId();
    }
    //nsid  --> ip
    static const CHAR * NSIDToIP(INT32  nGCNSID)
    {
        in_addr sTestAddr;
        sTestAddr.S_un.S_addr = nGCNSID;
        const CHAR *pszIP = inet_ntoa(sTestAddr);
        return  pszIP;
    }
    
    static UINT64  MyRandom()
    {
        std::random_device rd;
        std::mt19937 mt(rd());
        INT64  n = INT64( mt() );
        return abs(n);
    }
    // >0 or <0
    static INT32 RandInt(INT32 n32Min, INT32 n32Max)
    {
        if (n32Min == n32Max)
        {
            return n32Min;
        } 
        
        if (n32Min > n32Max) std::swap(n32Min, n32Max);

        UINT64  rnd = MyRandom();

        return rnd % (n32Max - n32Min+1) + n32Min;
    } 

    static INT32 FilterInvalidKey(const std::string &str){
        if (str.length() < 1){
            return -1;
        }
        const CHAR  szKey[] = { '\'', '@', '-', '\"', '(', ')', '~', '-', '#', '$', '*' };
        INT32  nLen = sizeof(szKey) / sizeof(CHAR);
        for (INT32 i = 0; i < nLen; i++)
        {
            INT32  nps = str.find(szKey[i]);
            if (nps != -1)
            {
                return -1;
            }
        }
        return 0;
    }


 
    static void    OnSetConsoleTitle(const CHAR* szName, INT32  userNum)
    {
        static  INT64  nLastUpTime;
        static std::vector<std::string> vec;
        if (time(NULL) < nLastUpTime)
        {
            return;
        }
        vec.clear();
        nLastUpTime = time(NULL) + 10;
        CFunction::GetProgramInfo(vec);
        std::stringstream  mystream;
        mystream << vec[0].c_str();
        mystream << "/";
        mystream << vec[1].c_str();
        mystream << "-";
        mystream << szName;
        mystream << "-";
        mystream << userNum;
        std::string strTitle = mystream.str();

        TCHAR szTitle[128] = {0};
        CFunction::CharToTchar(strTitle.c_str(), szTitle);
        SetConsoleTitle(szTitle);
    }

    static  void FormatTime(time_t t, std::string& str)
    {
         char tmp[64] = { 0 };     
        if (t < 1)
        {
            sprintf(tmp, "%d", t);
            str = string(tmp);
            return;
        } 
     
        struct tm p;
        p = *localtime(&t);
        strftime(tmp, 1000, "%Y-%m-%d %H:%M:%S", &p); 
        str = string(tmp);
    }

    static void strtokself(CHAR* char1, CHAR resChar[4][32], const CHAR* key){
        char *ptr;
        ptr = strtok(char1, key);
        INT32 i = 0;
        while (ptr != NULL){
            CFunction::StrSafeCopy(resChar[i], strlen(ptr) + 1, ptr);
            
            ptr = strtok(NULL, key);
            i++;
            if (i > 4){
                break;
            }
        }
        return;
    }

    static const CHAR*   UTF8_To_string(string & str)
    {
        int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

        wchar_t * pwBuf = new wchar_t[nwLen + 1]; 
        memset(pwBuf, 0, nwLen * 2 + 2);

        MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

        int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

        char * pBuf = new char[nLen + 1];
        memset(pBuf, 0, nLen + 1);

        WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

        std::string retStr = pBuf;

        delete[]pBuf;
        delete[]pwBuf;

        pBuf = NULL;
        pwBuf = NULL;

        return retStr.data();
    }

    static const CHAR* string_To_UTF8(string & str)
    {
        int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

        wchar_t * pwBuf = new wchar_t[nwLen + 1]; 
        ZeroMemory(pwBuf, nwLen * 2 + 2);

        ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

        int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

        char * pBuf = new char[nLen + 1];
        ZeroMemory(pBuf, nLen + 1);

        ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

        std::string retStr(pBuf);

        delete[]pwBuf;
        delete[]pBuf;

        pwBuf = NULL;
        pBuf = NULL;

        return retStr.data();
    }

    static bool VerifyNickname(const CHAR* cpszGameNickname){
        UINT32 un32Len = (UINT32)strlen(cpszGameNickname) + 1;
        if (un32Len < 3 || un32Len > 30){
            return false;
        }

        CHAR ch;
        for (UINT32 i = 1; i < un32Len - 1; i++){
            ch = cpszGameNickname[i];
            if (ch == '\'' || ch == '\\' || ch == '\"'){
                return false;
            }
        }
        return true;
    }


    static bool VerifyAccount(const CHAR* cpszName){
        UINT32 un32Len = (UINT32)strlen(cpszName) + 1;
        if (un32Len < 4 || un32Len > 64){
            return false;
        }
        CHAR* cpszGameUserName = const_cast<CHAR*>(cpszName);
        CHAR ch = cpszGameUserName[0];
        if ((ch >= 65) && (ch <= 90)){
            cpszGameUserName[0] = ch + 32;
            ch = cpszGameUserName[0];
        }
        if ((ch >= 97) && (ch <= 122) || ch == '_'){}
        else if ((ch >= 48) && (ch <= 57)){}
        else{
            return false;
        }
        for (UINT32 i = 1; i < un32Len - 1; i++){
            ch = cpszGameUserName[i];
            if ((ch >= 65) && (ch <= 90)){
                cpszGameUserName[i] = ch + 32;
                ch = cpszGameUserName[i];
            }
            if ((ch >= 97) && (ch <= 122) || (ch >= 45) && (ch <= 57) || (ch == 95)){}
            else{
                return false;
            }
        }
        return true;
    }
    static bool SetTempArr(INT32 nRndIdx, INT32* tempArr, INT32 nLen)
    {
        for (INT32 i = 0; i < nLen; i++)
        {
            if (tempArr[i] == nRndIdx)
                return true;
        }
        for (INT32 j = 0; j < nLen; j++)
        {
            if (tempArr[j] == 0)
            {
                tempArr[j] = nRndIdx;
                break;
            }
        }
        return false;
    }

    static INT32  GetNearestPos(UINT32    n32BetRadio[], UINT32 nLen, INT32  n32AimValue)
    {
        INT32  nFindPos = 0;
        INT32  nMaxValue = INT32_MAX;
        for (INT32 i = 0; i < nLen; i++)
        {
            if (n32BetRadio[i] >0)
            {
                INT32  nDist = ABS ( INT32 (n32AimValue - n32BetRadio[i]) );
                if (nMaxValue > nDist)
                {
                    nMaxValue = nDist;
                    nFindPos = i;
                }
            }
        }
        return nFindPos;
    }
};

 


classCFunction{
public:staticintStrSafeCopy(CHAR *pszDestBuffer, UINT32 un32DestBuffLen, const CHAR *pszFromStr){unsigned int un32FromStrLen = (unsigned int)strlen(pszFromStr);unsigned int  un32CopyStrLen = un32FromStrLen;if (un32CopyStrLen > un32DestBuffLen){un32CopyStrLen = un32DestBuffLen;}memcpy(pszDestBuffer, pszFromStr, un32CopyStrLen);pszDestBuffer[un32DestBuffLen - 1] = 0;return eNormal;}
staticboolStrStartWith(const char *cpszStr, const char *cpszCmp){if (NULL == cpszStr || NULL == cpszCmp){return false;}unsigned int  un32StrLen = (unsigned int)strlen(cpszStr); unsigned int  un32CmpLen = (unsigned int)strlen(cpszCmp); if (un32StrLen < un32CmpLen){return false;}for (unsigned int i = 0; i < un32CmpLen; i++){if (cpszStr[i] != cpszCmp[i]){ return false;}}return true;}
static bool IfChangeYear(TIME_SECONDtLastSec, TIME_SECOND tThisSec){UINT32un32LastYear = (UINT32)(tLastSec / 86400 * 365);UINT32un32ThisYear = (UINT32)(tThisSec / 86400 * 365);if (un32ThisYear != un32LastYear){return true;}return false;}
staticboolIfZeroGold(DOUBLE dThisGold){if (abs(dThisGold) > 0.001f){return false;}return true;}
staticUINT32GetTaskLineID(UINT32 un32TaskTempID){return un32TaskTempID / 1000;}
staticUINT32GetTaskStepID(UINT32 un32TaskTempID){return un32TaskTempID % 1000;}
staticUINT32MakeTaskID(UINT32 un32LineID, UINT32 un32StepID){return un32LineID * 1000 + un32StepID;}
staticINT32AbsDiff(INT32 n32Num1, INT32 n32Num2){if (n32Num1 > n32Num2){return n32Num1 - n32Num2;}else{return n32Num2 - n32Num1;}}
static void IntToIP(INT32 n32IP, char apsz[64]){int n32StrLen = 0;apsz[0] = 0;n32StrLen = (INT32)strlen(apsz);assert(0 == n32StrLen);int nIdx = 0;BYTE bThisByte1 = (BYTE)((n32IP >> 24)&0xFF);BYTE bThisByte2 = (BYTE)((n32IP >> 16) & 0xFF);BYTE bThisByte3 = (BYTE)((n32IP >> 8) & 0xFF);BYTE bThisByte4 = (BYTE)(n32IP & 0xFF);sprintf_s(apsz, 64, "%d.%d.%d.%d", bThisByte4, bThisByte3, bThisByte2, bThisByte1);}
static void ToUpper(char *cpszStr){UINT32 un32StrLen = (UINT32)strlen(cpszStr);for (UINT32 i = 0; i < un32StrLen; i++){if (97 <= cpszStr[i] && 122 >= cpszStr[i]){cpszStr[i] -= 32;}}}
static void ToLower(char *cpszStr){UINT32 un32StrLen = (UINT32)strlen(cpszStr);for (UINT32 i = 0; i < un32StrLen; i++){if (65 <= cpszStr[i] && 90 >= cpszStr[i]){cpszStr[i] += 32;}}}
staticvoidGenerateRobotNickName(CHAR * aszNickName, INT32 nLen){memset(aszNickName, 0, sizeof(aszNickName));
const char *c_szRandCharList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";INT32n32RandNumberCharNum = rand() % 10 + 6;INT32 n32RandIdx = 0;INT32 n32FillIdx = 0;for (INT32 i = 0; i < n32RandNumberCharNum; i++){if (n32FillIdx >= nLen-1){break;}n32RandIdx = rand() % 62;aszNickName[n32FillIdx++] = c_szRandCharList[n32RandIdx];}aszNickName[n32FillIdx] = 0;}
static vector<string>& splitEx(const std::string& src, const std::string &separate_character){static vector<string> strs;strs.clear();
int separate_characterLen = separate_character.size(); int lastPosition = 0, index = -1;while (-1 != (index = src.find(separate_character, lastPosition))){strs.push_back(src.substr(lastPosition, index - lastPosition));lastPosition = index + separate_characterLen;}string lastString = src.substr(lastPosition); if (!lastString.empty())strs.push_back(lastString); return strs;}
static void splitEx(const std::string& str, const std::string &separate_character, vector<string> & strsVec) strsVec.clear();
int separate_characterLen = separate_character.size(); int lastPosition = 0, index = -1;while (-1 != (index = str.find(separate_character, lastPosition))){strsVec.push_back(str.substr(lastPosition, index - lastPosition));lastPosition = index + separate_characterLen;}string lastString = str.substr(lastPosition); if (!lastString.empty())strsVec.push_back(lastString);  }//replacestatic std::string replaceStr(std::string &str,const std::string& oldk, const std::string& newk){while (true)   {string::size_type   pos(0);if ((pos = str.find(oldk)) != string::npos)str.replace(pos, oldk.length(), newk);else   break;}return   str;}//--------------windows------------------------static void CharToTchar(const char * _char, TCHAR * tchar){int iLength;
iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);}
static void TcharToChar(  TCHAR * tchar, char * _char){int iLength = 0;iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);}
static void MyReplace(std::string & old ){INT32 npos = old.find_first_of("\\");while (npos != -1){old.replace(npos, 1, "/");npos = old.find_first_of("\\");}}
//获得进程运行路径和进程名static void GetProgramInfo(std::vector<std::string> &vec)TCHAR exeFullPath[MAX_PATH] = { 0 };CHAR  szFullPath[MAX_PATH] = { 0 };::GetModuleFileName(NULL, exeFullPath, MAX_PATH);
CFunction::TcharToChar(exeFullPath, szFullPath);
std::string strPath = std::string(szFullPath);INT32  nLen = strPath.length();
INT32 npos = strPath.find_last_of("\\", nLen);std::string fullPath = strPath.substr(0, npos);std::string progressName = strPath.substr(npos + 1, nLen);
MyReplace(fullPath); 
vec.push_back(fullPath);vec.push_back(progressName);}//static UINT32  GetProcessId(){return GetCurrentProcessId();}//nsid  --> ipstatic const CHAR * NSIDToIP(INT32  nGCNSID){in_addr sTestAddr;sTestAddr.S_un.S_addr = nGCNSID;const CHAR *pszIP = inet_ntoa(sTestAddr);return  pszIP;}static UINT64  MyRandom(){std::random_device rd;std::mt19937 mt(rd());INT64  n = INT64( mt() );return abs(n);}// >0 or <0static INT32 RandInt(INT32 n32Min, INT32 n32Max){if (n32Min == n32Max){return n32Min;if (n32Min > n32Max) std::swap(n32Min, n32Max);
UINT64  rnd = MyRandom();
return rnd % (n32Max - n32Min+1) + n32Min;
static INT32 FilterInvalidKey(const std::string &str){if (str.length() < 1){return -1;}const CHAR  szKey[] = { '\'', '@', '-', '\"', '(', ')', '~', '-', '#', '$', '*' };INT32  nLen = sizeof(szKey) / sizeof(CHAR);for (INT32 i = 0; i < nLen; i++){INT32  nps = str.find(szKey[i]);if (nps != -1){return -1;}}return 0;}

 static voidOnSetConsoleTitle(const CHAR* szName, INT32  userNum){static  INT64  nLastUpTime;static std::vector<std::string> vec;if (time(NULL) < nLastUpTime){return;}vec.clear();nLastUpTime = time(NULL) + 10;CFunction::GetProgramInfo(vec);std::stringstream  mystream;mystream << vec[0].c_str();mystream << "/";mystream << vec[1].c_str();mystream << "-";mystream << szName;mystream << "-";mystream << userNum;std::string strTitle = mystream.str();
TCHAR szTitle[128] = {0};CFunction::CharToTchar(strTitle.c_str(), szTitle);SetConsoleTitle(szTitle);}
static  void FormatTime(time_t t, std::string& str){ char tmp[64] = { 0 }; if (t < 1){sprintf(tmp, "%d", t);str = string(tmp);return; struct tm p;p = *localtime(&t);strftime(tmp, 1000, "%Y-%m-%d %H:%M:%S", &p); str = string(tmp);}
static void strtokself(CHAR* char1, CHAR resChar[4][32], const CHAR* key){char *ptr;ptr = strtok(char1, key);INT32 i = 0;while (ptr != NULL){CFunction::StrSafeCopy(resChar[i], strlen(ptr) + 1, ptr);ptr = strtok(NULL, key);i++;if (i > 4){break;}}return;}
static const CHAR*   UTF8_To_string(string & str){int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1]; memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete[]pBuf;delete[]pwBuf;
pBuf = NULL;pwBuf = NULL;
return retStr.data();}
static const CHAR* string_To_UTF8(string & str){int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1]; ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete[]pwBuf;delete[]pBuf;
pwBuf = NULL;pBuf = NULL;
return retStr.data();}
static bool VerifyNickname(const CHAR* cpszGameNickname){UINT32 un32Len = (UINT32)strlen(cpszGameNickname) + 1;if (un32Len < 3 || un32Len > 30){return false;}
CHAR ch;for (UINT32 i = 1; i < un32Len - 1; i++){ch = cpszGameNickname[i];if (ch == '\'' || ch == '\\' || ch == '\"'){return false;}}return true;}

static bool VerifyAccount(const CHAR* cpszName){UINT32 un32Len = (UINT32)strlen(cpszName) + 1;if (un32Len < 4 || un32Len > 64){return false;}CHAR* cpszGameUserName = const_cast<CHAR*>(cpszName);CHAR ch = cpszGameUserName[0];if ((ch >= 65) && (ch <= 90)){cpszGameUserName[0] = ch + 32;ch = cpszGameUserName[0];}if ((ch >= 97) && (ch <= 122) || ch == '_'){}else if ((ch >= 48) && (ch <= 57)){}else{return false;}for (UINT32 i = 1; i < un32Len - 1; i++){ch = cpszGameUserName[i];if ((ch >= 65) && (ch <= 90)){cpszGameUserName[i] = ch + 32;ch = cpszGameUserName[i];}if ((ch >= 97) && (ch <= 122) || (ch >= 45) && (ch <= 57) || (ch == 95)){}else{return false;}}return true;}static bool SetTempArr(INT32 nRndIdx, INT32* tempArr, INT32 nLen){for (INT32 i = 0; i < nLen; i++){if (tempArr[i] == nRndIdx)return true;}for (INT32 j = 0; j < nLen; j++){if (tempArr[j] == 0){tempArr[j] = nRndIdx;break;}}return false;}
static INT32  GetNearestPos(UINT32n32BetRadio[], UINT32 nLen, INT32  n32AimValue){INT32  nFindPos = 0;INT32  nMaxValue = INT32_MAX;for (INT32 i = 0; i < nLen; i++){if (n32BetRadio[i] >0){INT32  nDist = ABS ( INT32 (n32AimValue - n32BetRadio[i]) );if (nMaxValue > nDist){nMaxValue = nDist;nFindPos = i;}}}return nFindPos;}};

posted @ 2020-07-28 14:52  沙漠中的雨滴  阅读(93)  评论(0编辑  收藏  举报