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