From 55f9e04485a5978eb5040203cf6ef9c39c7593aa Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sat, 21 Sep 2013 18:27:02 +0200 Subject: [PATCH] Cleaned the code a little. Remove already solved tasks and changed most of the *var == '\0' into !*var expressions. --- src/bookmark.c | 6 +++--- src/command.c | 2 +- src/ex.c | 47 ++++++++++++++++++++++++++--------------------- src/history.c | 4 ++-- src/main.c | 2 +- src/normal.c | 2 +- src/setting.c | 2 +- src/util.c | 2 +- 8 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/bookmark.c b/src/bookmark.c index 3cbd281..1ee6f0d 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -107,7 +107,7 @@ gboolean bookmark_fill_completion(GtkListStore *store, const char *input) src = load(vb.files[FILES_BOOKMARK]); src = g_list_reverse(src); - if (!input || *input == '\0') { + if (!input || !*input) { /* without any tags return all bookmarked items */ for (GList *l = src; l; l = l->next) { bm = (Bookmark*)l->data; @@ -172,7 +172,7 @@ gboolean bookmark_fill_tag_completion(GtkListStore *store, const char *input) } /* generate the completion with the found tags */ - if (!input || *input == '\0') { + if (!input || !*input) { for (l = tags; l; l = l->next) { gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1); @@ -317,7 +317,7 @@ static Bookmark *line_to_bookmark(const char *line) while (g_ascii_isspace(*line)) { line++; } - if (*line == '\0') { + if (!*line) { return NULL; } diff --git a/src/command.c b/src/command.c index 723e53f..04bb7ae 100644 --- a/src/command.c +++ b/src/command.c @@ -156,7 +156,7 @@ gboolean command_save(const Arg *arg) uri = arg->s; } - if (!uri || *uri == '\0') { + if (!uri || !*uri) { return false; } diff --git a/src/ex.c b/src/ex.c index 32c79a2..f753a47 100644 --- a/src/ex.c +++ b/src/ex.c @@ -423,9 +423,7 @@ static gboolean parse(const char **input, ExArg *arg) if (**input) { (*input)++; } -#if 0 - PRINT_DEBUG("CMD idx=%d, x=%d [%s][%s][%s]", arg->idx, arg->count, arg->name, arg->lhs->str, arg->rhs->str); -#endif + return true; } @@ -475,7 +473,7 @@ static gboolean parse_command_name(const char **input, ExArg *arg) } } (*input)++; - } while (matches > 0 && **input != '\0' && **input != ' '); + } while (matches > 0 && **input && **input != ' '); if (!matches) { /* TODO show readable error message */ @@ -494,25 +492,28 @@ static gboolean parse_command_name(const char **input, ExArg *arg) */ static gboolean parse_lhs(const char **input, ExArg *arg) { + char quote = '\\'; + if (!*input || !**input) { return false; } + /* get the char until the next none escaped whitespace and save it into * the lhs */ while (**input && **input != ' ') { /* if we find a backslash this escapes the next whitespace */ - if (**input == '\\') { + if (**input == quote) { /* move pointer to the next char */ (*input)++; if (!*input) { /* if input ends here - use only the backslash */ - g_string_append_c(arg->lhs, '\\'); + g_string_append_c(arg->lhs, quote); } else if (**input == ' ') { /* escaped whitespace becomes only whitespace */ g_string_append_c(arg->lhs, **input); } else { /* put escape char and next char into the result string */ - g_string_append_c(arg->lhs, '\\'); + g_string_append_c(arg->lhs, quote); g_string_append_c(arg->lhs, **input); } } else { @@ -529,24 +530,27 @@ static gboolean parse_lhs(const char **input, ExArg *arg) */ static gboolean parse_rhs(const char **input, ExArg *arg) { + char quote = '\\'; + if (!*input || !**input) { return false; } + /* get char until the end of command */ while (**input && **input != '\n' && **input != '|') { /* if we find a backslash this escapes the next whitespace */ - if (**input == '\\') { + if (**input == quote) { /* move pointer to the next char */ (*input)++; if (!*input) { /* if input ends here - use only the backslash */ - g_string_append_c(arg->rhs, '\\'); + g_string_append_c(arg->rhs, quote); } else if (**input == ' ') { /* escaped whitespace becomes only whitespace */ g_string_append_c(arg->rhs, **input); } else { /* put escape char and next char into the result string */ - g_string_append_c(arg->rhs, '\\'); + g_string_append_c(arg->rhs, quote); g_string_append_c(arg->rhs, **input); } } else { @@ -622,7 +626,7 @@ static gboolean ex_eval(const ExArg *arg) gboolean success; char *value = NULL; - if (!*arg->rhs->str) { + if (!arg->rhs->len) { return false; } @@ -641,14 +645,15 @@ static gboolean ex_eval(const ExArg *arg) static gboolean ex_map(const ExArg *arg) { - /* TODO implement parsing of chars */ - char *lhs = arg->lhs->str; - char *rhs = arg->rhs->str; + char *lhs, *rhs; - if (!*lhs || !*rhs) { + if (!arg->lhs->len || !arg->rhs->len) { return false; } + lhs = arg->lhs->str; + rhs = arg->rhs->str; + if (arg->code == EX_NMAP) { map_insert(lhs, rhs, 'n'); } else if (arg->code == EX_CMAP) { @@ -661,13 +666,13 @@ static gboolean ex_map(const ExArg *arg) static gboolean ex_unmap(const ExArg *arg) { - /* TODO implement parsing of chars */ - char *lhs = arg->lhs->str; - - if (!*lhs) { + char *lhs; + if (!arg->lhs->len) { return false; } + lhs = arg->lhs->str; + if (arg->code == EX_NUNMAP) { map_delete(lhs, 'n'); } else if (arg->code == EX_CUNMAP) { @@ -732,7 +737,7 @@ static gboolean ex_set(const ExArg *arg) gboolean success; char *param = NULL; - if (!*arg->rhs->str) { + if (!arg->rhs->len) { return false; } @@ -786,7 +791,7 @@ static gboolean ex_shortcut(const ExArg *arg) * shortcut[name]=http://donain.tld/?q=$0' */ switch (arg->code) { case EX_SCA: - if (*arg->rhs->str && (p = strchr(arg->rhs->str, '='))) { + if (arg->rhs->len && (p = strchr(arg->rhs->str, '='))) { *p++ = '\0'; return shortcut_add(arg->rhs->str, p); } diff --git a/src/history.c b/src/history.c index e13d090..05d1231 100644 --- a/src/history.c +++ b/src/history.c @@ -154,7 +154,7 @@ gboolean history_fill_completion(GtkListStore *store, HistoryType type, const ch src = load(get_file_by_type(type)); src = g_list_reverse(src); - if (!input || *input == '\0') { + if (!input || !*input) { /* without any tags return all items */ for (GList *l = src; l; l = l->next) { item = l->data; @@ -301,7 +301,7 @@ static History *line_to_history(const char *line) while (g_ascii_isspace(*line)) { line++; } - if (*line == '\0') { + if (!*line) { return NULL; } diff --git a/src/main.c b/src/main.c index e0837b2..c5d2d76 100644 --- a/src/main.c +++ b/src/main.c @@ -155,7 +155,7 @@ gboolean vb_load_uri(const Arg *arg) if (arg->s) { path = g_strstrip(arg->s); } - if (!path || *path == '\0') { + if (!path || !*path) { path = vb.config.home_page; } diff --git a/src/normal.c b/src/normal.c index 324b6e1..c265957 100644 --- a/src/normal.c +++ b/src/normal.c @@ -437,7 +437,7 @@ static VbResult normal_descent(const NormalCmdInfo *info) uri = GET_URI(); /* get domain part */ - if (!uri || *uri == '\0' + if (!uri || !*uri || !(domain = strstr(uri, "://")) || !(domain = strchr(domain + 3, '/')) ) { diff --git a/src/setting.c b/src/setting.c index 1d7f97b..4489a6a 100644 --- a/src/setting.c +++ b/src/setting.c @@ -204,7 +204,7 @@ gboolean setting_fill_completion(GtkListStore *store, const char *input) GtkTreeIter iter; GList *src = g_hash_table_get_keys(settings); - if (!input || input == '\0') { + if (!input || !*input) { for (GList *l = src; l; l = l->next) { gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1); diff --git a/src/util.c b/src/util.c index f3cb2df..f468da7 100644 --- a/src/util.c +++ b/src/util.c @@ -131,7 +131,7 @@ GList *util_file_to_unique_list(const char *filename, Util_Content_Func func, for (int i = len - 1; i >= 0; i--) { line = lines[i]; g_strstrip(line); - if (*line == '\0') { + if (!*line) { continue; } -- 2.20.1