123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- #include "AppConfig.h"
- #if defined(WIN32)
- #include <Windows.h>
- #include <direct.h>
- #include <io.h> //C (Windows) access
- #else
- #include <sys/time.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <vector>
- #include <stdarg.h>
- void Sleep(long mSeconds)
- {
- usleep(mSeconds * 1000);
- }
- #endif
- #include <time.h>
- int AppConfig::VERSION = 1;
- char AppConfig::VERSION_NAME[32] = "1.0.0";
- AppConfig::AppConfig()
- {
- }
- void AppConfig::mkdir(char *dirName)
- {
- #if defined(WIN32)
- ///如果目录不存在 则创建它
- if (access(dirName, 0)!=0)
- {
- _mkdir(dirName);
- }
- #else
- ///如果目录不存在 则创建它
- if (access(dirName, R_OK)!=0)
- {
- char cmd[128];
- sprintf(cmd,"mkdir %s", dirName);
- system(cmd);
- }
- #endif
- }
- void AppConfig::mkpath(char *path)
- {
- #if defined(WIN32)
- ///windows创建文件夹命令 路径得是反斜杠 因此这里需要替换一下
- char dirPath[128] = {0};
- strcpy(dirPath, path);
- AppConfig::replaceChar(dirPath, '/', '\\');
- ///如果目录不存在 则创建它
- if (access(dirPath, 0)!=0)
- {
- // _mkdir(dirPath);
- char cmd[128];
- sprintf(cmd,"mkdir %s", dirPath);
- system(cmd);
- }
- #else
- ///如果目录不存在 则创建它
- if (access(path,R_OK)!=0)
- {
- char cmd[128];
- sprintf(cmd,"mkdir %s -p",path);
- system(cmd);
- }
- #endif
- }
- void AppConfig::removeDir(char *dirName)
- {
- if (strlen(dirName) <= 0) return;
- if (access(dirName, 0) != 0 ) //文件夹不存在
- {
- return;
- }
- #if defined(WIN32)
- ///删除本地文件
- char cmd[128];
- sprintf(cmd,"rd /s /q \"%s\"", dirName);
- system(cmd);
- #else
- char cmd[128];
- sprintf(cmd,"rm -rf \"%s\"",dirName);
- system(cmd);
- #endif
- }
- bool AppConfig::removeFile(const char *filePath)
- {
- if (filePath == NULL || strlen(filePath) <= 0) return false;
- bool isSucceed = false;
- #if defined(WIN32)
- ///删除本地文件
- int ret = remove(filePath);
- if (ret == 0)
- {
- isSucceed = true;
- }
- #else
- ///删除本地文件
- char cmd[128] = {0};
- sprintf(cmd,"rm -rf \"%s\"",filePath);
- int ret = system(cmd);
- #endif
- return isSucceed;
- }
- void AppConfig::copyFile(const char *srcFile, const char *destFile)
- {
- #if defined(WIN32)
- CopyFileA(srcFile, destFile, FALSE);
- #else
- ///将文件拷贝到远端服务器
- char copyfilecmd[512];
- sprintf(copyfilecmd,"cp \"%s\" \"%s\"", srcFile, destFile);
- system(copyfilecmd);
- #endif
- }
- std::string AppConfig::stringFormat(const char * fmt, ...)
- {
- #if defined(WIN32)
- std::string _str;
- va_list marker = NULL;
- va_start(marker, fmt);
- size_t num_of_chars = _vscprintf(fmt, marker);
- _str.resize(num_of_chars);
- vsprintf_s((char *)_str.c_str(), num_of_chars + 1, fmt, marker);
- va_end(marker);
- return _str;
- #else
- std::string strResult="";
- if (NULL != fmt)
- {
- // va_list marker = NULL;
- va_list marker;
- va_start(marker, fmt); //初始化变量参数
- size_t nLength = vprintf(fmt, marker) + 1; //获取格式化字符串长度
- std::vector<char> vBuffer(nLength, '\0'); //创建用于存储格式化字符串的字符数组
- int nWritten = vsnprintf(&vBuffer[0], vBuffer.size(), fmt, marker);
- if (nWritten>0)
- {
- strResult = &vBuffer[0];
- }
- va_end(marker); //重置变量参数
- }
- return strResult;
- #endif
- }
- std::string AppConfig::stringReplaceAll(std::string &str, const std::string &old_value, const std::string &new_value)
- {
- for(std::string::size_type pos(0); pos!=std::string::npos; pos+=new_value.length())
- {
- if((pos=str.find(old_value,pos))!=std::string::npos)
- str.replace(pos,old_value.length(),new_value);
- else
- break;
- }
- return str;
- }
- void AppConfig::replaceChar(char *string, char oldChar, char newChar)
- {
- int len = strlen(string);
- int i;
- for (i = 0; i < len; i++){
- if (string[i] == oldChar){
- string[i] = newChar;
- }
- }
- }
- std::string AppConfig::removeFirstAndLastSpace(std::string &s)
- {
- if (s.empty())
- {
- return s;
- }
- s.erase(0,s.find_first_not_of(" "));
- s.erase(s.find_last_not_of(" ") + 1);
- return s;
- }
- std::string AppConfig::getIpFromRtspUrl(std::string rtspUrl)
- {
- std::string strIP;
- std::string strUrl = rtspUrl;
- if(strUrl.find("@") == std::string::npos)
- {
- long nPos1 = strUrl.find("//");
- long nPos2 = strUrl.rfind(":");
- if(nPos1 != std::string::npos && nPos2 != std::string::npos)
- {
- long nOffset = nPos2 - nPos1 - strlen("//");
- strIP = strUrl.substr(nPos1 + strlen("//"), nOffset);
- }
- }
- else
- {
- long nPos1 = strUrl.find("@");
- long nPos2 = strUrl.rfind(":");
- if(nPos1 != std::string::npos && nPos2 != std::string::npos)
- {
- long nOffset = nPos2 - nPos1 - strlen("@");
- strIP = strUrl.substr(nPos1 + strlen("@"), nOffset);
- int index = strIP.find("/");
- strIP = strIP.substr(0, index);
- }
- }
- return strIP;
- }
- void AppConfig::mSleep(int mSecond)
- {
- #if defined(WIN32)
- Sleep(mSecond);
- #else
- usleep(mSecond * 1000);
- #endif
- }
- int64_t AppConfig::getTimeStamp_MilliSecond()
- {
- int mSecond = 0; //当前毫秒数
- #if defined(WIN32)
- SYSTEMTIME sys;
- GetLocalTime( &sys );
- mSecond = sys.wMilliseconds;
- #else
- struct timeval tv;
- struct timezone tz;
- struct tm *p;
- gettimeofday(&tv, &tz);
- p = localtime(&tv.tv_sec);
- mSecond = tv.tv_usec / 1000;
- #endif
- int64_t timeStamp = (int64_t)time(NULL) * 1000 + mSecond;
- return timeStamp;
- }
|