SDL  2.0
SDL_sysfilesystem.m
Go to the documentation of this file.
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 #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: */
NULL
#define NULL
Definition: begin_code.h:167
SDL_LogCritical
#define SDL_LogCritical
Definition: SDL_dynapi_overrides.h:37
base
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst base
Definition: pixman-arm-simd-asm.h:108
SDL_error.h
SDL_log.h
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_filesystem.h
Include file for filesystem SDL API functions.
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:419
SDL_GetBasePath
char * SDL_GetBasePath(void)
Get the path where the application resides.
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2929
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
array
GLenum array
Definition: SDL_opengl_glext.h:6303
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_GetPrefPath
#define SDL_GetPrefPath
Definition: SDL_dynapi_overrides.h:134
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_stdinc.h
SDL_LOG_CATEGORY_SYSTEM
@ SDL_LOG_CATEGORY_SYSTEM
Definition: SDL_log.h:69
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
ptr
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF ptr
Definition: pixman-arm-simd-asm.h:171
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161