src/filesystem/beos/SDL_sysfilesystem.cc
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Aug 2013 19:57:11 -0400
changeset 7667 be1cc6f55840
child 7673 e3d59fb413ad
permissions -rw-r--r--
Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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_config.h"
    22 
    23 #ifdef SDL_FILESYSTEM_BEOS
    24 
    25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    26 /* System dependent filesystem routines                                */
    27 
    28 #include <os/kernel/image.h>
    29 #include <os/storage/Directory.h>
    30 #include <os/storage/Path.h>
    31 
    32 #include "SDL_error.h"
    33 #include "SDL_stdinc.h"
    34 #include "SDL_assert.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_strcpy(retval, str);
    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     const size_t len = SDL_strlen(home) + SDL_strlen(append) + SDL_strlen(app) + 2;
    79     char *retval = (char *) SDL_malloc(len);
    80     if (!retval) {
    81         SDL_OutOfMemory();
    82     } else {
    83         SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
    84         create_directory(retval, 0700);  // BeOS api: creates missing dirs
    85     }
    86 
    87     return retval;
    88 }
    89 
    90 #endif /* SDL_FILESYSTEM_BEOS */
    91 
    92 /* vi: set ts=4 sw=4 expandtab: */