PageRenderTime 30ms CodeModel.GetById 10ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSString+FindFolder.m

http://macfuse.googlecode.com/
Objective C | 96 lines | 66 code | 10 blank | 20 comment | 11 complexity | 018d023aa0b6dd7ea36550451510b038 MD5 | raw file
 1//
 2//  GTMNSString+FindFolder.m
 3//
 4//  Copyright 2006-2008 Google Inc.
 5//
 6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 7//  use this file except in compliance with the License.  You may obtain a copy
 8//  of the License at
 9// 
10//  http://www.apache.org/licenses/LICENSE-2.0
11// 
12//  Unless required by applicable law or agreed to in writing, software
13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15//  License for the specific language governing permissions and limitations under
16//  the License.
17//
18
19#import "GTMNSString+FindFolder.h"
20#import "GTMDefines.h"
21
22@implementation NSString (GTMStringFindFolderAdditions)
23
24+ (NSString *)gtm_stringWithPathForFolder:(OSType)theFolderType 
25                                 inDomain:(short)theDomain
26                                 doCreate:(BOOL)doCreate {
27  
28  NSString* folderPath = nil;
29  FSRef folderRef;
30  
31  OSErr err = FSFindFolder(theDomain, theFolderType, doCreate, &folderRef);
32  if (err == noErr) {
33    
34    CFURLRef folderURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, 
35                                              &folderRef);
36    if (folderURL) {
37      folderPath = GTMCFAutorelease(CFURLCopyFileSystemPath(folderURL, 
38                                                            kCFURLPOSIXPathStyle));      
39      CFRelease(folderURL);
40    }
41  }
42  return folderPath;
43}
44
45+ (NSString *)gtm_stringWithPathForFolder:(OSType)theFolderType 
46                            subfolderName:(NSString *)subfolderName
47                                 inDomain:(short)theDomain
48                                 doCreate:(BOOL)doCreate {
49  NSString *resultPath = nil;
50  NSString *subdirPath = nil;
51  NSString *parentFolderPath = [self gtm_stringWithPathForFolder:theFolderType
52                                                        inDomain:theDomain
53                                                        doCreate:doCreate];
54  if (parentFolderPath) {
55    
56    // find the path to the subdirectory
57    subdirPath = [parentFolderPath stringByAppendingPathComponent:subfolderName];
58
59    NSFileManager* fileMgr = [NSFileManager defaultManager];
60    BOOL isDir = NO;
61    if ([fileMgr fileExistsAtPath:subdirPath isDirectory:&isDir] && isDir) {
62      // it already exists
63      resultPath = subdirPath;
64    } else if (doCreate) {
65      parentFolderPath = [parentFolderPath stringByResolvingSymlinksInPath];
66      NSDictionary* attrs = nil;
67      BOOL createdSubDir = NO;
68      // create the subdirectory with the parent folder's attributes
69#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
70      NSError *error = nil;
71      attrs = [fileMgr attributesOfItemAtPath:parentFolderPath error:&error];
72      if (error) {
73        _GTMDevLog(@"Error %@ getting attributes of %@", 
74                   error, parentFolderPath);
75      }
76      createdSubDir = [fileMgr createDirectoryAtPath:subdirPath
77                         withIntermediateDirectories:YES
78                                          attributes:attrs
79                                               error:&error];
80      if (error) {
81        _GTMDevLog(@"Error %@ creating directory at %@", error, subdirPath);
82      }
83#else
84      attrs = [fileMgr fileAttributesAtPath:parentFolderPath traverseLink:YES];
85      createdSubDir = [fileMgr createDirectoryAtPath:subdirPath
86                                          attributes:attrs];
87#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
88      if (createdSubDir) {
89        resultPath = subdirPath;
90      }
91    }
92  }
93  return resultPath;
94}
95
96@end