src/file/cocoa/SDL_rwopsbundlesupport.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 15 Mar 2014 19:30:52 -0700
changeset 8627 704a0bfecf75
parent 8616 ec011c16e2fd
child 8986 1c81316ac642
permissions -rw-r--r--
Fixed iOS build
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8616
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     1
/*
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     2
  Simple DirectMedia Layer
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     4
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     5
  This software is provided 'as-is', without any express or implied
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     7
  arising from the use of this software.
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     8
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    11
  freely, subject to the following restrictions:
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    12
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    14
     claim that you wrote the original software. If you use this software
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    16
     appreciated but is not required.
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    18
     misrepresented as being the original software.
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    20
*/
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    21
#include "../../SDL_internal.h"
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    22
8627
704a0bfecf75 Fixed iOS build
Sam Lantinga <slouken@libsdl.org>
parents: 8616
diff changeset
    23
#ifdef __APPLE__
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    24
#import <Foundation/Foundation.h>
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    25
4472
791b3256fb22 Mostly cleaned up warnings with -Wmissing-prototypes
Sam Lantinga <slouken@libsdl.org>
parents: 4449
diff changeset
    26
#include "SDL_rwopsbundlesupport.h"
791b3256fb22 Mostly cleaned up warnings with -Wmissing-prototypes
Sam Lantinga <slouken@libsdl.org>
parents: 4449
diff changeset
    27
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    28
/* For proper OS X applications, the resources are contained inside the application bundle.
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    29
 So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    30
 Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    31
 We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    32
 but we would somehow need to know what the bundle identifiers we need to search are.
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    33
 Also, note the bundle layouts are different for iPhone and Mac.
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    34
*/
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    35
FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    36
{
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    37
    FILE* fp = NULL;
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    38
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    39
    /* If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only. */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    40
    if(strcmp("r", mode) && strcmp("rb", mode))
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    41
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    42
        return fopen(file, mode);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    43
    }
4449
8f73f4a3c972 Exempted writable modes from bundle check on OS X since bundle areas are typically read-only.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 4447
diff changeset
    44
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    45
    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    46
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    47
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    48
    NSFileManager* file_manager = [NSFileManager defaultManager];
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    49
    NSString* resource_path = [[NSBundle mainBundle] resourcePath];
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    50
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    51
    NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    52
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    53
    NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    54
    if([file_manager fileExistsAtPath:full_path_with_file_to_try])
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    55
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    56
        fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    57
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    58
    else
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    59
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    60
        fp = fopen(file, mode);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    61
    }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    62
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    63
    [autorelease_pool drain];
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    64
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
    65
    return fp;
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    66
}
8616
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    67
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    68
#endif /* __MACOSX__ */
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    69
ec011c16e2fd Added missing copyright notices
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    70
/* vi: set ts=4 sw=4 expandtab: */