OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright (c) 2001-2010 Troy N. Stephens | |
3 | |
4 Use and distribution of this source code is governed by the MIT License, who
se terms are as follows. | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal i
n the Software without restriction, including without limitation the rights to u
se, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: | |
7 | |
8 The above copyright notice and this permission notice shall be included in a
ll copies or substantial portions of the Software. | |
9 | |
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
MPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR C
OPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
11 */ | |
12 | |
13 #import "NSString+CarbonFSRefCreation.h" | |
14 | |
15 @implementation NSString (CarbonFSRefCreation) | |
16 | |
17 - (BOOL) getFSRef:(FSRef*)fsRef createFileIfNecessary:(BOOL)createFile | |
18 { | |
19 NSFileManager* fileManager = [NSFileManager defaultManager]; | |
20 CFURLRef urlRef; | |
21 Boolean gotFSRef; | |
22 | |
23 // Check whether the file exists already. If not, create an empty file if r
equested. | |
24 if (![fileManager fileExistsAtPath:self]) { | |
25 if (createFile) { | |
26 if (![(NSData*)[NSData data] writeToFile:self atomically:YES]) { | |
27 return NO; | |
28 } | |
29 } else { | |
30 return NO; | |
31 } | |
32 } | |
33 | |
34 // Create a CFURL with the specified POSIX path. | |
35 urlRef = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, | |
36 (CFStringRef) self, | |
37 kCFURLPOSIXPathStyle, | |
38 FALSE /* isDirectory */ ); | |
39 if (urlRef == NULL) { | |
40 // printf( "** Couldn't make a CFURLRef for the file.\n" ); | |
41 return NO; | |
42 } | |
43 | |
44 // Try to create an FSRef from the URL. (If the specified file doesn't exis
t, this | |
45 // function will return false, but if we've reached this code we've already
insured | |
46 // that the file exists.) | |
47 gotFSRef = CFURLGetFSRef( urlRef, fsRef ); | |
48 CFRelease( urlRef ); | |
49 | |
50 if (!gotFSRef) { | |
51 // printf( "** Couldn't get an FSRef for the file.\n" ); | |
52 return NO; | |
53 } | |
54 | |
55 return YES; | |
56 } | |
57 | |
58 @end | |
OLD | NEW |