summaryrefslogtreecommitdiffabout
path: root/lib/gdbmmap.c
blob: fd972f28a1a61435698007fa9f1dfab546538c83 (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
137
138
139
140
141
142
143
144
145
146
147
148
/* This file is part of Eclat.
   Copyright (C) 2012-2014 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "libeclat.h"
#include <gdbm.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

struct gdbm_map {
	char *name;
	GDBM_FILE file;
	struct grecs_locus locus;
	int nullflag;
};

static struct grecs_keyword gdbm_map_kw[] = {
	{ "type", "'gdbm", "Set map type",
	  grecs_type_null },
	{ "key", "<arg: string>", "key expression",
	  grecs_type_null },
	{ "file", NULL, "Database file name",
	  grecs_type_string, GRECS_DFLT, NULL,
	  offsetof(struct gdbm_map, name) },
	{ "null", NULL, "Count terminating null as part of the key",
	  grecs_type_bool, GRECS_DFLT, NULL,
	  offsetof(struct gdbm_map, nullflag) },
	{ NULL }
};

static void
gdbm_map_free(int dbg, void *data)
{
	struct gdbm_map *gdbm_map = data;
	free(gdbm_map->name);
	free(gdbm_map);
}

static void
gdbm_map_confhelp()
{
	static struct grecs_keyword gdbm_map_top[] = {
		{ "map", "name: string",
		  "Configuration for a GDBM map",
		  grecs_type_section, GRECS_INAC, NULL, 0, NULL, NULL,
		  gdbm_map_kw },
		{ NULL }
	};
	grecs_print_statement_array(gdbm_map_top, 1, 0, stdout);
}

static int
gdbm_map_config(int dbg, struct grecs_node *node, void *data)
{
	struct gdbm_map *gdbm_map, **return_gdbm_map = data;
	struct grecs_node *p;
	int i;

	gdbm_map = grecs_malloc(sizeof(*gdbm_map));
	for (i = 0; gdbm_map_kw[i].ident; i++)
		gdbm_map_kw[i].varptr = gdbm_map;
	if (grecs_tree_process(node->down, gdbm_map_kw)) {
		gdbm_map_free(dbg, gdbm_map);
		return eclat_map_failure;
	}
	if (!gdbm_map->name) {
		grecs_error(&node->locus, 0, "file not specified");
		gdbm_map_free(dbg, gdbm_map);
		return eclat_map_failure;
	}
	
	if (eclat_get_string_node(node, "file", 0, &p))
		abort();
	gdbm_map->locus = p->locus;
	*return_gdbm_map = gdbm_map;
	return eclat_map_ok;
}

static int
gdbm_map_open(int dbg, void *data)
{
	struct gdbm_map *gdbm_map = data;

	gdbm_map->file = gdbm_open(gdbm_map->name, 512, GDBM_READER, 0, NULL);
	if (gdbm_map->file == NULL) {
		err("cannot open database: %s", gdbm_strerror(gdbm_errno));
		return eclat_map_failure;
	}
	return eclat_map_ok;
}

static int
gdbm_map_close(int dbg, void *data)
{
	struct gdbm_map *gdbm_map = data;
	gdbm_close(gdbm_map->file);
	return eclat_map_ok;
}

static int
gdbm_map_get(int dbg, int dir, void *data, const char *key,
	     char **return_value)
{
	struct gdbm_map *gdbm_map = data;
	datum keydat, content;

	if (dir == MAP_REV)
		return eclat_map_bad_dir;

	keydat.dptr = (char*)key;
	keydat.dsize = strlen(key) + (gdbm_map->nullflag ? 1 : 0);
	gdbm_errno = 0;
	content = gdbm_fetch(gdbm_map->file, keydat);
	if (content.dptr == NULL) {
		if (gdbm_errno == GDBM_ITEM_NOT_FOUND)
			return eclat_map_not_found;
		err("%s: %s", gdbm_map->name, gdbm_strerror(gdbm_errno));
		return eclat_map_failure;
	}
	*return_value = grecs_malloc(content.dsize + 1);
	memcpy(*return_value, content.dptr, content.dsize);
	(*return_value)[content.dsize] = 0;
	free (content.dptr);
	return eclat_map_ok;
}

struct eclat_map_drv eclat_map_drv_gdbm = {
	"gdbm",
	gdbm_map_config,
	gdbm_map_open,
	gdbm_map_close,
	gdbm_map_get,
	gdbm_map_free,
	gdbm_map_confhelp
};

Return to:

Send suggestions and report system problems to the System administrator.