/core/externals/google-toolbox-for-mac/Foundation/GTMNSFileHandle+UniqueName.m

http://macfuse.googlecode.com/ · Objective C · 111 lines · 80 code · 14 blank · 17 comment · 12 complexity · 6340bbf21e98bba33a1c1439cf07359e MD5 · raw file

  1. //
  2. // GTMNSFileHandle+UniqueName.m
  3. //
  4. // Copyright 2010 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 "GTMNSFileHandle+UniqueName.h"
  19. #include <unistd.h>
  20. @implementation NSFileHandle (GTMFileHandleUniqueNameAdditions)
  21. + (id)gtm_fileHandleWithUniqueNameBasedOn:(NSString *)pathTemplate
  22. finalPath:(NSString **)path {
  23. if (!pathTemplate) return nil;
  24. NSString *extension = [pathTemplate pathExtension];
  25. char *pathTemplateCString = strdup([pathTemplate fileSystemRepresentation]);
  26. if (!pathTemplateCString) return nil;
  27. int fileDescriptor = mkstemps(pathTemplateCString, (int)[extension length]);
  28. if (fileDescriptor == -1) {
  29. free(pathTemplateCString);
  30. return nil;
  31. }
  32. NSFileHandle *handle
  33. = [[[NSFileHandle alloc] initWithFileDescriptor:fileDescriptor
  34. closeOnDealloc:YES] autorelease];
  35. if (handle && path) {
  36. *path = [NSString stringWithUTF8String:pathTemplateCString];
  37. }
  38. free(pathTemplateCString);
  39. return handle;
  40. }
  41. + (id)gtm_fileHandleWithUniqueNameBasedOn:(NSString *)nameTemplate
  42. inDirectory:(NSString *)directory
  43. finalPath:(NSString **)path {
  44. NSString *fullPath = [directory stringByAppendingPathComponent:nameTemplate];
  45. return [self gtm_fileHandleWithUniqueNameBasedOn:fullPath finalPath:path];
  46. }
  47. + (id)gtm_fileHandleWithUniqueNameBasedOn:(NSString *)nameTemplate
  48. inDirectory:(NSSearchPathDirectory)directory
  49. domainMask:(NSSearchPathDomainMask)mask
  50. finalPath:(NSString **)path {
  51. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(directory,
  52. mask,
  53. YES);
  54. if ([searchPaths count] == 0) return nil;
  55. NSString *searchPath = [searchPaths objectAtIndex:0];
  56. return [self gtm_fileHandleWithUniqueNameBasedOn:nameTemplate
  57. inDirectory:searchPath
  58. finalPath:path];
  59. }
  60. + (id)gtm_fileHandleForTemporaryFileBasedOn:(NSString *)nameTemplate
  61. finalPath:(NSString **)path {
  62. return [self gtm_fileHandleWithUniqueNameBasedOn:nameTemplate
  63. inDirectory:NSTemporaryDirectory()
  64. finalPath:path];
  65. }
  66. @end
  67. @implementation NSFileManager (GTMFileManagerUniqueNameAdditions)
  68. - (NSString *)gtm_createDirectoryWithUniqueNameBasedOn:(NSString *)pathTemplate {
  69. if (!pathTemplate) return nil;
  70. char *pathTemplateCString = strdup([pathTemplate fileSystemRepresentation]);
  71. if (!pathTemplateCString) return nil;
  72. char *outCName = mkdtemp(pathTemplateCString);
  73. NSString *outName = outCName ? [NSString stringWithUTF8String:outCName] : nil;
  74. free(pathTemplateCString);
  75. return outName;
  76. }
  77. - (NSString *)gtm_createDirectoryWithUniqueNameBasedOn:(NSString *)nameTemplate
  78. inDirectory:(NSString *)directory {
  79. NSString *fullPath = [directory stringByAppendingPathComponent:nameTemplate];
  80. return [self gtm_createDirectoryWithUniqueNameBasedOn:fullPath];
  81. }
  82. - (NSString *)gtm_createDirectoryWithUniqueNameBasedOn:(NSString *)nameTemplate
  83. inDirectory:(NSSearchPathDirectory)directory
  84. domainMask:(NSSearchPathDomainMask)mask {
  85. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(directory,
  86. mask,
  87. YES);
  88. if ([searchPaths count] == 0) return nil;
  89. NSString *searchPath = [searchPaths objectAtIndex:0];
  90. return [self gtm_createDirectoryWithUniqueNameBasedOn:nameTemplate
  91. inDirectory:searchPath];
  92. }
  93. - (NSString *)gtm_createTemporaryDirectoryBasedOn:(NSString *)nameTemplate {
  94. return [self gtm_createDirectoryWithUniqueNameBasedOn:nameTemplate
  95. inDirectory:NSTemporaryDirectory()];
  96. }
  97. @end