PageRenderTime 36ms CodeModel.GetById 11ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMNSFileManager+Path.m

http://macfuse.googlecode.com/
Objective C | 110 lines | 63 code | 23 blank | 24 comment | 19 complexity | 624708a008f26d69962dcb22c93d8432 MD5 | raw file
  1//
  2//  GTMNSFileManager+Path.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 "GTMNSFileManager+Path.h"
 20#import "GTMDefines.h"
 21
 22@implementation NSFileManager (GMFileManagerPathAdditions)
 23
 24#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 25
 26- (BOOL)gtm_createFullPathToDirectory:(NSString *)path
 27                           attributes:(NSDictionary *)attributes {
 28  if (!path) return NO;
 29
 30  BOOL isDir;
 31  BOOL exists = [self fileExistsAtPath:path isDirectory:&isDir];
 32
 33  // Quick check for the case where we have nothing to do.
 34  if (exists && isDir)
 35    return YES;
 36
 37  NSString *actualPath = @"/";
 38  NSString *directory;
 39
 40  GTM_FOREACH_OBJECT(directory, [path pathComponents]) {
 41    actualPath = [actualPath stringByAppendingPathComponent:directory];
 42
 43    if ([self fileExistsAtPath:actualPath isDirectory:&isDir] && isDir) {
 44      continue;
 45    } else if ([self createDirectoryAtPath:actualPath attributes:attributes]) {
 46      continue;
 47    } else {
 48      return NO;
 49    }
 50  }
 51
 52  return YES;
 53}
 54
 55#endif  // GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 56
 57- (NSArray *)gtm_filePathsWithExtension:(NSString *)extension
 58                            inDirectory:(NSString *)directoryPath {
 59  NSArray *extensions = nil;
 60
 61  // Treat no extension and an empty extension as the user requesting all files
 62  if (extension != nil && ![extension isEqualToString:@""])
 63    extensions = [NSArray arrayWithObject:extension];
 64
 65  return [self gtm_filePathsWithExtensions:extensions
 66                               inDirectory:directoryPath];
 67}
 68
 69- (NSArray *)gtm_filePathsWithExtensions:(NSArray *)extensions
 70                             inDirectory:(NSString *)directoryPath {
 71  if (!directoryPath) {
 72    return nil;
 73  }
 74
 75  // |basenames| will contain only the matching file names, not their full paths.
 76#if GTM_IPHONE_SDK || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
 77  NSArray *basenames = [self contentsOfDirectoryAtPath:directoryPath
 78                                                 error:nil];
 79#else
 80  NSArray *basenames = [self directoryContentsAtPath:directoryPath];
 81#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
 82
 83  // Check if dir doesn't exist or couldn't be opened.
 84  if (!basenames) {
 85    return nil;
 86  }
 87
 88  // Check if dir is empty.
 89  if ([basenames count] == 0) {
 90    return basenames;
 91  }
 92
 93  NSMutableArray *paths = [NSMutableArray arrayWithCapacity:[basenames count]];
 94  NSString *basename;
 95
 96  // Convert all the |basenames| to full paths.
 97  GTM_FOREACH_OBJECT(basename, basenames) {
 98    NSString *fullPath = [directoryPath stringByAppendingPathComponent:basename];
 99    [paths addObject:fullPath];
100  }
101
102  // Check if caller wants all files, regardless of extension.
103  if ([extensions count] == 0) {
104    return paths;
105  }
106
107  return [paths pathsMatchingExtensions:extensions];
108}
109
110@end