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