123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- // common_api.cpp
- #include <time.h>
- #include <sys/time.h>
- #include <pthread.h>
- #include <dlfcn.h>
- #include <unistd.h>
- #include <limits.h>
- #include "common_api.h"
- #include <iostream>
- // 全局互斥锁初始化
- 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<string>& 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<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, ...) {
- 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<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);
- }
- void SplitDeviceList(const char* deviceList, std::vector<std::string>& 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<string, vector<string>>& 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<string>& 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<export_functions> 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<export_functions> result;
- result.push_back(make_tuple(info.dli_fname, 0, 0));
- dlclose(handle);
- return result;
- }
|