common_api.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // common_api.cpp
  2. #include <time.h>
  3. #include <sys/time.h>
  4. #include <pthread.h>
  5. #include <dlfcn.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include "common_api.h"
  9. #include <iostream>
  10. // 全局互斥锁初始化
  11. common_api::common_api(void) {
  12. pthread_mutex_init(&m_NotifyMutex, NULL);
  13. }
  14. common_api::~common_api(void) {
  15. pthread_mutex_destroy(&m_NotifyMutex);
  16. }
  17. common_api g_common1_for_init;
  18. //=== 时间相关函数 ===//
  19. __time64_t GetCurrentRealTimeOfStk() {
  20. return time(NULL);
  21. }
  22. UINT64 GetDay(UINT64 TheTime) {
  23. struct tm tCur;
  24. gmtime_r((time_t*)&TheTime, &tCur);
  25. tCur.tm_sec = 0;
  26. tCur.tm_min = 0;
  27. tCur.tm_hour = 0;
  28. return mktime(&tCur);
  29. }
  30. int CompareTimeByDay(UINT64 Prev, UINT64 Cur) {
  31. return (Cur / (24 * 3600)) - (Prev / (24 * 3600));
  32. }
  33. //=== GUID转换 ===//
  34. bool string_2_guid(const char* pstr, GUID& stGuid) {
  35. return sscanf(pstr, "{%8lx-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx}",
  36. &stGuid.Data1, &stGuid.Data2, &stGuid.Data3,
  37. &stGuid.Data4[0], &stGuid.Data4[1], &stGuid.Data4[2], &stGuid.Data4[3],
  38. &stGuid.Data4[4], &stGuid.Data4[5], &stGuid.Data4[6], &stGuid.Data4[7]) == 11;
  39. }
  40. bool guid_2_string(GUID& stGuid, string& str) {
  41. char szBuff[128];
  42. snprintf(szBuff, sizeof(szBuff), "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  43. stGuid.Data1, stGuid.Data2, stGuid.Data3,
  44. stGuid.Data4[0], stGuid.Data4[1], stGuid.Data4[2], stGuid.Data4[3],
  45. stGuid.Data4[4], stGuid.Data4[5], stGuid.Data4[6], stGuid.Data4[7]);
  46. str = szBuff;
  47. return true;
  48. }
  49. //=== 环境变量操作 ===//
  50. bool AddEnvPath(const char* pPath) {
  51. const char* env_var_name = "LD_LIBRARY_PATH"; // Linux使用LD_LIBRARY_PATH
  52. const char* old_path = getenv(env_var_name);
  53. // 如果路径已存在,直接返回成功
  54. if (old_path && strstr(old_path, pPath) != nullptr)
  55. return true;
  56. // 构建新的环境变量值
  57. std::string new_path;
  58. if (old_path) {
  59. new_path = std::string(old_path) + ":" + pPath;
  60. }
  61. else {
  62. new_path = pPath;
  63. }
  64. // 设置环境变量
  65. return setenv(env_var_name, new_path.c_str(), 1) == 0;
  66. }
  67. bool DelEnvPath(const char* pPath) {
  68. return false; // Linux下不建议直接操作PATH字符串
  69. }
  70. static bool SplitStringSub(string& Path, char key, vector<string>& nodes) {
  71. string node;
  72. size_t readcount = 0;
  73. string::size_type firstHit = Path.find_first_of(key);
  74. if (firstHit == string::npos) {
  75. if (Path.size() > 0) {
  76. nodes.push_back(Path);
  77. }
  78. return true;
  79. }
  80. if (firstHit > 0) {
  81. node = Path.substr(0, firstHit);
  82. if (node.size() > 0) {
  83. nodes.push_back(node);
  84. }
  85. }
  86. while (firstHit < Path.size()) {
  87. firstHit += 1;
  88. if (firstHit >= Path.size()) break;
  89. string::size_type SecondHit = Path.find_first_of(key, firstHit);
  90. if (SecondHit == string::npos) {
  91. node = Path.substr(firstHit, Path.size() - firstHit);
  92. if (node.size() > 0) {
  93. nodes.push_back(node);
  94. }
  95. break;
  96. }
  97. node = Path.substr(firstHit, SecondHit - firstHit);
  98. if (node.size() > 0) {
  99. nodes.push_back(node);
  100. }
  101. firstHit = SecondHit;
  102. }
  103. return (nodes.size() > 0);
  104. }
  105. //=== 字符串处理 ===//
  106. bool SplitString(const char* pString, string args, vector<string>& nodes) {
  107. vector<string> req;
  108. vector<string> res;
  109. req.push_back(string(pString));
  110. for (size_t i = 0; i < args.size(); i++)
  111. {
  112. //cut all req by key
  113. for (size_t j = 0; j < req.size(); j++)
  114. {
  115. SplitStringSub(req[j], args[i], res);
  116. }
  117. //clear and reset
  118. req.clear();
  119. req = res;
  120. res.clear();
  121. }
  122. nodes = req;
  123. return true;
  124. }
  125. bool SplitTo84422222222String(const char* pString, vector<string>& nodes) {
  126. string total;
  127. vector<string> thelist;
  128. SplitString(pString, string("{}-"), thelist);
  129. for (size_t i = 0; i < thelist.size(); i++)
  130. {
  131. total += thelist[i];
  132. }
  133. if (total.size() != 32)
  134. {
  135. return false;
  136. }
  137. string temp;
  138. temp = total.substr(0, 8);//8
  139. nodes.push_back(temp);
  140. temp = total.substr(8, 4);//4
  141. nodes.push_back(temp);
  142. temp = total.substr(12, 4);//4
  143. nodes.push_back(temp);
  144. temp = total.substr(16, 2);//2
  145. nodes.push_back(temp);
  146. temp = total.substr(18, 2);//2
  147. nodes.push_back(temp);
  148. temp = total.substr(20, 2);//2
  149. nodes.push_back(temp);
  150. temp = total.substr(22, 2);//2
  151. nodes.push_back(temp);
  152. temp = total.substr(24, 2);//2
  153. nodes.push_back(temp);
  154. temp = total.substr(26, 2);//2
  155. nodes.push_back(temp);
  156. temp = total.substr(28, 2);//2
  157. nodes.push_back(temp);
  158. temp = total.substr(30, 2);//2
  159. nodes.push_back(temp);
  160. return true;
  161. }
  162. string FormatstdString(const char* fmt, ...) {
  163. va_list args;
  164. va_start(args, fmt);
  165. char buffer[1024];
  166. vsnprintf(buffer, sizeof(buffer), fmt, args);
  167. va_end(args);
  168. return string(buffer);
  169. }
  170. bool getBusIdFromFilePath(const char* pFilePath, string& BusId)
  171. {
  172. string Path = pFilePath;
  173. size_t readcount = 0;
  174. size_t firstHit = Path.find_first_of('/');
  175. if (firstHit == string::npos || firstHit != 0)
  176. {
  177. return false;
  178. }
  179. size_t SecondHit = Path.find_first_of('/', 1);
  180. if (SecondHit == string::npos)
  181. {
  182. BusId = Path.substr(1, Path.size() - 1);
  183. }
  184. else
  185. {
  186. BusId = Path.substr(1, SecondHit - 1);
  187. }
  188. if (BusId.size() == 0)
  189. {
  190. return false;
  191. }
  192. return true;
  193. }
  194. bool SplitDevicePath(const char* pDevicePath, vector<string>& nodes)
  195. {
  196. string node;
  197. string Path = pDevicePath;
  198. size_t readcount = 0;
  199. nodes.clear();
  200. string::size_type firstHit = Path.find_first_of('/');
  201. if (firstHit == string::npos || firstHit != 0)
  202. {
  203. return false;
  204. }
  205. while (firstHit < Path.size())
  206. {
  207. firstHit += 1;
  208. string::size_type SecondHit = Path.find_first_of('/', firstHit);
  209. if (SecondHit == string::npos)
  210. {
  211. node = Path.substr(firstHit, Path.size() - firstHit);
  212. if (node.size() > 0)
  213. {
  214. nodes.push_back(node);
  215. }
  216. return true;
  217. }
  218. node = Path.substr(firstHit, SecondHit - (firstHit));
  219. if (node.size() > 0)
  220. {
  221. nodes.push_back(node);
  222. }
  223. firstHit = SecondHit;
  224. }
  225. return (nodes.size() > 0);
  226. }
  227. void SplitDeviceList(const char* deviceList, std::vector<std::string>& result)
  228. {
  229. result.clear();
  230. // 处理空输入情况
  231. if (deviceList == nullptr || *deviceList == '\0') return;
  232. // 创建可修改副本(strtok会破坏原字符串)
  233. char* buffer = new char[strlen(deviceList) + 1];
  234. strcpy(buffer, deviceList);
  235. // 使用strtok分割字符串
  236. const char* delimiter = ";";
  237. char* token = strtok(buffer, delimiter);
  238. while (token != nullptr) {
  239. // 过滤空字符串(避免连续分号产生空项)
  240. if (strlen(token) > 0) {
  241. result.push_back(token);
  242. }
  243. token = strtok(nullptr, delimiter);
  244. }
  245. delete[] buffer; // 释放临时内存
  246. }
  247. //=== 文件路径操作 ===//
  248. string GetProcessDirectory() {
  249. char path[PATH_MAX];
  250. ssize_t len = readlink("/proc/self/exe", path, sizeof(path) - 1);
  251. std::cout << "[DEBUG] len == -1 GetProcessDirectory() len: " << len << "path:" << path << std::endl;
  252. if (len == -1) return "";
  253. path[len] = '\0';
  254. std::cout << "[DEBUG] GetProcessDirectory() len: " << len << "path:" << path << std::endl;
  255. char* last_slash = strrchr(path, '/');
  256. if (last_slash) *last_slash = '\0';
  257. return path;
  258. }
  259. string GetFileDirectory(string& fullpath) {
  260. size_t pos = fullpath.find_last_of('/');
  261. return (pos != string::npos) ? fullpath.substr(0, pos) : "";
  262. }
  263. bool FindSubVersionDirs(string rootDir, map<string, vector<string>>& dirlist)
  264. {
  265. return false;
  266. }
  267. string GetFileName(string& fullpath) {
  268. size_t pos = fullpath.find_last_of('/');
  269. return (pos != string::npos) ? fullpath.substr(pos + 1) : fullpath;
  270. }
  271. string GetFileTitle(string& fullpath) {
  272. string filename = GetFileName(fullpath);
  273. size_t dot = filename.find_last_of('.');
  274. return (dot != string::npos) ? filename.substr(0, dot) : filename;
  275. }
  276. bool CreateFileDirectory(string& FullFilePath) {
  277. string dir = GetFileDirectory(FullFilePath);
  278. if (dir.empty()) return false;
  279. string command = "mkdir -p " + dir;
  280. return system(command.c_str()) == 0;
  281. }
  282. //=== 文件系统遍历 ===//
  283. bool FindSubFiles(string rootDir, vector<string>& filelist, bool Recursive, string strWildcard) {
  284. DIR* dir = opendir(rootDir.c_str());
  285. if (!dir) return false;
  286. struct dirent* entry;
  287. while ((entry = readdir(dir)) != nullptr) {
  288. string name = entry->d_name;
  289. if (name == "." || name == "..") continue;
  290. string fullpath = rootDir + "/" + name;
  291. if (entry->d_type == DT_DIR && Recursive) {
  292. FindSubFiles(fullpath, filelist, Recursive, strWildcard);
  293. }
  294. else if (entry->d_type == DT_REG) {
  295. filelist.push_back(fullpath);
  296. }
  297. }
  298. closedir(dir);
  299. return !filelist.empty();
  300. }
  301. //=== 其他辅助函数 ===//
  302. void RawToHex(const char* pRaw, DWORD RawLen, string& Hex) {
  303. Hex.reserve(RawLen * 3);
  304. for (DWORD i = 0; i < RawLen; i++) {
  305. char buf[4];
  306. snprintf(buf, sizeof(buf), "%02X ", pRaw[i] & 0xFF);
  307. Hex.append(buf);
  308. }
  309. }
  310. string ReplaceSubString(string org, string& keystr, string& replacestr) {
  311. size_t pos = 0;
  312. while ((pos = org.find(keystr, pos)) != string::npos) {
  313. org.replace(pos, keystr.length(), replacestr);
  314. pos += replacestr.length();
  315. }
  316. return org;
  317. }
  318. //=== 动态库函数信息 ===//
  319. std::vector<export_functions> GetDllFunctionInfo(const char* moduleName) {
  320. void* handle = dlopen(moduleName, RTLD_LAZY);
  321. if (!handle) return {};
  322. // 获取动态库信息
  323. Dl_info info;
  324. if (dladdr(handle, &info) == 0) {
  325. dlclose(handle);
  326. return {};
  327. }
  328. // 简化实现:仅获取动态库路径
  329. std::vector<export_functions> result;
  330. result.push_back(make_tuple(info.dli_fname, 0, 0));
  331. dlclose(handle);
  332. return result;
  333. }