36@implementation NSFileManager (OOExtensions)
38- (NSArray *) commanderContentsOfPath:(NSString *)savePath
40 BOOL pathIsDirectory = NO;
41 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath isDirectory:&pathIsDirectory] && pathIsDirectory)
47 for (i = 0; i < [contents count]; i++)
49 NSString* path = [savePath stringByAppendingPathComponent: (NSString*)[contents objectAtIndex:i]];
52 if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&pathIsDirectory] && pathIsDirectory)
56 if (![[path pathExtension] isEqual:
@"oolite-save"])
58 [contents removeObjectAtIndex: i--];
66 OOLog(
@"savedGame.read.fail.notDictionary",
@">>>> %@ could not be parsed as a saved game.", path);
67 [contents removeObjectAtIndex: i--];
73 [contents replaceObjectAtIndex: i withObject: path];
81 OOLogERR(
@"savedGame.read.fail.fileNotFound",
@"File at path '%@' could not be found.", savePath);
89 NSString *savedir = [NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR];
90 BOOL pathIsDirectory = NO;
93 if (![[NSFileManager defaultManager] fileExistsAtPath:savedir isDirectory:&pathIsDirectory])
96 if([
self oo_createDirectoryAtPath:savedir attributes:
nil])
102 OOLogERR(
@"savedGame.defaultPath.create.failed",
@"Unable to create '%@'. Saved games will go to the home directory.", savedir);
103 return NSHomeDirectory();
108 if (!pathIsDirectory)
110 OOLogERR(
@"savedGame.defaultPath.notDirectory",
@"'%@' is not a directory, saved games will go to the home directory.", savedir);
111 return NSHomeDirectory();
120- (NSArray *) oo_directoryContentsAtPath:(NSString *)path
122 return [
self contentsOfDirectoryAtPath:path error:NULL];
126- (BOOL) oo_createDirectoryAtPath:(NSString *)path attributes:(NSDictionary *)attributes
128 return [
self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
132- (NSDictionary *) oo_fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)traverseLink
136 NSString *linkDest =
nil;
139 linkDest = [
self destinationOfSymbolicLinkAtPath:path error:NULL];
140 if (linkDest !=
nil) path = linkDest;
141 }
while (linkDest !=
nil);
144 return [
self attributesOfItemAtPath:path error:NULL];
148- (NSDictionary *) oo_fileSystemAttributesAtPath:(NSString *)path
150 return [
self attributesOfFileSystemForPath:path error:NULL];
154- (BOOL) oo_removeItemAtPath:(NSString *)path
156 return [
self removeItemAtPath:path error:NULL];
160- (BOOL) oo_moveItemAtPath:(NSString *)src toPath:(NSString *)dest
162 return [
self moveItemAtPath:src toPath:dest error:NULL];
167- (NSArray *) oo_directoryContentsAtPath:(NSString *)path
169 return [
self directoryContentsAtPath:path];
173- (BOOL) oo_createDirectoryAtPath:(NSString *)path attributes:(NSDictionary *)attributes
176 return [
self createDirectoryAtPath:path attributes:attributes];
180 return [
self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
185- (NSDictionary *) oo_fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)yorn
187 return [
self fileAttributesAtPath:path traverseLink:yorn];
191- (NSDictionary *) oo_fileSystemAttributesAtPath:(NSString *)path
193 return [
self fileSystemAttributesAtPath:path];
197- (BOOL) oo_removeItemAtPath:(NSString *)path
199 return [
self removeFileAtPath:path handler:nil];
203- (BOOL) oo_moveItemAtPath:(NSString *)src toPath:(NSString *)dest
205 return [
self movePath:src toPath:dest handler:nil];
212- (BOOL) chdirToSnapshotPath
215 NSString *savedir = [[NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
217 if (![
self changeCurrentDirectoryPath: savedir])
220 if (![
self createDirectoryAtPath: savedir attributes:
nil])
222 OOLog(
@"savedSnapshot.defaultPath.create.failed",
@"Unable to create directory %@", savedir);
225 if (![
self changeCurrentDirectoryPath: savedir])
227 OOLog(
@"savedSnapshot.defaultPath.chdir.failed",
@"Created %@ but couldn't make it the current directory.", savedir);
236- (BOOL) oo_oxzFileExistsAtPath:(NSString *)path
239 NSArray *components = [path pathComponents];
240 cl = [components count];
241 for (i = 0 ; i < cl ; i++)
243 NSString *component = [components objectAtIndex:i];
244 if ([[[component pathExtension] lowercaseString] isEqualToString:
@"oxz"])
253 BOOL result = [
self fileExistsAtPath:path isDirectory:&directory];
262 range.location = 0; range.length = i+1;
263 NSString *zipFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
264 range.location = i+1; range.length = cl-(i+1);
265 NSString *containedFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
268 const char* zipname = [zipFile cStringUsingEncoding:NSUTF8StringEncoding];
278 const char*
filename = [containedFile cStringUsingEncoding:NSUTF8StringEncoding];
#define OOLogERR(class, format,...)
#define OOLog(class, format,...)
NSDictionary * OODictionaryFromFile(NSString *path)
NSArray * oo_directoryContentsAtPath:(NSString *path)
NSString * defaultCommanderPath()
int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
unzFile ZEXPORT unzOpen64(const void *path)
int ZEXPORT unzClose(unzFile file)