Blame | Last modification | View Log | Download | RSS feed
/*** Apple Macintosh Developer Technical Support**** Routines for dealing with full pathnames... if you really must.**** by Jim Luther, Apple Developer Technical Support Emeritus**** File: FullPath.h**** Copyright © 1995-1996 Apple Computer, Inc.** All rights reserved.**** You may incorporate this sample code into your applications without** restriction, though the sample code has been provided "AS IS" and the** responsibility for its operation is 100% yours. However, what you are** not permitted to do is to redistribute the source as "DSC Sample Code"** after having made changes. If you're going to re-distribute the source,** we require that you make it clear in the source that the code was** descended from Apple Sample Code, but that you've made changes.*/#ifndef __FULLPATH__#define __FULLPATH__#include <Types.h>#include <Files.h>#ifdef __cplusplusextern "C" {#endif/*IMPORTANT NOTE:The use of full pathnames is strongly discouraged. Full pathnames areparticularly unreliable as a means of identifying files, directoriesor volumes within your application, for two primary reasons:¥ The user can change the name of any element in the path atvirtually any time.¥ Volume names on the Macintosh are *not* unique. Multiplemounted volumes can have the same name. For this reason, the use ofa full pathname to identify a specific volume may not produce theresults you expect. If more than one volume has the same name anda full pathname is used, the File Manager currently uses the firstmounted volume it finds with a matching name in the volume queue.In general, you should use a fileÕs name, parent directory ID, andvolume reference number to identify a file you want to open, delete,or otherwise manipulate.If you need to remember the location of a particular file acrosssubsequent system boots, use the Alias Manager to create an aliasrecord describing the file. If the Alias Manager is not available, youcan save the fileÕs name, its parent directory ID, and the name of thevolume on which itÕs located. Although none of these methods isfoolproof, they are much more reliable than using full pathnames toidentify files.Nonetheless, it is sometimes useful to display a fileÕs full pathnameto the user. For example, a backup utility might display a list of fullpathnames of files as it copies them onto the backup medium. Or, autility might want to display a dialog box showing the full pathname ofa file when it needs the userÕs confirmation to delete the file. Nomatter how unreliable full pathnames may be from a file-specificationviewpoint, users understand them more readily than volume referencenumbers or directory IDs. (Hint: Use the TruncString function fromTextUtils.h with truncMiddle as the truncWhere argument to shortenfull pathnames to a displayable length.)The following technique for constructing the full pathname of a file isintended for display purposes only. Applications that depend on anyparticular structure of a full pathname are likely to fail on alternateforeign file systems or under future system software versions.*//*****************************************************************************/pascal OSErr GetFullPath(short vRefNum,long dirID,StringPtr name,short *fullPathLength,Handle *fullPath);/* ¦ Get a full pathname to a volume, directory or file.The GetFullPath function builds a full pathname to the specifiedobject. The full pathname is returned in the newly created handlefullPath and the length of the full pathname is returned infullPathLength. Your program is responsible for disposing of thefullPath handle.vRefNum input: Volume specification.dirID input: Directory ID.name input: Pointer to object name, or nil when dirIDspecifies a directory that's the object.fullPathLength output: The number of characters in the full pathname.If the function fails to create a fullpathname, it sets fullPathLength to 0.fullPath output: A handle to the newly created full pathnamebuffer. If the function fails to create afull pathname, it sets fullPath to NULL.Result CodesnoErr 0 No errornsvErr -35 No such volumeioErr -36 I/O errorbdNamErr -37 Bad filenamefnfErr -43 File or directory does not existparamErr -50 No default volumememFullErr -108 Not enough memorydirNFErr -120 Directory not found or incomplete pathnameafpAccessDenied -5000 User does not have the correct accessafpObjectTypeErr -5025 Directory not found or incomplete pathname__________See also: FSpGetFullPath*//*****************************************************************************/pascal OSErr FSpGetFullPath(const FSSpec *spec,short *fullPathLength,Handle *fullPath);/* ¦ Get a full pathname to a volume, directory or file.The GetFullPath function builds a full pathname to the specifiedobject. The full pathname is returned in the newly created handlefullPath and the length of the full pathname is returned infullPathLength. Your program is responsible for disposing of thefullPath handle.spec input: An FSSpec record specifying the object.fullPathLength output: The number of characters in the full pathname.If the function fails to create a full pathname,it sets fullPathLength to 0.fullPath output: A handle to the newly created full pathnamebuffer. If the function fails to create afull pathname, it sets fullPath to NULL.Result CodesnoErr 0 No errornsvErr -35 No such volumeioErr -36 I/O errorbdNamErr -37 Bad filenamefnfErr -43 File or directory does not existparamErr -50 No default volumememFullErr -108 Not enough memorydirNFErr -120 Directory not found or incomplete pathnameafpAccessDenied -5000 User does not have the correct accessafpObjectTypeErr -5025 Directory not found or incomplete pathname__________See also: GetFullPath*//*****************************************************************************/pascal OSErr FSpLocationFromFullPath(short fullPathLength,const void *fullPath,FSSpec *spec);/* ¦ Get a FSSpec from a full pathname.The FSpLocationFromFullPath function returns a FSSpec to the objectspecified by full pathname. This function requires the Alias Manager.fullPathLength input: The number of characters in the full pathnameof the target.fullPath input: A pointer to a buffer that contains the fullpathname of the target. The full pathnamestarts with the name of the volume, includesall of the directory names in the path to thetarget, and ends with the target name.spec output: An FSSpec record specifying the object.Result CodesnoErr 0 No errornsvErr -35 The volume is not mountedfnfErr -43 Target not found, but volume and parentdirectory foundparamErr -50 Parameter errorusrCanceledErr -128 The user canceled the operation__________See also: LocationFromFullPath*//*****************************************************************************/pascal OSErr LocationFromFullPath(short fullPathLength,const void *fullPath,short *vRefNum,long *parID,Str31 name);/* ¦ Get an object's location from a full pathname.The LocationFromFullPath function returns the volume reference number,parent directory ID and name of the object specified by full pathname.This function requires the Alias Manager.fullPathLength input: The number of characters in the full pathnameof the target.fullPath input: A pointer to a buffer that contains the fullpathname of the target. The full pathname startswith the name of the volume, includes all ofthe directory names in the path to the target,and ends with the target name.vRefNum output: The volume reference number.parID output: The parent directory ID of the specified object.name output: The name of the specified object.Result CodesnoErr 0 No errornsvErr -35 The volume is not mountedfnfErr -43 Target not found, but volume and parentdirectory foundparamErr -50 Parameter errorusrCanceledErr -128 The user canceled the operation__________See also: FSpLocationFromFullPath*//*****************************************************************************/#ifdef __cplusplus}#endif#ifndef __COMPILINGMOREFILES#undef pascal#endif#endif /* __FULLPATH__ */