sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_sysfilesystem.cc (3166B)


      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_HAIKU
     24 
     25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     26 /* System dependent filesystem routines                                */
     27 
     28 #include <kernel/image.h>
     29 #include <storage/Directory.h>
     30 #include <storage/Entry.h>
     31 #include <storage/Path.h>
     32 
     33 #include "SDL_error.h"
     34 #include "SDL_stdinc.h"
     35 #include "SDL_filesystem.h"
     36 
     37 char *
     38 SDL_GetBasePath(void)
     39 {
     40     image_info info;
     41     int32 cookie = 0;
     42 
     43     while (get_next_image_info(0, &cookie, &info) == B_OK) {
     44         if (info.type == B_APP_IMAGE) {
     45             break;
     46         }
     47     }
     48 
     49     BEntry entry(info.name, true);
     50     BPath path;
     51     status_t rc = entry.GetPath(&path);  /* (path) now has binary's path. */
     52     SDL_assert(rc == B_OK);
     53     rc = path.GetParent(&path); /* chop filename, keep directory. */
     54     SDL_assert(rc == B_OK);
     55     const char *str = path.Path();
     56     SDL_assert(str != NULL);
     57 
     58     const size_t len = SDL_strlen(str);
     59     char *retval = (char *) SDL_malloc(len + 2);
     60     if (!retval) {
     61         SDL_OutOfMemory();
     62         return NULL;
     63     }
     64 
     65     SDL_memcpy(retval, str, len);
     66     retval[len] = '/';
     67     retval[len+1] = '\0';
     68     return retval;
     69 }
     70 
     71 
     72 char *
     73 SDL_GetPrefPath(const char *org, const char *app)
     74 {
     75     // !!! FIXME: is there a better way to do this?
     76     const char *home = SDL_getenv("HOME");
     77     const char *append = "/config/settings/";
     78     size_t len = SDL_strlen(home);
     79 
     80     if (!app) {
     81         SDL_InvalidParamError("app");
     82         return NULL;
     83     }
     84     if (!org) {
     85         org = "";
     86     }
     87 
     88     if (!len || (home[len - 1] == '/')) {
     89         ++append; // home empty or ends with separator, skip the one from append
     90     }
     91     len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
     92     char *retval = (char *) SDL_malloc(len);
     93     if (!retval) {
     94         SDL_OutOfMemory();
     95     } else {
     96         if (*org) {
     97             SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
     98         } else {
     99             SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
    100         }
    101         create_directory(retval, 0700);  // Haiku api: creates missing dirs
    102     }
    103 
    104     return retval;
    105 }
    106 
    107 #endif /* SDL_FILESYSTEM_HAIKU */
    108 
    109 /* vi: set ts=4 sw=4 expandtab: */