src/filesystem/cocoa/SDL_sysfilesystem.m
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Aug 2013 19:57:11 -0400
changeset 7667 be1cc6f55840
child 7668 66da4242ce53
permissions -rw-r--r--
Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
  Simple DirectMedia Layer
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
  arising from the use of this software.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
     appreciated but is not required.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
     misrepresented as being the original software.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
*/
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
#include "SDL_config.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#ifdef SDL_FILESYSTEM_COCOA
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
/* System dependent filesystem routines                                */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#include <Cocoa/Cocoa.h>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#include <sys/stat.h>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include "SDL_error.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#include "SDL_stdinc.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include "SDL_filesystem.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
char *
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
SDL_GetBasePath(void)
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
{
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    const char *base = [[[NSBundle mainBundle] bundlePath] UTF8String];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    char *retval = NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    if (base) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
        const size_t len = SDL_strlen(base) + 2;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
        retval = (char *) SDL_malloc(len);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
        if (retval == NULL) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
            SDL_OutOfMemory();
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        } else {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
            SDL_snprintf(retval, len, "%s/", base);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
        }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    [pool release];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    return retval;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
}
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
char *
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
SDL_GetPrefPath(const char *org, const char *app)
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
{
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    char *retval = NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    (void) org;  // unused on Mac OS X and iOS.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    if ([array count] > 0) {  // we only want the first item in the list.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        NSString *str = [array objectAtIndex:0];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        const char *base = [str UTF8String];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        if (base) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
            const size_t len = SDL_strlen(base) + SDL_strlen(app) + 3;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
            retval = (char *) SDL_malloc(len);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
            if (retval == NULL) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
                SDL_OutOfMemory();
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
            } else {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
                char *ptr;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
                SDL_snprintf(retval, len, "%s/%s/", base, app);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
                for (ptr = retval+1; *ptr; ptr++) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
                    if (*ptr == '/') {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
                        *ptr = '\0';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
                        mkdir(retval, 0700);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
                        *ptr = '/';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
                    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
                }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
                mkdir(retval, 0700);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
            }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
        }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    [pool release];
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    return retval;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
}
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
#endif /* SDL_FILESYSTEM_COCOA */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
/* vi: set ts=4 sw=4 expandtab: */