test/automated/rwops/TestSupportRWops_Cocoa.m
changeset 4451 033c455bbe99
parent 4449 8f73f4a3c972
child 6832 156e608ec4ef
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/automated/rwops/TestSupportRWops_Cocoa.m	Sun May 09 06:58:30 2010 -0700
     1.3 @@ -0,0 +1,89 @@
     1.4 +#import "TestSupportRWops.h"
     1.5 +#import <Foundation/Foundation.h>
     1.6 +#include "SDL.h"
     1.7 +
     1.8 +/* For proper OS X applications, the resources are contained inside the application bundle.
     1.9 + So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
    1.10 + Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
    1.11 + We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
    1.12 + but we would somehow need to know what the bundle identifiers we need to search are.
    1.13 + Conversely, write directories are usually not in the bundles. This implementation uses NSTemporaryDirectory,
    1.14 + but consider Preferences, Application Support, Documents, etc.
    1.15 + Also, note the bundle layouts are different for iPhone and Mac.
    1.16 +*/
    1.17 +
    1.18 +const char* RWOPS_READ = "rwops/read";
    1.19 +const char* RWOPS_WRITE = "write";
    1.20 +
    1.21 +
    1.22 +FILE* TestSupportRWops_OpenFPFromReadDir(const char *file, const char *mode)
    1.23 +{
    1.24 +    FILE* fp = NULL;
    1.25 +
    1.26 +	// If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only.
    1.27 +	if(strcmp("r", mode) && strcmp("rb", mode))
    1.28 +	{
    1.29 +		return fopen(file, mode);
    1.30 +	}
    1.31 +	
    1.32 +	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
    1.33 +
    1.34 +
    1.35 +	NSFileManager* file_manager = [NSFileManager defaultManager];
    1.36 +	NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    1.37 +
    1.38 +	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    1.39 +
    1.40 +	NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
    1.41 +	if([file_manager fileExistsAtPath:full_path_with_file_to_try])
    1.42 +	{
    1.43 +		fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    1.44 +	}
    1.45 +	else
    1.46 +	{
    1.47 +		fp = fopen(file, mode);
    1.48 +	}
    1.49 +
    1.50 +	[autorelease_pool drain];
    1.51 +
    1.52 +	return fp;
    1.53 +}
    1.54 +
    1.55 +FILE* TestSupportRWops_OpenFPFromWriteDir(const char *file, const char *mode)
    1.56 +{
    1.57 +    FILE* fp = NULL;
    1.58 +	
    1.59 +	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
    1.60 +	
    1.61 +	NSFileManager* file_manager = [NSFileManager defaultManager];
    1.62 +	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    1.63 +	NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
    1.64 +
    1.65 +	fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    1.66 +	
    1.67 +	[autorelease_pool drain];
    1.68 +	
    1.69 +	return fp;
    1.70 +}
    1.71 +
    1.72 +SDL_RWops* TestSupportRWops_OpenRWopsFromReadDir(const char *file, const char *mode)
    1.73 +{
    1.74 +	return SDL_RWFromFile(file, mode);
    1.75 +}
    1.76 +
    1.77 +SDL_RWops* TestSupportRWops_OpenRWopsFromWriteDir(const char *file, const char *mode)
    1.78 +{
    1.79 +	SDL_RWops* rw = NULL;
    1.80 +
    1.81 +	NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
    1.82 +	
    1.83 +	NSFileManager* file_manager = [NSFileManager defaultManager];
    1.84 +	NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    1.85 +	NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
    1.86 +	
    1.87 +	rw = SDL_RWFromFile( [full_path_with_file_to_try fileSystemRepresentation], mode );
    1.88 +
    1.89 +	[autorelease_pool drain];
    1.90 +	return rw;
    1.91 +}
    1.92 +