123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- #include "stdafx.h"
- #include <time.h>
- #include "common_api.h"
- common_api::common_api(void)
- {
- m_NotifyEvent = CreateMutexA(NULL, FALSE, NULL);
- if (m_NotifyEvent == NULL)
- {
- printf("CreateMutex error: %d\n", GetLastError());
- return;
- }
- }
- common_api::~common_api(void)
- {
- CloseHandle(m_NotifyEvent);
- }
- common_api g_common1_for_init;
- bool string_2_guid(const char *pstr, GUID &stGuid)
- {
- string temp;
- vector<string> thelist;
- if (SplitTo84422222222String(pstr, thelist) == false)
- {
- return false;
- }
- temp = "0x";
- temp += thelist[0].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data1);
- //sscanf_s(thelist[0].c_str(), "%08x", &stGuid.Data1);
- temp = "0x";
- temp += thelist[1].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data2);
- //sscanf_s(thelist[1].c_str(), "%04x", &stGuid.Data2);
- temp = "0x";
- temp += thelist[2].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data3);
- //sscanf_s(thelist[2].c_str(), "%04x", &stGuid.Data3);
- for (size_t i = 3; i < 11; i++)
- {
- //sscanf_s(thelist[i].c_str(), "%02x", &stGuid.Data4[i - 3],2);
- temp = "0x";
- temp += thelist[i].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data4[i - 3]);
- }
- return true;
- }
- bool guid_2_string(GUID &stGuid, string &str)
- {
- char szBuff[MAX_PATH] = { 0 };
- sprintf_s(szBuff,"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", stGuid.Data1, stGuid.Data2, stGuid.Data3,
- stGuid.Data4[0], stGuid.Data4[1], stGuid.Data4[2], stGuid.Data4[3],
- stGuid.Data4[4], stGuid.Data4[5], stGuid.Data4[6], stGuid.Data4[7]);
- str = szBuff;
- return true;
- }
- bool AddEnvPath(const char *pPath)
- {
- DWORD PathLen = 32768;
- char *pszPath = new char[PathLen];
- DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
- if (Len > PathLen)
- {
- delete[]pszPath;
- return false;
- }
- std::string::size_type pos = 0;
- string EnvPath = pszPath;
- delete[]pszPath;
- //EnvPath.find(pPath, 0);
- if ((pos = EnvPath.find(pPath, pos)) == std::string::npos)
- {
- string NewPath = EnvPath;
- NewPath += ";";
- NewPath += pPath;
- if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
- {
- return false;
- }
- }
- return true;
- }
- __time64_t GetCurrentRealTimeOfStk()
- {
- __time64_t now;
- _time64(&now);
- return now;
- }
- UINT64 GetDay(UINT64 TheTime)
- {
- struct tm tCur;
- _gmtime64_s(&tCur, (__time64_t*)&TheTime);
- tCur.tm_sec = 0;
- tCur.tm_min = 0;
- tCur.tm_hour = 0;
- __time64_t CurDay = _mkgmtime64(&tCur);
- return (UINT64)CurDay;
- }
- int CompareTimeByDay(UINT64 Prev, UINT64 Cur)
- {
- struct tm tPrev,tCur;
- _gmtime64_s(&tPrev, (__time64_t*)&Prev);
- tPrev.tm_sec = 0;
- tPrev.tm_min = 0;
- tPrev.tm_hour = 0;
- __time64_t PrevDay = _mkgmtime64(&tPrev);
- _gmtime64_s(&tCur, (__time64_t*)&Cur);
- tCur.tm_sec = 0;
- tCur.tm_min = 0;
- tCur.tm_hour = 0;
- __time64_t CurDay = _mkgmtime64(&tPrev);
- if (CurDay == PrevDay)
- {
- return 0;
- }
- if (CurDay > PrevDay)
- {
- return 1;
- }
- return -1;
- }
- bool getBusIdFromFilePath(const char *pFilePath,string &BusId)
- {
- string Path = pFilePath;
- size_t readcount = 0;
- size_t firstHit = Path.find_first_of('/');
- if(firstHit == string::npos || firstHit != 0)
- {
- return false;
- }
- size_t SecondHit = Path.find_first_of('/',1);
- if(SecondHit == string::npos)
- {
- BusId = Path.substr(1,Path.size()-1);
- }
- else
- {
- BusId = Path.substr(1,SecondHit - 1);
- }
- if(BusId.size() == 0)
- {
- return false;
- }
- return true;
- }
- void RawToHex(const char *pRaw, DWORD RawLen, string &Hex)
- {
- char szMsg[4] = { 0 };
- Hex.reserve(RawLen * 3);
- for (DWORD i = 0; i < RawLen; i++)
- {
- _snprintf_s(szMsg, 3, "%02X ", pRaw[i]);
- Hex += szMsg;
- }
- }
- bool MergeDevicePath(vector<string> &nodes,string &Devpath)
- {
- Devpath = "";
- for (size_t i = 0; i < nodes.size(); i++)
- {
- if (nodes[i].size() > 0)
- {
- Devpath += "/";
- Devpath += nodes[i];
- }
- }
- return (Devpath.size() > 0);
- }
- bool SplitStringSub(string &Path, char key, vector<string> &nodes)
- {
- string node;
- size_t readcount = 0;
- //nodes.clear();
- string::size_type firstHit = Path.find_first_of(key);
- if (firstHit == string::npos)
- {
- if (Path.size() > 0)
- {
- nodes.push_back(Path);
- }
- return true;
- }
- //firstHit += 1;
- if (firstHit > 0)
- {
- node = Path.substr(0, firstHit);
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- }
- while (firstHit < Path.size())
- {
- firstHit += 1;
- string::size_type SecondHit = Path.find_first_of(key, firstHit);
- if (SecondHit == string::npos)
- {
- node = Path.substr(firstHit, Path.size() - (firstHit));
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- return true;
- }
- node = Path.substr(firstHit, SecondHit - (firstHit));
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- firstHit = SecondHit;
- }
- return (nodes.size() > 0);
- }
- bool SplitString(const char *pString,string args, vector<string> &nodes)
- {
- vector<string> req;
- vector<string> res;
- req.push_back(string(pString));
- for (size_t i = 0; i < args.size(); i++)
- {
- //cut all req by key
- for (size_t j = 0; j < req.size(); j++)
- {
- SplitStringSub(req[j], args[i], res);
- }
- //clear and reset
- req.clear();
- req = res;
- res.clear();
- }
- nodes = req;
- return true;
- }
- bool SplitTo84422222222String(const char *pString, vector<string> &nodes)
- {
- string total;
- vector<string> thelist;
- SplitString(pString, string("{}-"), thelist);
- for (size_t i = 0; i < thelist.size(); i++)
- {
- total += thelist[i];
- }
- if (total.size() != 32)
- {
- return false;
- }
- string temp;
- temp = total.substr(0, 8);//8
- nodes.push_back(temp);
- temp = total.substr(8, 4);//4
- nodes.push_back(temp);
- temp = total.substr(12, 4);//4
- nodes.push_back(temp);
- temp = total.substr(16, 2);//2
- nodes.push_back(temp);
- temp = total.substr(18, 2);//2
- nodes.push_back(temp);
- temp = total.substr(20, 2);//2
- nodes.push_back(temp);
- temp = total.substr(22, 2);//2
- nodes.push_back(temp);
- temp = total.substr(24, 2);//2
- nodes.push_back(temp);
- temp = total.substr(26, 2);//2
- nodes.push_back(temp);
- temp = total.substr(28, 2);//2
- nodes.push_back(temp);
- temp = total.substr(30, 2);//2
- nodes.push_back(temp);
- return true;
- }
- string FormatstdString(const char *fmt, ...)
- {
- std::string strResult = "";
- WaitForSingleObject(g_common1_for_init.m_NotifyEvent, INFINITE);
- {
- if (NULL != fmt)
- {
- va_list marker = NULL;
- va_start(marker, fmt);
- size_t nLength = _vscprintf(fmt, marker) + 1;
- std::vector<char> vBuffer(nLength, '\0');
- int nWritten = vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
- if (nWritten > 0)
- {
- strResult = &vBuffer[0];
- }
- va_end(marker);
- }
- ReleaseMutex(g_common1_for_init.m_NotifyEvent);
- }
- return strResult;
- }
- bool SplitDevicePath(const char *pDevicePath, vector<string> &nodes)
- {
- string node;
- string Path = pDevicePath;
- size_t readcount = 0;
- nodes.clear();
- string::size_type firstHit = Path.find_first_of('/');
- if (firstHit == string::npos || firstHit != 0)
- {
- return false;
- }
- while (firstHit < Path.size())
- {
- firstHit += 1;
- string::size_type SecondHit = Path.find_first_of('/', firstHit);
- if (SecondHit == string::npos)
- {
- node = Path.substr(firstHit, Path.size() - firstHit);
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- return true;
- }
- node = Path.substr(firstHit, SecondHit - (firstHit));
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- firstHit = SecondHit;
- }
- return (nodes.size() > 0);
- }
- string GetProcessDirectory()
- {
- string ret = "";
- char szFilename[MAX_PATH] = { 0 };
- DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
- if (res == 0)
- {
- return ret;
- }
- string fullpath = szFilename;
- string::size_type firstHit = fullpath.find_last_of('\\');
- if (firstHit == string::npos || firstHit == 0)
- {
- return ret;
- }
- ret = fullpath.substr(0, firstHit);//kick last \
- return ret;
- }
- //fullpath must be like X:\filetitle.x.y.z...,or X:/filetitle.x.y.z...
- string GetFileTitle(string &fullpath)
- {
- string ret = "";
- string::size_type firstHit = fullpath.find_last_of('\\');
- string::size_type firstHitSec = fullpath.find_last_of('/');
- if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
- {
- return ret;
- }
- if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHitSec + 1);
- }
- else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHit + 1);
- }
- else
- {
- //effective both
- if (firstHit > firstHitSec)
- {
- ret = fullpath.substr(firstHit + 1);//kick last
- }
- else
- {
- ret = fullpath.substr(firstHitSec + 1);//kick last
- }
- }
- //got filetitle with ext
- if (ret.size() > 0)
- {
- string::size_type firstHit = ret.find_first_of('.');
- ret = ret.substr(0, firstHit);
- }
- return ret;
- }
- string GetFileName(string &fullpath)
- {
- string ret = "";
- string::size_type firstHit = fullpath.find_last_of('\\');
- string::size_type firstHitSec = fullpath.find_last_of('/');
- if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
- {
- return ret;
- }
- if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHitSec + 1);
- }
- else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHit + 1);
- }
- else
- {
- //effective both
- if (firstHit > firstHitSec)
- {
- ret = fullpath.substr(firstHit + 1);//kick last
- }
- else
- {
- ret = fullpath.substr(firstHitSec + 1);//kick last
- }
- }
- //got filetitle with ext
- //if (ret.size() > 0)
- //{
- // string::size_type ExtHit = ret.find_last_of('.');
- // if (ExtHit == 0 || ExtHit == string::npos || ((ExtHit + 1) == fullpath.size()))
- // {
- // ret = "";
- // }
- // else
- // {
- // ret = ret.substr(ExtHit + 1);
- // }
- //}
- return ret;
- }
- string ReplaceFileTitle(string FilePath, string NewTitle)
- {
- string::size_type ExtHit = FilePath.find_last_of('.');
- if (ExtHit != string::npos)
- {
- FilePath.replace(ExtHit + 1, string::npos, NewTitle);
- }
- return FilePath;
- }
- bool CreateFileDirectory(string &FullFilePath)
- {
- string path = GetFileDirectory(FullFilePath);
- string workpath = GetProcessDirectory() + string("\\");
- transform(path.begin(), path.end(), path.begin(), tolower);
- transform(workpath.begin(), workpath.end(), workpath.begin(), tolower);
- vector<string> folders;
- string subpath = ReplaceSubString(path, workpath, string(""));
- SplitString(subpath.c_str(), string("/\\"), folders);
-
- for (size_t i = 0; i < folders.size(); i++)
- {
- workpath += folders[i] + string("\\");
- DWORD attr = GetFileAttributes(workpath.c_str());
- if (attr == (DWORD)-1)
- {
- if (CreateDirectory(workpath.c_str(), 0) == 0)
- {
- return false;
- }
- }
- if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
- {
- return false;
- }
- }
- return true;
- }
- string GetFileDirectory(string &fullpath)
- {
- string ret = "";
- string::size_type firstHit = fullpath.find_last_of('\\');
- string::size_type firstHitSec = fullpath.find_last_of('/');
- if ((firstHit == string::npos || firstHit == 0) && (firstHitSec == string::npos || firstHitSec == 0))
- {
- return ret;
- }
- if (firstHit == string::npos || firstHit == 0)
- {
- ret = fullpath.substr(0, firstHitSec);//kick last
- }
- else if (firstHitSec == string::npos || firstHitSec == 0)
- {
- ret = fullpath.substr(0, firstHit);//kick last
- }
- else
- {
- //effective both
- if (firstHit > firstHitSec)
- {
- ret = fullpath.substr(0, firstHit);//kick last
- }
- else
- {
- ret = fullpath.substr(0, firstHitSec);//kick last
- }
- }
- return ret;
- }
- string ReplaceSubString(string org, string &keystr, string &replacestr)
- {
- std::string::size_type pos = 0;
- std::string::size_type keylen = keystr.size();
- std::string::size_type replen = replacestr.size();
- while ((pos = org.find(keystr, pos)) != std::string::npos)
- {
- org.replace(pos, keylen, replacestr);
- pos += replen;
- }
- return org;
- }
- bool FindSortedSubFiles(string rootFile, vector<string> &filelist)
- {
- WIN32_FIND_DATA fd;
- map<string,FILETIME> sortmap;
- ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
- if (rootFile.size() == 0)
- {
- return false;
- }
- HANDLE hFile;
- BOOL bRet = TRUE;
- string rootDir = GetFileDirectory(rootFile);
- if (rootDir[rootDir.size() - 1] != '\\')
- {
- rootDir += "\\";
- }
- string subpath = rootFile;
- subpath += ".*";//log file looks like xxx.log.20161107_185132
- hFile = FindFirstFile(subpath.c_str(), &fd);
- while (hFile != INVALID_HANDLE_VALUE && bRet)
- {
- if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
- strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
- {
- //ignore dir
- }
- else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
- {
- //ignore it
- }
- else
- {
- //full file name
- subpath = rootDir;
- subpath += fd.cFileName;
- sortmap[subpath] = fd.ftLastWriteTime;
- }
- bRet = FindNextFile(hFile, &fd);
- }
- FindClose(hFile);
- //sort it
- while (sortmap.size() > 0)
- {
- ULARGE_INTEGER fTime1 = { 0 };
- map<string, FILETIME>::iterator itermax;
- map<string, FILETIME>::iterator iter = sortmap.begin();
- while (iter != sortmap.end())
- {
- ULARGE_INTEGER *pfTime2 = (ULARGE_INTEGER *)&(iter->second);
- if (fTime1.QuadPart < (*pfTime2).QuadPart)
- {
- itermax = iter;
- }
- ++iter;
- }
- //got bigest one
- filelist.push_back(itermax->first);
- //del iter
- sortmap.erase(itermax);
- }
- return (filelist.size() > 0);
- }
- bool FindSubVersionDirs(string rootDir, map<string,vector<string>>& dirlist)
- {
- WIN32_FIND_DATA fd;
- ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
- if (rootDir.size() == 0)
- {
- return false;
- }
- HANDLE hFile;
- BOOL bRet = TRUE;
- if (rootDir[rootDir.size() - 1] != '\\')
- {
- rootDir += "\\";
- }
- string subpath = rootDir;
- //define the format of the basepath
- subpath += "*";
- hFile = FindFirstFile(subpath.c_str(), &fd);
- while (hFile != INVALID_HANDLE_VALUE && bRet)
- {
- if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
- strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
- {
- string TheDir = rootDir;
- TheDir += fd.cFileName;
- dirlist[fd.cFileName].push_back(TheDir);
- }
- else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
- {
- //ignore it
- }
- else
- {
- //ignore it
- }
- bRet = FindNextFile(hFile, &fd);
- }
- FindClose(hFile);
- return (dirlist.size() > 0);
- }
- bool FindSubFiles(string rootDir, vector<string> &filelist,bool Recursive, string strWildcard)
- {
- WIN32_FIND_DATA fd;
- ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
- if (rootDir.size() == 0)
- {
- return false;
- }
- HANDLE hFile;
- BOOL bRet = TRUE;
- if (rootDir[rootDir.size() - 1] != '\\')
- {
- rootDir += "\\";
- }
- string subpath = rootDir;
- //define the format of the basepath
- subpath += strWildcard;
- hFile = FindFirstFile(subpath.c_str(), &fd);
- while (hFile != INVALID_HANDLE_VALUE && bRet)
- {
- if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
- strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
- {
- if (Recursive)
- {
- subpath = rootDir;
- subpath += fd.cFileName;
- FindSubFiles(subpath, filelist);
- }
- }
- else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
- {
- //ignore it
- }
- else
- {
- //full file name
- subpath = rootDir;
- subpath += fd.cFileName;
- filelist.push_back(subpath);
- }
- bRet = FindNextFile(hFile, &fd);
- }
- FindClose(hFile);
- return (filelist.size() > 0);
- }
- string & makeLowerStr(string & src)
- {
- size_t len = src.size();
- for (size_t i = 0; i < len; i++)
- {
- src.at(i) = tolower(src.at(i));
- }
- return src;
- }
|