#include "stdafx.h" #include "BaseFun.h" #include "array.h" #ifdef RVC_OS_WIN #include #include #include #include #else #include #include #include #endif // RVC_OS_WIN #ifndef RVC_MAX_INI_LEN #define RVC_MAX_INI_LEN 1024*16 #endif // !RVC_MAX_INI_LEN void split(const string& src, const string& separator, vector& 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) { if (ExistsDir(strPath.c_str())){ return true; } else{ return false; } } bool checkFileExist(string fileName) { if (ExistsFile(fileName.c_str())){ return true; } else{ return false; } } //TODO: CrossPlaform [Gifur@2025730] 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 } //TODO: CrossPlaform [Gifur@2025730] 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 } //TODO: CrossPlaform [Gifur@2025730] 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 } //TODO: CrossPlaform 采用框架统一的函数 [Gifur@2025728] BOOL IsDirectory(const char *pDir) { char szCurPath[MAX_PATH*2] = {0}; #ifdef RVC_OS_WIN _snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir); WIN32_FIND_DATAA FindFileData = {0}; //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 snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir); struct stat buf; if (0 == stat(szCurPath, &buf)) { return S_ISDIR(buf.st_mode); } else { return FALSE; } #endif // RVC_OS_WIN } void getDirs(string path, vector &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 }