#include "CivCommonUtils.h"
#ifdef _WIN32
#include <Windows.h>
#include <wchar.h>
#endif // _WIN32
#include <time.h> 


std::string CivCommonUtils::string_To_UTF8(const std::string& str)
{
#ifdef _WIN32
    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pwBuf = new wchar_t[nwLen + 1];    //一定要加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 strRet(pBuf);

    delete[]pwBuf;
    delete[]pBuf;
    pwBuf = NULL;
    pBuf = NULL;
    return strRet;
#elif
    return str;
#endif // _WIND32

}


std::string CivCommonUtils::UTF8_To_string(const std::string& str)
{
#ifdef _WIN32
    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pwBuf = new wchar_t[nwLen + 1];    //一定要加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 strRet = pBuf;

    delete[]pBuf;
    delete[]pwBuf;
    pBuf = NULL;
    pwBuf = NULL;
    return strRet;
#elif
    return str;
#endif // _WIN32
  
}

void CivCommonUtils::genSeqNo(const std::string& prefix, int size,std::vector<std::string>& resVec)
{
    
    int base = 10000001;
    for (int i = base; i < base + size; i++)
    {
        std::string res = std::to_string(i);
        res = prefix + res.substr(1, res.length());
        resVec.push_back(res);
    }
}

std::string CivCommonUtils::currentTime()
{
    time_t rawtime;
    struct tm* timeinfo;
    char s[100];
    time(&rawtime);

    timeinfo = localtime(&rawtime);
    time_t tick = mktime(timeinfo);
    strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", timeinfo);

    return s;
}

std::string CivCommonUtils::currentDate()
{
    time_t rawtime;
    struct tm* timeinfo;
    char s[100];
    time(&rawtime);

    timeinfo = localtime(&rawtime);
    time_t tick = mktime(timeinfo);
    strftime(s, sizeof(s), "%Y-%m-%d", timeinfo);

    return s;
}

int CivCommonUtils::currentHour()
{
    time_t rawtime;
    struct tm* timeinfo;
    char s[100];
    time(&rawtime);

    timeinfo = localtime(&rawtime);
    time_t tick = mktime(timeinfo);
    
    return timeinfo->tm_hour;
}

std::string CivCommonUtils::lstrip(char aa[], char c)
{
    int i = 0, j = 0, k = 0, z = 0;

    k = strlen(aa);
    z = k - 1;
    while (aa[z] == c)
    {
        --z;
    }
    int x = 0, y = 0;
    while (aa[y] == c)
    {
        y++;
    }

    char bb[256];
    int  leng = 0;
    for (x = y; x < z + 1; x++)
    {
        bb[leng] = aa[x];
        leng++;
    }
    std::string res = bb;
    return res;
}

std::string  CivCommonUtils::getExePath(void)
{
#ifdef _WIN32
    char szFilePath[MAX_PATH + 1] = { 0 };
    GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
    (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名，只获得路径字串  
    std::string path = szFilePath;
    return path;
#endif // _WIN32
   
}