summaryrefslogtreecommitdiff
path: root/src/base/storage.c
blob: 99082bea845dc01075814fe6a30e248a5bae9aa1 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "main.h"
#include "list.h"

typedef struct struct_storage_item {
	char *name;
	char *group;
	void *data;
} storage_item_t;

list_t *storage_new()
{
	return list_new();
}

static storage_item_t *storage_new_item(char *group, char *name, void *data)
{
	storage_item_t *new;

	assert(name != NULL);
	assert(group != NULL);
	assert(data != NULL);
	new = malloc(sizeof(storage_item_t));
	new->name = strdup(name);
	new->group = strdup(group);
	new->data = data;

	return new;
}

static void storage_destroy_item(storage_item_t * p, void *f)
{
	void (*fce) (void *);

	assert(p != NULL);
	assert(f != NULL);

	fce = f;
	fce(p->data);
	free(p->name);
	free(p->group);
	free(p);
}

void storage_add(list_t * list, char *group, char *name, void *data)
{
	assert(list != NULL);
	assert(group != NULL);
	assert(name != NULL);

	list_add(list, storage_new_item(group, name, data));
}

void *storage_get(list_t * list, char *group, char *name)
{
	storage_item_t *this;
	int i;

	assert(list != NULL);
	assert(group != NULL);
	assert(name != NULL);

	for (i = 0; i < list->count; i++) {
		this = (storage_item_t *) list->list[i];

		if (strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0)
			return this->data;
	}

	DEBUG_MSG(_("%s %s was not found in storage!\n"), group, name);

	return NULL;
}

void storage_del(list_t * list, char *group, char *name, void *f)
{
	storage_item_t *this;
	int i;

	assert(list != NULL);
	assert(group != NULL);
	assert(name != NULL);

	for (i = 0; i < list->count; i++) {
		this = (storage_item_t *) list->list[i];

		if (strcmp(group, this->group) == 0 && strcmp(name, this->name) == 0) {
			storage_destroy_item(this, f);
			list_del(list, i);
			return;
		}
	}

	return;
}

void storage_del_all(list_t * list, char *group, void *f)
{
	storage_item_t *this;
	int i;

	assert(list != NULL);
	assert(group != NULL);

	for (i = 0; i < list->count; i++) {
		this = (storage_item_t *) list->list[i];

		if (strcmp(group, this->group) == 0) {
			storage_destroy_item(this, f);
			list_del(list, i);
			i--;
		}
	}
}

void storage_destroy(list_t * p, void *f)
{
	storage_item_t *this;
	int i;

	assert(p != NULL);
	assert(f != NULL);

	for (i = 0; i < p->count; i++) {
		this = (storage_item_t *) p->list[i];

		storage_destroy_item(this, f);
	}

	list_destroy(p);
}