File: mru.c

package info (click to toggle)
tagtool 0.12.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,352 kB
  • ctags: 1,010
  • sloc: ansic: 9,013; sh: 3,571; makefile: 121
file content (51 lines) | stat: -rw-r--r-- 952 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include "elist.h"

#include "mru.h"


MRUList* mru_new (guint max)
{
	MRUList *mru = malloc(sizeof(MRUList));
	mru->max = max;
	mru->list = g_elist_new();
	return mru;
}

MRUList* mru_new_from_list (guint max, GEList *list)
{
	MRUList *mru = malloc(sizeof(MRUList));
	mru->max = max;
	mru->list = list;
	return mru;
}


void mru_free (MRUList *mru)
{
	g_elist_free_data(mru->list);
	free(mru);
}


void mru_add (MRUList *mru, const gchar *entry)
{
	GList *found = g_list_find_custom(GLIST(mru->list), entry, (GCompareFunc)strcmp);

	if (found) {
		g_elist_remove_link(mru->list, found);
		g_elist_prepend(mru->list, found->data);
		g_list_free_1(found);
	} else {
		g_elist_prepend(mru->list, strdup(entry));
		if (g_elist_length(mru->list) > mru->max) {
			GList *last = g_elist_last(mru->list);
			g_elist_remove_link(mru->list, last);
			free(last->data);
			g_list_free_1(last);
		}
	}
}