Import from SVN r48.
[sparkle2.git] / Framework / NDAlias / NSString+NDCarbonUtilities.m
bloba1e4c04036799f45148824772b9bb009f44696fb
1 /*
2  *  NSString+NDCarbonUtilities.m category
3  *
4  *  Created by Nathan Day on Sat Aug 03 2002.
5  *  Copyright (c) 2002 Nathan Day. All rights reserved.
6  */
8 #import "NSString+NDCarbonUtilities.h"
11  * class implementation NSString (NDCarbonUtilities)
12  */
13 @implementation NSString (NDCarbonUtilities)
16  * +stringWithFSRef:
17  */
18 + (NSString *)stringWithFSRef:(const FSRef *)aFSRef
20         UInt8                   thePath[PATH_MAX + 1];          // plus 1 for \0 terminator
21         
22         return (FSRefMakePath ( aFSRef, thePath, PATH_MAX ) == noErr) ? [NSString stringWithUTF8String:(const char*)thePath] : nil;
26  * -getFSRef:
27  */
28 - (BOOL)getFSRef:(FSRef *)aFSRef
30         return FSPathMakeRef( (const UInt8 *)[self UTF8String], aFSRef, NULL ) == noErr;
34  * -getFSRef:
35  */
36 - (BOOL)getFSSpec:(FSSpec *)aFSSpec
38         FSRef                   aFSRef;
40         return [self getFSRef:&aFSRef] && (FSGetCatalogInfo( &aFSRef, kFSCatInfoNone, NULL, NULL, aFSSpec, NULL ) == noErr);
44  * -fileSystemPathHFSStyle
45  */
46 - (NSString *)fileSystemPathHFSStyle
48         return [(NSString *)CFURLCopyFileSystemPath((CFURLRef)[NSURL fileURLWithPath:self], kCFURLHFSPathStyle) autorelease];
52  * -pathFromFileSystemPathHFSStyle
53  */
54 - (NSString *)pathFromFileSystemPathHFSStyle
56         return [[(NSURL *)CFURLCreateWithFileSystemPath( kCFAllocatorDefault, (CFStringRef)self, kCFURLHFSPathStyle, [self hasSuffix:@":"] ) autorelease] path];
60  * -resolveAliasFile
61  */
62 - (NSString *)resolveAliasFile
64         FSRef                   theRef;
65         Boolean         theIsTargetFolder,
66                                         theWasAliased = NO;
67         NSString                * theResolvedAlias = self;
69         [self getFSRef:&theRef];
71         if( (FSResolveAliasFile( &theRef, YES, &theIsTargetFolder, &theWasAliased ) == noErr) )
72         {
73                 theResolvedAlias = (theWasAliased) ? [NSString stringWithFSRef:&theRef] : self;
74         }
75         else
76                 NSLog( @"Failed to resolve file %@ as alias file", self );
78         return theResolvedAlias ? theResolvedAlias : self;
82  * +stringWithPascalString:
83  */
84 + (NSString *)stringWithPascalString:(const ConstStr255Param )aPStr
86         return [(NSString*)CFStringCreateWithPascalString( kCFAllocatorDefault, aPStr, kCFStringEncodingMacRomanLatin1 ) autorelease];
90  * -getPascalString:length:
91  */
92 - (BOOL)getPascalString:(StringPtr)aBuffer length:(short)aLength
94         return CFStringGetPascalString( (CFStringRef)self, aBuffer, aLength, kCFStringEncodingMacRomanLatin1) != 0;
98  * -pascalString
99  */
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;
108  * -trimWhitespace
109  */
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:
122  */
123 - (BOOL)finderInfoFlags:(UInt16*)aFlags type:(OSType*)aType creator:(OSType*)aCreator
125         FSRef                   theRef;
126         FSCatalogInfo   theCatalogInfo = {0};
127         FileInfo                        * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
128         
129         if( [self getFSRef:&theRef]
130                  && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )     
131         {
132                 if( aFlags ) *aFlags = theInfo->fileCreator;
133                 if( aType ) *aType = theInfo->fileType;
134                 if( aCreator ) *aCreator = theInfo->finderFlags;
135                 
136                 return YES;
137         }
138         else
139                 return NO;
143  * -finderLocation
144  */
145 - (NSPoint)finderLocation
147         FSRef                           theRef;
148         FSCatalogInfo   theCatalogInfo = {0};
149         FileInfo                        * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
150         NSPoint                 thePoint = NSMakePoint( 0, 0 );
151         
152         if( [self getFSRef:&theRef] && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )
153         {
154                 thePoint = NSMakePoint(theInfo->location.h, theInfo->location.v );
155         }
156         
157         return thePoint;
161  * -setFinderInfoFlags:mask:type:creator:
162  */
163 - (BOOL)setFinderInfoFlags:(UInt16)aFlags mask:(UInt16)aMask type:(OSType)aType creator:(OSType)aCreator
165         BOOL                            theResult = NO;
166         FSRef                           theRef;
167         FSCatalogInfo   theCatalogInfo = {0};
168         FileInfo                        * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
169         
170         //      if( [self getFSSpec:&theFSSpec] && FSpGetFInfo( &theFSSpec, &theInfo) == noErr )
171         if( [self getFSRef:&theRef]
172                  && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )     
173         {
174                 theInfo->finderFlags = (aFlags & aMask) | (theInfo->finderFlags & !aMask);
175                 theInfo->fileType = aType;
176                 theInfo->fileCreator = aCreator;
177                 
178                 theResult = FSSetCatalogInfo(&theRef, kFSCatInfoFinderInfo, &theCatalogInfo)  == noErr;
179                 //              theResult = FSpSetFInfo( &theFSSpec, &theInfo) == noErr;
180         }
181         
182         return theResult;
186  * -setFinderLocation:
187  */
188 - (BOOL)setFinderLocation:(NSPoint)aLocation
190         BOOL                            theResult = NO;
191         FSRef                           theRef;
192         FSCatalogInfo   theCatalogInfo = {0};
193         FileInfo                        * theInfo = (FileInfo*)&theCatalogInfo.finderInfo;
194         
195         //      if( [self getFSSpec:&theFSSpec] && FSpGetFInfo( &theFSSpec, &theInfo) == noErr )
196         if( [self getFSRef:&theRef]
197                  && FSGetCatalogInfo( &theRef, kFSCatInfoFinderInfo, &theCatalogInfo, NULL, NULL, NULL ) == noErr )     
198         {
199                 theInfo->location.h = aLocation.x;
200                 theInfo->location.v = aLocation.y;
201                 
202                 theResult = FSSetCatalogInfo(&theRef, kFSCatInfoFinderInfo, &theCatalogInfo)  == noErr;
203                 //              theResult = FSpSetFInfo( &theFSSpec, &theInfo) == noErr;
204         }
205         
206         return theResult;
209 @end