src/file/cocoa/SDL_rwopsbundlesupport.m
author Ryan C. Gordon
Wed, 07 Feb 2018 13:07:59 -0500
changeset 11847 4249379583bc
parent 11811 5d94cb6b24d3
child 12503 806492103856
permissions -rw-r--r--
cmake: Minor cleanup (reuse a string we already calculated).
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #ifdef __APPLE__
    24 #import <Foundation/Foundation.h>
    25 
    26 #include "SDL_rwopsbundlesupport.h"
    27 
    28 /* For proper OS X applications, the resources are contained inside the application bundle.
    29  So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
    30  Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
    31  We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
    32  but we would somehow need to know what the bundle identifiers we need to search are.
    33  Also, note the bundle layouts are different for iPhone and Mac.
    34 */
    35 FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
    36 { @autoreleasepool
    37 {
    38     FILE* fp = NULL;
    39 
    40     /* If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only. */
    41     if(strcmp("r", mode) && strcmp("rb", mode)) {
    42         return fopen(file, mode);
    43     }
    44 
    45     NSFileManager* file_manager = [NSFileManager defaultManager];
    46     NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    47 
    48     NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    49 
    50     NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
    51     if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
    52         fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    53     } else {
    54         fp = fopen(file, mode);
    55     }
    56 
    57     return fp;
    58 }}
    59 
    60 #endif /* __APPLE__ */
    61 
    62 /* vi: set ts=4 sw=4 expandtab: */