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_assert.h"
36 #include "SDL_filesystem.h"
37 
38 char *
SDL_GetBasePath(void)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 *
SDL_GetPrefPath(const char * org,const char * app)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 
81     if (!app) {
82         SDL_InvalidParamError("app");
83         return NULL;
84     }
85     if (!org) {
86         org = "";
87     }
88 
89     if (!len || (home[len - 1] == '/')) {
90         ++append; // home empty or ends with separator, skip the one from append
91     }
92     len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
93     char *retval = (char *) SDL_malloc(len);
94     if (!retval) {
95         SDL_OutOfMemory();
96     } else {
97         if (*org) {
98             SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
99         } else {
100             SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
101         }
102         create_directory(retval, 0700);  // Haiku api: creates missing dirs
103     }
104 
105     return retval;
106 }
107 
108 #endif /* SDL_FILESYSTEM_HAIKU */
109 
110 /* vi: set ts=4 sw=4 expandtab: */
111