37@implementation NSFileManager (OOExtensions)
39- (NSArray *) commanderContentsOfPath:(NSString *)savePath
41 BOOL pathIsDirectory = NO;
42 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath isDirectory:&pathIsDirectory] && pathIsDirectory)
48 for (i = 0; i < [
contents count]; i++)
50 NSString* path = [
savePath stringByAppendingPathComponent: (NSString*)[
contents objectAtIndex:i]];
53 if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&pathIsDirectory] && pathIsDirectory)
57 if (![[path pathExtension] isEqual:
@"oolite-save"])
67 OOLog(
@"savedGame.read.fail.notDictionary",
@">>>> %@ could not be parsed as a saved game.", path);
74 [
contents replaceObjectAtIndex: i withObject: path];
82 OOLogERR(
@"savedGame.read.fail.fileNotFound",
@"File at path '%@' could not be found.", savePath);
90 const char *savedirEnv = SDL_getenv(
"OO_SAVEDIR");
95 savedir = [
NSString stringWithUTF8String:savedirEnv];
101 BOOL pathIsDirectory = NO;
104 if (![[NSFileManager defaultManager] fileExistsAtPath:savedir isDirectory:&pathIsDirectory])
107 if([
self oo_createDirectoryAtPath:savedir attributes:
nil])
113 OOLogERR(
@"savedGame.defaultPath.create.failed",
@"Unable to create '%@'. Saved games will go to the home directory.", savedir);
114 return NSHomeDirectory();
119 if (!pathIsDirectory)
121 OOLogERR(
@"savedGame.defaultPath.notDirectory",
@"'%@' is not a directory, saved games will go to the home directory.", savedir);
122 return NSHomeDirectory();
131- (NSArray *) oo_directoryContentsAtPath:(NSString *)path
133 return [
self contentsOfDirectoryAtPath:path error:NULL];
137- (BOOL) oo_createDirectoryAtPath:(NSString *)path attributes:(NSDictionary *)attributes
139 return [
self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
143- (NSDictionary *) oo_fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)traverseLink
147 NSString *linkDest =
nil;
150 linkDest = [
self destinationOfSymbolicLinkAtPath:path error:NULL];
151 if (linkDest !=
nil) path = linkDest;
152 }
while (linkDest !=
nil);
155 return [
self attributesOfItemAtPath:path error:NULL];
159- (NSDictionary *) oo_fileSystemAttributesAtPath:(NSString *)path
161 return [
self attributesOfFileSystemForPath:path error:NULL];
165- (BOOL) oo_removeItemAtPath:(NSString *)path
167 return [
self removeItemAtPath:path error:NULL];
171- (BOOL) oo_moveItemAtPath:(NSString *)src toPath:(NSString *)dest
173 return [
self moveItemAtPath:src toPath:dest error:NULL];
178- (NSArray *) oo_directoryContentsAtPath:(NSString *)path
180 return [
self directoryContentsAtPath:path];
184- (BOOL) oo_createDirectoryAtPath:(NSString *)path attributes:(NSDictionary *)attributes
186 #if OOLITE_WINDOWS && !OOLITE_MODERN_BUILD
187 return [
self createDirectoryAtPath:path attributes:attributes];
191 return [
self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
196- (NSDictionary *) oo_fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)yorn
198 return [
self fileAttributesAtPath:path traverseLink:yorn];
202- (NSDictionary *) oo_fileSystemAttributesAtPath:(NSString *)path
204 return [
self fileSystemAttributesAtPath:path];
208- (BOOL) oo_removeItemAtPath:(NSString *)path
210 return [
self removeFileAtPath:path handler:nil];
214- (BOOL) oo_moveItemAtPath:(NSString *)src toPath:(NSString *)dest
216 return [
self movePath:src toPath:dest handler:nil];
223- (BOOL) chdirToSnapshotPath
225 const char *snapshotEnv = SDL_getenv(
"OO_SNAPSHOTSDIR");
230 savedir = [NSString stringWithUTF8String:snapshotEnv];
235 savedir = [[NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
238 if (![
self changeCurrentDirectoryPath: savedir])
241 if (![
self oo_createDirectoryAtPath: savedir attributes:
nil])
243 OOLog(
@"savedSnapshot.defaultPath.create.failed",
@"Unable to create directory %@", savedir);
246 if (![
self changeCurrentDirectoryPath: savedir])
248 OOLog(
@"savedSnapshot.defaultPath.chdir.failed",
@"Created %@ but couldn't make it the current directory.", savedir);
257- (BOOL) oo_oxzFileExistsAtPath:(NSString *)path
260 NSArray *components = [
path pathComponents];
262 for (i = 0 ; i < cl ; i++)
264 NSString *component = [
components objectAtIndex:i];
265 if ([[[component pathExtension] lowercaseString] isEqualToString:
@"oxz"])
274 BOOL result = [
self fileExistsAtPath:path isDirectory:&directory];
283 range.location = 0; range.length = i+1;
284 NSString *zipFile = [
NSString pathWithComponents:[
components subarrayWithRange:range]];
285 range.location = i+1; range.length = cl-(i+1);
286 NSString *containedFile = [
NSString pathWithComponents:[
components subarrayWithRange:range]];
289 const char* zipname = [
zipFile 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)
struct unz_file_info64_s unz_file_info64