SDL_sysfilesystem.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_FILESYSTEM_COCOA
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent filesystem routines */
  22. #include <Foundation/Foundation.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include "SDL_error.h"
  26. #include "SDL_stdinc.h"
  27. #include "SDL_filesystem.h"
  28. char *
  29. SDL_GetBasePath(void)
  30. {
  31. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  32. NSBundle *bundle = [NSBundle mainBundle];
  33. const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
  34. const char *base = NULL;
  35. char *retval = NULL;
  36. if (baseType == NULL) {
  37. baseType = "resource";
  38. }
  39. if (SDL_strcasecmp(baseType, "bundle")==0) {
  40. base = [[bundle bundlePath] fileSystemRepresentation];
  41. } else if (SDL_strcasecmp(baseType, "parent")==0) {
  42. base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
  43. } else {
  44. /* this returns the exedir for non-bundled and the resourceDir for bundled apps */
  45. base = [[bundle resourcePath] fileSystemRepresentation];
  46. }
  47. if (base) {
  48. const size_t len = SDL_strlen(base) + 2;
  49. retval = (char *) SDL_malloc(len);
  50. if (retval == NULL) {
  51. SDL_OutOfMemory();
  52. } else {
  53. SDL_snprintf(retval, len, "%s/", base);
  54. }
  55. }
  56. [pool release];
  57. return retval;
  58. }
  59. char *
  60. SDL_GetPrefPath(const char *org, const char *app)
  61. {
  62. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  63. NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  64. char *retval = NULL;
  65. if ([array count] > 0) { /* we only want the first item in the list. */
  66. NSString *str = [array objectAtIndex:0];
  67. const char *base = [str fileSystemRepresentation];
  68. if (base) {
  69. const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
  70. retval = (char *) SDL_malloc(len);
  71. if (retval == NULL) {
  72. SDL_OutOfMemory();
  73. } else {
  74. char *ptr;
  75. SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
  76. for (ptr = retval+1; *ptr; ptr++) {
  77. if (*ptr == '/') {
  78. *ptr = '\0';
  79. mkdir(retval, 0700);
  80. *ptr = '/';
  81. }
  82. }
  83. mkdir(retval, 0700);
  84. }
  85. }
  86. }
  87. [pool release];
  88. return retval;
  89. }
  90. #endif /* SDL_FILESYSTEM_COCOA */
  91. /* vi: set ts=4 sw=4 expandtab: */