AppConfig.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <vector>
  11. #include <stdarg.h>
  12. void Sleep(long mSeconds)
  13. {
  14. usleep(mSeconds * 1000);
  15. }
  16. #endif
  17. #include <time.h>
  18. int AppConfig::VERSION = 1;
  19. char AppConfig::VERSION_NAME[32] = "1.0.0";
  20. AppConfig::AppConfig()
  21. {
  22. }
  23. void AppConfig::mkdir(char *dirName)
  24. {
  25. #if defined(WIN32)
  26. ///如果目录不存在 则创建它
  27. if (access(dirName, 0)!=0)
  28. {
  29. _mkdir(dirName);
  30. }
  31. #else
  32. ///如果目录不存在 则创建它
  33. if (access(dirName, R_OK)!=0)
  34. {
  35. char cmd[128];
  36. sprintf(cmd,"mkdir %s", dirName);
  37. system(cmd);
  38. }
  39. #endif
  40. }
  41. void AppConfig::mkpath(char *path)
  42. {
  43. #if defined(WIN32)
  44. ///windows创建文件夹命令 路径得是反斜杠 因此这里需要替换一下
  45. char dirPath[128] = {0};
  46. strcpy(dirPath, path);
  47. AppConfig::replaceChar(dirPath, '/', '\\');
  48. ///如果目录不存在 则创建它
  49. if (access(dirPath, 0)!=0)
  50. {
  51. // _mkdir(dirPath);
  52. char cmd[128];
  53. sprintf(cmd,"mkdir %s", dirPath);
  54. system(cmd);
  55. }
  56. #else
  57. ///如果目录不存在 则创建它
  58. if (access(path,R_OK)!=0)
  59. {
  60. char cmd[128];
  61. sprintf(cmd,"mkdir %s -p",path);
  62. system(cmd);
  63. }
  64. #endif
  65. }
  66. void AppConfig::removeDir(char *dirName)
  67. {
  68. if (strlen(dirName) <= 0) return;
  69. if (access(dirName, 0) != 0 ) //文件夹不存在
  70. {
  71. return;
  72. }
  73. #if defined(WIN32)
  74. ///删除本地文件
  75. char cmd[128];
  76. sprintf(cmd,"rd /s /q \"%s\"", dirName);
  77. system(cmd);
  78. #else
  79. char cmd[128];
  80. sprintf(cmd,"rm -rf \"%s\"",dirName);
  81. system(cmd);
  82. #endif
  83. }
  84. bool AppConfig::removeFile(const char *filePath)
  85. {
  86. if (filePath == NULL || strlen(filePath) <= 0) return false;
  87. bool isSucceed = false;
  88. #if defined(WIN32)
  89. ///删除本地文件
  90. int ret = remove(filePath);
  91. if (ret == 0)
  92. {
  93. isSucceed = true;
  94. }
  95. #else
  96. ///删除本地文件
  97. char cmd[128] = {0};
  98. sprintf(cmd,"rm -rf \"%s\"",filePath);
  99. int ret = system(cmd);
  100. #endif
  101. return isSucceed;
  102. }
  103. void AppConfig::copyFile(const char *srcFile, const char *destFile)
  104. {
  105. #if defined(WIN32)
  106. CopyFileA(srcFile, destFile, FALSE);
  107. #else
  108. ///将文件拷贝到远端服务器
  109. char copyfilecmd[512];
  110. sprintf(copyfilecmd,"cp \"%s\" \"%s\"", srcFile, destFile);
  111. system(copyfilecmd);
  112. #endif
  113. }
  114. std::string AppConfig::stringFormat(const char * fmt, ...)
  115. {
  116. #if defined(WIN32)
  117. std::string _str;
  118. va_list marker = NULL;
  119. va_start(marker, fmt);
  120. size_t num_of_chars = _vscprintf(fmt, marker);
  121. _str.resize(num_of_chars);
  122. vsprintf_s((char *)_str.c_str(), num_of_chars + 1, fmt, marker);
  123. va_end(marker);
  124. return _str;
  125. #else
  126. std::string strResult="";
  127. if (NULL != fmt)
  128. {
  129. // va_list marker = NULL;
  130. va_list marker;
  131. va_start(marker, fmt); //初始化变量参数
  132. size_t nLength = vprintf(fmt, marker) + 1; //获取格式化字符串长度
  133. std::vector<char> vBuffer(nLength, '\0'); //创建用于存储格式化字符串的字符数组
  134. int nWritten = vsnprintf(&vBuffer[0], vBuffer.size(), fmt, marker);
  135. if (nWritten>0)
  136. {
  137. strResult = &vBuffer[0];
  138. }
  139. va_end(marker); //重置变量参数
  140. }
  141. return strResult;
  142. #endif
  143. }
  144. std::string AppConfig::stringReplaceAll(std::string &str, const std::string &old_value, const std::string &new_value)
  145. {
  146. for(std::string::size_type pos(0); pos!=std::string::npos; pos+=new_value.length())
  147. {
  148. if((pos=str.find(old_value,pos))!=std::string::npos)
  149. str.replace(pos,old_value.length(),new_value);
  150. else
  151. break;
  152. }
  153. return str;
  154. }
  155. void AppConfig::replaceChar(char *string, char oldChar, char newChar)
  156. {
  157. int len = strlen(string);
  158. int i;
  159. for (i = 0; i < len; i++){
  160. if (string[i] == oldChar){
  161. string[i] = newChar;
  162. }
  163. }
  164. }
  165. std::string AppConfig::removeFirstAndLastSpace(std::string &s)
  166. {
  167. if (s.empty())
  168. {
  169. return s;
  170. }
  171. s.erase(0,s.find_first_not_of(" "));
  172. s.erase(s.find_last_not_of(" ") + 1);
  173. return s;
  174. }
  175. std::string AppConfig::getIpFromRtspUrl(std::string rtspUrl)
  176. {
  177. std::string strIP;
  178. std::string strUrl = rtspUrl;
  179. if(strUrl.find("@") == std::string::npos)
  180. {
  181. long nPos1 = strUrl.find("//");
  182. long nPos2 = strUrl.rfind(":");
  183. if(nPos1 != std::string::npos && nPos2 != std::string::npos)
  184. {
  185. long nOffset = nPos2 - nPos1 - strlen("//");
  186. strIP = strUrl.substr(nPos1 + strlen("//"), nOffset);
  187. }
  188. }
  189. else
  190. {
  191. long nPos1 = strUrl.find("@");
  192. long nPos2 = strUrl.rfind(":");
  193. if(nPos1 != std::string::npos && nPos2 != std::string::npos)
  194. {
  195. long nOffset = nPos2 - nPos1 - strlen("@");
  196. strIP = strUrl.substr(nPos1 + strlen("@"), nOffset);
  197. int index = strIP.find("/");
  198. strIP = strIP.substr(0, index);
  199. }
  200. }
  201. return strIP;
  202. }
  203. void AppConfig::mSleep(int mSecond)
  204. {
  205. #if defined(WIN32)
  206. Sleep(mSecond);
  207. #else
  208. usleep(mSecond * 1000);
  209. #endif
  210. }
  211. int64_t AppConfig::getTimeStamp_MilliSecond()
  212. {
  213. int mSecond = 0; //当前毫秒数
  214. #if defined(WIN32)
  215. SYSTEMTIME sys;
  216. GetLocalTime( &sys );
  217. mSecond = sys.wMilliseconds;
  218. #else
  219. struct timeval tv;
  220. struct timezone tz;
  221. struct tm *p;
  222. gettimeofday(&tv, &tz);
  223. p = localtime(&tv.tv_sec);
  224. mSecond = tv.tv_usec / 1000;
  225. #endif
  226. int64_t timeStamp = (int64_t)time(NULL) * 1000 + mSecond;
  227. return timeStamp;
  228. }