refactor: remove tr_variantDictFindStr() (#2180)

* refactor: remove tr_variantDictFindStr
This commit is contained in:
Charles Kerr
2021-11-15 23:20:06 -06:00
committed by GitHub
parent a21594b0ce
commit 73edd7b642
5 changed files with 26 additions and 48 deletions

View File

@@ -245,7 +245,6 @@ int tr_main(int argc, char* argv[])
char const* configDir; char const* configDir;
uint8_t* fileContents; uint8_t* fileContents;
size_t fileLength; size_t fileLength;
char const* str;
tr_formatter_mem_init(MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); tr_formatter_mem_init(MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
tr_formatter_size_init(DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); tr_formatter_size_init(DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
@@ -283,15 +282,22 @@ int tr_main(int argc, char* argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (tr_variantDictFindStr(&settings, TR_KEY_download_dir, &str, nullptr) && !tr_sys_path_exists(str, nullptr)) auto sv = std::string_view{};
if (tr_variantDictFindStrView(&settings, TR_KEY_download_dir, &sv))
{ {
tr_error* error = nullptr; // tr_sys_path_exists and tr_sys_dir_create need zero-terminated strs
auto const sz_download_dir = std::string{ sv };
if (!tr_sys_dir_create(str, TR_SYS_DIR_CREATE_PARENTS, 0700, &error)) if (!tr_sys_path_exists(sz_download_dir.c_str(), nullptr))
{ {
fprintf(stderr, "Unable to create download directory \"%s\": %s\n", str, error->message); tr_error* error = nullptr;
tr_error_free(error);
return EXIT_FAILURE; if (!tr_sys_dir_create(sz_download_dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0700, &error))
{
fprintf(stderr, "Unable to create download directory \"%s\": %s\n", sz_download_dir.c_str(), error->message);
tr_error_free(error);
return EXIT_FAILURE;
}
} }
} }

View File

@@ -649,7 +649,6 @@ static void daemon_stop(void* /*arg*/)
static int daemon_start(void* varg, [[maybe_unused]] bool foreground) static int daemon_start(void* varg, [[maybe_unused]] bool foreground)
{ {
bool boolVal; bool boolVal;
char const* pid_filename;
bool pidfile_created = false; bool pidfile_created = false;
tr_session* session = nullptr; tr_session* session = nullptr;
struct event* status_ev = nullptr; struct event* status_ev = nullptr;
@@ -684,13 +683,14 @@ static int daemon_start(void* varg, [[maybe_unused]] bool foreground)
tr_logAddNamedInfo(nullptr, "Using settings from \"%s\"", configDir); tr_logAddNamedInfo(nullptr, "Using settings from \"%s\"", configDir);
tr_sessionSaveSettings(session, configDir, settings); tr_sessionSaveSettings(session, configDir, settings);
pid_filename = nullptr; auto sv = std::string_view{};
(void)tr_variantDictFindStr(settings, key_pidfile, &pid_filename, nullptr); (void)tr_variantDictFindStrView(settings, key_pidfile, &sv);
if (!tr_str_is_empty(pid_filename)) auto const sz_pid_filename = std::string{ sv };
if (!std::empty(sz_pid_filename))
{ {
tr_error* error = nullptr; tr_error* error = nullptr;
tr_sys_file_t fp = tr_sys_file_open( tr_sys_file_t fp = tr_sys_file_open(
pid_filename, sz_pid_filename.c_str(),
TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
0666, 0666,
&error); &error);
@@ -699,12 +699,12 @@ static int daemon_start(void* varg, [[maybe_unused]] bool foreground)
{ {
tr_sys_file_write_fmt(fp, "%d", nullptr, (int)getpid()); tr_sys_file_write_fmt(fp, "%d", nullptr, (int)getpid());
tr_sys_file_close(fp, nullptr); tr_sys_file_close(fp, nullptr);
tr_logAddInfo("Saved pidfile \"%s\"", pid_filename); tr_logAddInfo("Saved pidfile \"%s\"", sz_pid_filename.c_str());
pidfile_created = true; pidfile_created = true;
} }
else else
{ {
tr_logAddError("Unable to save pidfile \"%s\": %s", pid_filename, error->message); tr_logAddError("Unable to save pidfile \"%s\": %s", sz_pid_filename.c_str(), error->message);
tr_error_free(error); tr_error_free(error);
} }
} }
@@ -826,7 +826,7 @@ CLEANUP:
/* cleanup */ /* cleanup */
if (pidfile_created) if (pidfile_created)
{ {
tr_sys_path_remove(pid_filename, nullptr); tr_sys_path_remove(sz_pid_filename.c_str(), nullptr);
} }
sd_notify(0, "STATUS=\n"); sd_notify(0, "STATUS=\n");

View File

@@ -1735,9 +1735,8 @@ static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_varia
return "no filename or metainfo specified"; return "no filename or metainfo specified";
} }
char const* download_dir = nullptr; auto download_dir = std::string_view{};
if (tr_variantDictFindStrView(args_in, TR_KEY_download_dir, &download_dir) && tr_sys_path_is_relative(download_dir))
if (tr_variantDictFindStr(args_in, TR_KEY_download_dir, &download_dir, nullptr) && tr_sys_path_is_relative(download_dir))
{ {
return "download directory path is not absolute"; return "download directory path is not absolute";
} }
@@ -1752,9 +1751,10 @@ static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_varia
auto cookies = std::string_view{}; auto cookies = std::string_view{};
(void)tr_variantDictFindStrView(args_in, TR_KEY_cookies, &cookies); (void)tr_variantDictFindStrView(args_in, TR_KEY_cookies, &cookies);
if (download_dir != nullptr) if (!std::empty(download_dir))
{ {
tr_ctorSetDownloadDir(ctor, TR_FORCE, download_dir); auto const sz_download_dir = std::string{ download_dir };
tr_ctorSetDownloadDir(ctor, TR_FORCE, sz_download_dir.c_str());
} }
if (tr_variantDictFindBool(args_in, TR_KEY_paused, &boolVal)) if (tr_variantDictFindBool(args_in, TR_KEY_paused, &boolVal))

View File

@@ -326,27 +326,6 @@ bool tr_variantGetStrView(tr_variant const* v, std::string_view* setme)
return true; return true;
} }
static bool tr_variantGetStr(tr_variant const* v, char const** setme, size_t* len)
{
auto sv = std::string_view{};
if (!tr_variantGetStrView(v, &sv))
{
return false;
}
if (setme != nullptr)
{
*setme = std::data(sv);
}
if (len != nullptr)
{
*len = std::size(sv);
}
return true;
}
bool tr_variantGetRaw(tr_variant const* v, uint8_t const** setme_raw, size_t* setme_len) bool tr_variantGetRaw(tr_variant const* v, uint8_t const** setme_raw, size_t* setme_len)
{ {
bool const success = tr_variantIsString(v); bool const success = tr_variantIsString(v);
@@ -452,12 +431,6 @@ bool tr_variantDictFindStrView(tr_variant* dict, tr_quark const key, std::string
return tr_variantGetStrView(child, setme); return tr_variantGetStrView(child, setme);
} }
bool tr_variantDictFindStr(tr_variant* dict, tr_quark const key, char const** setme, size_t* len)
{
tr_variant const* const child = tr_variantDictFind(dict, key);
return tr_variantGetStr(child, setme, len);
}
bool tr_variantDictFindList(tr_variant* dict, tr_quark const key, tr_variant** setme) bool tr_variantDictFindList(tr_variant* dict, tr_quark const key, tr_variant** setme)
{ {
return tr_variantDictFindType(dict, key, TR_VARIANT_TYPE_LIST, setme); return tr_variantDictFindType(dict, key, TR_VARIANT_TYPE_LIST, setme);

View File

@@ -239,7 +239,6 @@ bool tr_variantDictFindDict(tr_variant* dict, tr_quark const key, tr_variant** s
bool tr_variantDictFindInt(tr_variant* dict, tr_quark const key, int64_t* setme); bool tr_variantDictFindInt(tr_variant* dict, tr_quark const key, int64_t* setme);
bool tr_variantDictFindReal(tr_variant* dict, tr_quark const key, double* setme); bool tr_variantDictFindReal(tr_variant* dict, tr_quark const key, double* setme);
bool tr_variantDictFindBool(tr_variant* dict, tr_quark const key, bool* setme); bool tr_variantDictFindBool(tr_variant* dict, tr_quark const key, bool* setme);
bool tr_variantDictFindStr(tr_variant* dict, tr_quark const key, char const** setme, size_t* len);
bool tr_variantDictFindStrView(tr_variant* dict, tr_quark const key, std::string_view* setme); bool tr_variantDictFindStrView(tr_variant* dict, tr_quark const key, std::string_view* setme);
bool tr_variantDictFindRaw(tr_variant* dict, tr_quark const key, uint8_t const** setme_raw, size_t* setme_len); bool tr_variantDictFindRaw(tr_variant* dict, tr_quark const key, uint8_t const** setme_raw, size_t* setme_len);