test/automated/rwops/Test_rwopsbundlesupport.m
author Eric Wing <ewing . public |-at-| gmail . com>
Sat, 08 May 2010 04:52:17 -0700
changeset 4449 8f73f4a3c972
parent 4447 947201caa46e
permissions -rw-r--r--
Exempted writable modes from bundle check on OS X since bundle areas are typically read-only.
     1 #ifdef __APPLE__
     2 #import <Foundation/Foundation.h>
     3 
     4 /* For proper OS X applications, the resources are contained inside the application bundle.
     5  So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
     6  Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
     7  We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
     8  but we would somehow need to know what the bundle identifiers we need to search are.
     9  Also, note the bundle layouts are different for iPhone and Mac.
    10 */
    11 FILE* Test_OpenFPFromBundleOrFallback(const char *file, const char *mode)
    12 {
    13     FILE* fp = NULL;
    14 
    15 	// If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only.
    16 	if(strcmp("r", mode) && strcmp("rb", mode))
    17 	{
    18 		return fopen(file, mode);
    19 	}
    20 	
    21 	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
    22 
    23 
    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 	[autorelease_pool drain];
    40 
    41 	return fp;
    42 }
    43 
    44 FILE* Test_OpenFPFromTemporaryDir(const char *file, const char *mode)
    45 {
    46     FILE* fp = NULL;
    47 	
    48 	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
    49 	
    50 	
    51 	NSFileManager* file_manager = [NSFileManager defaultManager];
    52 	
    53 	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    54 	
    55 	NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
    56 	fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    57 	
    58 	[autorelease_pool drain];
    59 	
    60 	return fp;
    61 }
    62 #endif