summaryrefslogtreecommitdiffstats
path: root/tag.c
blob: e464dbcfe35ed7fa9357ef33566ec15ee8faabc2 (plain) (blame)
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
52
53
#include "tag.h"
#include "link.h"

static struct link *
tagrefs_ffind(struct link *head, struct tag *tag)
{
	struct link *iter;

	for (iter = head->next; iter; iter = iter->next) {
		if (UPCAST(iter, struct tag_ref)->tag == tag)
			return iter;
	}

	return NULL;
}

int
tagrefs_incl(struct link *head, struct tag *tag)
{
	struct link *ref;

	ref = tagrefs_ffind(head, tag);
	return ref != NULL;
}

void
tagrefs_add(struct link *head, struct tag *tag)
{
	struct tag_ref *ref;

	if (tagrefs_incl(head, tag))
		return;

	ref = malloc(sizeof(struct tag_ref));
	ASSERT(ref != NULL);
	ref->link = LINK_EMPTY;
	ref->tag = tag;
	link_push_back(head, &ref->link);
}

void
tagrefs_rm(struct link *head, struct tag *tag)
{
	struct link *ref;
	struct tag_ref *tagref;

	ref = tagrefs_ffind(head, tag);
	if (!ref) return;

	tagref = UPCAST(ref, struct tag_ref);
	link_pop(ref);
	free(tagref);
}