add fs_get_user_storage_path

This commit is contained in:
xianjimli 2020-04-01 17:30:42 +08:00
parent 5abaf2dbd6
commit cdd3100f5a
5 changed files with 73 additions and 1 deletions

View File

@ -805,6 +805,7 @@ static ret_t wm_on_request_quit(void* ctx, event_t* evt) {
}
ret_t application_init() {
char path[MAX_PATH + 1];
widget_t* wm = window_manager();
/*enable screen saver*/
@ -818,6 +819,9 @@ ret_t application_init() {
widget_on(wm, EVT_OUT_OF_MEMORY, wm_on_out_of_memory, wm);
widget_on(wm, EVT_REQUEST_QUIT_APP, wm_on_request_quit, wm);
fs_get_user_storage_path(os_fs(), path);
log_debug("user storage path:%s\n", path);
return show_preload_res_window();
}

View File

@ -2,6 +2,7 @@
* 2020/04/01
* 修正 overlay 的注释(感谢大恒提供补丁)。
* 完善 fs 的创建和删除目录的处理(感谢大恒提供补丁)。
* 增加函数 fs\_get\_user\_storage\_path 用于统一 PC 和 android 平台保存数据的目录。
* 2020/03/31
* 支持一次关闭多个模态窗口(感谢尧燊提供补丁)。

View File

@ -1,13 +1,20 @@
#include "tkc/types_def.h"
#ifdef ANDROID
#include "SDL.h"
#endif /*ANDROID*/
#if defined(__APPLE__) || defined(LINUX)
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <pwd.h>
#elif defined(WIN32)
#include <stdio.h>
#include <windows.h>
#include <io.h>
#include <direct.h>
#include <Shlobj.h>
#define unlink _unlink
#define rename MoveFileA
#define ftruncate _chsize
@ -162,7 +169,7 @@ fs_file_t* fs_os_open_file(fs_t* fs, const char* name, const char* mode) {
ret_t fs_os_remove_file(fs_t* fs, const char* name) {
(void)fs;
return_value_if_fail(name != NULL, FALSE);
return_value_if_fail(name != NULL, RET_FAIL);
#ifdef WIN32
int16_t len = 0;
@ -402,6 +409,44 @@ ret_t fs_os_get_exe(fs_t* fs, char path[MAX_PATH + 1]) {
return RET_OK;
}
ret_t fs_os_get_user_storage_path(fs_t* fs, char path[MAX_PATH + 1]) {
#if defined(ANDROID)
const char* homedir = SDL_AndroidGetInternalStoragePath();
memset(path, 0x00, MAX_PATH + 1);
return_value_if_fail(homedir != NULL, RET_FAIL);
tk_strncpy(path, homedir, MAX_PATH);
return RET_OK;
#elif defined(LINUX) || defined(__APPLE__) || defined(IOS)
const char* homedir = NULL;
memset(path, 0x00, MAX_PATH + 1);
if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
return_value_if_fail(homedir != NULL, RET_FAIL);
tk_strncpy(path, homedir, MAX_PATH);
return RET_OK;
#elif defined(WIN32)
WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
str_t str;
str_init(&str, MAX_PATH);
str_from_wstr(&str, path);
tk_strncpy(path, str.str, MAX_PATH);
str_reset(&str);
return RET_OK;
}
#endif
return RET_FAIL;
}
static ret_t fs_os_get_cwd(fs_t* fs, char path[MAX_PATH + 1]) {
return_value_if_fail(fs != NULL && path != NULL, RET_BAD_PARAMS);
@ -478,6 +523,7 @@ static const fs_t s_os_fs = {.open_file = fs_os_open_file,
.get_disk_info = fs_os_get_disk_info,
.get_cwd = fs_os_get_cwd,
.get_exe = fs_os_get_exe,
.get_user_storage_path = fs_os_get_user_storage_path,
.stat = fs_os_stat};
fs_t* os_fs(void) {

7
src/tkc/fs.c Executable file → Normal file
View File

@ -161,6 +161,13 @@ ret_t fs_get_exe(fs_t* fs, char path[MAX_PATH + 1]) {
return fs->get_exe(fs, path);
}
ret_t fs_get_user_storage_path(fs_t* fs, char path[MAX_PATH + 1]) {
return_value_if_fail(fs != NULL && fs->get_user_storage_path != NULL && path != NULL,
RET_BAD_PARAMS);
return fs->get_user_storage_path(fs, path);
}
ret_t fs_get_cwd(fs_t* fs, char path[MAX_PATH + 1]) {
return_value_if_fail(fs != NULL && fs->get_cwd != NULL && path != NULL, RET_BAD_PARAMS);

View File

@ -281,6 +281,7 @@ typedef ret_t (*fs_get_disk_info_t)(fs_t* fs, const char* volume, int32_t* free_
typedef ret_t (*fs_get_exe_t)(fs_t* fs, char path[MAX_PATH + 1]);
typedef ret_t (*fs_get_cwd_t)(fs_t* fs, char path[MAX_PATH + 1]);
typedef ret_t (*fs_stat_t)(fs_t* fs, const char* name, fs_stat_info_t* fst);
typedef ret_t (*fs_get_user_storage_path_t)(fs_t* fs, char path[MAX_PATH + 1]);
/**
* @class fs_t
@ -304,6 +305,7 @@ struct _fs_t {
fs_get_disk_info_t get_disk_info;
fs_get_cwd_t get_cwd;
fs_get_exe_t get_exe;
fs_get_user_storage_path_t get_user_storage_path;
fs_stat_t stat;
};
@ -469,6 +471,18 @@ ret_t fs_stat(fs_t* fs, const char* name, fs_stat_info_t* fst);
*/
ret_t fs_get_exe(fs_t* fs, char path[MAX_PATH + 1]);
/**
* @method fs_get_user_storage_path
*
* home目录或者应用程序可以写入数据的目录
*
* @param {fs_t*} fs os_fs()
* @param {char*} path
*
* @return {ret_t} RET_OK表示成功
*/
ret_t fs_get_user_storage_path(fs_t* fs, char path[MAX_PATH + 1]);
/**
* @method fs_get_cwd
*