📄 nsfilemanager-extensions.m
字号:
/* NSFileManager_Extensions.m created by andrew on Tue 18-Aug-1998 */#import "NSFileManager-Extensions.h"@implementation NSFileManager(NSFileManager_Extensions)#define FILE_OPERATION_ERROR NSLocalizedStringFromTable(@"File operation error: %@ with file: %@", @"DOC", "alert message on failed file operation")#define PROCEED NSLocalizedStringFromTable(@"Proceed", @"DOC", "msg when you cannot rename a file")#define STOP NSLocalizedStringFromTable(@"Stop", @"DOC", "msg when you cannot rename a file")- (BOOL)fileManager:(NSFileManager *)manager shouldProceedAfterError:(NSDictionary *)errorDict{ int result; result = NSRunAlertPanel([[NSProcessInfo processInfo] processName], FILE_OPERATION_ERROR,PROCEED, STOP, NULL, [errorDict objectForKey:@"Error"], [errorDict objectForKey:@"Path"]); if (result == NSAlertDefaultReturn) return YES; else return NO;}//// Temporary Directory stuff: useful code.//BOOL directoryOK(NSString *path){ BOOL isDirectory; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:path isDirectory:&isDirectory] || !isDirectory) { NSDictionary *dict = [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedLong:0777] forKey:NSFilePosixPermissions]; if (![fileManager createDirectoryAtPath:path attributes:dict]) return NO; } return YES;}NSString * existingPath(NSString *path){ while (path && ![path isEqualToString:@""] && ![[NSFileManager defaultManager] fileExistsAtPath:path]) path = [path stringByDeletingLastPathComponent]; return path; }NSArray *directoriesToAdd(NSString *path, NSString *existing){ NSMutableArray *a = [NSMutableArray arrayWithCapacity:4]; if (path != nil && existing != nil) { while (![path isEqualToString:existing]) { [a insertObject:[path lastPathComponent] atIndex:0]; path = [path stringByDeletingLastPathComponent]; } } return a;}// this will go up the path until it finds an existing directory// and will add each subpath and return YES if succeeds, NO if fails:- (BOOL)createWritableDirectory:(NSString *)path{ BOOL isDirectory; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory && [fileManager isWritableFileAtPath:path]) return YES; // no work to do else { NSString *existing = existingPath(path); NSArray *dirsToAdd = directoriesToAdd(path,existing); int i; BOOL good = YES; for (i = 0; i < [dirsToAdd count]; i++) { existing = [existing stringByAppendingPathComponent: [dirsToAdd objectAtIndex:i]]; if (!directoryOK(existing)) { good = NO; break; } } return good; }}- (NSString *)nextUniqueNameUsing:(NSString *)templatier{ if (![[NSFileManager defaultManager] fileExistsAtPath:templatier]) return templatier; else { int unique = 1; NSString *tempName = nil; do { tempName =[NSString stringWithFormat:@"%@_%d.%@", [templatier stringByDeletingPathExtension],++unique, [templatier pathExtension]]; } while ([[NSFileManager defaultManager] fileExistsAtPath:tempName]); return tempName; }}- (NSString *)temporaryDirectory{ static NSString *tempDir =nil; NSString *s = [[NSUserDefaults standardUserDefaults] stringForKey:UserTemporaryDirectory]; if (tempDir == nil || (NOT_NULL(s) && ![tempDir isEqualToString:s])) { if (NOT_NULL(s)) tempDir = [s retain]; else tempDir =[[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",[[NSProcessInfo processInfo] processName],NSUserName()]] retain]; } if (! [self createWritableDirectory:tempDir]) { NSLog(@"Couldn't create %@, using %@",tempDir, NSTemporaryDirectory()); tempDir = NSTemporaryDirectory(); } return tempDir;}@end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -