test/automated/rwops/TestSupportRWops_Cocoa.m
author stopiccot <alexey.petruchik@gmail.com>
Sun, 06 Jan 2013 19:04:53 +0300
changeset 6832 156e608ec4ef
parent 4451 033c455bbe99
child 6848 478ecc8a58b3
permissions -rw-r--r--
Replaced manual NSAutoreleasePool handing with @autorelease
     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 		return fopen(file, mode);
    26 	}
    27 	
    28 	@autoreleasepool {
    29         NSFileManager* file_manager = [NSFileManager defaultManager];
    30         NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    31 
    32         NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    33 
    34         NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
    35         if ([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
    36             fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    37         } else {
    38             fp = fopen(file, mode);
    39         }
    40     }
    41 
    42 	return fp;
    43 }
    44 
    45 FILE* TestSupportRWops_OpenFPFromWriteDir(const char *file, const char *mode)
    46 {
    47     FILE* fp = NULL;
    48 	
    49 	@autoreleasepool {
    50         NSFileManager* file_manager = [NSFileManager defaultManager];
    51         NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    52         NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
    53 
    54         fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    55     }
    56     
    57 	return fp;
    58 }
    59 
    60 SDL_RWops* TestSupportRWops_OpenRWopsFromReadDir(const char *file, const char *mode)
    61 {
    62 	return SDL_RWFromFile(file, mode);
    63 }
    64 
    65 SDL_RWops* TestSupportRWops_OpenRWopsFromWriteDir(const char *file, const char *mode)
    66 {
    67 	SDL_RWops* rw = NULL;
    68 
    69 	@autoreleasepool {
    70         NSFileManager* file_manager = [NSFileManager defaultManager];
    71         NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    72         NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
    73         
    74         rw = SDL_RWFromFile( [full_path_with_file_to_try fileSystemRepresentation], mode );
    75     }
    76     
    77 	return rw;
    78 }
    79