test/automated/rwops/TestSupportRWops_Cocoa.m
author Eric Wing <ewing . public |-at-| gmail . com>
Sun, 09 May 2010 06:58:30 -0700
changeset 4451 033c455bbe99
parent 4449 test/automated/rwops/Test_rwopsbundlesupport.m@8f73f4a3c972
child 6832 156e608ec4ef
permissions -rw-r--r--
Refactored automated rwops tests so read and write directories can be more easily customized.

The refactored tests were written in recognition that Mac and iPhone current working directories are usually not going to work. Resource directories are in bundles and write directories are restricted to certain areas. In theory, other platforms may have this problem too, hence the refactoring.

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