AppConfig.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "AppConfig.h"
  2. #if defined(WIN32)
  3. #include <Windows.h>
  4. #include <direct.h>
  5. #include <io.h> //C (Windows) access
  6. #else
  7. #include <sys/time.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. void Sleep(long mSeconds)
  11. {
  12. usleep(mSeconds * 1000);
  13. }
  14. #endif
  15. #include <time.h>
  16. int AppConfig::VERSION = 1;
  17. char AppConfig::VERSION_NAME[32] = "1.0.0";
  18. AppConfig::AppConfig()
  19. {
  20. }
  21. void AppConfig::mkdir(char *dirName)
  22. {
  23. #if defined(WIN32)
  24. ///如果目录不存在 则创建它
  25. if (access(dirName, 0)!=0)
  26. {
  27. _mkdir(dirName);
  28. }
  29. #else
  30. ///如果目录不存在 则创建它
  31. if (access(dirName, R_OK)!=0)
  32. {
  33. char cmd[128];
  34. sprintf(cmd,"mkdir %s", dirName);
  35. system(cmd);
  36. }
  37. #endif
  38. }
  39. void AppConfig::mkpath(char *path)
  40. {
  41. #if defined(WIN32)
  42. ///windows创建文件夹命令 路径得是反斜杠 因此这里需要替换一下
  43. char dirPath[128] = {0};
  44. strcpy(dirPath, path);
  45. AppConfig::replaceChar(dirPath, '/', '\\');
  46. ///如果目录不存在 则创建它
  47. if (access(dirPath, 0)!=0)
  48. {
  49. // _mkdir(dirPath);
  50. char cmd[128];
  51. sprintf(cmd,"mkdir %s", dirPath);
  52. system(cmd);
  53. }
  54. #else
  55. ///如果目录不存在 则创建它
  56. if (access(path,R_OK)!=0)
  57. {
  58. char cmd[128];
  59. sprintf(cmd,"mkdir %s -p",path);
  60. system(cmd);
  61. }
  62. #endif
  63. }
  64. void AppConfig::removeDir(char *dirName)
  65. {
  66. if (strlen(dirName) <= 0) return;
  67. if (access(dirName, 0) != 0 ) //文件夹不存在
  68. {
  69. return;
  70. }
  71. #if defined(WIN32)
  72. ///删除本地文件
  73. char cmd[128];
  74. sprintf(cmd,"rd /s /q \"%s\"", dirName);
  75. system(cmd);
  76. #else
  77. char cmd[128];
  78. sprintf(cmd,"rm -rf \"%s\"",dirName);
  79. system(cmd);
  80. #endif
  81. }
  82. void AppConfig::removeFile(const char *filePath)
  83. {
  84. if (filePath == NULL || strlen(filePath) <= 0) return;
  85. #if defined(WIN32)
  86. ///删除本地文件
  87. remove(filePath);
  88. #else
  89. ///删除本地文件
  90. char cmd[128] = {0};
  91. sprintf(cmd,"rm -rf \"%s\"",filePath);
  92. system(cmd);
  93. #endif
  94. }
  95. void AppConfig::copyFile(const char *srcFile, const char *destFile)
  96. {
  97. #if defined(WIN32)
  98. CopyFileA(srcFile, destFile, FALSE);
  99. #else
  100. ///将文件拷贝到远端服务器
  101. char copyfilecmd[512];
  102. sprintf(copyfilecmd,"cp \"%s\" \"%s\"", srcFile, destFile);
  103. system(copyfilecmd);
  104. #endif
  105. }
  106. void AppConfig::replaceChar(char *string, char oldChar, char newChar)
  107. {
  108. int len = strlen(string);
  109. int i;
  110. for (i = 0; i < len; i++){
  111. if (string[i] == oldChar){
  112. string[i] = newChar;
  113. }
  114. }
  115. }
  116. std::string AppConfig::removeFirstAndLastSpace(std::string &s)
  117. {
  118. if (s.empty())
  119. {
  120. return s;
  121. }
  122. s.erase(0,s.find_first_not_of(" "));
  123. s.erase(s.find_last_not_of(" ") + 1);
  124. return s;
  125. }
  126. std::string AppConfig::getIpFromRtspUrl(std::string rtspUrl)
  127. {
  128. std::string strIP;
  129. std::string strUrl = rtspUrl;
  130. if(strUrl.find("@") == std::string::npos)
  131. {
  132. long nPos1 = strUrl.find("//");
  133. long nPos2 = strUrl.rfind(":");
  134. if(nPos1 != std::string::npos && nPos2 != std::string::npos)
  135. {
  136. long nOffset = nPos2 - nPos1 - strlen("//");
  137. strIP = strUrl.substr(nPos1 + strlen("//"), nOffset);
  138. }
  139. }
  140. else
  141. {
  142. long nPos1 = strUrl.find("@");
  143. long nPos2 = strUrl.rfind(":");
  144. if(nPos1 != std::string::npos && nPos2 != std::string::npos)
  145. {
  146. long nOffset = nPos2 - nPos1 - strlen("@");
  147. strIP = strUrl.substr(nPos1 + strlen("@"), nOffset);
  148. int index = strIP.find("/");
  149. strIP = strIP.substr(0, index);
  150. }
  151. }
  152. return strIP;
  153. }
  154. void AppConfig::mSleep(int mSecond)
  155. {
  156. #if defined(WIN32)
  157. Sleep(mSecond);
  158. #else
  159. usleep(mSecond * 1000);
  160. #endif
  161. }
  162. int64_t AppConfig::getTimeStamp_MilliSecond()
  163. {
  164. int mSecond = 0; //当前毫秒数
  165. #if defined(WIN32)
  166. SYSTEMTIME sys;
  167. GetLocalTime( &sys );
  168. mSecond = sys.wMilliseconds;
  169. #else
  170. struct timeval tv;
  171. struct timezone tz;
  172. struct tm *p;
  173. gettimeofday(&tv, &tz);
  174. p = localtime(&tv.tv_sec);
  175. mSecond = tv.tv_usec / 1000;
  176. #endif
  177. int64_t timeStamp = (int64_t)time(NULL) * 1000 + mSecond;
  178. return timeStamp;
  179. }