File: filter-kvs.c

package info (click to toggle)
sylfilter 0.8-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,124 kB
  • sloc: ansic: 12,806; sh: 8,910; makefile: 349
file content (220 lines) | stat: -rw-r--r-- 4,174 bytes parent folder | download | duplicates (4)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* SylFilter - a message filter
 *
 * Copyright (C) 2011 Hiroyuki Yamamoto
 * Copyright (C) 2011 Sylpheed Development Team
 */

#include <glib.h>

#include "filter-kvs.h"

struct _XFilterKVS
{
	char *dbfile;
	void *dbhandle;
};

static XFilterKVSEngine ke;


int xfilter_kvs_set_engine(XFilterKVSEngine *engine)
{
	g_return_val_if_fail(engine != NULL, -1);

	ke = *engine;

	return 0;
}

XFilterKVS *xfilter_kvs_new(const char *dbfile, void *dbhandle)
{
	XFilterKVS *kvs;

	kvs = g_new(XFilterKVS, 1);
	kvs->dbfile = g_strdup(dbfile);
	kvs->dbhandle = dbhandle;

	return kvs;
}

const char *xfilter_kvs_get_file(XFilterKVS *kvs)
{
	g_return_val_if_fail(kvs != NULL, NULL);
	return kvs->dbfile;
}

void *xfilter_kvs_get_handle(XFilterKVS *kvs)
{
	g_return_val_if_fail(kvs != NULL, NULL);
	return kvs->dbhandle;
}

XFilterKVS *xfilter_kvs_open(const char *dbfile)
{
	g_return_val_if_fail(ke.open != NULL, NULL);
	return ke.open(dbfile);
}

int xfilter_kvs_close(XFilterKVS *kvs)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.close(kvs);
}

int xfilter_kvs_insert(XFilterKVS *kvs, const char *key, void *value, int size)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.insert(kvs, key, value, size);
}

int xfilter_kvs_delete(XFilterKVS *kvs, const char *key)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.delete(kvs, key);
}

int xfilter_kvs_update(XFilterKVS *kvs, const char *key, void *value, int size)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.update(kvs, key, value, size);
}

int xfilter_kvs_fetch(XFilterKVS *kvs, const char *key, void *vbuf, int vsize)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.fetch(kvs, key, vbuf, vsize);
}

int xfilter_kvs_begin(XFilterKVS *kvs)
{
	g_return_val_if_fail(kvs != NULL, -1);
	if (ke.begin)
		return ke.begin(kvs);
	else
		return 0;
}

int xfilter_kvs_end(XFilterKVS *kvs)
{
	g_return_val_if_fail(kvs != NULL, -1);
	if (ke.end)
		return ke.end(kvs);
	else
		return 0;
}

int xfilter_kvs_get_record_size(XFilterKVS *kvs)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.size(kvs);
}

int xfilter_kvs_foreach(XFilterKVS *kvs, XFilterKVSForeachFunc func, void *data)
{
	g_return_val_if_fail(kvs != NULL, -1);
	return ke.foreach(kvs, func, data);
}

int xfilter_kvs_fetch_int(XFilterKVS *kvs, const char *key)
{
	int ival = 0;
	int size;
	char vbuf[4];

	g_return_val_if_fail(kvs != NULL, -1);

	size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
	if (size == 4) {
		ival = *(gint32 *)vbuf;
		return ival;
	}

	return 0;
}

int xfilter_kvs_set_int(XFilterKVS *kvs, const char *key, int num)
{
	gint32 ival = num;
	int size;
	char vbuf[4];

	g_return_val_if_fail(kvs != NULL, -1);

	size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
	if (size == 4) {
		if (num > 0)
			return xfilter_kvs_update(kvs, key, (char *)&ival, 4);
		else
			return xfilter_kvs_delete(kvs, key);
	} else if (size <= 0) {
		if (num > 0)
			return xfilter_kvs_insert(kvs, key, (char *)&ival, 4);
	}

	return -1;
}

int xfilter_kvs_increment(XFilterKVS *kvs, const char *key, int num)
{
	gint32 ival = 0;
	int size;
	char vbuf[4];

	g_return_val_if_fail(kvs != NULL, -1);

	size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
	if (size == 4) {
		ival = *(gint32 *)vbuf;
		ival += num;
		return xfilter_kvs_update(kvs, key, (char *)&ival, 4);
	} else if (size <= 0) {
		ival = num;
		return xfilter_kvs_insert(kvs, key, (char *)&ival, 4);
	}

	return -1;
}

int xfilter_kvs_decrement(XFilterKVS *kvs, const char *key, int num)
{
	gint32 ival = 0;
	int size;
	char vbuf[4];

	g_return_val_if_fail(kvs != NULL, -1);

	size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
	if (size == 4) {
		ival = *(gint32 *)vbuf;
		ival -= num;
		if (ival <= 0)
			return xfilter_kvs_delete(kvs, key);
		else
			return xfilter_kvs_update(kvs, key, (char *)&ival, 4);
	} else if (size <= 0) {
		return 0;
	}

	return -1;
}

static int count_func(XFilterKVS *kvs, const char *key, void *value, int size, void *data)
{
	int *sum = (int *)data;

	if (size == 4) {
		*sum += *(gint32 *)value;
	}

	return 0;
}

int xfilter_kvs_count_sum(XFilterKVS *kvs)
{
	int sum = 0;

	g_return_val_if_fail(kvs != NULL, -1);

	xfilter_kvs_foreach(kvs, count_func, &sum);
	return sum;
}