diff --git a/src/binocle/core/binocle_sdl.c b/src/binocle/core/binocle_sdl.c index 0839aa92..b9635168 100644 --- a/src/binocle/core/binocle_sdl.c +++ b/src/binocle/core/binocle_sdl.c @@ -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; } \ No newline at end of file diff --git a/src/binocle/core/binocle_sdl.h b/src/binocle/core/binocle_sdl.h index 36361d68..bbe86af3 100644 --- a/src/binocle/core/binocle_sdl.h +++ b/src/binocle/core/binocle_sdl.h @@ -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); diff --git a/src/binocle/core/binocle_sdl_wrap.c b/src/binocle/core/binocle_sdl_wrap.c index a4782925..5abe31d8 100644 --- a/src/binocle/core/binocle_sdl_wrap.c +++ b/src/binocle/core/binocle_sdl_wrap.c @@ -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} };