// common_api.cpp #include #include #include #include #include #include #include "common_api.h" #include // 全局互斥锁初始化 common_api::common_api(void) { pthread_mutex_init(&m_NotifyMutex, NULL); } common_api::~common_api(void) { pthread_mutex_destroy(&m_NotifyMutex); } common_api g_common1_for_init; //=== 时间相关函数 ===// __time64_t GetCurrentRealTimeOfStk() { return time(NULL); } UINT64 GetDay(UINT64 TheTime) { struct tm tCur; gmtime_r((time_t*)&TheTime, &tCur); tCur.tm_sec = 0; tCur.tm_min = 0; tCur.tm_hour = 0; return mktime(&tCur); } int CompareTimeByDay(UINT64 Prev, UINT64 Cur) { return (Cur / (24 * 3600)) - (Prev / (24 * 3600)); } //=== GUID转换 ===// bool string_2_guid(const char* pstr, GUID& stGuid) { return sscanf(pstr, "{%8lx-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx}", &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]) == 11; } bool guid_2_string(GUID& stGuid, string& str) { char szBuff[128]; snprintf(szBuff, sizeof(szBuff), "{%08lX-%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) { const char* env_var_name = "LD_LIBRARY_PATH"; // Linux使用LD_LIBRARY_PATH const char* old_path = getenv(env_var_name); // 如果路径已存在,直接返回成功 if (old_path && strstr(old_path, pPath) != nullptr) return true; // 构建新的环境变量值 std::string new_path; if (old_path) { new_path = std::string(old_path) + ":" + pPath; } else { new_path = pPath; } // 设置环境变量 return setenv(env_var_name, new_path.c_str(), 1) == 0; } bool DelEnvPath(const char* pPath) { return false; // Linux下不建议直接操作PATH字符串 } static bool SplitStringSub(string& Path, char key, vector& nodes) { string node; size_t readcount = 0; string::size_type firstHit = Path.find_first_of(key); if (firstHit == string::npos) { if (Path.size() > 0) { nodes.push_back(Path); } return true; } if (firstHit > 0) { node = Path.substr(0, firstHit); if (node.size() > 0) { nodes.push_back(node); } } while (firstHit < Path.size()) { firstHit += 1; if (firstHit >= Path.size()) break; 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); } break; } 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, ...) { va_list args; va_start(args, fmt); char buffer[1024]; vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); return string(buffer); } 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; } 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); } void SplitDeviceList(const char* deviceList, std::vector& result) { result.clear(); // 处理空输入情况 if (deviceList == nullptr || *deviceList == '\0') return; // 创建可修改副本(strtok会破坏原字符串) char* buffer = new char[strlen(deviceList) + 1]; strcpy(buffer, deviceList); // 使用strtok分割字符串 const char* delimiter = ";"; char* token = strtok(buffer, delimiter); while (token != nullptr) { // 过滤空字符串(避免连续分号产生空项) if (strlen(token) > 0) { result.push_back(token); } token = strtok(nullptr, delimiter); } delete[] buffer; // 释放临时内存 } //=== 文件路径操作 ===// string GetProcessDirectory() { char path[PATH_MAX]; ssize_t len = readlink("/proc/self/exe", path, sizeof(path) - 1); std::cout << "[DEBUG] len == -1 GetProcessDirectory() len: " << len << "path:" << path << std::endl; if (len == -1) return ""; path[len] = '\0'; std::cout << "[DEBUG] GetProcessDirectory() len: " << len << "path:" << path << std::endl; char* last_slash = strrchr(path, '/'); if (last_slash) *last_slash = '\0'; return path; } string GetFileDirectory(string& fullpath) { size_t pos = fullpath.find_last_of('/'); return (pos != string::npos) ? fullpath.substr(0, pos) : ""; } bool FindSubVersionDirs(string rootDir, map>& dirlist) { return false; } string GetFileName(string& fullpath) { size_t pos = fullpath.find_last_of('/'); return (pos != string::npos) ? fullpath.substr(pos + 1) : fullpath; } string GetFileTitle(string& fullpath) { string filename = GetFileName(fullpath); size_t dot = filename.find_last_of('.'); return (dot != string::npos) ? filename.substr(0, dot) : filename; } bool CreateFileDirectory(string& FullFilePath) { string dir = GetFileDirectory(FullFilePath); if (dir.empty()) return false; string command = "mkdir -p " + dir; return system(command.c_str()) == 0; } //=== 文件系统遍历 ===// bool FindSubFiles(string rootDir, vector& filelist, bool Recursive, string strWildcard) { DIR* dir = opendir(rootDir.c_str()); if (!dir) return false; struct dirent* entry; while ((entry = readdir(dir)) != nullptr) { string name = entry->d_name; if (name == "." || name == "..") continue; string fullpath = rootDir + "/" + name; if (entry->d_type == DT_DIR && Recursive) { FindSubFiles(fullpath, filelist, Recursive, strWildcard); } else if (entry->d_type == DT_REG) { filelist.push_back(fullpath); } } closedir(dir); return !filelist.empty(); } //=== 其他辅助函数 ===// void RawToHex(const char* pRaw, DWORD RawLen, string& Hex) { Hex.reserve(RawLen * 3); for (DWORD i = 0; i < RawLen; i++) { char buf[4]; snprintf(buf, sizeof(buf), "%02X ", pRaw[i] & 0xFF); Hex.append(buf); } } string ReplaceSubString(string org, string& keystr, string& replacestr) { size_t pos = 0; while ((pos = org.find(keystr, pos)) != string::npos) { org.replace(pos, keystr.length(), replacestr); pos += replacestr.length(); } return org; } //=== 动态库函数信息 ===// std::vector GetDllFunctionInfo(const char* moduleName) { void* handle = dlopen(moduleName, RTLD_LAZY); if (!handle) return {}; // 获取动态库信息 Dl_info info; if (dladdr(handle, &info) == 0) { dlclose(handle); return {}; } // 简化实现:仅获取动态库路径 std::vector result; result.push_back(make_tuple(info.dli_fname, 0, 0)); dlclose(handle); return result; }