1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 01:27:28 +00:00

refactor: remove tr_strvPath() (#3509)

This commit is contained in:
Charles Kerr 2022-07-22 13:07:23 -05:00 committed by GitHub
parent 0da1cbb6ec
commit 9bf2918ad0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 58 deletions

View file

@ -121,7 +121,7 @@ static std::string xdgConfigHome()
return ret; return ret;
} }
return tr_strvPath(getHomeDir(), ".config"sv); return fmt::format("{:s}/.config"sv, getHomeDir());
} }
void tr_setConfigDir(tr_session* session, std::string_view config_dir) void tr_setConfigDir(tr_session* session, std::string_view config_dir)
@ -135,8 +135,8 @@ void tr_setConfigDir(tr_session* session, std::string_view config_dir)
#endif #endif
session->config_dir = config_dir; session->config_dir = config_dir;
session->resume_dir = tr_strvPath(config_dir, ResumeSubdir); session->resume_dir = fmt::format("{:s}/{:s}"sv, config_dir, ResumeSubdir);
session->torrent_dir = tr_strvPath(config_dir, TorrentSubdir); session->torrent_dir = fmt::format("{:s}/{:s}"sv, config_dir, TorrentSubdir);
tr_sys_dir_create(session->resume_dir, TR_SYS_DIR_CREATE_PARENTS, 0777); tr_sys_dir_create(session->resume_dir, TR_SYS_DIR_CREATE_PARENTS, 0777);
tr_sys_dir_create(session->torrent_dir, TR_SYS_DIR_CREATE_PARENTS, 0777); tr_sys_dir_create(session->torrent_dir, TR_SYS_DIR_CREATE_PARENTS, 0777);
} }
@ -168,23 +168,23 @@ char const* tr_getDefaultConfigDir(char const* appname)
{ {
#ifdef __APPLE__ #ifdef __APPLE__
s = tr_strvDup(tr_strvPath(getHomeDir(), "Library", "Application Support", appname)); s = tr_strvDup(fmt::format("{:s}/Library/Application Support/{:s}"sv, getHomeDir(), appname));
#elif defined(_WIN32) #elif defined(_WIN32)
char* appdata = win32_get_known_folder(FOLDERID_LocalAppData); char* appdata = win32_get_known_folder(FOLDERID_LocalAppData);
s = tr_strvDup(tr_strvPath(appdata, appname)); s = tr_strvDup(fmt::format("{:s}/{:s}"sv, appdata, appname));
tr_free(appdata); tr_free(appdata);
#elif defined(__HAIKU__) #elif defined(__HAIKU__)
char buf[PATH_MAX]; char buf[PATH_MAX];
find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof(buf)); find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof(buf));
s = tr_strvDup(tr_strvPath(buf, appname)); s = tr_strvDup(fmt::format("{:s}/{:s}"sv, buf, appname);
#else #else
s = tr_strvDup(tr_strvPath(xdgConfigHome(), appname)); s = tr_strvDup(fmt::format("{:s}/{:s}"sv, xdgConfigHome(), appname));
#endif #endif
} }
@ -196,7 +196,7 @@ char const* tr_getDefaultConfigDir(char const* appname)
static std::string getXdgEntryFromUserDirs(std::string_view key) static std::string getXdgEntryFromUserDirs(std::string_view key)
{ {
auto content = std::vector<char>{}; auto content = std::vector<char>{};
auto const filename = tr_strvPath(xdgConfigHome(), "user-dirs.dirs"sv); auto const filename = fmt::format("{:s}/{:s}"sv, xdgConfigHome(), "user-dirs.dirs"sv);
if (!tr_sys_path_exists(filename) || !tr_loadFile(filename, content) || std::empty(content)) if (!tr_sys_path_exists(filename) || !tr_loadFile(filename, content) || std::empty(content))
{ {
return {}; return {};
@ -248,9 +248,9 @@ char const* tr_getDefaultDownloadDir()
if (user_dir == nullptr) if (user_dir == nullptr)
{ {
#ifdef __HAIKU__ #ifdef __HAIKU__
user_dir = tr_strvDup(tr_strvPath(getHomeDir(), "Desktop")); user_dir = tr_strvDup(fmt::format("{:s}/Desktop"sv, getHomeDir()));
#else #else
user_dir = tr_strvDup(tr_strvPath(getHomeDir(), "Downloads")); user_dir = tr_strvDup(fmt::format("{:s}/Downloads"sv, getHomeDir()));
#endif #endif
} }
} }
@ -371,7 +371,7 @@ char const* tr_getWebClientDir([[maybe_unused]] tr_session const* session)
} }
else else
{ {
candidates.emplace_back(tr_strvPath(getHomeDir(), ".local"sv, "share"sv)); candidates.emplace_back(fmt::format("{:s}/.local/share"sv, getHomeDir()));
} }
tr_free(tmp); tr_free(tmp);
@ -419,7 +419,7 @@ std::string tr_getSessionIdDir()
#else #else
char* program_data_dir = win32_get_known_folder_ex(FOLDERID_ProgramData, KF_FLAG_CREATE); char* program_data_dir = win32_get_known_folder_ex(FOLDERID_ProgramData, KF_FLAG_CREATE);
auto const result = tr_strvPath(program_data_dir, "Transmission"); auto result = fmt::format("{:s}/Transmission"sv, program_data_dir);
tr_free(program_data_dir); tr_free(program_data_dir);
tr_sys_dir_create(result, 0, 0); tr_sys_dir_create(result, 0, 0);
return result; return result;

View file

@ -2665,11 +2665,11 @@ static void renameTorrentFileString(tr_torrent* tor, char const* oldpath, char c
{ {
if (oldpath_len >= std::size(subpath)) if (oldpath_len >= std::size(subpath))
{ {
name = tr_strvPath(newname); name = newname;
} }
else else
{ {
name = tr_strvPath(newname, subpath.substr(oldpath_len + 1)); name = fmt::format(FMT_STRING("{:s}/{:s}"sv), newname, subpath.substr(oldpath_len + 1));
} }
} }
else else
@ -2683,11 +2683,11 @@ static void renameTorrentFileString(tr_torrent* tor, char const* oldpath, char c
if (oldpath_len >= std::size(subpath)) if (oldpath_len >= std::size(subpath))
{ {
name = tr_strvPath(tmp, newname); name = fmt::format(FMT_STRING("{:s}/{:s}"sv), tmp, newname);
} }
else else
{ {
name = tr_strvPath(tmp, newname, subpath.substr(oldpath_len + 1)); name = fmt::format(FMT_STRING("{:s}/{:s}/{:s}"sv), tmp, newname, subpath.substr(oldpath_len + 1));
} }
} }

View file

@ -232,27 +232,6 @@ template<typename T>
**** std::string_view utils **** std::string_view utils
***/ ***/
template<typename... T, typename std::enable_if_t<(std::is_convertible_v<T, std::string_view> && ...), bool> = true>
[[nodiscard]] std::string tr_strvPath(T... args)
{
auto setme = std::string{};
auto const n_args = sizeof...(args);
auto const n = n_args + (std::size(std::string_view{ args }) + ...);
if (setme.capacity() < n)
{
setme.reserve(n);
}
auto const foo = [&setme](std::string_view a)
{
setme += a;
setme += TR_PATH_DELIMITER;
};
(foo(args), ...);
setme.resize(setme.size() - 1);
return setme;
}
template<typename T> template<typename T>
[[nodiscard]] constexpr bool tr_strvContains(std::string_view sv, T key) noexcept // c++23 [[nodiscard]] constexpr bool tr_strvContains(std::string_view sv, T key) noexcept // c++23
{ {

View file

@ -27,6 +27,8 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
using namespace std::literals;
inline std::ostream& operator<<(std::ostream& os, tr_error const& err) inline std::ostream& operator<<(std::ostream& os, tr_error const& err)
{ {
os << err.message << ' ' << err.code; os << err.message << ' ' << err.code;
@ -53,7 +55,8 @@ static void depthFirstWalk(char const* path, file_func_t func)
{ {
if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0) if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
{ {
depthFirstWalk(tr_strvPath(path, name).c_str(), func); auto const child = fmt::format("{:s}/{:s}"sv, path, name);
depthFirstWalk(child.c_str(), func);
} }
} }
@ -135,7 +138,7 @@ protected:
static std::string create_sandbox(std::string const& parent_dir, std::string const& tmpl) static std::string create_sandbox(std::string const& parent_dir, std::string const& tmpl)
{ {
auto path = tr_strvPath(parent_dir, tmpl); auto path = fmt::format(FMT_STRING("{:s}/{:s}"sv), parent_dir, tmpl);
tr_sys_dir_create_temp(std::data(path)); tr_sys_dir_create_temp(std::data(path));
tr_sys_path_native_separators(std::data(path)); tr_sys_path_native_separators(std::data(path));
return path; return path;
@ -310,18 +313,18 @@ private:
ensureFormattersInited(); ensureFormattersInited();
// download dir // download dir
auto sv = std::string_view{}; auto sv = "Downloads"sv;
auto q = TR_KEY_download_dir; auto q = TR_KEY_download_dir;
auto const download_dir = tr_variantDictFindStrView(settings, q, &sv) ? tr_strvPath(sandboxDir(), sv) : (void)tr_variantDictFindStrView(settings, q, &sv);
tr_strvPath(sandboxDir(), "Downloads"); auto const download_dir = tr_pathbuf{ sandboxDir(), '/', sv };
tr_sys_dir_create(download_dir, TR_SYS_DIR_CREATE_PARENTS, 0700); tr_sys_dir_create(download_dir, TR_SYS_DIR_CREATE_PARENTS, 0700);
tr_variantDictAddStr(settings, q, download_dir.data()); tr_variantDictAddStr(settings, q, download_dir);
// incomplete dir // incomplete dir
sv = "Incomplete"sv;
q = TR_KEY_incomplete_dir; q = TR_KEY_incomplete_dir;
auto const incomplete_dir = tr_variantDictFindStrView(settings, q, &sv) ? tr_strvPath(sandboxDir(), sv) : (void)tr_variantDictFindStrView(settings, q, &sv);
tr_strvPath(sandboxDir(), "Incomplete"); tr_variantDictAddStr(settings, q, tr_pathbuf{ sandboxDir(), '/', sv });
tr_variantDictAddStr(settings, q, incomplete_dir.c_str());
// blocklists // blocklists
tr_sys_dir_create(tr_pathbuf{ sandboxDir(), "/blocklists" }, TR_SYS_DIR_CREATE_PARENTS, 0700); tr_sys_dir_create(tr_pathbuf{ sandboxDir(), "/blocklists" }, TR_SYS_DIR_CREATE_PARENTS, 0700);

View file

@ -122,18 +122,6 @@ TEST_F(UtilsTest, trStrvDup)
tr_free(str); tr_free(str);
} }
TEST_F(UtilsTest, trStrvPath)
{
EXPECT_EQ("foo" TR_PATH_DELIMITER_STR "bar", tr_strvPath("foo", "bar"));
EXPECT_EQ(TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", tr_strvPath("", "foo", "bar"));
EXPECT_EQ("", tr_strvPath(""sv));
EXPECT_EQ("foo"sv, tr_strvPath("foo"sv));
EXPECT_EQ(
"foo" TR_PATH_DELIMITER_STR "bar" TR_PATH_DELIMITER_STR "baz" TR_PATH_DELIMITER_STR "mum"sv,
tr_strvPath("foo"sv, "bar", std::string{ "baz" }, "mum"sv));
}
TEST_F(UtilsTest, trStrvUtf8Clean) TEST_F(UtilsTest, trStrvUtf8Clean)
{ {
auto in = "hello world"sv; auto in = "hello world"sv;