From 4dd99dd3413aa7c03c1deb47d31cc802402ec50e Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Fri, 26 Jul 2013 20:27:39 +0200 Subject: [PATCH] Moved functions from util to history and bookmark. Some of the functions where only used one time, so they where moved to the place where they are used. --- src/bookmark.c | 35 ++++++++++++++++++++++++++++++++--- src/history.c | 25 ++++++++++++++++++++++++- src/util.c | 50 -------------------------------------------------- src/util.h | 2 -- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/bookmark.c b/src/bookmark.c index 84ce880..ffbe341 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -31,6 +31,8 @@ typedef struct { } Bookmark; static GList *load(const char *file); +static gboolean bookmark_contains_all_tags(Bookmark *bm, char **query, + unsigned int qlen); static Bookmark *line_to_bookmark(const char *line); static int bookmark_comp(Bookmark *a, Bookmark *b); static void free_bookmark(Bookmark *bm); @@ -135,9 +137,7 @@ gboolean bookmark_fill_completion(GtkListStore *store, const char *input) for (GList *l = src; l; l = l->next) { bm = (Bookmark*)l->data; - if (bm->tags - && util_array_contains_all_tags(bm->tags, g_strv_length(bm->tags), parts, len) - ) { + if (bookmark_contains_all_tags(bm, parts, len)) { gtk_list_store_append(store, &iter); gtk_list_store_set( store, &iter, @@ -166,6 +166,35 @@ static GList *load(const char *file) ); } +/** + * Checks if the given bookmark have all given query strings as prefix. + */ +static gboolean bookmark_contains_all_tags(Bookmark *bm, char **query, + unsigned int qlen) +{ + unsigned int i, n, tlen; + + if (!qlen || !bm->tags || !(tlen = g_strv_length(bm->tags))) { + return true; + } + + /* iterate over all query parts */ + for (i = 0; i < qlen; i++) { + gboolean found = false; + for (n = 0; n < tlen; n++) { + if (g_str_has_prefix(bm->tags[n], query[i])) { + found = true; + break; + } + } + if (!found) { + return false; + } + } + + return true; +} + static Bookmark *line_to_bookmark(const char *line) { char **parts; diff --git a/src/history.c b/src/history.c index 9545242..6c5f7b3 100644 --- a/src/history.c +++ b/src/history.c @@ -47,6 +47,8 @@ static const char *get_file_by_type(HistoryType type); static GList *load(const char *file); static void write_to_file(GList *list, const char *file); static History *line_to_history(const char *line); +static gboolean history_item_contains_all_tags(History *item, char **query, + unsigned int qlen); static int history_comp(History *a, History *b); static void free_history(History *item); @@ -175,7 +177,7 @@ gboolean history_fill_completion(GtkListStore *store, HistoryType type, const ch for (GList *l = src; l; l = l->next) { item = l->data; - if (util_string_contains_all_tags(item->first, parts, len)) { + if (history_item_contains_all_tags(item, parts, len)) { gtk_list_store_append(store, &iter); gtk_list_store_set( store, &iter, @@ -333,6 +335,27 @@ static History *line_to_history(const char *line) return item; } +/** + * Checks if the given array of tags are all found in history item. + */ +static gboolean history_item_contains_all_tags(History *item, char **query, + unsigned int qlen) +{ + unsigned int i; + if (!qlen) { + return true; + } + + /* iterate over all query parts */ + for (i = 0; i < qlen; i++) { + if (!util_strcasestr(item->first, query[i])) { + return false; + } + } + + return true; +} + static int history_comp(History *a, History *b) { /* compare only the first part */ diff --git a/src/util.c b/src/util.c index 03c81bb..da3b646 100644 --- a/src/util.c +++ b/src/util.c @@ -158,56 +158,6 @@ next: return NULL; } - -/** - * Checks if the given source array of pointer are prefixes to all those - * entries given as array of search strings. - */ -gboolean util_array_contains_all_tags(char **src, unsigned int s, char **query, unsigned int q) -{ - unsigned int i, n; - - if (!s || !q) { - return true; - } - - /* iterate over all query parts */ - for (i = 0; i < q; i++) { - gboolean found = false; - for (n = 0; n < s; n++) { - if (g_str_has_prefix(src[n], query[i])) { - found = true; - break; - } - } - if (!found) { - return false; - } - } - - return true; -} - -/** - * Checks if the given array of tags are all found in source string. - */ -gboolean util_string_contains_all_tags(char *src, char **query, unsigned int q) -{ - unsigned int i; - if (!q) { - return true; - } - - /* iterate over all query parts */ - for (i = 0; i < q; i++) { - if (!util_strcasestr(src, query[i])) { - return false; - } - } - - return true; -} - /** * Replaces appearances of search in string by given replace. * Returne a new allocated string of search was found. diff --git a/src/util.h b/src/util.h index f93e6b1..6b51e13 100644 --- a/src/util.h +++ b/src/util.h @@ -35,8 +35,6 @@ char** util_get_lines(const char* filename); GList *util_file_to_unique_list(const char *filename, Util_Content_Func func, GCompareFunc unique_func, GDestroyNotify free_func); char* util_strcasestr(const char* haystack, const char* needle); -gboolean util_array_contains_all_tags(char **src, unsigned int s, char **query, unsigned int q); -gboolean util_string_contains_all_tags(char *src, char **query, unsigned int q); char *util_str_replace(const char* search, const char* replace, const char* string); gboolean util_create_tmp_file(const char *content, char **file); char *util_build_path(const char *path, const char *dir); -- 2.20.1