/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. #import "GTMNSString+FindFolder.h"
  19. #import "GTMDefines.h"
  20. @implementation NSString (GTMStringFindFolderAdditions)
  21. + (NSString *)gtm_stringWithPathForFolder:(OSType)theFolderType
  22. inDomain:(short)theDomain
  23. doCreate:(BOOL)doCreate {
  24. NSString* folderPath = nil;
  25. FSRef folderRef;
  26. OSErr err = FSFindFolder(theDomain, theFolderType, doCreate, &folderRef);
  27. if (err == noErr) {
  28. CFURLRef folderURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault,
  29. &folderRef);
  30. if (folderURL) {
  31. folderPath = GTMCFAutorelease(CFURLCopyFileSystemPath(folderURL,
  32. kCFURLPOSIXPathStyle));
  33. CFRelease(folderURL);
  34. }
  35. }
  36. return folderPath;
  37. }
  38. + (NSString *)gtm_stringWithPathForFolder:(OSType)theFolderType
  39. subfolderName:(NSString *)subfolderName
  40. inDomain:(short)theDomain
  41. doCreate:(BOOL)doCreate {
  42. NSString *resultPath = nil;
  43. NSString *subdirPath = nil;
  44. NSString *parentFolderPath = [self gtm_stringWithPathForFolder:theFolderType
  45. inDomain:theDomain
  46. doCreate:doCreate];
  47. if (parentFolderPath) {
  48. // find the path to the subdirectory
  49. subdirPath = [parentFolderPath stringByAppendingPathComponent:subfolderName];
  50. NSFileManager* fileMgr = [NSFileManager defaultManager];
  51. BOOL isDir = NO;
  52. if ([fileMgr fileExistsAtPath:subdirPath isDirectory:&isDir] && isDir) {
  53. // it already exists
  54. resultPath = subdirPath;
  55. } else if (doCreate) {
  56. parentFolderPath = [parentFolderPath stringByResolvingSymlinksInPath];
  57. NSDictionary* attrs = nil;
  58. BOOL createdSubDir = NO;
  59. // create the subdirectory with the parent folder's attributes
  60. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  61. NSError *error = nil;
  62. attrs = [fileMgr attributesOfItemAtPath:parentFolderPath error:&error];
  63. if (error) {
  64. _GTMDevLog(@"Error %@ getting attributes of %@",
  65. error, parentFolderPath);
  66. }
  67. createdSubDir = [fileMgr createDirectoryAtPath:subdirPath
  68. withIntermediateDirectories:YES
  69. attributes:attrs
  70. error:&error];
  71. if (error) {
  72. _GTMDevLog(@"Error %@ creating directory at %@", error, subdirPath);
  73. }
  74. #else
  75. attrs = [fileMgr fileAttributesAtPath:parentFolderPath traverseLink:YES];
  76. createdSubDir = [fileMgr createDirectoryAtPath:subdirPath
  77. attributes:attrs];
  78. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  79. if (createdSubDir) {
  80. resultPath = subdirPath;
  81. }
  82. }
  83. }
  84. return resultPath;
  85. }
  86. @end