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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7667
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
  Simple DirectMedia Layer
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
  arising from the use of this software.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
     appreciated but is not required.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
     misrepresented as being the original software.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
*/
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
#include "SDL_config.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#ifdef SDL_FILESYSTEM_BEOS
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
/* System dependent filesystem routines                                */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#include <os/kernel/image.h>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#include <os/storage/Directory.h>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include <os/storage/Path.h>
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#include "SDL_error.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include "SDL_stdinc.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#include "SDL_assert.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include "SDL_filesystem.h"
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
char *
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
SDL_GetBasePath(void)
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
{
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    image_info info;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    int32 cookie = 0;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    while (get_next_image_info(0, &cookie, &info) == B_OK) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
        if (info.type == B_APP_IMAGE) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
            break;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    BEntry entry(info.name, true);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    BPath path;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    status_t rc = entry.GetPath(&path);  /* (path) now has binary's path. */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    SDL_assert(rc == B_OK);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    rc = path.GetParent(&path); /* chop filename, keep directory. */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    SDL_assert(rc == B_OK);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    const char *str = path.Path();
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    SDL_assert(str != NULL);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    const size_t len = SDL_strlen(str);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    char *retval = (char *) SDL_malloc(len + 2);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    if (!retval) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        SDL_OutOfMemory();
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
        return NULL;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    SDL_strcpy(retval, str);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    retval[len] = '/';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    retval[len+1] = '\0';
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    return retval;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
}
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
char *
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
SDL_GetPrefPath(const char *org, const char *app)
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
{
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    // !!! FIXME: is there a better way to do this?
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    const char *home = SDL_getenv("HOME");
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    const char *append = "config/settings/";
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    const size_t len = SDL_strlen(home) + SDL_strlen(append) + SDL_strlen(app) + 2;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    char *retval = (char *) SDL_malloc(len);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    if (!retval) {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        SDL_OutOfMemory();
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    } else {
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
        SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
        create_directory(retval, 0700);  // BeOS api: creates missing dirs
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    }
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    return retval;
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
}
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
#endif /* SDL_FILESYSTEM_BEOS */
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
be1cc6f55840 Added SDL_GetBasePath() and SDL_GetPrefPath() in new filesystem module.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
/* vi: set ts=4 sw=4 expandtab: */