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
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.
alexey@6832
    24
	if (strcmp("r", mode) && strcmp("rb", mode)) {
ewing@4449
    25
		return fopen(file, mode);
ewing@4449
    26
	}
ewing@4449
    27
	
alexey@6832
    28
	@autoreleasepool {
alexey@6832
    29
        NSFileManager* file_manager = [NSFileManager defaultManager];
alexey@6832
    30
        NSString* resource_path = [[NSBundle mainBundle] resourcePath];
ewing@4447
    31
alexey@6832
    32
        NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
ewing@4447
    33
alexey@6832
    34
        NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
alexey@6832
    35
        if ([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
alexey@6832
    36
            fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
alexey@6832
    37
        } else {
alexey@6832
    38
            fp = fopen(file, mode);
alexey@6832
    39
        }
alexey@6832
    40
    }
ewing@4447
    41
ewing@4447
    42
	return fp;
ewing@4447
    43
}
ewing@4447
    44
ewing@4451
    45
FILE* TestSupportRWops_OpenFPFromWriteDir(const char *file, const char *mode)
ewing@4447
    46
{
ewing@4447
    47
    FILE* fp = NULL;
ewing@4447
    48
	
alexey@6832
    49
	@autoreleasepool {
alexey@6832
    50
        NSFileManager* file_manager = [NSFileManager defaultManager];
alexey@6832
    51
        NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
alexey@6832
    52
        NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
ewing@4451
    53
alexey@6832
    54
        fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
alexey@6832
    55
    }
alexey@6832
    56
    
ewing@4447
    57
	return fp;
ewing@4447
    58
}
ewing@4451
    59
ewing@4451
    60
SDL_RWops* TestSupportRWops_OpenRWopsFromReadDir(const char *file, const char *mode)
ewing@4451
    61
{
ewing@4451
    62
	return SDL_RWFromFile(file, mode);
ewing@4451
    63
}
ewing@4451
    64
ewing@4451
    65
SDL_RWops* TestSupportRWops_OpenRWopsFromWriteDir(const char *file, const char *mode)
ewing@4451
    66
{
ewing@4451
    67
	SDL_RWops* rw = NULL;
ewing@4451
    68
alexey@6832
    69
	@autoreleasepool {
alexey@6832
    70
        NSFileManager* file_manager = [NSFileManager defaultManager];
alexey@6832
    71
        NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
alexey@6832
    72
        NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
alexey@6832
    73
        
alexey@6832
    74
        rw = SDL_RWFromFile( [full_path_with_file_to_try fileSystemRepresentation], mode );
alexey@6832
    75
    }
alexey@6832
    76
    
ewing@4451
    77
	return rw;
ewing@4451
    78
}
ewing@4451
    79