📄 fullpathname.cp
字号:
return PathFromFSRefInternal(kCFURLPOSIXPathStyle, pRef, fullPathName);
}
OSStatus FSRefFromPosixPath (const char *path, FSRef *pRef)
{
OSStatus err;
CHXString strPath;
err = FullFromPartialPOSIXPath(path, strPath);
return FSRefFromPathInternal(kCFURLPOSIXPathStyle, path, pRef);
}
// url <--> path routines
OSStatus URLFromPOSIXPath(const char *pszPath, CHXString& strURL)
{
OSStatus err;
CHXString strPath;
err = FullFromPartialPOSIXPath(pszPath, strPath);
return URLFromPathInternal(kCFURLPOSIXPathStyle, pszPath, strURL);
}
OSStatus POSIXPathFromURL(const char *pszURL, CHXString& fullPathName)
{
return PathFromURLInternal(kCFURLPOSIXPathStyle, pszURL, fullPathName);
}
OSStatus URLFromHFSPath(const char *pszPath, CHXString& strURL)
{
OSStatus err;
CHXString strPath;
err = FullFromPartialHFSPath(pszPath, strPath);
return URLFromPathInternal(kCFURLHFSPathStyle, pszPath, strURL);
}
OSStatus HFSPathFromURL(const char *pszURL, CHXString& fullPathName)
{
return PathFromURLInternal(kCFURLHFSPathStyle, pszURL, fullPathName);
}
OSErr FullFromPartialHFSPath(const char *pszPartialOrFullPath, CHXString& fullPathName)
{
// icky; hopefully no one will ever pass in a file name or partial path
//
// If there's no colon in the HFS path, or it begins with a colon,
// we assume it's a local file name or partial path and use the
// current directory to make a full path
CHXString strTemp;
OSErr err;
err = noErr;
strTemp = pszPartialOrFullPath;
INT32 colonOffset = strTemp.Find(':');
if (colonOffset == -1 || colonOffset == 0)
{
FSVolumeRefNum vRefNum;
long dirID;
err = HGetVol(nil, &vRefNum, &dirID);
if (err == noErr)
{
err = PathNameFromDirID(dirID, vRefNum, strTemp);
if (err == noErr)
{
if (colonOffset == 0)
{
// concatenate a partial path starting with :
// (skip the initial colon, though)
strTemp += &pszPartialOrFullPath[1];
}
else
{
// concatenate just a file name
strTemp += pszPartialOrFullPath;
}
}
}
}
fullPathName = strTemp;
return err;
}
OSErr FullFromPartialPOSIXPath(const char *pszPartialOrFullPath, CHXString& fullPathName)
{
// icky; hopefully no one will ever pass in a file name or partial path
//
// If the path does not start with a slash,
// we assume it's a local file name or partial path and use the
// current directory to make a full path
CHXString strTemp;
OSErr err;
err = noErr;
#if defined(_MAC_MACHO)
if (pszPartialOrFullPath[0] != '/')
{
char buff[MAXPATHLEN];
getcwd(buff, MAXPATHLEN);
strTemp = buff;
strTemp += (strTemp.Right(1) == "/" ? "" : "/");
strTemp += pszPartialOrFullPath;
}
else
{
strTemp = pszPartialOrFullPath;
}
#else
strTemp = pszPartialOrFullPath;
#endif
fullPathName = strTemp;
return err;
}
#endif // defined _CARBON
#pragma mark -
#pragma mark [Pre-Carbon routines]
#pragma mark -
OSErr PathNameFromDirID(long dirID, short vRefNum, CHXString &fullPathName)
{
#if defined(_MAC_UNIX) || defined(_CARBON)
FSRef fsref;
OSErr err;
// get the FSRef for this directory
err = FSMakeFSRef(vRefNum, dirID, NULL, &fsref); // MoreFilesX passes null for the name, so we can, too
if (err == noErr)
{
// get the path name from the FSRef
err = HFSPathFromFSRef(&fsref, fullPathName);
}
return err;
#else // !defined _CARBON
DirInfo block;
Str63 directoryName;
OSErr err = noErr;
fullPathName.Empty();
block.ioDrParID = dirID;
block.ioNamePtr = directoryName;
do {
block.ioVRefNum = vRefNum;
block.ioFDirIndex = -1;
block.ioDrDirID = block.ioDrParID;
err = PBGetCatInfoSync ((CInfoPBPtr) &block);
if (noErr != err) return (err);
directoryName[++directoryName[0]] = ':'; // append a colon
if (noErr != err) return (err);
fullPathName.InsertFromStr255(directoryName);
} while (block.ioDrDirID != fsRtDirID);
return (err);
#endif // !defined _CARBON
}
/*
PathNameFromWD
*/
OSErr PathNameFromWD(long vRefNum, CHXString &pathName)
{
// Working Directories are obsolete under System 7 and later
//
// We shouldn't be calling this routine
HX_ASSERT(FALSE);
return paramErr;
}
/*
PathNameFromFSSpec
*/
OSErr PathNameFromFSSpec (const FSSpec* fsSpec, CHXString &fullPathName)
{
OSErr err;
short leafNameLength;
if (fsSpec->parID == fsRtParID)
{
// parID of 1 means the name is a volume name
//
// just add a colon to the end to make the path (like "Hard Disk:")
fullPathName.SetFromStr255(fsSpec->name);
if (fullPathName.GetAt(fullPathName.GetLength() - 1) != ':')
{
fullPathName += ":";
}
return noErr;
}
// generate the path up to the parent directory
err = PathNameFromDirID (fsSpec->parID, fsSpec->vRefNum, fullPathName);
if (err) return err;
// add the leaf item's name
leafNameLength = fsSpec->name[0];
if (leafNameLength)
{
fullPathName.AppendFromStr255(fsSpec->name);
// if the leaf item doesn't end in a colon, it might
// or might not be a directory. Let's check, and add a colon if
// necessary
if (fsSpec->name[leafNameLength] != ':')
{
CInfoPBRec catInfo;
Str63 name;
// copy the name to avoid changing the input parameters
BlockMoveData(fsSpec->name, name, 1 + leafNameLength);
catInfo.hFileInfo.ioVRefNum = fsSpec->vRefNum;
catInfo.hFileInfo.ioDirID = fsSpec->parID;
catInfo.hFileInfo.ioNamePtr = name;
catInfo.hFileInfo.ioFDirIndex = 0; // use name and parID
// we might get an error from PBGetCatInfo if the
// leaf item doesn't actually exist
err = PBGetCatInfoSync(&catInfo);
if (err == noErr
&& (catInfo.hFileInfo.ioFlAttrib & ioDirMask) != 0)
{
// the leaf item is a directory
fullPathName += ":";
}
}
}
return noErr;
}
// FSSpecFromPathName converts a full pathname (in a null-terminated
// string) to a Mac file spec.
//
// The fields of spec are set to zero if the conversion fails.
// This routine returns fnfErr if the conversion succeeded but
// the file doesn't (yet) exist.
OSErr FSSpecFromPathName(const char *path, FSSpecPtr spec)
{
// revised to remove 255 character limit length path names
UInt32 len;
OSErr err;
FSSpec fileSpec;
fileSpec.vRefNum = 0;
fileSpec.parID = 0;
fileSpec.name[0] = 0;
len = strlen(path);
if (len == 0)
{
// empty path... leave the fileSpec invalid
err = nsvErr;
}
else if (len <= 255)
{
// path is under 256 characters, so let FSMakeFSSpec make the path for us
//
// Warning: passing zeros as vRefNum and dirID and a *partial* pathname
// is interpreted as being relative to the poorly-defined "current" directory.
// This conversion should really only be used for full paths, starting
// from the drive name.
Str255 name;
name[0] = len;
BlockMoveData(path, &name[1], len);
err = FSMakeFSSpec(0, 0, name, &fileSpec);
}
else {
Boolean wasChanged;
Str32 nullString;
AliasHandle alias;
// longer paths, let the alias manager make the file spec for us
//
// The alias manager expects a full pathname, so this won't
// work on partial paths longer than 255 characters.
HX_ASSERT(path[0] != ':'); // be sure it's not a partial path
nullString[0] = 0; // null string to indicate no zone or server name
err = NewAliasMinimalFromFullPath(len, path, nullString, nullString, &alias);
if (err == noErr)
{
// Let the Alias Manager resolve the alias.
err = ResolveAlias(NULL, alias, &fileSpec, &wasChanged);
// ResolveAlias returns fnfErr if the target doesn't exist but
// the fileSpec is valid, just like FSMakeFSSpec does
DisposeHandle((Handle) alias); // we don't really need the alias
}
}
*spec = fileSpec;
return err;
}
#ifdef __cplusplus
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -