From 2081f2df729a5dd5e6af1f24508ad4be8a632676 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Mon, 25 Sep 2023 02:54:40 +0200 Subject: Add default data path --- source/kit/file.c | 36 ++++++++++++++++++++++++++++++++---- source/kit/file.h | 12 ++++++++---- source/tests/file.test.c | 29 ++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 9 deletions(-) (limited to 'source') diff --git a/source/kit/file.c b/source/kit/file.c index ec878c2..07e18f4 100644 --- a/source/kit/file.c +++ b/source/kit/file.c @@ -150,17 +150,17 @@ kit_str_builder_t kit_path_user(kit_allocator_t *alloc) { kit_str_builder_t kit_path_cache(kit_allocator_t *alloc) { kit_str_builder_t cache, user; -#if defined(_WIN32) && !defined(__CYGWIN__) - cache = kit_get_env_("LOCALAPPDATA", alloc); + cache = kit_get_env_("XDG_CACHE_HOME", alloc); if (cache.size != 0) return cache; DA_DESTROY(cache); -#endif - cache = kit_get_env_("XDG_CACHE_HOME", alloc); +#if defined(_WIN32) && !defined(__CYGWIN__) + cache = kit_get_env_("TEMP", alloc); if (cache.size != 0) return cache; DA_DESTROY(cache); +#endif user = kit_path_user(alloc); cache = @@ -175,6 +175,34 @@ kit_str_builder_t kit_path_cache(kit_allocator_t *alloc) { return cache; } +kit_str_builder_t kit_path_data(kit_allocator_t *alloc) { + kit_str_builder_t data, user; + + data = kit_get_env_("XDG_DATA_HOME", alloc); + if (data.size != 0) + return data; + DA_DESTROY(data); + +#if defined(_WIN32) && !defined(__CYGWIN__) + data = kit_get_env_("LOCALAPPDATA", alloc); + if (data.size != 0) + return data; + DA_DESTROY(data); +#endif + + user = kit_path_user(alloc); + data = +#ifdef __APPLE__ + kit_path_join(WRAP_STR(user), SZ("Library"), alloc); +#else + kit_path_join(WRAP_STR(user), SZ(".local" PATH_DELIM "share"), + alloc); +#endif + DA_DESTROY(user); + + return data; +} + kit_str_t kit_path_index(kit_str_t path, i64 index) { str_t s = { .size = 0, .values = NULL }; diff --git a/source/kit/file.h b/source/kit/file.h index b2e4f20..7eddfb8 100644 --- a/source/kit/file.h +++ b/source/kit/file.h @@ -64,6 +64,8 @@ kit_str_builder_t kit_path_user(kit_allocator_t *alloc); kit_str_builder_t kit_path_cache(kit_allocator_t *alloc); +kit_str_builder_t kit_path_data(kit_allocator_t *alloc); + kit_str_t kit_path_index(kit_str_t path, i64 index); kit_str_t kit_path_take(kit_str_t path, i64 count); @@ -97,10 +99,16 @@ kit_status_t kit_file_unmap(kit_mapped_file_t *mf); #endif #ifndef KIT_DISABLE_SHORT_NAMES +# define path_type_t kit_path_type_t +# define file_info_t kit_file_info_t +# define path_list_t kit_path_list_t +# define mapped_file_t kit_mapped_file_t + # define path_norm kit_path_norm # define path_join kit_path_join # define path_user kit_path_user # define path_cache kit_path_cache +# define path_data kit_path_data # define path_index kit_path_index # define path_take kit_path_take # define folder_create kit_folder_create @@ -108,14 +116,10 @@ kit_status_t kit_file_unmap(kit_mapped_file_t *mf); # define file_remove kit_file_remove # define folder_remove kit_folder_remove # define file_remove_recursive kit_file_remove_recursive -# define path_type_t kit_path_type_t # define path_type kit_path_type -# define file_info_t kit_file_info_t # define file_info kit_file_info -# define path_list_t kit_path_list_t # define folder_enum kit_folder_enum # define path_list_destroy kit_path_list_destroy -# define mapped_file_t kit_mapped_file_t # define file_map kit_file_map # define file_sync kit_file_sync # define file_unmap kit_file_unmap diff --git a/source/tests/file.test.c b/source/tests/file.test.c index 9ec51bf..6faef1b 100644 --- a/source/tests/file.test.c +++ b/source/tests/file.test.c @@ -14,7 +14,8 @@ TEST("path cache") { str_builder_t expected = #if defined(_WIN32) && !defined(__CYGWIN__) - path_join(WRAP_STR(user), SZ("AppData" PATH_DELIM "Local"), + path_join(WRAP_STR(user), + SZ("AppData" PATH_DELIM "Local" PATH_DELIM "Temp"), NULL); #elif defined(__APPLE__) path_join(WRAP_STR(user), SZ("Library" PATH_DELIM "Caches"), @@ -30,6 +31,32 @@ TEST("path cache") { DA_DESTROY(expected); } +TEST("path data") { + str_builder_t user = path_user(NULL); + str_builder_t data = path_data(NULL); + + DA_RESIZE(data, data.size + 1); + data.values[data.size - 1] = '\0'; + DA_RESIZE(data, data.size - 1); + + str_builder_t expected = +#if defined(_WIN32) && !defined(__CYGWIN__) + path_join(WRAP_STR(user), SZ("AppData" PATH_DELIM "Local"), + NULL); +#elif defined(__APPLE__) + path_join(WRAP_STR(user), SZ("Library"), NULL); +#else + path_join(WRAP_STR(user), SZ(".local" PATH_DELIM "share"), + NULL); +#endif + + REQUIRE(AR_EQUAL(data, expected)); + + DA_DESTROY(user); + DA_DESTROY(data); + DA_DESTROY(expected); +} + TEST("path normalize one") { str_t foo = SZ("foo/bar/../baz"); str_t foo_norm = SZ("foo" PATH_DELIM "baz"); -- cgit v1.2.3