Introduce `tr_str_is_empty` to relay intent better

This commit is contained in:
Mike Gelfand 2019-07-13 11:52:44 +03:00
parent bff188c2cf
commit a3e4919385
30 changed files with 73 additions and 66 deletions

View File

@ -649,7 +649,7 @@ static int daemon_start(void* raw_arg, bool foreground)
pid_filename = NULL;
tr_variantDictFindStr(settings, key_pidfile, &pid_filename, NULL);
if (pid_filename != NULL && *pid_filename != '\0')
if (!tr_str_is_empty(pid_filename))
{
tr_error* error = NULL;
tr_sys_file_t fp = tr_sys_file_open(pid_filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666,
@ -693,7 +693,7 @@ static int daemon_start(void* raw_arg, bool foreground)
force_generic = false;
}
if (tr_variantDictFindStr(settings, TR_KEY_watch_dir, &dir, NULL) && dir != NULL && *dir != '\0')
if (tr_variantDictFindStr(settings, TR_KEY_watch_dir, &dir, NULL) && !tr_str_is_empty(dir))
{
tr_logAddInfo("Watching \"%s\" for new .torrent files", dir);

View File

@ -721,7 +721,7 @@ static void refreshInfo(struct DetailsImpl* di, tr_torrent** torrents, int n)
mixed_date |= date != infos[i]->dateCreated;
}
gboolean const empty_creator = *creator == '\0';
gboolean const empty_creator = tr_str_is_empty(creator);
gboolean const empty_date = date == 0;
if (mixed_date || mixed_creator)
@ -1079,7 +1079,7 @@ static void refreshInfo(struct DetailsImpl* di, tr_torrent** torrents, int n)
str = is_uniform ? baseline : mixed;
}
if (str == NULL || *str == '\0')
if (tr_str_is_empty(str))
{
str = _("No errors");
}
@ -1760,7 +1760,7 @@ static gboolean onPeerViewQueryTooltip(GtkWidget* widget, gint x, gint y, gboole
g_string_append_printf(gstr, "<b>%s</b>\n%s\n \n", markup, addr);
g_free(markup);
for (char const* pch = flagstr; pch != NULL && *pch != '\0'; ++pch)
for (char const* pch = flagstr; !tr_str_is_empty(pch); ++pch)
{
char const* s = NULL;
@ -2515,7 +2515,7 @@ static void on_edit_trackers_response(GtkDialog* dialog, int response, gpointer
{
char* const str = tracker_strings[i];
if (*str == '\0')
if (tr_str_is_empty(str))
{
++tier;
}
@ -2661,7 +2661,7 @@ static void on_add_tracker_response(GtkDialog* dialog, int response, gpointer gd
char* url = g_strdup(gtk_entry_get_text(GTK_ENTRY(e)));
g_strstrip(url);
if (url != NULL && *url != '\0')
if (!tr_str_is_empty(url))
{
if (tr_urlIsValidTracker(url))
{

View File

@ -679,10 +679,10 @@ static gboolean onRowActivated(GtkTreeView* view, GtkTreePath* path, GtkTreeView
g_free(filename);
filename = tmp;
}
while (filename != NULL && *filename != '\0' && !g_file_test(filename, G_FILE_TEST_EXISTS));
while (!tr_str_is_empty(filename) && !g_file_test(filename, G_FILE_TEST_EXISTS));
}
if ((handled = filename != NULL && *filename != '\0'))
if ((handled = !tr_str_is_empty(filename)))
{
gtr_open_file(filename);
}

View File

@ -703,7 +703,7 @@ static gboolean testText(tr_torrent const* tor, char const* key)
{
gboolean ret = FALSE;
if (key == NULL || *key == '\0')
if (tr_str_is_empty(key))
{
ret = TRUE;
}

View File

@ -252,7 +252,7 @@ static void onResponse(GtkDialog* d, int response, gpointer user_data)
{
char* const str = tracker_strings[i];
if (*str == '\0')
if (tr_str_is_empty(str))
{
++tier;
}

View File

@ -298,7 +298,7 @@ static void getStatusString(GString* gstr, tr_torrent const* tor, tr_stat const*
char buf[256];
getShortTransferString(tor, st, uploadSpeed_KBps, downloadSpeed_KBps, buf, sizeof(buf));
if (*buf != '\0')
if (!tr_str_is_empty(buf))
{
g_string_append_printf(gstr, " - %s", buf);
}

View File

@ -94,14 +94,14 @@ static char* announce_url_new(tr_session const* session, tr_announce_request con
str = get_event_string(req);
if (str != NULL && *str != '\0')
if (!tr_str_is_empty(str))
{
evbuffer_add_printf(buf, "&event=%s", str);
}
str = req->tracker_id_str;
if (str != NULL && *str != '\0')
if (!tr_str_is_empty(str))
{
evbuffer_add_printf(buf, "&trackerid=%s", str);
}

View File

@ -183,7 +183,7 @@ static struct tr_scrape_info* tr_announcerGetScrapeInfo(struct tr_announcer* ann
{
struct tr_scrape_info* info = NULL;
if (url != NULL && *url != '\0')
if (!tr_str_is_empty(url))
{
bool found;
struct tr_scrape_info const key = { .url = (char*)url };

View File

@ -639,7 +639,7 @@ char* tr_clientForId(char* buf, size_t buflen, void const* id_in)
tr_snprintf(buf, buflen, "Baidu Netdisk");
}
if (*buf != '\0')
if (!tr_str_is_empty(buf))
{
return buf;
}
@ -664,7 +664,7 @@ char* tr_clientForId(char* buf, size_t buflen, void const* id_in)
strint(id + 5, 2), getMnemonicEnd(id[7]));
}
if (*buf != '\0')
if (!tr_str_is_empty(buf))
{
return buf;
}
@ -683,7 +683,7 @@ char* tr_clientForId(char* buf, size_t buflen, void const* id_in)
mainline_style(buf, buflen, "Queen Bee", id);
}
if (*buf != '\0')
if (!tr_str_is_empty(buf))
{
return buf;
}
@ -806,7 +806,7 @@ char* tr_clientForId(char* buf, size_t buflen, void const* id_in)
}
/* Shad0w-style */
if (*buf == '\0')
if (tr_str_is_empty(buf))
{
int a;
int b;
@ -856,7 +856,7 @@ char* tr_clientForId(char* buf, size_t buflen, void const* id_in)
}
/* No match */
if (*buf == '\0')
if (tr_str_is_empty(buf))
{
char out[32];
char* walk = out;

View File

@ -595,7 +595,7 @@ cleanup:
char* tr_sys_path_basename(char const* path, tr_error** error)
{
if (path == NULL || path[0] == '\0')
if (tr_str_is_empty(path))
{
return tr_strdup(".");
}
@ -635,7 +635,7 @@ char* tr_sys_path_basename(char const* path, tr_error** error)
char* tr_sys_path_dirname(char const* path, tr_error** error)
{
if (path == NULL || path[0] == '\0')
if (tr_str_is_empty(path))
{
return tr_strdup(".");
}

View File

@ -246,7 +246,7 @@ void tr_logAddMessage(char const* file, int line, tr_log_level level, char const
#endif
if (*buf != '\0')
if (!tr_str_is_empty(buf))
{
if (tr_logGetQueueEnabled())
{

View File

@ -122,7 +122,7 @@ tr_magnet_info* tr_magnetParse(char const* uri)
if (uri != NULL && strncmp(uri, "magnet:?", 8) == 0)
{
for (char const* walk = uri + 8; walk != NULL && *walk != '\0';)
for (char const* walk = uri + 8; !tr_str_is_empty(walk);)
{
char const* key = walk;
char const* delim = strchr(key, '=');

View File

@ -376,7 +376,7 @@ static void getFileInfo(char const* topFile, tr_metainfo_builder_file const* fil
while ((token = tr_strsep(&walk, TR_PATH_DELIMITER_STR)) != NULL)
{
if (*token != '\0')
if (!tr_str_is_empty(token))
{
tr_variantListAddStr(uninitialized_path, token);
}
@ -479,7 +479,7 @@ static void tr_realMakeMetaInfo(tr_metainfo_builder* builder)
if (builder->result == TR_MAKEMETA_OK && !builder->abortFlag)
{
if (builder->comment != NULL && *builder->comment != '\0')
if (!tr_str_is_empty(builder->comment))
{
tr_variantDictAddStr(&top, TR_KEY_comment, builder->comment);
}
@ -596,7 +596,7 @@ void tr_makeMetaInfo(tr_metainfo_builder* builder, char const* outputFile, tr_tr
builder->comment = tr_strdup(comment);
builder->isPrivate = isPrivate;
if (outputFile != NULL && *outputFile != '\0')
if (!tr_str_is_empty(outputFile))
{
builder->outputFile = tr_strdup(outputFile);
}

View File

@ -621,7 +621,7 @@ static char const* tr_metainfoParseImpl(tr_session const* session, tr_info* inf,
}
}
if (str == NULL || *str == '\0')
if (tr_str_is_empty(str))
{
return "name";
}

View File

@ -431,7 +431,7 @@ error:
static void maybeSetCongestionAlgorithm(tr_socket_t socket, char const* algorithm)
{
if (algorithm != NULL && *algorithm != '\0')
if (!tr_str_is_empty(algorithm))
{
tr_netSetCongestionControl(socket, algorithm);
}

View File

@ -328,7 +328,7 @@ char const* tr_getDefaultConfigDir(char const* appname)
{
static char* s = NULL;
if (appname == NULL || *appname == '\0')
if (tr_str_is_empty(appname))
{
appname = "Transmission";
}
@ -390,7 +390,7 @@ char const* tr_getDefaultDownloadDir(void)
/* figure out where to look for user-dirs.dirs */
config_home = tr_env_get_string("XDG_CONFIG_HOME", NULL);
if (config_home != NULL && *config_home != '\0')
if (!tr_str_is_empty(config_home))
{
config_file = tr_buildPath(config_home, "user-dirs.dirs", NULL);
}
@ -576,7 +576,7 @@ char const* tr_getWebClientDir(tr_session const* session UNUSED)
/* XDG_DATA_HOME should be the first in the list of candidates */
tmp = tr_env_get_string("XDG_DATA_HOME", NULL);
if (tmp != NULL && *tmp != '\0')
if (!tr_str_is_empty(tmp))
{
tr_list_append(&candidates, tmp);
}
@ -596,7 +596,7 @@ char const* tr_getWebClientDir(tr_session const* session UNUSED)
tr_free(xdg);
tmp = buf;
while (tmp != NULL && *tmp != '\0')
while (!tr_str_is_empty(tmp))
{
char const* end = strchr(tmp, ':');
@ -609,7 +609,7 @@ char const* tr_getWebClientDir(tr_session const* session UNUSED)
tmp = (char*)end + 1;
}
else if (tmp != NULL && *tmp != '\0')
else if (!tr_str_is_empty(tmp))
{
tr_list_append(&candidates, tr_strdup(tmp));
break;

View File

@ -847,7 +847,7 @@ static uint64_t loadFromFile(tr_torrent* tor, uint64_t fieldsToLoad, bool* didRe
}
if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_DOWNLOAD_DIR)) != 0 &&
tr_variantDictFindStr(&top, TR_KEY_destination, &str, &len) && str != NULL && *str != '\0')
tr_variantDictFindStr(&top, TR_KEY_destination, &str, &len) && !tr_str_is_empty(str))
{
bool const is_current_dir = tor->currentDir == tor->downloadDir;
tr_free(tor->downloadDir);
@ -862,7 +862,7 @@ static uint64_t loadFromFile(tr_torrent* tor, uint64_t fieldsToLoad, bool* didRe
}
if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_INCOMPLETE_DIR)) != 0 &&
tr_variantDictFindStr(&top, TR_KEY_incomplete_dir, &str, &len) && str != NULL && *str != '\0')
tr_variantDictFindStr(&top, TR_KEY_incomplete_dir, &str, &len) && !tr_str_is_empty(str))
{
bool const is_current_dir = tor->currentDir == tor->incompleteDir;
tr_free(tor->incompleteDir);
@ -1024,7 +1024,7 @@ static uint64_t setFromCtor(tr_torrent* tor, uint64_t fields, tr_ctor const* cto
{
char const* path;
if (tr_ctorGetDownloadDir(ctor, mode, &path) && path != NULL && *path != '\0')
if (tr_ctorGetDownloadDir(ctor, mode, &path) && !tr_str_is_empty(path))
{
ret |= TR_FR_DOWNLOAD_DIR;
tr_free(tor->downloadDir);

View File

@ -430,7 +430,7 @@ static void handle_web_client(struct evhttp_request* req, struct tr_rpc_server*
{
char const* webClientDir = tr_getWebClientDir(server->session);
if (webClientDir == NULL || *webClientDir == '\0')
if (tr_str_is_empty(webClientDir))
{
send_simple_response(req, HTTP_NOTFOUND,
"<p>Couldn't find Transmission's web interface files!</p>"
@ -461,7 +461,7 @@ static void handle_web_client(struct evhttp_request* req, struct tr_rpc_server*
else
{
char* filename = tr_strdup_printf("%s%s%s", webClientDir, TR_PATH_DELIMITER_STR,
*subpath != '\0' ? subpath : "index.html");
tr_str_is_empty(subpath) ? "index.html" : subpath);
serve_file(req, server, filename);
tr_free(filename);
}
@ -938,7 +938,7 @@ static void tr_rpcSetList(char const* whitelistStr, tr_list** list)
}
/* build the new whitelist entries */
for (char const* walk = whitelistStr; walk != NULL && *walk != '\0';)
for (char const* walk = whitelistStr; !tr_str_is_empty(walk);)
{
char const* delimiters = " ,;";
size_t const len = strcspn(walk, delimiters);

View File

@ -960,7 +960,7 @@ static char const* setLabels(tr_torrent* tor, tr_variant* list)
{
char* label = tr_strndup(str, str_len);
tr_strstrip(label);
if (*label == '\0')
if (tr_str_is_empty(label))
{
errmsg = "labels cannot be empty";
}
@ -1641,7 +1641,7 @@ static void gotNewBlocklist(tr_session* session, bool did_connect UNUSED, bool d
tr_sys_file_close(fd, NULL);
if (*result != '\0')
if (!tr_str_is_empty(result))
{
tr_logAddError("%s", result);
}

View File

@ -487,7 +487,7 @@ bool tr_sessionLoadSettings(tr_variant* dict, char const* configDir, char const*
tr_variantFree(&oldDict);
/* if caller didn't specify a config dir, use the default */
if (configDir == NULL || *configDir == '\0')
if (tr_str_is_empty(configDir))
{
configDir = tr_getDefaultConfigDir(appName);
}

View File

@ -253,7 +253,7 @@ static void append_argument(char** arguments, char const* argument)
(*arguments)[arguments_len++] = ' ';
}
if (argument[0] != '\0' && strpbrk(argument, " \t\n\v\"") == NULL)
if (!tr_str_is_empty(argument) && strpbrk(argument, " \t\n\v\"") == NULL)
{
*arguments = tr_renew(char, *arguments, arguments_len + argument_len + 2);
strcpy(*arguments + arguments_len, argument);

View File

@ -159,7 +159,7 @@ int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename)
}
}
if (name == NULL || *name == '\0')
if (tr_str_is_empty(name))
{
char* base = tr_sys_path_basename(filename, NULL);
@ -338,7 +338,7 @@ void tr_ctorSetDownloadDir(tr_ctor* ctor, tr_ctorMode mode, char const* director
args->downloadDir = NULL;
args->isSet_downloadDir = false;
if (directory != NULL && *directory != '\0')
if (!tr_str_is_empty(directory))
{
args->isSet_downloadDir = true;
args->downloadDir = tr_strdup(directory);

View File

@ -418,7 +418,7 @@ char* tr_torrentInfoGetMagnetLink(tr_info const* inf)
name = inf->name;
if (name != NULL && *name != '\0')
if (!tr_str_is_empty(name))
{
evbuffer_add_printf(s, "%s", "&dn=");
tr_http_escape(s, name, TR_BAD_SIZE, true);

View File

@ -2233,7 +2233,7 @@ static void get_local_time_str(char* const buffer, size_t const buffer_len)
static void torrentCallScript(tr_torrent const* tor, char const* script)
{
if (script == NULL || *script == '\0')
if (tr_str_is_empty(script))
{
return;
}
@ -3794,8 +3794,8 @@ void tr_torrentSetQueueStartCallback(tr_torrent* torrent, void (* callback)(tr_t
static bool renameArgsAreValid(char const* oldpath, char const* newname)
{
return oldpath != NULL && *oldpath != '\0' && newname != NULL && *newname != '\0' && strcmp(newname, ".") != 0 &&
strcmp(newname, "..") != 0 && strchr(newname, TR_PATH_DELIMITER) == NULL;
return !tr_str_is_empty(oldpath) && !tr_str_is_empty(newname) && strcmp(newname, ".") != 0 && strcmp(newname, "..") != 0 &&
strchr(newname, TR_PATH_DELIMITER) == NULL;
}
static tr_file_index_t* renameFindAffectedFiles(tr_torrent* tor, char const* oldpath, size_t* setme_n)

View File

@ -11,8 +11,10 @@
#include <stdlib.h> /* exit() */
#include <string.h>
#include "transmission.h"
#include "tr-getopt.h"
#include "tr-macros.h"
#include "utils.h"
int tr_optind = 1;
@ -67,8 +69,8 @@ static void getopts_usage_line(tr_option const* opt, int longWidth, int shortWid
int const d_width = 80 - d_indent;
char const* d = opt->description;
printf(" %s%-*s %s%-*s %-*s ", (shortName != NULL && *shortName != '\0') ? "-" : " ", shortWidth, shortName,
(longName != NULL && *longName != '\0') ? "--" : " ", longWidth, longName, argWidth, arg);
printf(" %s%-*s %s%-*s %-*s ", tr_str_is_empty(shortName) ? " " : "-", shortWidth, shortName,
tr_str_is_empty(longName) ? " " : "--", longWidth, longName, argWidth, arg);
len = get_next_line_len(d, d_width);
printf("%*.*s\n", len, len, d);

View File

@ -192,7 +192,7 @@ char const* tr_strip_positional_args(char const* str)
out = buf;
for (; str != NULL && *str != '\0'; ++str)
for (; !tr_str_is_empty(str); ++str)
{
*out++ = *str;
@ -363,7 +363,7 @@ int64_t tr_getDirFreeSpace(char const* dir)
{
int64_t free_space;
if (dir == NULL || *dir == '\0')
if (tr_str_is_empty(dir))
{
errno = EINVAL;
free_space = -1;
@ -1526,7 +1526,7 @@ int* tr_parseNumberRange(char const* str_in, size_t len, int* setmeCount)
walk = str;
while (walk != NULL && *walk != '\0' && success)
while (!tr_str_is_empty(walk) && success)
{
struct number_range range;
char const* pch = strchr(walk, ',');
@ -2110,7 +2110,7 @@ int tr_env_get_int(char const* key, int default_value)
char const* value = getenv(key);
if (value != NULL && *value != '\0')
if (!tr_str_is_empty(value))
{
return atoi(value);
}

View File

@ -202,6 +202,11 @@ char* tr_strdup(void const* in);
*/
int tr_strcmp0(char const* str1, char const* str2);
static inline bool tr_str_is_empty(char const* value)
{
return value == NULL || *value == '\0';
}
/**
* @brief like memcmp() but gracefully handles NULL pointers
*/

View File

@ -323,7 +323,7 @@ static void stripWhitespace(char* in)
{
char* out = in;
for (; in != NULL && *in != '\0'; ++in)
for (; !tr_str_is_empty(in); ++in)
{
if (!isspace(*in))
{

View File

@ -548,11 +548,11 @@ static char* getEncodedMetainfo(char const* filename)
static void addIdArg(tr_variant* args, char const* id, char const* fallback)
{
if (id == NULL || *id == '\0')
if (tr_str_is_empty(id))
{
id = fallback;
if (id == NULL || *id == '\0')
if (tr_str_is_empty(id))
{
fprintf(stderr, "No torrent specified! Please use the -t option first.\n");
id = "-1"; /* no torrent will have this ID, so will act as a no-op */
@ -666,7 +666,7 @@ static void addLabels(tr_variant* args, char const* arg)
while ((token = tr_strsep(&argcpy, ",")) != NULL)
{
tr_strstrip(token);
if (*token != '\0')
if (!tr_str_is_empty(token))
{
tr_variantListAddStr(labels, token);
}
@ -679,7 +679,7 @@ static void addFiles(tr_variant* args, tr_quark const key, char const* arg)
{
tr_variant* files = tr_variantDictAddList(args, key, 100);
if (*arg == '\0')
if (tr_str_is_empty(arg))
{
fprintf(stderr, "No files specified!\n");
arg = "-1"; /* no file will have this index, so should be a no-op */
@ -1058,7 +1058,7 @@ static void printDetails(tr_variant* top)
printf(" Corrupt DL: %s\n", buf);
}
if (tr_variantDictFindStr(t, TR_KEY_errorString, &str, NULL) && str != NULL && *str != '\0' &&
if (tr_variantDictFindStr(t, TR_KEY_errorString, &str, NULL) && !tr_str_is_empty(str) &&
tr_variantDictFindInt(t, TR_KEY_error, &i) && i != 0)
{
switch (i)
@ -1150,12 +1150,12 @@ static void printDetails(tr_variant* top)
printf(" Public torrent: %s\n", (boolVal ? "No" : "Yes"));
}
if (tr_variantDictFindStr(t, TR_KEY_comment, &str, NULL) && str != NULL && *str != '\0')
if (tr_variantDictFindStr(t, TR_KEY_comment, &str, NULL) && !tr_str_is_empty(str))
{
printf(" Comment: %s\n", str);
}
if (tr_variantDictFindStr(t, TR_KEY_creator, &str, NULL) && str != NULL && *str != '\0')
if (tr_variantDictFindStr(t, TR_KEY_creator, &str, NULL) && !tr_str_is_empty(str))
{
printf(" Creator: %s\n", str);
}
@ -2608,7 +2608,7 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv)
tr_variant* targs = NULL;
tr_variant* sargs = NULL;
if (*id != '\0')
if (!tr_str_is_empty(id))
{
targs = ensure_tset(&tset);
}

View File

@ -142,7 +142,7 @@ static void showInfo(tr_info const* inf)
printf(" Created by: %s\n", inf->creator ? inf->creator : "Unknown");
printf(" Created on: %s\n", unix_timestamp_to_str(inf->dateCreated));
if (inf->comment != NULL && *inf->comment != '\0')
if (!tr_str_is_empty(inf->comment))
{
printf(" Comment: %s\n", inf->comment);
}