#include "stdafx.h" #include #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 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 &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 &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 &nodes) { vector req; vector 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 &nodes) { string total; vector 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 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 &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 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 &filelist) { WIN32_FIND_DATA fd; map 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::iterator itermax; map::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>& 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 &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; }