{
return util_file_to_unique_list(
file, (Util_Content_Func)line_to_bookmark, (GCompareFunc)bookmark_comp,
- (GDestroyNotify)free_bookmark
+ (GDestroyNotify)free_bookmark, vb.config.history_max
);
}
static GList *load(const char *file)
{
/* read the history items from file */
- GList *list = NULL;
-
- list = util_file_to_unique_list(
+ return util_file_to_unique_list(
file, (Util_Content_Func)line_to_history, (GCompareFunc)history_comp,
- (GDestroyNotify)free_history
+ (GDestroyNotify)free_history, vb.config.history_max
);
-
- /* if list is too long - remove items from end (oldest entries) */
- if (vb.config.history_max < g_list_length(list)) {
- while (vb.config.history_max < g_list_length(list)) {
- GList *last = g_list_first(list);
- g_free(last->data);
- list = g_list_delete_link(list, last);
- }
- }
-
- return list;
}
/**
return lines;
}
+/**
+ * Retrieves a list with unique items from file.
+ *
+ * @filename: file to read items from
+ * @func: function to parse a single line to item
+ * @unique_func: function to decide if two items are equal
+ * @free_func: function to free already converted item if this isn't unque
+ * @max_items: maximum number of items that are returned, use 0 for
+ * unlimited items
+ */
GList *util_file_to_unique_list(const char *filename, Util_Content_Func func,
- GCompareFunc unique_func, GDestroyNotify free_func)
+ GCompareFunc unique_func, GDestroyNotify free_func, unsigned int max_items)
{
GList *gl = NULL;
+ /* yes, the whole file is read and wen possible don not need all the
+ * lines, but this is easier to implement compared to reading the file
+ * line wise from ent to begining */
char *line, **lines = util_get_lines(filename);
void *value;
- int len;
+ int len, num_items = 0;
len = g_strv_length(lines);
if (!len) {
free_func(value);
} else {
gl = g_list_prepend(gl, value);
+ /* skip the loop if we precessed max_items unique items */
+ if (++num_items >= max_items) {
+ break;
+ }
}
}
}
char* util_get_file_contents(const char* filename, gsize* length);
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);
+ GCompareFunc unique_func, GDestroyNotify free_func, unsigned int max_items);
char* util_strcasestr(const char* haystack, const char* needle);
char *util_str_replace(const char* search, const char* replace, const char* string);
gboolean util_create_tmp_file(const char *content, char **file);