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.
     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 <Cocoa/Cocoa.h>
    29 #include <sys/stat.h>
    30 
    31 #include "SDL_error.h"
    32 #include "SDL_stdinc.h"
    33 #include "SDL_filesystem.h"
    34 
    35 char *
    36 SDL_GetBasePath(void)
    37 {
    38     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    39     const char *base = [[[NSBundle mainBundle] bundlePath] UTF8String];
    40     char *retval = NULL;
    41     if (base) {
    42         const size_t len = SDL_strlen(base) + 2;
    43         retval = (char *) SDL_malloc(len);
    44         if (retval == NULL) {
    45             SDL_OutOfMemory();
    46         } else {
    47             SDL_snprintf(retval, len, "%s/", base);
    48         }
    49     }
    50 
    51     [pool release];
    52     return retval;
    53 }
    54 
    55 char *
    56 SDL_GetPrefPath(const char *org, const char *app)
    57 {
    58     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    59     NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    60     char *retval = NULL;
    61 
    62     (void) org;  // unused on Mac OS X and iOS.
    63 
    64     if ([array count] > 0) {  // we only want the first item in the list.
    65         NSString *str = [array objectAtIndex:0];
    66         const char *base = [str UTF8String];
    67         if (base) {
    68             const size_t len = SDL_strlen(base) + SDL_strlen(app) + 3;
    69             retval = (char *) SDL_malloc(len);
    70             if (retval == NULL) {
    71                 SDL_OutOfMemory();
    72             } else {
    73                 char *ptr;
    74                 SDL_snprintf(retval, len, "%s/%s/", base, app);
    75                 for (ptr = retval+1; *ptr; ptr++) {
    76                     if (*ptr == '/') {
    77                         *ptr = '\0';
    78                         mkdir(retval, 0700);
    79                         *ptr = '/';
    80                     }
    81                 }
    82                 mkdir(retval, 0700);
    83             }
    84         }
    85     }
    86 
    87     [pool release];
    88     return retval;
    89 }
    90 
    91 #endif /* SDL_FILESYSTEM_COCOA */
    92 
    93 /* vi: set ts=4 sw=4 expandtab: */