Merged [18171]: An exception like [http://www.visualdistortion.org/crash/view.jsp...
[adiumx.git] / Other / XtrasCreator / NSString+CarbonFSSpecCreation.m
blob1c99d7d687d6084ae4fee76083a5dc91f91aafe7
1 //IconFamily depends heavily upon Carbon calls and thus will only work in Mac OS X
2 #ifdef MAC_OS_X_VERSION_10_0
4 #import "NSString+CarbonFSSpecCreation.h"
6 @implementation NSString (CarbonFSSpecCreation)
8 - (BOOL) getFSRef:(FSRef*)fsRef createFileIfNecessary:(BOOL)createFile
10     NSFileManager* fileManager = [NSFileManager defaultManager];
11     CFURLRef urlRef;
12     Boolean gotFSRef;
13     
14     // Check whether the file exists already.  If not, create an empty file if requested.
15     if (![fileManager fileExistsAtPath:self]) {
16         if (createFile) {
17             if (![@"" writeToFile:self atomically:YES]) {
18                 return NO;
19             }
20         } else {
21             return NO;
22         }
23     }
25     // Create a CFURL with the specified POSIX path.
26     urlRef = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
27                                             (CFStringRef) self,
28                                             kCFURLPOSIXPathStyle,
29                                             FALSE /* isDirectory */ );
30     if (urlRef == NULL) {
31 //        printf( "** Couldn't make a CFURLRef for the file.\n" );
32         return NO;
33     }
34     
35     // Try to create an FSRef from the URL.  (If the specified file doesn't exist, this
36     // function will return false, but if we've reached this code we've already insured
37     // that the file exists.)
38     gotFSRef = CFURLGetFSRef( urlRef, fsRef );
39     CFRelease( urlRef );
41     if (!gotFSRef) {
42 //        printf( "** Couldn't get an FSRef for the file.\n" );
43         return NO;
44     }
45     
46     return YES;
49 - (BOOL) getFSSpec:(FSSpec*)fsSpec createFileIfNecessary:(BOOL)createFile
51     FSRef fsRef;
53     if (![self getFSRef:&fsRef createFileIfNecessary:createFile])
54         return NO;
55     
56     if (FSGetCatalogInfo( &fsRef,
57                           kFSCatInfoNone,
58                           NULL,
59                           NULL,
60                           fsSpec,
61                           NULL ) != noErr) {
62         //        printf( "** Couldn't get an FSSpec for the file.\n" );
63         return NO;
64     }
66     return YES;
69 + (NSString *)pathForFSRef:(FSRef *)object
71         NSString *path = NULL;
73         UInt8 *pathbuf = malloc(PATH_MAX);
74         if (pathbuf != NULL) {
75                 OSStatus err = FSRefMakePath(object, pathbuf, PATH_MAX);
77                 if (err == noErr) {
78                         path = [(NSString *)CFStringCreateWithCString(kCFAllocatorDefault, (char *)pathbuf, kCFStringEncodingUTF8) autorelease];
79                 }
81                 free(pathbuf);
82         }
84         return path;
87 @end
89 #endif