Oolite
Loading...
Searching...
No Matches
NSFileManager(OOExtensions) Category Reference

#include <NSFileManagerOOExtensions.h>

Instance Methods

(NSArray *) - commanderContentsOfPath:
(NSString *) - defaultCommanderPath
(NSArray *) - oo_directoryContentsAtPath:
(BOOL) - oo_createDirectoryAtPath:attributes:
(NSDictionary *) - oo_fileAttributesAtPath:traverseLink:
(NSDictionary *) - oo_fileSystemAttributesAtPath:
(BOOL) - oo_removeItemAtPath:
(BOOL) - oo_moveItemAtPath:toPath:
(BOOL) - oo_oxzFileExistsAtPath:

Detailed Description

Definition at line 39 of file NSFileManagerOOExtensions.h.

Method Documentation

◆ commanderContentsOfPath:

- (NSArray *) commanderContentsOfPath: (NSString*) savePath

Definition at line 39 of file NSFileManagerOOExtensions.m.

39 :(NSString*)savePath
40{
41 BOOL pathIsDirectory = NO;
42 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath isDirectory:&pathIsDirectory] && pathIsDirectory) {
43 NSMutableArray* contents = [NSMutableArray arrayWithArray:[self oo_directoryContentsAtPath:savePath]];
44
45 // at this point we should strip out any files not loadable as Oolite saved games
46 unsigned i;
47 for (i = 0; i < [contents count]; i++) {
48 NSString* path = [savePath stringByAppendingPathComponent:(NSString*)[contents objectAtIndex:i]];
49
50 // ensure it's not a directory
51 if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&pathIsDirectory] && pathIsDirectory) {
52
53 // check file extension
54 if (![[path pathExtension] isEqual:@"oolite-save"]) {
55 [contents removeObjectAtIndex:i--];
56 continue;
57 }
58
59 // check to see if we can parse the file okay
60 NSDictionary* cdr = OODictionaryFromFile(path);
61 if (!cdr) {
62 OOLog(@"savedGame.read.fail.notDictionary", @">>>> %@ could not be parsed as a saved game.", path);
63 [contents removeObjectAtIndex:i--];
64 continue;
65 }
66 }
67
68 // all okay - we can use this path!
69 [contents replaceObjectAtIndex:i withObject:path];
70 }
71
72 return contents;
73 } else {
74 OOLogERR(@"savedGame.read.fail.fileNotFound", @"File at path '%@' could not be found.", savePath);
75 return nil;
76 }
77}
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSDictionary * OODictionaryFromFile(NSString *path)
return nil

References nil, oo_directoryContentsAtPath:, OODictionaryFromFile(), OOLog, and OOLogERR.

Here is the call graph for this function:

◆ defaultCommanderPath

- (NSString *) defaultCommanderPath

Definition at line 79 of file NSFileManagerOOExtensions.m.

80{
81 const char* savedirEnv = SDL_getenv("OO_SAVEDIR");
82 NSString* savedir;
83
84 if (savedirEnv) {
85 savedir = [NSString stringWithUTF8String:savedirEnv];
86 } else {
87 savedir = [NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR];
88 }
89 BOOL pathIsDirectory = NO;
90
91 // does it exist?
92 if (![[NSFileManager defaultManager] fileExistsAtPath:savedir isDirectory:&pathIsDirectory]) {
93 // it doesn't exist.
94 if ([self oo_createDirectoryAtPath:savedir attributes:nil]) {
95 return savedir;
96 } else {
97 OOLogERR(@"savedGame.defaultPath.create.failed", @"Unable to create '%@'. Saved games will go to the home directory.", savedir);
98 return NSHomeDirectory();
99 }
100 }
101
102 // is it a directory?
103 if (!pathIsDirectory) {
104 OOLogERR(@"savedGame.defaultPath.notDirectory", @"'%@' is not a directory, saved games will go to the home directory.", savedir);
105 return NSHomeDirectory();
106 }
107
108 return savedir;
109}

References defaultCommanderPath, nil, and OOLogERR.

Referenced by defaultCommanderPath.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ oo_createDirectoryAtPath:attributes:

- (BOOL) oo_createDirectoryAtPath: (NSString*) path
attributes: (NSDictionary*) attributes 

Definition at line 118 of file NSFileManagerOOExtensions.m.

118 :(NSString*)path attributes:(NSDictionary*)attributes
119{
120 return [self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
121}

◆ oo_directoryContentsAtPath:

- (NSArray *) oo_directoryContentsAtPath: (NSString*) path

Definition at line 113 of file NSFileManagerOOExtensions.m.

113 :(NSString*)path
114{
115 return [self contentsOfDirectoryAtPath:path error:NULL];
116}

Referenced by commanderContentsOfPath:.

Here is the caller graph for this function:

◆ oo_fileAttributesAtPath:traverseLink:

- (NSDictionary *) oo_fileAttributesAtPath: (NSString*) path
traverseLink: (BOOL) yorn 

Definition at line 123 of file NSFileManagerOOExtensions.m.

123 :(NSString*)path traverseLink:(BOOL)traverseLink
124{
125 if (traverseLink) {
126 NSString* linkDest = nil;
127 do {
128 linkDest = [self destinationOfSymbolicLinkAtPath:path error:NULL];
129 if (linkDest != nil)
130 path = linkDest;
131 } while (linkDest != nil);
132 }
133
134 return [self attributesOfItemAtPath:path error:NULL];
135}

References nil.

◆ oo_fileSystemAttributesAtPath:

- (NSDictionary *) oo_fileSystemAttributesAtPath: (NSString*) path

Definition at line 137 of file NSFileManagerOOExtensions.m.

137 :(NSString*)path
138{
139 return [self attributesOfFileSystemForPath:path error:NULL];
140}

◆ oo_moveItemAtPath:toPath:

- (BOOL) oo_moveItemAtPath: (NSString*) src
toPath: (NSString*) dest 

Definition at line 147 of file NSFileManagerOOExtensions.m.

147 :(NSString*)src toPath:(NSString*)dest
148{
149 return [self moveItemAtPath:src toPath:dest error:NULL];
150}

◆ oo_oxzFileExistsAtPath:

- (BOOL) oo_oxzFileExistsAtPath: (NSString*) path

Definition at line 215 of file NSFileManagerOOExtensions.m.

215 :(NSString*)path
216{
217 NSUInteger i, cl;
218 NSArray* components = [path pathComponents];
219 cl = [components count];
220 for (i = 0; i < cl; i++) {
221 NSString* component = [components objectAtIndex:i];
222 if ([[[component pathExtension] lowercaseString] isEqualToString:@"oxz"]) {
223 break;
224 }
225 }
226 // if i == cl then the path is entirely uncompressed
227 if (i == cl) {
228 BOOL directory = NO;
229 BOOL result = [self fileExistsAtPath:path isDirectory:&directory];
230 if (directory) {
231 return NO;
232 }
233 return result;
234 }
235
236 NSRange range;
237 range.location = 0;
238 range.length = i + 1;
239 NSString* zipFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
240 range.location = i + 1;
241 range.length = cl - (i + 1);
242 NSString* containedFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
243
244 unzFile uf = NULL;
245 const char* zipname = [zipFile cStringUsingEncoding:NSUTF8StringEncoding];
246 if (zipname != NULL) {
247 uf = unzOpen64(zipname);
248 }
249 if (uf == NULL) {
250 // no such zip file
251 return NO;
252 }
253 const char* filename = [containedFile cStringUsingEncoding:NSUTF8StringEncoding];
254 // unzLocateFile(*, *, 1) = case-sensitive extract
255 BOOL result = YES;
256 if (unzLocateFile(uf, filename, 1) != UNZ_OK) {
257 result = NO;
258 } else {
259 int err = UNZ_OK;
260 unz_file_info64 file_info = { 0 };
261 err = unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, NULL, 0, NULL, 0);
262 if (err != UNZ_OK) {
263 result = NO;
264 } else {
265 }
266 }
267 unzClose(uf);
268 return result;
269}
const char * filename
Definition ioapi.h:125
int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
Definition unzip.c:1161
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
Definition unzip.c:1058
unzFile ZEXPORT unzOpen64(const void *path)
Definition unzip.c:758
int ZEXPORT unzClose(unzFile file)
Definition unzip.c:768
struct unz_file_info64_s unz_file_info64
voidp unzFile
Definition unzip.h:72
#define UNZ_OK
Definition unzip.h:75

References UNZ_OK, unzClose(), unzGetCurrentFileInfo64(), unzLocateFile(), and unzOpen64().

Here is the call graph for this function:

◆ oo_removeItemAtPath:

- (BOOL) oo_removeItemAtPath: (NSString*) path

Definition at line 142 of file NSFileManagerOOExtensions.m.

142 :(NSString*)path
143{
144 return [self removeItemAtPath:path error:NULL];
145}

The documentation for this category was generated from the following files: