mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-26 17:24:27 +00:00
Added SDL_OpenFileStorage() for local file storage
This commit is contained in:
@@ -137,6 +137,11 @@ SDL_Storage *SDL_OpenUserStorage(const char *org, const char *app, SDL_Propertie
|
||||
return storage;
|
||||
}
|
||||
|
||||
SDL_Storage *SDL_OpenFileStorage(const char *path)
|
||||
{
|
||||
return GENERIC_OpenFileStorage(path);
|
||||
}
|
||||
|
||||
SDL_Storage *SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata)
|
||||
{
|
||||
SDL_Storage *storage;
|
||||
|
||||
@@ -46,4 +46,6 @@ extern TitleStorageBootStrap GENERIC_titlebootstrap;
|
||||
extern UserStorageBootStrap GENERIC_userbootstrap;
|
||||
extern UserStorageBootStrap STEAM_userbootstrap;
|
||||
|
||||
extern SDL_Storage *GENERIC_OpenFileStorage(const char *path);
|
||||
|
||||
#endif /* SDL_sysstorage_h_ */
|
||||
|
||||
@@ -25,10 +25,16 @@
|
||||
|
||||
static char *GENERIC_INTERNAL_CreateFullPath(const char *base, const char *relative)
|
||||
{
|
||||
size_t fulllen = SDL_strlen(base) + SDL_strlen(relative) + 1;
|
||||
char *result = (char*) SDL_malloc(fulllen);
|
||||
size_t len = 0;
|
||||
|
||||
if (base) {
|
||||
len += SDL_strlen(base);
|
||||
}
|
||||
len += SDL_strlen(relative) + 1;
|
||||
|
||||
char *result = (char*) SDL_malloc(len);
|
||||
if (result != NULL) {
|
||||
SDL_snprintf(result, fulllen, "%s%s", base, relative);
|
||||
SDL_snprintf(result, len, "%s%s", base, relative);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -186,3 +192,40 @@ UserStorageBootStrap GENERIC_userbootstrap = {
|
||||
"SDL generic user storage driver",
|
||||
GENERIC_User_Create
|
||||
};
|
||||
|
||||
static const SDL_StorageInterface GENERIC_file_iface = {
|
||||
GENERIC_StorageClose,
|
||||
GENERIC_StorageReady,
|
||||
GENERIC_StorageFileSize,
|
||||
GENERIC_StorageReadFile,
|
||||
GENERIC_StorageWriteFile,
|
||||
GENERIC_StorageSpaceRemaining
|
||||
};
|
||||
|
||||
SDL_Storage *GENERIC_OpenFileStorage(const char *path)
|
||||
{
|
||||
SDL_Storage *result;
|
||||
size_t len = 0;
|
||||
char *basepath = NULL;
|
||||
|
||||
if (path) {
|
||||
len += SDL_strlen(path);
|
||||
}
|
||||
if (len > 0) {
|
||||
if (path[len-1] == '/') {
|
||||
basepath = SDL_strdup(path);
|
||||
if (!basepath) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
if (SDL_asprintf(&basepath, "%s/", path) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
result = SDL_OpenStorage(&GENERIC_file_iface, basepath);
|
||||
if (result == NULL) {
|
||||
SDL_free(basepath);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user