📄 filespecutils.cpp
字号:
} if ( 0 == ::FindNextFile( hFile, &findFileData ) ) { if ( ERROR_NO_MORE_FILES != GetLastError() ) { fSize = 0; outResult = HXR_FAIL; } break; } } ::FindClose( hFile ); return HXR_OK;}//******************************************************************************CHXFileSpecifier CHXFileSpecUtils::GetCurrentApplication(void){ CHXFileSpecifier spec; char szBuf[_MAX_PATH]; /* Flawfinder: ignore */ if (GetModuleFileName(NULL,OS_STRING2(szBuf,_MAX_PATH),_MAX_PATH)) { spec = CHXFileSpecifier(szBuf); } return spec;}//******************************************************************************CHXDirSpecifier CHXFileSpecUtils::GetCurrentApplicationDir(void){ CHXFileSpecifier appSpec; CHXDirSpecifier dirSpec; appSpec = GetCurrentApplication(); if (appSpec.IsSet()) { dirSpec = appSpec.GetParentDirectory(); } return dirSpec;}//******************************************************************************BOOL CHXFileSpecUtils::FileExists(const CHXFileSpecifier& fileSpec){ BOOL bRet = FALSE; //struct stat statBuff; //ZeroInit(&statBuff); //if ( (stat(fileSpec.GetPathName(), &statBuff) == 0) && (statBuff.st_mode & _S_IFREG)) //bRet = TRUE; ULONG32 ulAtts = ::GetFileAttributes(OS_STRING(fileSpec.GetPathName())); if (ulAtts != 0xFFFFFFFF && !(ulAtts & FILE_ATTRIBUTE_DIRECTORY)) bRet = TRUE; return bRet;}//******************************************************************************BOOL CHXFileSpecUtils::DirectoryExists(const CHXDirSpecifier& dirSpec){ ULONG32 ulAtts = ::GetFileAttributes(OS_STRING(dirSpec.GetPathName())); if (ulAtts != 0xFFFFFFFF && (ulAtts & FILE_ATTRIBUTE_DIRECTORY)) return TRUE; return FALSE;}//******************************************************************************HX_RESULT CHXFileSpecUtils::CreateDir(const CHXDirSpecifier& dirSpec){ if (::CreateDirectory(OS_STRING(dirSpec.GetPathName()),NULL)) return TRUE; return FALSE;}//******************************************************************************static CHXFileSpecifier GetUniqueFileSpecInternal(const CHXDirSpecifier& locationSpec, const char *pszNameFirst, const char *pszTemplate, const char *pszWildcard, UINT32 nStartNum); const UINT32 kNumWrapValue = 9999+1; // limit insertions to 4-digit numbers CHXFileSpecifier CHXFileSpecUtils::GetUniqueFileSpec(const CHXDirSpecifier& locationSpec, const char *pszNameFirst, const char *pszTemplate, const char *pszWildcard){ return GetUniqueFileSpecInternal(locationSpec, pszNameFirst, pszTemplate, pszWildcard, 0);}CHXFileSpecifier CHXFileSpecUtils::GetUniqueTempFileSpec(const CHXDirSpecifier& locationSpec, const char *pszTemplate, const char *pszWildcard){ CMultiplePrimeRandom rand(HX_GET_TICKCOUNT()); UINT32 num; num = rand.GetRandomNumber(); num %= kNumWrapValue; if (num == 0 || num == 1) num = 2; return GetUniqueFileSpecInternal(locationSpec, NULL, pszTemplate, pszWildcard, num);}static CHXFileSpecifier GetUniqueFileSpecInternal(const CHXDirSpecifier& locationSpec, const char *pszNameFirst, const char *pszTemplate, const char *pszWildcard, UINT32 nStartNum){ CHXFileSpecifier resultFileSpec; require_return(locationSpec.IsSet(), resultFileSpec); require_return(pszTemplate != NULL && pszWildcard != NULL, resultFileSpec); require_return(pszNameFirst != NULL || nStartNum != 0, resultFileSpec); CHXFileSpecifier testFileSpec; CHXDirSpecifier testDirSpec; CHXString strNumber; CHXString strName; UINT32 nCurrentNum; nCurrentNum = nStartNum; while (1) { // if the number is non-zero, make a string from the template; // if the number is zero, user the initial name string if (nCurrentNum == 0) { // replace the wildcard in the template with the number string strName = pszNameFirst; } else { // replace the wildcard in the template with the number string strNumber.Empty(); strNumber.AppendULONG(nCurrentNum); strName = pszTemplate; strName.FindAndReplace(pszWildcard, strNumber); // replace first wildcard with number string } // test if a file or directory exists with that name testFileSpec = locationSpec.SpecifyChildFile(strName); testDirSpec = locationSpec.SpecifyChildDirectory(strName); if (CHXFileSpecUtils::FileExists(testFileSpec) || CHXFileSpecUtils::DirectoryExists(testDirSpec)) { // an item already has that name, so increment & wrap the number nCurrentNum++; nCurrentNum %= kNumWrapValue; // don't use 0 again, and skip 1 since "MyFile2.txt" logically follows "MyFile.txt" if (nCurrentNum == 0 || nCurrentNum == 1) { nCurrentNum = 2; } // a quick sanity check if (nCurrentNum == nStartNum) { check(!"GetUniqueFileSpecInternal number wrapped"); break; } } else { // the name is unique resultFileSpec = testFileSpec; break; } } // while return resultFileSpec;}//******************************************************************************CHXDirSpecifier CHXFileSpecUtils::GetSystemTempDirectory(){ char szBuf[MAX_PATH] = ""; /* Flawfinder: ignore */#if !defined(WIN32_PLATFORM_PSPC) ::GetTempPath(MAX_PATH,szBuf);#endif /* !defined(WIN32_PLATFORM_PSPC) */ CHXDirSpecifier retSpec(szBuf); return retSpec;}BOOL CHXFileSpecUtils::MakeNameLegal(char *pszName){ const char *badChars = "\\/:*?\"<>|"; const char replacementChar = '-'; const long maxNameLength = 255; char *pCurrChar = pszName; BOOL bChanged; require_nonnull_return(pszName, FALSE); bChanged = FALSE; // replace any illegal characters while (*pCurrChar) { if (strchr(badChars, *pCurrChar)) { *pCurrChar = replacementChar; bChanged = TRUE; } pCurrChar = HXGetNextChar(pCurrChar); // be sure the name isn't too long if (pCurrChar - pszName >= maxNameLength) { if (pCurrChar - pszName >= maxNameLength + 1) pCurrChar = HXGetPrevChar(pszName, pCurrChar); *pCurrChar = 0; bChanged = TRUE; } } return bChanged;}//******************************************************************************CHXDirSpecifier CHXFileSpecUtils::GetAppDataDir(const char* szAppName){ CHXDirSpecifier dirResult; CHXString strPath; HINSTANCE hShell = ::LoadLibrary(OS_STRING("shell32.dll")); FPSHGetSpecialFolderLocation fpSHGetSpecialFolderLocation = NULL; FPSHGetPathFromIDList fpSHGetPathFromIDList = NULL; FPSHGetMalloc fpSHGetMalloc = NULL; if (hShell) { fpSHGetSpecialFolderLocation = (FPSHGetSpecialFolderLocation) GetProcAddress(hShell,OS_STRING("SHGetSpecialFolderLocation")); fpSHGetPathFromIDList = (FPSHGetPathFromIDList) GetProcAddress(hShell, OS_STRING("SHGetPathFromIDListA")); // just in case if (!fpSHGetPathFromIDList) { fpSHGetPathFromIDList = (FPSHGetPathFromIDList) GetProcAddress(hShell, OS_STRING("SHGetPathFromIDList")); } fpSHGetMalloc = (FPSHGetMalloc) GetProcAddress(hShell, OS_STRING("SHGetMalloc")); } if (!fpSHGetSpecialFolderLocation || !fpSHGetPathFromIDList || !fpSHGetMalloc) { goto exit; }#if !defined(WIN32_PLATFORM_PSPC) ITEMIDLIST* pidl; pidl = NULL; if(fpSHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl) == NOERROR) { HX_ASSERT(pidl); fpSHGetPathFromIDList(pidl, strPath.GetBuffer(_MAX_PATH + 1)); IMalloc* pMalloc = NULL; if (SUCCEEDED(fpSHGetMalloc(&pMalloc))) { pMalloc->Free(pidl); pMalloc->Release(); pMalloc = NULL; pidl = NULL; } strPath.ReleaseBuffer(); } else { // In Win95 OSR2 SHGetSpecialFolderLocation() fails on CSIDL_APPDATA. HKEY hKey = NULL; if(RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", &hKey) == ERROR_SUCCESS) { DWORD nValueType = REG_NONE; DWORD bufSize = _MAX_PATH; RegQueryValueEx(hKey, OS_STRING("ProgramFilesDir"), NULL, &nValueType, (Byte *)strPath.GetBuffer(_MAX_PATH + 1), &bufSize); strPath.ReleaseBuffer(); RegCloseKey(hKey); } }#endif /* !defined(WIN32_PLATFORM_PSPC) */ if(!strPath.IsEmpty()) { dirResult = strPath; if(!DirectoryExists(dirResult)) CreateDir(dirResult); dirResult = dirResult.SpecifyChildDirectory("Real"); if(!DirectoryExists(dirResult)) CreateDir(dirResult); if(szAppName) { dirResult = dirResult.SpecifyChildDirectory(szAppName); if(!DirectoryExists(dirResult)) CreateDir(dirResult); } }exit: if (hShell) { ::FreeLibrary(hShell); } return dirResult;}//******************************************************************************HX_RESULT CHXFileSpecUtils::ReadBinaryFile(const CHXFileSpecifier& fileSpec, IHXBuffer*& pOutBuffer){ HX_ASSERT(!"Not yet implemented on this platform"); return HXR_FAIL;}HX_RESULT CHXFileSpecUtils::ReadTextFile(const CHXFileSpecifier& fileSpec, CHXString& outStr){ HX_ASSERT(!"Not yet implemented on this platform"); return HXR_FAIL;}HX_RESULT CHXFileSpecUtils::WriteBinaryFile(CHXFileSpecifier& fileSpec, IHXBuffer* inBuffer, BOOL bReplaceExistingFile){ // the first parameter is not const because on the Mac it can change during the call HX_ASSERT(!"Not yet implemented on this platform"); return HXR_FAIL;}HX_RESULT CHXFileSpecUtils::WriteTextFile(CHXFileSpecifier& fileSpec, const CHXString& inStr, BOOL bReplaceExistingFile){ // the first parameter is not const because on the Mac it can change during the call HX_ASSERT(!"Not yet implemented on this platform"); return HXR_FAIL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -