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