src/filesystem/haiku/SDL_sysfilesystem.cc
author Philipp Wiesemann
Sat, 17 Jun 2017 22:30:09 +0200
changeset 11106 3aadcf084374
parent 10737 3406a0f8b041
child 11164 3862687cef22
permissions -rw-r--r--
haiku: Fixed missing slash in path from SDL_GetPrefPath().
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 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 <os/kernel/image.h>
    29 #include <os/storage/Directory.h>
    30 #include <os/storage/Entry.h>
    31 #include <os/storage/Path.h>
    32 
    33 #include "SDL_error.h"
    34 #include "SDL_stdinc.h"
    35 #include "SDL_assert.h"
    36 #include "SDL_filesystem.h"
    37 
    38 char *
    39 SDL_GetBasePath(void)
    40 {
    41     image_info info;
    42     int32 cookie = 0;
    43 
    44     while (get_next_image_info(0, &cookie, &info) == B_OK) {
    45         if (info.type == B_APP_IMAGE) {
    46             break;
    47         }
    48     }
    49 
    50     BEntry entry(info.name, true);
    51     BPath path;
    52     status_t rc = entry.GetPath(&path);  /* (path) now has binary's path. */
    53     SDL_assert(rc == B_OK);
    54     rc = path.GetParent(&path); /* chop filename, keep directory. */
    55     SDL_assert(rc == B_OK);
    56     const char *str = path.Path();
    57     SDL_assert(str != NULL);
    58 
    59     const size_t len = SDL_strlen(str);
    60     char *retval = (char *) SDL_malloc(len + 2);
    61     if (!retval) {
    62         SDL_OutOfMemory();
    63         return NULL;
    64     }
    65 
    66     SDL_memcpy(retval, str, len);
    67     retval[len] = '/';
    68     retval[len+1] = '\0';
    69     return retval;
    70 }
    71 
    72 
    73 char *
    74 SDL_GetPrefPath(const char *org, const char *app)
    75 {
    76     // !!! FIXME: is there a better way to do this?
    77     const char *home = SDL_getenv("HOME");
    78     const char *append = "/config/settings/";
    79     size_t len = SDL_strlen(home);
    80     if (!len || (home[len - 1] == '/')) {
    81         ++append; // home empty or ends with separator, skip the one from append
    82     }
    83     len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
    84     char *retval = (char *) SDL_malloc(len);
    85     if (!retval) {
    86         SDL_OutOfMemory();
    87     } else {
    88         SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
    89         create_directory(retval, 0700);  // Haiku api: creates missing dirs
    90     }
    91 
    92     return retval;
    93 }
    94 
    95 #endif /* SDL_FILESYSTEM_HAIKU */
    96 
    97 /* vi: set ts=4 sw=4 expandtab: */