File: tcl_dbm.c

package info (click to toggle)
db 2%3A2.4.14-2.7.7.1.c
  • links: PTS
  • area: main
  • in suites: potato
  • size: 12,716 kB
  • ctags: 9,382
  • sloc: ansic: 35,556; tcl: 8,564; cpp: 4,890; sh: 2,075; makefile: 1,723; java: 1,632; sed: 419; awk: 153; asm: 41
file content (200 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download | duplicates (2)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998
 *	Sleepycat Software.  All rights reserved.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)tcl_dbm.c	10.8 (Sleepycat) 4/27/98";
#endif /* not lint */

/*
 * This file is divided up into 2 sets of functions:
 * 1. The dbminit command.
 * 3. The dbm support functions (e.g. fetch, store, delete)
 */
#include <string.h>
#include <tcl.h>

#define	DB_DBM_HSEARCH	1
#include <db2/db.h>

#include "dbtest.h"
#include "test_ext.h"

/*
 * dbminit_cmd --
 *	Implements dbminit for dbtest.  Dbminit creates a widget that
 * implements all the commands found off in the historical dbm interface.
 */


#define DBMINIT_USAGE "dbminit file"

int
dbminit_cmd(notused, interp, argc, argv)
	ClientData notused;
	Tcl_Interp *interp;
	int argc;
	char *argv[];
{
	char *name;
	int ret;

	notused = NULL;

	/* Check number of arguments. */
	USAGE_GE(argc, 2, DBMINIT_USAGE, 0);

	name = strcmp(argv[1], "NULL") == 0 ? NULL : argv[1];

	debug_check();

	/* Call dbminit. */
	ret = dbminit(name);
	if (ret != 0) {
		Tcl_SetResult(interp, "dbminit: ", TCL_STATIC);
		Tcl_AppendResult(interp, Tcl_PosixError(interp), NULL);
		return (TCL_OK);
	}

	Tcl_SetResult(interp, "0", TCL_STATIC);
	return (TCL_OK);
}

/* DBM support functions. */

#define DBMDEL_USAGE "delete key"
int
dbm_delete_cmd(notused, interp, argc, argv)
	ClientData notused;
	Tcl_Interp *interp;
	int argc;
	char *argv[];
{
	datum key;

	USAGE(argc, 2, DBMDEL_USAGE, 0);
	notused = NULL;

	key.dptr = argv[1];
	key.dsize = (int)strlen(argv[1]) + 1;	/* Add NULL on end. */

	debug_check();

	if (delete(key) == 0)
		Tcl_SetResult(interp, "0", TCL_STATIC);
	else
		Tcl_SetResult(interp, "-1", TCL_STATIC);

	return (TCL_OK);
}

#define DBMFETCH_USAGE "fetch key"
int
dbm_fetch_cmd(notused, interp, argc, argv)
	ClientData notused;
	Tcl_Interp *interp;
	int argc;
	char *argv[];
{
	datum data, key;

	USAGE_GE(argc, 2, DBMFETCH_USAGE, 0);
	notused = NULL;

	key.dptr = argv[1];
	key.dsize = strlen(argv[1]) + 1;	/* Add Null on end */

	debug_check();

	data = fetch(key);
	if (data.dptr == NULL)
		Tcl_SetResult(interp, "-1", TCL_STATIC);
	else
		Tcl_SetResult(interp, data.dptr, TCL_VOLATILE);
	return (TCL_OK);
}


#define DBMSTORE_USAGE "store key data"

int
dbm_store_cmd(notused, interp, argc, argv)
	ClientData notused;
	Tcl_Interp *interp;
	int argc;
	char *argv[];
{
	datum data, key;

	USAGE_GE(argc, 3, DBMSTORE_USAGE, 0);
	notused = NULL;

	key.dptr = argv[1];
	key.dsize = strlen(argv[1]) + 1;	/* Add Null on end */

	data.dptr = argv[2];
	data.dsize = strlen(argv[2]) + 1;

	debug_check();

	if (store(key, data) == 0)
		Tcl_SetResult(interp, "0", TCL_STATIC);
	else
		Tcl_SetResult(interp, "-1", TCL_STATIC);
	return (TCL_OK);
}

#define DBMFIRST_USAGE "firstkey"
int
dbm_first_cmd(notused, interp, argc, argv)
	ClientData notused;
	Tcl_Interp *interp;
	int argc;
	char *argv[];
{
	datum key;

	USAGE(argc, 1, DBMFIRST_USAGE, 0);
	argv = argv;
	notused = NULL;

	key = firstkey();

	if (key.dptr != 0)
		Tcl_SetResult(interp, key.dptr, TCL_VOLATILE);
	else
		Tcl_SetResult(interp, "-1", TCL_STATIC);

	return (TCL_OK);
}

#define DBMNEXT_USAGE "nextkey key"
int
dbm_next_cmd(notused, interp, argc, argv)
	ClientData notused;
	Tcl_Interp *interp;
	int argc;
	char *argv[];
{
	datum nkey, key;

	USAGE(argc, 2, DBMNEXT_USAGE, 0);
	notused = NULL;

	key.dptr = argv[1];
	key.dsize = strlen(argv[1]) + 1;

	nkey = nextkey(key);

	if (nkey.dptr != 0)
		Tcl_SetResult(interp, nkey.dptr, TCL_VOLATILE);
	else
		Tcl_SetResult(interp, "-1", TCL_STATIC);

	return (TCL_OK);
}