2013-08-20 19:57:11 -04:00
/ *
Simple DirectMedia Layer
2019-01-04 22:01:14 -08:00
Copyright ( C ) 1997 -2019 Sam Lantinga < slouken @ libsdl . org >
2013-08-20 19:57:11 -04:00
This software is provided ' as - is ' , without any express or implied
warranty . In no event will the authors be held liable for any damages
arising from the use of this software .
Permission is granted to anyone to use this software for any purpose ,
including commercial applications , and to alter it and redistribute it
freely , subject to the following restrictions :
1. The origin of this software must not be misrepresented ; you must not
claim that you wrote the original software . If you use this software
in a product , an acknowledgment in the product documentation would be
appreciated but is not required .
2. Altered source versions must be plainly marked as such , and must not be
misrepresented as being the original software .
3. This notice may not be removed or altered from any source distribution .
* /
2013-11-24 23:56:17 -05:00
# include "../../SDL_internal.h"
2013-08-20 19:57:11 -04:00
# ifdef SDL_FILESYSTEM _COCOA
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * System dependent filesystem routines * /
2013-08-20 21:21:57 -04:00
# include < Foundation / Foundation . h >
2013-08-20 19:57:11 -04:00
# include < sys / stat . h >
2013-08-20 20:15:15 -04:00
# include < sys / types . h >
2013-08-20 19:57:11 -04:00
# include "SDL_error.h"
# include "SDL_stdinc.h"
# include "SDL_filesystem.h"
2019-07-18 19:33:17 -07:00
# include "SDL_log.h"
2013-08-20 19:57:11 -04:00
char *
SDL_GetBasePath ( void )
2014-08-17 15:07:00 -07:00
{ @ autoreleasepool
2013-08-20 19:57:11 -04:00
{
2013-08-25 11:24:01 -04:00
NSBundle * bundle = [ NSBundle mainBundle ] ;
const char * baseType = [ [ [ bundle infoDictionary ] objectForKey : @ "SDL_FILESYSTEM_BASE_DIR_TYPE" ] UTF8String ] ;
const char * base = NULL ;
2013-08-20 19:57:11 -04:00
char * retval = NULL ;
2014-07-14 16:50:25 -03:00
2013-08-25 11:24:01 -04:00
if ( baseType = = NULL ) {
baseType = "resource" ;
}
if ( SDL_strcasecmp ( baseType , "bundle" ) = = 0 ) {
2013-10-21 00:20:27 -07:00
base = [ [ bundle bundlePath ] fileSystemRepresentation ] ;
2013-08-25 11:24:01 -04:00
} else if ( SDL_strcasecmp ( baseType , "parent" ) = = 0 ) {
2013-10-21 00:20:27 -07:00
base = [ [ [ bundle bundlePath ] stringByDeletingLastPathComponent ] fileSystemRepresentation ] ;
2013-08-25 11:24:01 -04:00
} else {
/ * this returns the exedir for non - bundled and the resourceDir for bundled apps * /
2013-10-21 00:20:27 -07:00
base = [ [ bundle resourcePath ] fileSystemRepresentation ] ;
2013-08-25 11:24:01 -04:00
}
2014-07-14 16:50:25 -03:00
2013-08-20 19:57:11 -04:00
if ( base ) {
const size_t len = SDL_strlen ( base ) + 2 ;
retval = ( char * ) SDL_malloc ( len ) ;
if ( retval = = NULL ) {
SDL_OutOfMemory ( ) ;
} else {
SDL_snprintf ( retval , len , "%s/" , base ) ;
}
}
return retval ;
2014-08-17 15:07:00 -07:00
} }
2013-08-20 19:57:11 -04:00
char *
SDL_GetPrefPath ( const char * org , const char * app )
2014-08-17 15:07:00 -07:00
{ @ autoreleasepool
2013-08-20 19:57:11 -04:00
{
2017-08-11 11:32:00 -07:00
if ( ! app ) {
SDL_InvalidParamError ( "app" ) ;
return NULL ;
}
if ( ! org ) {
org = "" ;
}
2013-08-20 19:57:11 -04:00
2017-08-11 11:32:00 -07:00
char * retval = NULL ;
2019-07-18 19:33:17 -07:00
# if ! TARGET_OS _TV
2014-08-20 17:20:22 -03:00
NSArray * array = NSSearchPathForDirectoriesInDomains ( NSApplicationSupportDirectory , NSUserDomainMask , YES ) ;
2019-07-18 19:33:17 -07:00
# else
/ * tvOS does not have persistent local storage !
* The only place on - device where we can store data is
* a cache directory that the OS can empty at any time .
*
* It ' s therefore very likely that save data will be erased
* between sessions . If you want your app ' s save data to
* actually stick around , you ' ll need to use iCloud storage .
* /
static SDL_bool shown = SDL_FALSE ;
if ( ! shown )
{
shown = SDL_TRUE ;
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" ) ;
}
NSArray * array = NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) ;
# endif / * ! TARGET_OS _TV * /
2014-08-20 17:20:22 -03:00
2013-09-30 22:35:32 -07:00
if ( [ array count ] > 0 ) { / * we only want the first item in the list . * /
2013-08-20 19:57:11 -04:00
NSString * str = [ array objectAtIndex : 0 ] ;
2013-10-21 00:20:27 -07:00
const char * base = [ str fileSystemRepresentation ] ;
2013-08-20 19:57:11 -04:00
if ( base ) {
2013-10-24 00:00:10 -04:00
const size_t len = SDL_strlen ( base ) + SDL_strlen ( org ) + SDL_strlen ( app ) + 4 ;
2013-08-20 19:57:11 -04:00
retval = ( char * ) SDL_malloc ( len ) ;
if ( retval = = NULL ) {
SDL_OutOfMemory ( ) ;
} else {
char * ptr ;
2017-08-11 11:32:00 -07:00
if ( * org ) {
SDL_snprintf ( retval , len , "%s/%s/%s/" , base , org , app ) ;
} else {
SDL_snprintf ( retval , len , "%s/%s/" , base , app ) ;
}
2013-08-20 19:57:11 -04:00
for ( ptr = retval + 1 ; * ptr ; ptr + + ) {
if ( * ptr = = ' / ' ) {
* ptr = ' \ 0 ' ;
mkdir ( retval , 0700 ) ;
* ptr = ' / ' ;
}
}
mkdir ( retval , 0700 ) ;
}
}
}
return retval ;
2014-08-17 15:07:00 -07:00
} }
2013-08-20 19:57:11 -04:00
# endif / * SDL_FILESYSTEM _COCOA * /
/ * vi : set ts = 4 sw = 4 expandtab : * /