src/filesystem/cocoa/SDL_sysfilesystem.m
author Alex Szpakowski <slime73@gmail.com>
Mon, 14 Jul 2014 16:50:25 -0300
branchiOS-improvements
changeset 9487 f9fbc2d1edb0
parent 8149 681eb46b8ac4
child 9515 63f5d07c7d70
permissions -rw-r--r--
Misc. iOS code improvements.

- Use @autoreleasepool instead of NSAutoReleasePool.

- Code style fixups.
icculus@7667
     1
/*
icculus@7667
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
icculus@7667
     4
icculus@7667
     5
  This software is provided 'as-is', without any express or implied
icculus@7667
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@7667
     7
  arising from the use of this software.
icculus@7667
     8
icculus@7667
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@7667
    10
  including commercial applications, and to alter it and redistribute it
icculus@7667
    11
  freely, subject to the following restrictions:
icculus@7667
    12
icculus@7667
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@7667
    14
     claim that you wrote the original software. If you use this software
icculus@7667
    15
     in a product, an acknowledgment in the product documentation would be
icculus@7667
    16
     appreciated but is not required.
icculus@7667
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@7667
    18
     misrepresented as being the original software.
icculus@7667
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@7667
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
icculus@7667
    22
icculus@7667
    23
#ifdef SDL_FILESYSTEM_COCOA
icculus@7667
    24
icculus@7667
    25
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
icculus@7667
    26
/* System dependent filesystem routines                                */
icculus@7667
    27
icculus@7674
    28
#include <Foundation/Foundation.h>
icculus@7667
    29
#include <sys/stat.h>
icculus@7668
    30
#include <sys/types.h>
icculus@7667
    31
icculus@7667
    32
#include "SDL_error.h"
icculus@7667
    33
#include "SDL_stdinc.h"
icculus@7667
    34
#include "SDL_filesystem.h"
icculus@7667
    35
icculus@7667
    36
char *
icculus@7667
    37
SDL_GetBasePath(void)
icculus@7667
    38
{
icculus@7667
    39
    char *retval = NULL;
slime73@9487
    40
slime73@9487
    41
    @autoreleasepool {
slime73@9487
    42
        const char *base = NULL;
slime73@9487
    43
        NSBundle *bundle = [NSBundle mainBundle];
slime73@9487
    44
        const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
slime73@9487
    45
        if (baseType == NULL) {
slime73@9487
    46
            baseType = "resource";
slime73@9487
    47
        }
slime73@9487
    48
        if (SDL_strcasecmp(baseType, "bundle")==0) {
slime73@9487
    49
            base = [[bundle bundlePath] fileSystemRepresentation];
slime73@9487
    50
        } else if (SDL_strcasecmp(baseType, "parent")==0) {
slime73@9487
    51
            base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
icculus@7667
    52
        } else {
slime73@9487
    53
            /* this returns the exedir for non-bundled  and the resourceDir for bundled apps */
slime73@9487
    54
            base = [[bundle resourcePath] fileSystemRepresentation];
slime73@9487
    55
        }
slime73@9487
    56
slime73@9487
    57
        if (base) {
slime73@9487
    58
            const size_t len = SDL_strlen(base) + 2;
slime73@9487
    59
            retval = (char *) SDL_malloc(len);
slime73@9487
    60
            if (retval == NULL) {
slime73@9487
    61
                SDL_OutOfMemory();
slime73@9487
    62
            } else {
slime73@9487
    63
                SDL_snprintf(retval, len, "%s/", base);
slime73@9487
    64
            }
icculus@7667
    65
        }
icculus@7667
    66
    }
icculus@7667
    67
icculus@7667
    68
    return retval;
icculus@7667
    69
}
icculus@7667
    70
icculus@7667
    71
char *
icculus@7667
    72
SDL_GetPrefPath(const char *org, const char *app)
icculus@7667
    73
{
icculus@7667
    74
    char *retval = NULL;
icculus@7667
    75
slime73@9487
    76
    @autoreleasepool {
slime73@9487
    77
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
slime73@9487
    78
slime73@9487
    79
        if ([array count] > 0) {  /* we only want the first item in the list. */
slime73@9487
    80
            NSString *str = [array objectAtIndex:0];
slime73@9487
    81
            const char *base = [str fileSystemRepresentation];
slime73@9487
    82
            if (base) {
slime73@9487
    83
                const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
slime73@9487
    84
                retval = (char *) SDL_malloc(len);
slime73@9487
    85
                if (retval == NULL) {
slime73@9487
    86
                    SDL_OutOfMemory();
slime73@9487
    87
                } else {
slime73@9487
    88
                    char *ptr;
slime73@9487
    89
                    SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
slime73@9487
    90
                    for (ptr = retval+1; *ptr; ptr++) {
slime73@9487
    91
                        if (*ptr == '/') {
slime73@9487
    92
                            *ptr = '\0';
slime73@9487
    93
                            mkdir(retval, 0700);
slime73@9487
    94
                            *ptr = '/';
slime73@9487
    95
                        }
icculus@7667
    96
                    }
slime73@9487
    97
                    mkdir(retval, 0700);
icculus@7667
    98
                }
icculus@7667
    99
            }
icculus@7667
   100
        }
icculus@7667
   101
    }
icculus@7667
   102
    return retval;
icculus@7667
   103
}
icculus@7667
   104
icculus@7667
   105
#endif /* SDL_FILESYSTEM_COCOA */
icculus@7667
   106
icculus@7667
   107
/* vi: set ts=4 sw=4 expandtab: */