Atlas - SDL_sysfilesystem.cc

Home / ext / SDL / src / filesystem / haiku Lines: 1 | Size: 4373 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 28extern "C" { 29#include "../SDL_sysfilesystem.h" 30} 31 32#include <kernel/image.h> 33#include <storage/Directory.h> 34#include <storage/Entry.h> 35#include <storage/FindDirectory.h> 36#include <storage/Path.h> 37 38 39char *SDL_SYS_GetBasePath(void) 40{ 41 char name[MAXPATHLEN]; 42 43 if (find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, name, sizeof(name)) != B_OK) { 44 return NULL; 45 } 46 47 BEntry entry(name, true); 48 BPath path; 49 status_t rc = entry.GetPath(&path); // (path) now has binary's path. 50 SDL_assert(rc == B_OK); 51 rc = path.GetParent(&path); // chop filename, keep directory. 52 SDL_assert(rc == B_OK); 53 const char *str = path.Path(); 54 SDL_assert(str != NULL); 55 56 const size_t len = SDL_strlen(str); 57 char *result = (char *) SDL_malloc(len + 2); 58 if (result) { 59 SDL_memcpy(result, str, len); 60 result[len] = '/'; 61 result[len+1] = '\0'; 62 } 63 64 return result; 65} 66 67 68char *SDL_SYS_GetPrefPath(const char *org, const char *app) 69{ 70 // !!! FIXME: is there a better way to do this? 71 const char *home = SDL_getenv("HOME"); 72 const char *append = "/config/settings/"; 73 size_t len = SDL_strlen(home); 74 75 if (!len || (home[len - 1] == '/')) { 76 ++append; // home empty or ends with separator, skip the one from append 77 } 78 len += SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3; 79 char *result = (char *) SDL_malloc(len); 80 if (result) { 81 if (*org) { 82 SDL_snprintf(result, len, "%s%s%s/%s/", home, append, org, app); 83 } else { 84 SDL_snprintf(result, len, "%s%s%s/", home, append, app); 85 } 86 create_directory(result, 0700); // Haiku api: creates missing dirs 87 } 88 89 return result; 90} 91 92char *SDL_SYS_GetUserFolder(SDL_Folder folder) 93{ 94 const char *home = NULL; 95 char *result; 96 97 home = SDL_getenv("HOME"); 98 if (!home) { 99 SDL_SetError("No $HOME environment variable available"); 100 return NULL; 101 } 102 103 switch (folder) { 104 case SDL_FOLDER_HOME: 105 result = (char *) SDL_malloc(SDL_strlen(home) + 2); 106 if (!result) { 107 return NULL; 108 } 109 110 if (SDL_snprintf(result, SDL_strlen(home) + 2, "%s/", home) < 0) { 111 SDL_SetError("Couldn't snprintf home path for Haiku: %s", home); 112 SDL_free(result); 113 return NULL; 114 } 115 116 return result; 117 118 // TODO: Is Haiku's desktop folder always ~/Desktop/ ? 119 case SDL_FOLDER_DESKTOP: 120 result = (char *) SDL_malloc(SDL_strlen(home) + 10); 121 if (!result) { 122 return NULL; 123 } 124 125 if (SDL_snprintf(result, SDL_strlen(home) + 10, "%s/Desktop/", home) < 0) { 126 SDL_SetError("Couldn't snprintf desktop path for Haiku: %s/Desktop/", home); 127 SDL_free(result); 128 return NULL; 129 } 130 131 return result; 132 133 case SDL_FOLDER_DOCUMENTS: 134 case SDL_FOLDER_DOWNLOADS: 135 case SDL_FOLDER_MUSIC: 136 case SDL_FOLDER_PICTURES: 137 case SDL_FOLDER_PUBLICSHARE: 138 case SDL_FOLDER_SAVEDGAMES: 139 case SDL_FOLDER_SCREENSHOTS: 140 case SDL_FOLDER_TEMPLATES: 141 case SDL_FOLDER_VIDEOS: 142 default: 143 SDL_SetError("Only HOME and DESKTOP available on Haiku"); 144 return NULL; 145 } 146} 147 148#endif // SDL_FILESYSTEM_HAIKU 149
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.