src/filesystem/cocoa/SDL_sysfilesystem.m
author Ryan C. Gordon <icculus@icculus.org>
Wed, 23 Oct 2013 00:58:20 -0400
changeset 7883 62f562a475e1
parent 7866 42da269b7977
child 7891 b34f5e226458
permissions -rw-r--r--
SDL_GetPrefPath() now uses the organization on all platforms.

Even if that's not the general convention for a given platform.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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_config.h"
    22 
    23 #ifdef SDL_FILESYSTEM_COCOA
    24 
    25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    26 /* System dependent filesystem routines                                */
    27 
    28 #include <Foundation/Foundation.h>
    29 #include <sys/stat.h>
    30 #include <sys/types.h>
    31 
    32 #include "SDL_error.h"
    33 #include "SDL_stdinc.h"
    34 #include "SDL_filesystem.h"
    35 
    36 char *
    37 SDL_GetBasePath(void)
    38 {
    39     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    40     NSBundle *bundle = [NSBundle mainBundle];
    41     const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
    42     const char *base = NULL;
    43     char *retval = NULL;
    44     if (baseType == NULL) {
    45         baseType = "resource";
    46     }
    47     if (SDL_strcasecmp(baseType, "bundle")==0) {
    48         base = [[bundle bundlePath] fileSystemRepresentation];
    49     } else if (SDL_strcasecmp(baseType, "parent")==0) {
    50         base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
    51     } else {
    52         /* this returns the exedir for non-bundled  and the resourceDir for bundled apps */
    53         base = [[bundle resourcePath] fileSystemRepresentation];
    54     }
    55     if (base) {
    56         const size_t len = SDL_strlen(base) + 2;
    57         retval = (char *) SDL_malloc(len);
    58         if (retval == NULL) {
    59             SDL_OutOfMemory();
    60         } else {
    61             SDL_snprintf(retval, len, "%s/", base);
    62         }
    63     }
    64 
    65     [pool release];
    66     return retval;
    67 }
    68 
    69 char *
    70 SDL_GetPrefPath(const char *org, const char *app)
    71 {
    72     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    73     NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    74     char *retval = NULL;
    75 
    76     if ([array count] > 0) {  /* we only want the first item in the list. */
    77         NSString *str = [array objectAtIndex:0];
    78         const char *base = [str fileSystemRepresentation];
    79         if (base) {
    80             const size_t len = SDL_strlen(base) + SDL_strlen(app) + 4;
    81             retval = (char *) SDL_malloc(len);
    82             if (retval == NULL) {
    83                 SDL_OutOfMemory();
    84             } else {
    85                 char *ptr;
    86                 SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
    87                 for (ptr = retval+1; *ptr; ptr++) {
    88                     if (*ptr == '/') {
    89                         *ptr = '\0';
    90                         mkdir(retval, 0700);
    91                         *ptr = '/';
    92                     }
    93                 }
    94                 mkdir(retval, 0700);
    95             }
    96         }
    97     }
    98 
    99     [pool release];
   100     return retval;
   101 }
   102 
   103 #endif /* SDL_FILESYSTEM_COCOA */
   104 
   105 /* vi: set ts=4 sw=4 expandtab: */