📄 hxdir_carbon.cpp
字号:
}BOOL CHXDirectory::DeleteFile(const char* szRelPath){ CHXString relativeStr(szRelPath); CHXString fullFileStr; OSErr err; // make a full pathname to the file if we don't have a // directory set (since some callers use a null dir // just to delete files) if (IsPartialMacPath(relativeStr) && (m_strPath.GetLength() > 0)) { // we're deleting for a partial path from the current obj directory fullFileStr = m_strPath; // add seperator if needed if ((fullFileStr.GetLength() > 0) && (fullFileStr.Right(1) != ':')) { fullFileStr += ':'; } fullFileStr += relativeStr; } else { HX_ASSERT(IsFullMacPath(relativeStr)); // object's dir not set, so this works only for full paths fullFileStr = relativeStr; } // delete the file err = CHXFileSpecUtils::RemoveFile(fullFileStr); // we really should return an error // this gets called frequently if the file doesn't exist - not an error // so return error only if file is busy return (err != fBsyErr);}/* Sets itself to current directory. */BOOL CHXDirectory::SetCurrentDir(){ OSErr err; long dirID; short vRefNum; FSRef currDir; err = HGetVol(NULL, &vRefNum, &dirID); if (err == noErr) { err = FSMakeFSRef(vRefNum, dirID, NULL, &currDir); if (err == noErr) { CHXDirSpecifier dirSpec(currDir); SetPath(dirSpec.GetPathName()); } } return (err == noErr);}/* Makes itself a current directory. */BOOL CHXDirectory::MakeCurrentDir(){ CHXDirSpecifier dirSpec(m_strPath); if (dirSpec.IsSet()) { FSRef newDir, oldDir; OSErr err; newDir = (FSRef) dirSpec; err = FSSetDefault(&newDir, &oldDir); return (err == noErr); } return FALSE;}UINT32 CHXDirectory::Rename(const char* szOldName, const char* szNewName){ UINT32 err; // Unfortunately, the semantics of the parameters for this call aren't clear // // presumably, szOldName is a full path, or a partial path in the current directory // presumably, szNewName is a full path, or just a name CHXString oldFileStr(szOldName); CHXString newFileStr(szNewName); CHXFileSpecifier oldFileSpec; CHXFileSpecifier newFileSpec; CHXDirSpecifier destDirSpec; if (oldFileStr.Find(':') >= 0) { // the old name has a colon; convert it to a file spec oldFileSpec = oldFileStr; } if (!oldFileSpec.IsSet()) { // we couldn't get a valid FSSpec for the old name, // so assume it's relative to the current directory, // and make a file spec for it CHXDirSpecifier currPathSpec(m_strPath); oldFileSpec = currPathSpec.SpecifyChildFile((const char *) oldFileStr); } require_action(oldFileSpec.IsSet(), CantGetSourceForRename, err = fnfErr); if (newFileStr.Find(':') >= 0) { // the new name has a colon; try to convert it to a file spec newFileSpec = newFileStr; } // make a filespec for the destination folder // // use the directory of the new file if it was specified, otherwise use // the directory of the old file FSRef destFSRef; if (newFileSpec.IsSet()) { CHXDirSpecifier newParentDir = newFileSpec.GetParentDirectory(); err = FSMakeFSRef(newFileSpec.GetVRefNum(), newParentDir.GetDirID(), NULL, &destFSRef); } else { CHXDirSpecifier oldParentDir = oldFileSpec.GetParentDirectory(); err = FSMakeFSRef(oldFileSpec.GetVRefNum(), oldParentDir.GetDirID(), NULL, &destFSRef); } check_noerr(err); destDirSpec = destFSRef; // make sure we're not trying to move to another volume require_action(destDirSpec.GetVRefNum() == oldFileSpec.GetVRefNum(), CantChangeVolumes, err = HXR_FAILED); // they're on the same drive; possibly in different folders // use the name from the new file spec, if we have one, or else from the parameter HFSUniStr255 uniName; if (newFileSpec.IsSet()) { uniName = newFileSpec.GetNameHFSUniStr255(); } else { newFileStr.MakeHFSUniStr255(uniName, CFStringGetSystemEncoding()); } FSRef newFSRef; err = FSMoveRenameObjectUnicode(oldFileSpec, destDirSpec, uniName.length, uniName.unicode, kTextEncodingUnknown, &newFSRef); if (err == dupFNErr) { err = FSDeleteObject(newFileSpec); if (err == noErr) { err = FSMoveRenameObjectUnicode(oldFileSpec, destDirSpec, uniName.length, uniName.unicode, kTextEncodingUnknown, &newFSRef); } } CantChangeVolumes:CantGetSourceForRename: if (err == noErr) err = HXR_OK; else err = HXR_FAILED; return err;}// this moves or copies and renames a file//// This is not related to the current directoryBOOLCHXDirectory::MoveRename(const char* szSrcName, const char* szDestName, BOOL bMove){ OSErr err; BOOL bOnSameVolume; Str255 pascDestFileName; CHXFileSpecifier srcFileSpec = szSrcName; CHXFileSpecifier destFileSpec = szDestName; CHXDirSpecifier destFileDir = destFileSpec.GetParentDirectory(); CHXString strDestFileName = destFileSpec.GetName(); // delete anything at our target location CHXFileSpecUtils::RemoveFile(destFileSpec); bOnSameVolume = (srcFileSpec.GetVRefNum() == destFileSpec.GetVRefNum()); if (bMove && bOnSameVolume) { strDestFileName.MakeStr255(pascDestFileName); err = FSpMoveRenameCompat((FSSpec *) srcFileSpec, (FSSpec *) destFileDir, pascDestFileName); } if (!bMove || !bOnSameVolume) { CHXString strBuffer; const int kBufferSize = 40000; char *pBuff = strBuffer.GetBuffer(kBufferSize); check_nonnull(pBuff); strDestFileName.MakeStr255(pascDestFileName); err = FSpFileCopy((FSSpec *) srcFileSpec, (FSSpec *) destFileDir, pascDestFileName, pBuff, kBufferSize, FALSE); } // should we delete the source if bMove and we successfully copied it? return (err == noErr);}/* Checks if directory is on CD or removable drive. */ BOOL CHXDirectory::IsRemovable(){ return CHXFileSpecUtils::IsDiskEjectable(m_strPath);}#ifdef _DEBUGvoid CHXDirectory::TestHXDir(){ char *pszTestDir = "Tenspot:123456789a123456789b123456789c123456789d:"; char *pszScanDir = "Tenspot:"; char *pszFakeDir = "Tenspot:123456789a123456789b123456789c123456789dXXX:"; CHXDirectory dirTest; dirTest.SetPath(pszTestDir); CHXDirectory dirInvalid; dirInvalid.SetPath(pszFakeDir); BOOL bIsValid = dirTest.IsValid(); check(bIsValid); bIsValid = dirInvalid.IsValid(); check(!bIsValid); CHXDirectory dirTemp; dirTemp.SetTempPath(0, ""); bIsValid = dirTemp.IsValid(); check(bIsValid); dirTemp.SetTempPath(0, "NotInTemp"); bIsValid = dirTemp.IsValid(); check(!bIsValid); CHXDirectory dirNew; CHXString strNewPath; strNewPath = pszTestDir; strNewPath += "New Folder"; dirNew.SetPath(strNewPath); bIsValid = dirNew.IsValid(); check(!bIsValid); dirNew.Create(); bIsValid = dirNew.IsValid(); check(bIsValid); dirNew.Destroy(TRUE); bIsValid = dirNew.IsValid(); check(!bIsValid); CHXDirectory oldCurrentDir, newCurrentDir; oldCurrentDir.SetCurrentDir(); dirTest.MakeCurrentDir(); newCurrentDir.SetCurrentDir(); check(strcmp(oldCurrentDir.GetPath(), dirTest.GetPath())); check(!strcmp(newCurrentDir.GetPath(), dirTest.GetPath())); oldCurrentDir.MakeCurrentDir(); BOOL bIsRemovable = dirTest.IsRemovable(); check(!bIsRemovable); CHXDirectory scanDir; scanDir.SetPath(pszScanDir); FSOBJ obj; char foundPath[4000]; /* Flawfinder: ignore */ obj = scanDir.FindFirst("*.*", foundPath, 4000); while (obj != FSOBJ_NOTVALID) { obj = scanDir.FindNext(foundPath, 4000); } long dirID; OSErr err; err = scanDir.GetDirID(dirID); FSSpec spec = scanDir.GetFSSpec(); /* CHXString strTemp(pszScanDir); CHXString strTempFrog(pszScanDir); scanDir.Rename("test", "test frog"); strTempFrog += "test frog"; strTemp += "test"; scanDir.Rename(strTempFrog, strTemp); */ scanDir.DeleteFile("123456789a123456789b123456789c123456789d copy 1.png"); }#endif _DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -