2 * NSString+NDCarbonUtilities.m category
4 * Created by Nathan Day on Sat Aug 03 2002.
5 * Copyright (c) 2002 Nathan Day. All rights reserved.
8 #import "NSString+NDCarbonUtilities.h"
11 * class implementation NSString (NDCarbonUtilities)
13 @implementation NSString (NDCarbonUtilities)
18 + (NSString *)stringWithFSRef:(const FSRef *)aFSRef
20 UInt8 thePath[PATH_MAX + 1]; // plus 1 for \0 terminator
22 return (FSRefMakePath ( aFSRef, thePath, PATH_MAX ) == noErr) ? [NSString stringWithUTF8String:(const char*)thePath] : nil;
28 - (BOOL)getFSRef:(FSRef *)aFSRef
30 return FSPathMakeRef( (const UInt8 *)[self UTF8String], aFSRef, NULL ) == noErr;
36 - (BOOL)getFSSpec:(FSSpec *)aFSSpec
40 return [self getFSRef:&aFSRef] && (FSGetCatalogInfo( &aFSRef, kFSCatInfoNone, NULL, NULL, aFSSpec, NULL ) == noErr);
44 * -fileSystemPathHFSStyle
46 - (NSString *)fileSystemPathHFSStyle
48 return [(NSString *)CFURLCopyFileSystemPath((CFURLRef)[NSURL fileURLWithPath:self], kCFURLHFSPathStyle) autorelease];
52 * -pathFromFileSystemPathHFSStyle
54 - (NSString *)pathFromFileSystemPathHFSStyle
56 return [[(NSURL *)CFURLCreateWithFileSystemPath( kCFAllocatorDefault, (CFStringRef)self, kCFURLHFSPathStyle, [self hasSuffix:@":"] ) autorelease] path];
62 - (NSString *)resolveAliasFile
65 Boolean theIsTargetFolder,
67 NSString * theResolvedAlias = self;
69 [self getFSRef:&theRef];
71 if( (FSResolveAliasFile( &theRef, YES, &theIsTargetFolder, &theWasAliased ) == noErr) )
73 theResolvedAlias = (theWasAliased) ? [NSString stringWithFSRef:&theRef] : self;
76 NSLog( @"Failed to resolve file %@ as alias file", self );
78 return theResolvedAlias ? theResolvedAlias : self;
82 * +stringWithPascalString:
84 + (NSString *)stringWithPascalString:(const ConstStr255Param )aPStr
86 return [(NSString*)CFStringCreateWithPascalString( kCFAllocatorDefault, aPStr, kCFStringEncodingMacRomanLatin1 ) autorelease];
90 * -getPascalString:length:
92 - (BOOL)getPascalString:(StringPtr)aBuffer length:(short)aLength
94 return CFStringGetPascalString( (CFStringRef)self, aBuffer, aLength, kCFStringEncodingMacRomanLatin1) != 0;
100 - (const char *)pascalString
102 const unsigned int kPascalStringLen = 256;
103 NSMutableData * theData = [NSMutableData dataWithCapacity:kPascalStringLen];
104 return [self getPascalString:(StringPtr)[theData mutableBytes] length:kPascalStringLen] ? [theData bytes] : NULL;
110 - (NSString *)trimWhitespace
112 CFMutableStringRef theString;
114 theString = CFStringCreateMutableCopy( kCFAllocatorDefault, 0, (CFStringRef)self);
115 CFStringTrimWhitespace( theString );
117 return [(NSString *)theString autorelease];
121 * -finderInfoFlags:type:creator:
123 - (BOOL)finderInfoFlags:(UInt16*)aFlags type:(OSType*)aType creator:(OSType*)aCreator
126 FSCatalogInfo theCatalogInfo = {0};
127 FileInfo * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
129 if( [self getFSRef:&theRef]
130 && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )
132 if( aFlags ) *aFlags = theInfo->fileCreator;
133 if( aType ) *aType = theInfo->fileType;
134 if( aCreator ) *aCreator = theInfo->finderFlags;
145 - (NSPoint)finderLocation
148 FSCatalogInfo theCatalogInfo = {0};
149 FileInfo * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
150 NSPoint thePoint = NSMakePoint( 0, 0 );
152 if( [self getFSRef:&theRef] && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )
154 thePoint = NSMakePoint(theInfo->location.h, theInfo->location.v );
161 * -setFinderInfoFlags:mask:type:creator:
163 - (BOOL)setFinderInfoFlags:(UInt16)aFlags mask:(UInt16)aMask type:(OSType)aType creator:(OSType)aCreator
167 FSCatalogInfo theCatalogInfo = {0};
168 FileInfo * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
170 // if( [self getFSSpec:&theFSSpec] && FSpGetFInfo( &theFSSpec, &theInfo) == noErr )
171 if( [self getFSRef:&theRef]
172 && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )
174 theInfo->finderFlags = (aFlags & aMask) | (theInfo->finderFlags & !aMask);
175 theInfo->fileType = aType;
176 theInfo->fileCreator = aCreator;
178 theResult = FSSetCatalogInfo(&theRef, kFSCatInfoFinderInfo, &theCatalogInfo) == noErr;
179 // theResult = FSpSetFInfo( &theFSSpec, &theInfo) == noErr;
186 * -setFinderLocation:
188 - (BOOL)setFinderLocation:(NSPoint)aLocation
192 FSCatalogInfo theCatalogInfo = {0};
193 FileInfo * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
195 // if( [self getFSSpec:&theFSSpec] && FSpGetFInfo( &theFSSpec, &theInfo) == noErr )
196 if( [self getFSRef:&theRef]
197 && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )
199 theInfo->location.h = aLocation.x;
200 theInfo->location.v = aLocation.y;
202 theResult = FSSetCatalogInfo(&theRef, kFSCatInfoFinderInfo, &theCatalogInfo) == noErr;
203 // theResult = FSpSetFInfo( &theFSSpec, &theInfo) == noErr;