1/*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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_internal.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
36char *
37SDL_GetBasePath(void)
38{ @autoreleasepool
39{
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
45    if (baseType == NULL) {
46        baseType = "resource";
47    }
48    if (SDL_strcasecmp(baseType, "bundle")==0) {
49        base = [[bundle bundlePath] fileSystemRepresentation];
50    } else if (SDL_strcasecmp(baseType, "parent")==0) {
51        base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
52    } else {
53        /* this returns the exedir for non-bundled  and the resourceDir for bundled apps */
54        base = [[bundle resourcePath] fileSystemRepresentation];
55    }
56
57    if (base) {
58        const size_t len = SDL_strlen(base) + 2;
59        retval = (char *) SDL_malloc(len);
60        if (retval == NULL) {
61            SDL_OutOfMemory();
62        } else {
63            SDL_snprintf(retval, len, "%s/", base);
64        }
65    }
66
67    return retval;
68}}
69
70char *
71SDL_GetPrefPath(const char *org, const char *app)
72{ @autoreleasepool
73{
74    if (!app) {
75        SDL_InvalidParamError("app");
76        return NULL;
77    }
78    if (!org) {
79        org = "";
80    }
81
82    char *retval = NULL;
83#if !TARGET_OS_TV
84    NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
85#else
86    /* tvOS does not have persistent local storage!
87     * The only place on-device where we can store data is
88     * a cache directory that the OS can empty at any time.
89     *
90     * It's therefore very likely that save data will be erased
91     * between sessions. If you want your app's save data to
92     * actually stick around, you'll need to use iCloud storage.
93     */
94
95    static SDL_bool shown = SDL_FALSE;
96    if (!shown)
97    {
98        shown = SDL_TRUE;
99        SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "tvOS does not have persistent local storage! Use iCloud storage if you want your data to persist between sessions.\n");
100    }
101
102    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
103#endif /* !TARGET_OS_TV */
104
105    if ([array count] > 0) {  /* we only want the first item in the list. */
106        NSString *str = [array objectAtIndex:0];
107        const char *base = [str fileSystemRepresentation];
108        if (base) {
109            const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
110            retval = (char *) SDL_malloc(len);
111            if (retval == NULL) {
112                SDL_OutOfMemory();
113            } else {
114                char *ptr;
115                if (*org) {
116                    SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
117                } else {
118                    SDL_snprintf(retval, len, "%s/%s/", base, app);
119                }
120                for (ptr = retval+1; *ptr; ptr++) {
121                    if (*ptr == '/') {
122                        *ptr = '\0';
123                        mkdir(retval, 0700);
124                        *ptr = '/';
125                    }
126                }
127                mkdir(retval, 0700);
128            }
129        }
130    }
131
132    return retval;
133}}
134
135#endif /* SDL_FILESYSTEM_COCOA */
136
137/* vi: set ts=4 sw=4 expandtab: */
138