123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- #include "stdafx.h"
- #include "BaseFun.h"
- #include "array.h"
- #ifdef RVC_OS_WIN
- #include <ShlDisp.h>
- #include <direct.h>
- #include <io.h>
- #else
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #endif // RVC_OS_WIN
- #ifndef RVC_MAX_INI_LEN
- #define RVC_MAX_INI_LEN 1024*16
- #endif // !RVC_MAX_INI_LEN
- bool Unzip2Folder(BSTR lpZipFile, BSTR lpFolder)
- {
- #ifdef RVC_OS_WIN
- IShellDispatch* pISD;
- Folder* pZippedFile = 0L;
- Folder* pDestination = 0L;
- long FilesCount = 0;
- IDispatch* pItem = 0L;
- FolderItems* pFilesInside = 0L;
- VARIANT Options, OutFolder, InZipFile, Item;
- CoInitialize(NULL);
- __try {
- if (CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)& pISD) != S_OK)
- return 1;
- InZipFile.vt = VT_BSTR;
- InZipFile.bstrVal = lpZipFile;
- pISD->NameSpace(InZipFile, &pZippedFile);
- if (!pZippedFile)
- {
- pISD->Release();
- return 1;
- }
- OutFolder.vt = VT_BSTR;
- OutFolder.bstrVal = lpFolder;
- pISD->NameSpace(OutFolder, &pDestination);
- if (!pDestination)
- {
- pZippedFile->Release();
- pISD->Release();
- return 1;
- }
- pZippedFile->Items(&pFilesInside);
- if (!pFilesInside)
- {
- pDestination->Release();
- pZippedFile->Release();
- pISD->Release();
- return 1;
- }
- pFilesInside->get_Count(&FilesCount);
- if (FilesCount < 1)
- {
- pFilesInside->Release();
- pDestination->Release();
- pZippedFile->Release();
- pISD->Release();
- return 0;
- }
- pFilesInside->QueryInterface(IID_IDispatch, (void**)& pItem);
- Item.vt = VT_DISPATCH;
- Item.pdispVal = pItem;
- Options.vt = VT_I4;
- Options.lVal = 1024 | 512 | 16 | 4;
- bool retval = pDestination->CopyHere(Item, Options) == S_OK;
- pItem->Release(); pItem = 0L;
- pFilesInside->Release(); pFilesInside = 0L;
- pDestination->Release(); pDestination = 0L;
- pZippedFile->Release(); pZippedFile = 0L;
- pISD->Release(); pISD = 0L;
- return retval;
- }
- __finally
- {
- CoUninitialize();
- }
- #else
- return true;
- #endif
- }
- void split(const string& src, const string& separator, vector<string>& dest)
- {
- string str = src;
- string substring;
- string::size_type start = 0, index;
- do
- {
- index = str.find_first_of(separator, start);
- if (index != string::npos)
- {
- substring = str.substr(start, index - start);
- dest.push_back(substring);
- start = str.find_first_not_of(separator, index);
- if (start == string::npos) return;
- }
- } while (index != string::npos);
- //the last token
- substring = str.substr(start);
- dest.push_back(substring);
- }
- bool checkDirExist(const string &strPath)
- {
- #ifdef RVC_OS_WIN
- WIN32_FIND_DATA wfd;
- bool rValue = false;
- HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
- if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- rValue = true;
- FindClose(hFind);
- return rValue;
- #else
- if (ExistsDir(strPath.c_str()))
- {
- return true;
- }
- else
- {
- return false;
- }
- #endif
- }
- bool checkFileExist(string fileName)
- {
- #ifdef RVC_OS_WIN
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind;
- hFind = FindFirstFile(fileName.c_str(), &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
- return false;
- FindClose(hFind);
- return true;
- #else
- if (ExistsFile(fileName.c_str()))
- {
- return true;
- }
- else
- {
- return false;
- }
- #endif
- }
- void Wchar_tToString(std::string& szDst, wchar_t *wchar)
- {
- #ifdef RVC_OS_WIN
- wchar_t* wText = wchar;
- DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);// WideCharToMultiByte的运用
- char* psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
- psText = new char[dwNum];
- WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);// WideCharToMultiByte的再次运用
- szDst = psText;// std::string赋值
- delete[]psText;// psText的清除
- #else
- std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
- setlocale(LC_ALL, "chs");
- size_t _Dsize = 2 * wcslen(wchar) + 1;
- char* _Dest = new char[_Dsize];
- memset(_Dest, 0, _Dsize);
- wcstombs(_Dest, wchar, _Dsize);
- std::string result = _Dest;
- delete[]_Dest;
- setlocale(LC_ALL, curLocale.c_str());
- szDst = result;
- #endif
- }
- // string to wstring
- void StringToWstring(std::wstring& szDst, std::string str)
- {
- #ifdef RVC_OS_WIN
- std::string temp = str;
- int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
- wchar_t* wszUtf8 = new wchar_t[len + 1];
- memset(wszUtf8, 0, len * 2 + 2);
- MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
- szDst = wszUtf8;
- std::wstring r = wszUtf8;
- delete[] wszUtf8;
- #else
- setlocale(LC_ALL, "chs");
- const char* _Source = str.c_str();
- size_t _Dsize = str.size() + 1;
- wchar_t* _Dest = new wchar_t[_Dsize];
- wmemset(_Dest, 0, _Dsize);
- mbstowcs(_Dest, _Source, _Dsize);
- std::wstring result = _Dest;
- delete[]_Dest;
- setlocale(LC_ALL, "C");
- szDst = result;
- #endif
- }
- bool createDir(const string &filePath)
- {
- #ifdef RVC_OS_WIN
- return 0 == _mkdir(filePath.c_str());
- #else
- if (CreateDirRecursive(filePath.c_str()))
- {
- return true;
- }
- else
- {
- return false;
- }
- #endif
- }
- BOOL IsDirectory(const char *pDir)
- {
- char szCurPath[500] = {0};
- sprintf_s(szCurPath, 500, "%s//*", pDir);
- #ifdef RVC_OS_WIN
- WIN32_FIND_DATAA FindFileData;
- ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
- HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
- if (hFile == INVALID_HANDLE_VALUE)
- {
- FindClose(hFile);
- return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
- }
- else
- {
- FindClose(hFile);
- return TRUE;
- }
- #else
- struct stat buf;
- if (0 == stat(szCurPath, &buf))
- {
- return S_ISDIR(buf.st_mode);
- }
- else
- {
- return FALSE;
- }
- #endif // RVC_OS_WIN
- }
- BOOL DeleteDirectory(const char * DirName)
- {
- #ifdef RVC_OS_WIN
- // CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索
- char szCurPath[MAX_PATH]; //用于定义搜索格式
- _snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式为*.*,即该目录下的所有文件
- WIN32_FIND_DATAA FindFileData;
- ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
- HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData);
- BOOL IsFinded = TRUE;
- while (IsFinded)
- {
- IsFinded = FindNextFileA(hFile, &FindFileData); //递归搜索其他的文件
- if (strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) //如果不是"." ".."目录
- {
- std::string strFileName = "";
- strFileName = strFileName + DirName + "//" + FindFileData.cFileName;
- std::string strTemp;
- strTemp = strFileName;
- if (IsDirectory(strFileName.c_str())) //如果是目录,则递归地调用
- {
- printf("目录为:%s/n", strFileName.c_str());
- DeleteDirectory(strTemp.c_str());
- }
- else
- {
- DeleteFileA(strTemp.c_str());
- }
- }
- }
- FindClose(hFile);
- BOOL bRet = RemoveDirectoryA(DirName);
- if (bRet == 0) //删除目录
- {
- printf("删除%s目录失败!/n", DirName);
- return FALSE;
- }
- return TRUE;
- #else
- return RemoveDirRecursive(DirName);
- #endif // RVC_OS_WIN
- }
- bool removeDir(const string &filePaht)
- {
- return DeleteDirectory(filePaht.c_str());
- }
- void stopForDebug()
- {
- #ifdef RVC_OS_WIN
- DWORD processId = GetCurrentProcessId();
- char showMsg[100] = "";
- sprintf_s(showMsg, "Current pross id is %d", processId);
- MessageBox(NULL, showMsg, NULL, 0);
- #else
- #endif // RVC_OS_WIN
- }
- void getDirs(string path, vector<string> &ownname)
- {
- #ifdef RVC_OS_WIN
- long hFile = 0;
- struct _finddata_t fileinfo;
- string p;
- if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
- {
- do
- {
- //如果是目录加入列表
- if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") && strcmp(fileinfo.name, ".."))
- ownname.push_back(fileinfo.name);
- } while (_findnext(hFile, &fileinfo) == 0);
- _findclose(hFile);
- }
- #else
- array_header_t* subdirs = fileutil_get_sub_files(path.c_str());
- int i;
- for (i = 0; i < subdirs->nelts; ++i) {
- char* strsubdir = ARRAY_IDX(subdirs, i, char*);
- ownname.push_back(strsubdir);
- }
- #endif // RVC_OS_WIN
- }
- bool getUniqueDir(string path, string &dirName)
- {
- vector<string> dirs;
- getDirs(path, dirs);
- if (1 == dirs.size())
- {
- dirName = dirs[0];
- return true;
- }
- return false;
- }
- UINT GetPrivateProfileIntEx(LPCSTR lpAppName,LPCSTR lpKeyName,INT nDefault,LPCSTR lpFileName)
- {
- char lpReturnedString[MAX_PATH] = { 0 };
- DWORD nRet = GetPrivateProfileStringEx(
- lpAppName,
- lpKeyName,
- "",
- lpReturnedString,
- sizeof(lpReturnedString),
- lpFileName
- );
- if (nRet == 0) {
- return nDefault;
- }
-
- return atoi(lpReturnedString);
- }
- BOOL WritePrivateProfileStringEx(LPCSTR lpAppName,LPCSTR lpKeyName,LPCSTR lpString,LPCSTR lpFileName)
- {
- FILE* fp = NULL;
- static char szLine[RVC_MAX_INI_LEN] = { 0 };
- static char tmpstr[RVC_MAX_INI_LEN] = { 0 };
- memset(szLine, 0, sizeof(szLine));
- memset(tmpstr, 0, sizeof(tmpstr));
- int rtnval;
- int i = 0;
- int secFlag = 0;
- if ((fp = fopen(lpFileName, "rw+")) == NULL){
- return FALSE;
- }
- int lineLen = 0;//整行长度
- int orgEqualPos = 0;//=号在原行中的位置
- int equalPos = 0; //=号在去空格后的位置
- strcpy(tmpstr, "[");
- strcat(tmpstr, lpAppName);
- strcat(tmpstr, "]");
- int endFlag;
- while (!feof(fp))
- {
- rtnval = fgetc(fp);
- if (rtnval == EOF){
- //最后一行可能无换行符号
- rtnval = '\n';
- endFlag = 1;
- }
- //注释行
- if ('#' == rtnval || ';' == rtnval)
- {
- fgets(szLine, sizeof(szLine), fp);
- //reset
- i = 0;
- lineLen = 0;
- orgEqualPos = 0;
- equalPos = 0;
- memset(szLine, 0, sizeof(szLine));
- continue;
- }
- else if ('/' == rtnval)
- {
- szLine[i++] = rtnval;
- lineLen++;
- if ('/' == (rtnval = fgetc(fp))) //注释行
- {
- fgets(szLine, sizeof(szLine), fp);
- //reset
- i = 0;
- lineLen = 0;
- orgEqualPos = 0;
- equalPos = 0;
- memset(szLine, 0, sizeof(szLine));
- continue;
- }
- }
- if (rtnval != ' ' && rtnval != '\t')
- {
- szLine[i++] = rtnval; //去掉空格和tab后的字符串
- if (rtnval == '=')
- {
- orgEqualPos = lineLen;
- equalPos = i - 1;
- }
- }
- lineLen++; //字符
- if (rtnval == '\n'){
- szLine[--i] = '\0';
- if (szLine[--i] == '\r')
- szLine[i--] = '\0';
- if ((equalPos != 0) && (secFlag == 1))
- {
- szLine[equalPos] = '\0';
- if (strcasecmp(szLine, lpKeyName) == 0)
- {
- //找到key对应变量
- int leftPos = ftell(fp);
- int writePos = leftPos - lineLen + orgEqualPos + 1;
- fseek(fp, 0, SEEK_END);
- int leftLen = ftell(fp) - leftPos;
- char* pLeft = new char[leftLen];
- fseek(fp, leftPos, SEEK_SET);
- fread(pLeft, leftLen, 1, fp);
- fseek(fp, writePos, SEEK_SET);
- fwrite(lpString, strlen(lpString), 1, fp);
- fwrite("\n", sizeof(char), 1, fp);
- fwrite(pLeft, leftLen, 1, fp);
- delete[]pLeft;
- pLeft = 0;
- fclose(fp);
- return TRUE;
- }
- }
- else
- {
- if (strcasecmp(tmpstr, szLine) == 0)
- {
- //找到section
- secFlag = 1;
- }
- else if (secFlag == 1 && szLine[0] == '[' && szLine[i] == ']')
- {//进入下个section了,说明没找到
- int leftPos = ftell(fp) - lineLen;
- int writePos = leftPos;
- fseek(fp, 0, SEEK_END);
- int leftLen = ftell(fp) - leftPos;
- char* pLeft = new char[leftLen];
- fseek(fp, leftPos, SEEK_SET);
- fread(pLeft, leftLen, 1, fp);
- fseek(fp, writePos, SEEK_SET);
- fwrite("\n", sizeof(char), 1, fp);
- fwrite(lpKeyName, strlen(lpKeyName), 1, fp);
- fwrite("=", sizeof(char), 1, fp);
- fwrite(lpString, strlen(lpString), 1, fp);
- fwrite("\n", sizeof(char), 1, fp);
- fwrite(pLeft, leftLen, 1, fp);
- delete[]pLeft;
- pLeft = 0;
- fclose(fp);
- return TRUE;
- }
- }
- //reset
- if (endFlag == 1)
- break;
- i = 0;
- lineLen = 0;
- orgEqualPos = 0;
- equalPos = 0;
- memset(szLine, 0, sizeof(szLine));
- }
- }
- //到文件尾了
- if (secFlag)
- {//必须有section
- fseek(fp, 0, SEEK_END);
- fwrite("\n", sizeof(char), 1, fp);
- fwrite(lpKeyName, strlen(lpKeyName), 1, fp);
- fwrite("=", sizeof(char), 1, fp);
- fwrite(lpString, strlen(lpString), 1, fp);
- fwrite("\n", sizeof(char), 1, fp);
- }
- fclose(fp);
- return TRUE;
- }
- DWORD GetPrivateProfileStringEx(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName)
- {
- FILE* fp = NULL;
- static char szLine[RVC_MAX_INI_LEN] = { 0 };
- static char tmpstr[RVC_MAX_INI_LEN] = { 0 };
- int rtnval;
- int i = 0;
- int secFlag = 0;
- if ((fp = fopen(lpFileName, "r")) == NULL){
- Dbg("have no such file[%s]", lpFileName);
- return -1;
- }
- int equalPos = 0; //=号在去空格后的位置
- strcpy(tmpstr, "[");
- strcat(tmpstr, lpAppName);
- strcat(tmpstr, "]");
- int endFlag = 0;
- while (!feof(fp))
- {
- rtnval = fgetc(fp);
- if (rtnval == EOF){
- //最后一行可能无换行符号
- rtnval = '\n';
- endFlag = 1;
- }
- //注释行
- if ('#' == rtnval || ';' == rtnval)
- {
- fgets(szLine, sizeof(szLine), fp);
- //reset
- i = 0;
- equalPos = 0;
- memset(szLine, 0, sizeof(szLine));
- continue;
- }
- else if ('/' == rtnval)
- {
- szLine[i++] = rtnval;
- if ('/' == (rtnval = fgetc(fp))) //注释行
- {
- fgets(szLine, sizeof(szLine), fp);
- //reset
- i = 0;
- equalPos = 0;
- memset(szLine, 0, sizeof(szLine));
- continue;
- }
- }
- if (rtnval != ' ' && rtnval != '\t')
- {
- szLine[i++] = rtnval; //去掉空格和tab后的字符串
- if (rtnval == '=')
- {
- equalPos = i - 1;
- }
- }
- if (rtnval == '\n'){
- szLine[--i] = '\0';
- if (szLine[--i] == '\r')
- szLine[i--] = '\0';
- if ((equalPos != 0) && (secFlag == 1))
- {
- szLine[equalPos] = '\0'; //=号变0
- if (strcasecmp(szLine, lpKeyName) == 0)
- {
- //找到key对应变量
- strncpy(lpReturnedString, szLine + equalPos + 1, nSize - 1);
- lpReturnedString[nSize - 1] = '\0';
- fclose(fp);
- return 1;
- }
- }
- else
- {
- if (strcasecmp(tmpstr, szLine) == 0)
- {
- //找到section
- secFlag = 1;
- }
- else if (secFlag == 1 && szLine[0] == '[' && szLine[i] == ']')
- {//进入下个section了,说明没找到
- break;
- }
- }
- if (endFlag == 1)
- break;
- //reset
- i = 0;
- equalPos = 0;
- memset(szLine, 0, sizeof(szLine));
- }
- }
- fclose(fp);
- //没找到则用默认
- strncpy(lpReturnedString, lpDefault, nSize - 1);
- lpReturnedString[nSize - 1] = '\0';
- return 0;
- }
- DWORD GetPrivateProfileSectionEx(LPCSTR lpAppName, LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName)
- {
- //由于项目中未使用,暂未实现
- assert(0);
- return 0;
- }
- DWORD GetPrivateProfileSectionNamesEx(LPSTR lpszReturnBuffer, DWORD nSize, LPCSTR lpFileName)
- {
- FILE* fp = NULL;
- static char szLine[RVC_MAX_INI_LEN] = { 0 };
- static char tmpstr[RVC_MAX_INI_LEN] = { 0 };
- int rPos = 0;
- memset(lpszReturnBuffer, 0, nSize);
- int rtnval;
- int i = 0;
- int endFlag;
- if ((fp = fopen(lpFileName, "r")) == NULL){
- return -1;
- }
- while (!feof(fp))
- {
- rtnval = fgetc(fp);
- if (rtnval == EOF){
- //最后一行可能无换行符号
- rtnval = '\n';
- endFlag = 1;
- }
- //注释行
- if ('#' == rtnval || ';' == rtnval)
- {
- fgets(szLine, sizeof(szLine), fp);
- //reset
- i = 0;
- memset(szLine, 0, sizeof(szLine));
- continue;
- }
- else if ('/' == rtnval)
- {
- szLine[i++] = rtnval;
- if ('/' == (rtnval = fgetc(fp))) //注释行
- {
- fgets(szLine, sizeof(szLine), fp);
- //reset
- i = 0;
- memset(szLine, 0, sizeof(szLine));
- continue;
- }
- }
- if (rtnval != ' ' && rtnval != '\t')
- {
- szLine[i++] = rtnval; //去掉空格和tab后的字符串
- }
- if (rtnval == '\n')
- {
- szLine[--i] = '\0';
- if (szLine[--i] == '\r')
- szLine[i--] = '\0';
- if (szLine[0] == '[' && szLine[i] == ']')
- {
- //找到section
- for (int j = 1; j < i && rPos < nSize - 1; j++)
- lpszReturnBuffer[rPos++] = szLine[j];
- lpszReturnBuffer[rPos++] = '\0';
- if (rPos >= nSize)
- {
- break;
- }
- }
- if (endFlag == 1)
- break;
- //reset
- i = 0;
- memset(szLine, 0, sizeof(szLine));
- }
- }
- lpszReturnBuffer[rPos] = '\0';
- fclose(fp);
- return 0;
- }
|