PageRenderTime 72ms CodeModel.GetById 43ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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