src/file/cocoa/SDL_rwopsbundlesupport.m
author stopiccot <alexey.petruchik@gmail.com>
Sun, 06 Jan 2013 19:04:53 +0300
changeset 6832 156e608ec4ef
parent 4472 791b3256fb22
child 6848 478ecc8a58b3
permissions -rw-r--r--
Replaced manual NSAutoreleasePool handing with @autorelease
     1 #ifdef __APPLE__
     2 #import <Foundation/Foundation.h>
     3 
     4 #include "SDL_rwopsbundlesupport.h"
     5 
     6 /* For proper OS X applications, the resources are contained inside the application bundle.
     7  So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
     8  Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
     9  We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
    10  but we would somehow need to know what the bundle identifiers we need to search are.
    11  Also, note the bundle layouts are different for iPhone and Mac.
    12 */
    13 FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
    14 {
    15     FILE* fp = NULL;
    16 
    17 	// If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only.
    18 	if(strcmp("r", mode) && strcmp("rb", mode))
    19 	{
    20 		return fopen(file, mode);
    21 	}
    22 
    23 	@autoreleasepool {
    24         NSFileManager* file_manager = [NSFileManager defaultManager];
    25         NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    26 
    27         NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    28 
    29         NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
    30         if([file_manager fileExistsAtPath:full_path_with_file_to_try])
    31         {
    32             fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    33         }
    34         else
    35         {
    36             fp = fopen(file, mode);
    37         }
    38     }
    39 
    40 	return fp;
    41 }
    42 #endif