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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* gdbm.c: Elk/GDBM-interface.
* Original version by Martin Stut <stut@informatik.tu-muenchen.dbp.de>.
*
* $Id$
*
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
* Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
*
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
*
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
* owners or individual owners of copyright in this software, grant to any
* person or company a worldwide, royalty free, license to
*
* i) copy this software,
* ii) prepare derivative works based on this software,
* iii) distribute copies of this software or derivative works,
* iv) perform this software, or
* v) display this software,
*
* provided that this notice is not removed and that neither Oliver Laumann
* nor Teles nor Nixdorf are deemed to have made any representations as to
* the suitability of this software for any purpose nor are held responsible
* for any defects of this software.
*
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
*/
/* Functions exported:
*
* (gdbm-file? obj)
*
* Type predicate for the newly defined type gdbm-file.
*
* (gdbm-open filename block-size type [filemode])
*
* Opens a gdbm file and returns an object of type gdbm-file.
* Returns #f if file cannot be opened.
* filename is a string or a symbol, block-size is an integer,
* type is one of the symbols 'reader, 'writer, 'create, and 'new,
* the optional file mode is an integer (default: #o644).
*
* (gdbm-close gf)
*
* Closes a gdbm file. Attempts to use a closed gdbm file as
* an argument to any gdbm-function causes the error message
* "invalid gdbm-file" to be displayed.
*
* (gdbm-store gf key data mode)
*
* Stores an item in the gdbm file pointed to by gf.
* key and data are strings, mode is a symbol (either 'insert
* or 'replace).
* Returns -1 if called by a reader, 1 if called with 'insert and
* the key is already stored, 0 otherwise.
*
* (gdbm-fetch gf key)
*
* Searches the gdbm file pointed to by gf for data stored under
* the given key and returns the data as a string.
* Returns #f if nothing is stored under that key.
*
* (gdbm-delete gf key)
*
* Removes data stored under the specified key from the gdbm file gf.
* Returns #f if the key is not present in the gdbm file, #t otherwise.
*
* (gdbm-firstkey gf)
* (gdbm-nextkey gf key)
*
* These functions are used to access all items in a gdbm file.
* Both return a key. gdbm-firstkey returns #f if the gdbm file
* is empty; gdbm-nextkey returns #f if there is no next key.
*
* (gdbm-reorganize gf)
*
* Shortens the specified gdbm file (reclaims deleted space).
*
* (gdbm-error)
*
* Returns a cons cell; the car is the last error number set by
* the gdbm library, the cdr is the current UNIX errno.
*
* (gdbm-error-text)
*
* Returns the last error message passed to the fatal error
* function by the gdbm library (a string).
*
* Loading gdbm.o provides the symbol 'gdbm.o.
*/
#include "config.h"
#include <gdbm.h>
#include <errno.h>
#include <string.h>
#include "scheme.h"
extern gdbm_error gdbm_errno;
extern int errno;
static char *gdbm_error_message = "";
static SYMDESCR RW_Syms[] = {
{ "reader", GDBM_READER },
{ "writer", GDBM_WRITER },
{ "create", GDBM_WRCREAT },
{ "new", GDBM_NEWDB },
{ 0, 0 }
};
static SYMDESCR Flag_Syms[] = {
{ "insert", GDBM_INSERT },
{ "replace", GDBM_REPLACE },
{ 0, 0 }
};
int T_Gdbm_fh;
struct S_gdbm_fh{
Object tag;
GDBM_FILE fptr;
char free;
};
#define GDBM_FH(obj) ((struct S_gdbm_fh *)POINTER(obj))
int Gdbm_fh_Equal (Object a, Object b) {
return !GDBM_FH(a)->free && !GDBM_FH(b)->free &&
GDBM_FH(a)->fptr == GDBM_FH(b)->fptr;
}
/*ARGSUSED*/
int Gdbm_fh_Print (Object fh, Object port, int raw, int depth, int len) {
Printf (port, "#[gdbm-file %lu]", GDBM_FH(fh)->fptr);
return 0;
}
Object P_Gdbm_filep (Object x) {
return TYPE(x) == T_Gdbm_fh ? True : False;
}
static void Fatal_Func (char *s) {
gdbm_error_message = s;
fprintf (stderr, "gdbm error: %s\n", s);
}
Object P_Gdbm_Open (int argc, Object *argv) {
Object Gdbm_fh;
GDBM_FILE dbf;
Disable_Interrupts;
dbf = gdbm_open (Get_Strsym (argv[0]), Get_Integer (argv[1]),
Symbols_To_Bits (argv[2], 0, RW_Syms),
argc == 4 ? Get_Integer (argv[3]) : 0644, Fatal_Func);
if (dbf == 0) {
Enable_Interrupts;
return False;
}
Gdbm_fh = Alloc_Object (sizeof (struct S_gdbm_fh), T_Gdbm_fh, 0);
GDBM_FH (Gdbm_fh)->tag = Null;
GDBM_FH (Gdbm_fh)->fptr = dbf;
GDBM_FH (Gdbm_fh)->free = 0;
Enable_Interrupts;
return Gdbm_fh;
}
void Check_Fh (Object fh) {
Check_Type (fh, T_Gdbm_fh);
if (GDBM_FH(fh)->free)
Primitive_Error ("invalid gdbm-file: ~s", fh);
}
Object P_Gdbm_Close (Object fh) {
Check_Fh (fh);
GDBM_FH(fh)->free = 1;
Disable_Interrupts;
gdbm_close (GDBM_FH(fh)->fptr);
Enable_Interrupts;
return Void;
}
Object P_Gdbm_Store (Object fh, Object key, Object content, Object flag) {
int res;
datum k, c;
Check_Fh (fh);
Check_Type (key, T_String);
Check_Type (content, T_String);
k.dptr = STRING(key)->data;
k.dsize = STRING(key)->size;
c.dptr = STRING(content)->data;
c.dsize = STRING(content)->size;
Disable_Interrupts;
res = gdbm_store (GDBM_FH(fh)->fptr, k, c,
Symbols_To_Bits (flag, 0, Flag_Syms));
Enable_Interrupts;
return Make_Integer (res);
}
static Object Gdbm_Get (Object fh, Object key, datum (*func)()) {
Object res;
datum k, c;
Check_Fh (fh);
Check_Type (key, T_String);
k.dptr = STRING(key)->data;
k.dsize = STRING(key)->size;
Disable_Interrupts;
c = (*func) (GDBM_FH(fh)->fptr, k);
Enable_Interrupts;
if (c.dptr == 0)
return False;
res = Make_String (c.dptr, c.dsize);
free (c.dptr);
return res;
}
Object P_Gdbm_Fetch (Object fh, Object key) {
return Gdbm_Get (fh, key, gdbm_fetch);
}
Object P_Gdbm_Nextkey (Object fh, Object key) {
return Gdbm_Get (fh, key, gdbm_nextkey);
}
Object P_Gdbm_Delete (Object fh, Object key) {
int res;
datum k;
Check_Fh (fh);
Check_Type (key, T_String);
k.dptr = STRING(key)->data;
k.dsize = STRING(key)->size;
Disable_Interrupts;
res = gdbm_delete (GDBM_FH(fh)->fptr, k);
Enable_Interrupts;
return res == 0 ? True : False;
}
Object P_Gdbm_Firstkey (Object fh) {
Object res;
datum k;
Check_Fh (fh);
Disable_Interrupts;
k = gdbm_firstkey (GDBM_FH(fh)->fptr);
Enable_Interrupts;
if (k.dptr == 0)
return False;
res = Make_String (k.dptr, k.dsize);
free (k.dptr);
return res;
}
Object P_Gdbm_Reorganize (Object fh) {
Check_Fh (fh);
Disable_Interrupts;
gdbm_reorganize (GDBM_FH(fh)->fptr);
Enable_Interrupts;
return Void;
}
Object P_Gdbm_Error () {
return Cons (Make_Integer ((int)gdbm_errno), Make_Integer (errno));
}
Object P_Gdbm_Error_Text () {
return Make_String (gdbm_error_message, strlen (gdbm_error_message));
}
void elk_init_lib_gdbm () {
Define_Primitive (P_Gdbm_Open, "gdbm-open", 3, 4, VARARGS);
Define_Primitive (P_Gdbm_filep, "gdbm-file?", 1, 1, EVAL);
Define_Primitive (P_Gdbm_Close, "gdbm-close", 1, 1, EVAL);
Define_Primitive (P_Gdbm_Store, "gdbm-store", 4, 4, EVAL);
Define_Primitive (P_Gdbm_Fetch, "gdbm-fetch", 2, 2, EVAL);
Define_Primitive (P_Gdbm_Delete, "gdbm-delete", 2, 2, EVAL);
Define_Primitive (P_Gdbm_Firstkey, "gdbm-firstkey", 1, 1, EVAL);
Define_Primitive (P_Gdbm_Nextkey, "gdbm-nextkey", 2, 2, EVAL);
Define_Primitive (P_Gdbm_Reorganize, "gdbm-reorganize", 1, 1, EVAL);
Define_Primitive (P_Gdbm_Error, "gdbm-error", 0, 0, EVAL);
Define_Primitive (P_Gdbm_Error_Text, "gdbm-error-text", 0, 0, EVAL);
T_Gdbm_fh = Define_Type (0, "gdbm-file", NOFUNC,
sizeof (struct S_gdbm_fh), Gdbm_fh_Equal, Gdbm_fh_Equal,
Gdbm_fh_Print, NOFUNC);
P_Provide (Intern ("gdbm.la"));
}
|