test/automated/rwops/TestSupportRWops_Cocoa.m
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 4451 033c455bbe99
child 6832 156e608ec4ef
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
ewing@4451
     1
#import "TestSupportRWops.h"
ewing@4447
     2
#import <Foundation/Foundation.h>
ewing@4451
     3
#include "SDL.h"
ewing@4447
     4
ewing@4447
     5
/* For proper OS X applications, the resources are contained inside the application bundle.
ewing@4447
     6
 So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
ewing@4447
     7
 Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
ewing@4447
     8
 We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
ewing@4447
     9
 but we would somehow need to know what the bundle identifiers we need to search are.
ewing@4451
    10
 Conversely, write directories are usually not in the bundles. This implementation uses NSTemporaryDirectory,
ewing@4451
    11
 but consider Preferences, Application Support, Documents, etc.
ewing@4447
    12
 Also, note the bundle layouts are different for iPhone and Mac.
ewing@4447
    13
*/
ewing@4451
    14
ewing@4451
    15
const char* RWOPS_READ = "rwops/read";
ewing@4451
    16
const char* RWOPS_WRITE = "write";
ewing@4451
    17
ewing@4451
    18
ewing@4451
    19
FILE* TestSupportRWops_OpenFPFromReadDir(const char *file, const char *mode)
ewing@4447
    20
{
ewing@4447
    21
    FILE* fp = NULL;
ewing@4447
    22
ewing@4449
    23
	// If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only.
ewing@4449
    24
	if(strcmp("r", mode) && strcmp("rb", mode))
ewing@4449
    25
	{
ewing@4449
    26
		return fopen(file, mode);
ewing@4449
    27
	}
ewing@4449
    28
	
ewing@4447
    29
	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
ewing@4447
    30
ewing@4447
    31
ewing@4447
    32
	NSFileManager* file_manager = [NSFileManager defaultManager];
ewing@4447
    33
	NSString* resource_path = [[NSBundle mainBundle] resourcePath];
ewing@4447
    34
ewing@4447
    35
	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
ewing@4447
    36
ewing@4447
    37
	NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
ewing@4447
    38
	if([file_manager fileExistsAtPath:full_path_with_file_to_try])
ewing@4447
    39
	{
ewing@4447
    40
		fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
ewing@4447
    41
	}
ewing@4447
    42
	else
ewing@4447
    43
	{
ewing@4447
    44
		fp = fopen(file, mode);
ewing@4447
    45
	}
ewing@4447
    46
ewing@4447
    47
	[autorelease_pool drain];
ewing@4447
    48
ewing@4447
    49
	return fp;
ewing@4447
    50
}
ewing@4447
    51
ewing@4451
    52
FILE* TestSupportRWops_OpenFPFromWriteDir(const char *file, const char *mode)
ewing@4447
    53
{
ewing@4447
    54
    FILE* fp = NULL;
ewing@4447
    55
	
ewing@4447
    56
	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
ewing@4447
    57
	
ewing@4447
    58
	NSFileManager* file_manager = [NSFileManager defaultManager];
ewing@4447
    59
	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
ewing@4447
    60
	NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
ewing@4451
    61
ewing@4447
    62
	fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
ewing@4447
    63
	
ewing@4447
    64
	[autorelease_pool drain];
ewing@4447
    65
	
ewing@4447
    66
	return fp;
ewing@4447
    67
}
ewing@4451
    68
ewing@4451
    69
SDL_RWops* TestSupportRWops_OpenRWopsFromReadDir(const char *file, const char *mode)
ewing@4451
    70
{
ewing@4451
    71
	return SDL_RWFromFile(file, mode);
ewing@4451
    72
}
ewing@4451
    73
ewing@4451
    74
SDL_RWops* TestSupportRWops_OpenRWopsFromWriteDir(const char *file, const char *mode)
ewing@4451
    75
{
ewing@4451
    76
	SDL_RWops* rw = NULL;
ewing@4451
    77
ewing@4451
    78
	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
ewing@4451
    79
	
ewing@4451
    80
	NSFileManager* file_manager = [NSFileManager defaultManager];
ewing@4451
    81
	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
ewing@4451
    82
	NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
ewing@4451
    83
	
ewing@4451
    84
	rw = SDL_RWFromFile( [full_path_with_file_to_try fileSystemRepresentation], mode );
ewing@4451
    85
ewing@4451
    86
	[autorelease_pool drain];
ewing@4451
    87
	return rw;
ewing@4451
    88
}
ewing@4451
    89