Skip to content

Commit

Permalink
Added support for the preferences path and writing there
Browse files Browse the repository at this point in the history
  • Loading branch information
tanis2000 committed Sep 8, 2023
1 parent 2cc0cb4 commit ac41825
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/binocle/core/binocle_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,13 @@ bool binocle_sdl_directory_exists(const char *path) {
else
binocle_log_warning( "%s is not a directory\n", path );
return false;
}


char *binocle_sdl_preferences_dir(const char *org, const char *app) {
char *binocle_preferences_dir = SDL_GetPrefPath(org, app);
if (binocle_preferences_dir == NULL) {
return "";
}
return binocle_preferences_dir;
}
9 changes: 9 additions & 0 deletions src/binocle/core/binocle_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ char *binocle_sdl_assets_dir();
*/
bool binocle_sdl_filename_ends_with(const char *str, const char *suffix);

/**
* \brief Returns the path where files can be written.
* This is specific per platform.
* @param org the name of the organization
* @param app the name of the application
* @return the path where files can be written. An empty string if the path can't be found.
*/
char *binocle_sdl_preferences_dir(const char *org, const char *app);

char *binocle_sdl_str_replace(char *orig, char *rep, char *with);
bool binocle_sdl_file_exists(const char *filename);
bool binocle_sdl_directory_exists(const char *path);
Expand Down
19 changes: 19 additions & 0 deletions src/binocle/core/binocle_sdl_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,29 @@ int l_binocle_sdl_load_text_file(lua_State *L) {
// TODO: keep track of the buffer to free it when done
return 1;
}

int l_binocle_sdl_save_text_file(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
const char *buffer = luaL_checkstring(L, 2);
size_t buffer_size = (size_t)luaL_checknumber(L, 3);
bool res = binocle_sdl_write_text_file(filename, buffer, buffer_size);
return 1;
}

int l_binocle_sdl_preferences_dir(lua_State *L) {
const char *org = luaL_checkstring(L, 1);
const char *app = luaL_checkstring(L, 2);
char *preferences_dir = binocle_sdl_preferences_dir(org, app);
lua_pushstring(L, preferences_dir);
return 1;
}

static const struct luaL_Reg sdl [] = {
{"assets_dir", l_binocle_sdl_assets_dir},
{"preferences_dir", l_binocle_sdl_preferences_dir},
{"get_last_modification_time", l_binocle_sdl_get_last_modification_time},
{"load_text_file", l_binocle_sdl_load_text_file},
{"save_text_file", l_binocle_sdl_save_text_file},
{NULL, NULL}
};

Expand Down

0 comments on commit ac41825

Please sign in to comment.