/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- bsddb_print
- bsddb_allocate
- db_name_get
- db_name_set
- db_type_get
- db_type_set
- Scm_Init_bdbm
1 /*
2 * bsddb.c - BSD DB interface
3 *
4 * Copyright(C) 2001 by Shiro Kawai (shiro@acm.org)
5 *
6 * Permission to use, copy, modify, distribute this software and
7 * accompanying documentation for any purpose is hereby granted,
8 * provided that existing copyright notices are retained in all
9 * copies and that this notice is included verbatim in all
10 * distributions.
11 * This software is provided as is, without express or implied
12 * warranty. In no circumstances the author(s) shall be liable
13 * for any damages arising out of the use of this software.
14 *
15 * $Id: bdbm.c,v 1.3 2005/07/22 09:26:54 shirok Exp $
16 */
17
18 #include "bsddb.h"
19 #include <gauche/class.h>
20
21 static ScmObj bsddb_allocate(ScmClass *klass, ScmObj initargs);
22 static void bsddb_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx);
23
24 SCM_DEFINE_BUILTIN_CLASS(Scm_BsdDbClass,
25 bsddb_print, NULL, NULL,
26 bsddb_allocate,
27 SCM_CLASS_COLLECTION_CPL);
28
29 static void bsddb_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx)
30 {
31 char *type;
32 switch (SCM_BSD_DB_TYPE(obj)) {
33 case DB_BTREE: type = "btree"; break;
34 case DB_HASH: type = "hash"; break;
35 case DB_RECNO: type = "recno"; break;
36 default: type = "unknown"; break;
37 }
38 Scm_Printf(port, "#<bsd-db:%s %S>", type, SCM_BSD_DB(obj)->name);
39 }
40
41 static ScmObj bsddb_allocate(ScmClass *klass, ScmObj initargs)
42 {
43 ScmBsdDb *db = SCM_NEW(ScmBsdDb);
44 SCM_SET_CLASS(db, SCM_CLASS_BSD_DB);
45 db->name = SCM_FALSE;
46 db->db = NULL;
47 db->type = DB_UNKNOWN;
48 db->flags = 0;
49 db->cachesize = 0;
50 db->maxkeypage = 0;
51 db->minkeypage = 0;
52 db->psize = 0;
53 db->lorder = 0;
54 db->bsize = 0;
55 db->ffactor = 0;
56 db->nelem = 0;
57 db->reclen = 0;
58 db->bval = 0;
59 return SCM_OBJ(db);
60 }
61
62 static ScmObj db_name_get(ScmBsdDb *db)
63 {
64 return db->name;
65 }
66
67 static void db_name_set(ScmBsdDb *db, ScmObj obj)
68 {
69 if (!SCM_STRINGPP(obj)) {
70 Scm_Error("string required, but got %S", obj);
71 }
72 db->name = obj;
73 }
74
75 static ScmObj db_type_get(ScmBsdDb *db)
76 {
77 return SCM_MAKE_INT(db->type);
78 }
79
80 static void db_type_set(ScmBsdDb *db, ScmObj obj)
81 {
82 if (!SCM_INTP(obj)) {
83 Scm_Error("small integer required, but got %S", obj);
84 }
85 switch (SCM_INT_VALUE(obj)) {
86 case DB_BTREE:;
87 case DB_HASH:;
88 case DB_RECNO:;
89 case DB_UNKNOWN: db->type = SCM_INT_VALUE(obj); break;
90 default:
91 Scm_Error("invalid BSD DB type: %S", obj);
92 }
93 }
94
95 #define CAT2(a, b) a##b
96 #define CAT3(a, b, c) a##b##c
97
98 #define INTEGER_SLOT(name) \
99 static ScmObj CAT3(db_, name, _get)(ScmBsdDb *db) \
100 { \
101 return SCM_MAKE_INT(db->name); \
102 } \
103 static void CAT3(db_, name, _set)(ScmBsdDb *db, ScmObj obj) \
104 { \
105 if (!SCM_INTP(obj)) { \
106 Scm_Error("small integer required, but got %S", obj); \
107 } \
108 db->name = SCM_INT_VALUE(obj); \
109 }
110
111 INTEGER_SLOT(flags)
112 INTEGER_SLOT(cachesize)
113 INTEGER_SLOT(maxkeypage)
114 INTEGER_SLOT(minkeypage)
115 INTEGER_SLOT(psize)
116 INTEGER_SLOT(lorder)
117 INTEGER_SLOT(bsize)
118 INTEGER_SLOT(ffactor)
119 INTEGER_SLOT(nelem)
120 INTEGER_SLOT(reclen)
121 INTEGER_SLOT(bval)
122
123 static ScmClassStaticSlotSpec db_slots[] = {
124 SCM_CLASS_SLOT_SPEC("name", db_name_get, db_name_set),
125 SCM_CLASS_SLOT_SPEC("type", db_type_get, db_type_set),
126 SCM_CLASS_SLOT_SPEC("flags", db_flags_get, db_flags_set),
127 SCM_CLASS_SLOT_SPEC("cachesize", db_cachesize_get, db_cachesize_set),
128 SCM_CLASS_SLOT_SPEC("maxkeypage", db_maxkeypage_get, db_maxkeypage_set),
129 SCM_CLASS_SLOT_SPEC("minkeypage", db_minkeypage_get, db_minkeypage_set),
130 SCM_CLASS_SLOT_SPEC("psize", db_psize_get, db_psize_set),
131 SCM_CLASS_SLOT_SPEC("lorder", db_lorder_get, db_lorder_set),
132 SCM_CLASS_SLOT_SPEC("bsize", db_bsize_get, db_bsize_set),
133 SCM_CLASS_SLOT_SPEC("ffactor", db_ffactor_get, db_ffactor_set),
134 SCM_CLASS_SLOT_SPEC("nelem", db_nelem_get, db_nelem_set),
135 SCM_CLASS_SLOT_SPEC("reclen", db_reclen_get, db_reclen_set),
136 SCM_CLASS_SLOT_SPEC("bval", db_bval_get, db_bval_set),
137 { NULL }
138 };
139
140 void Scm_Init_bdbm(void)
141 {
142 ScmModule *mod = SCM_FIND_MODULE("dbm.bdbm", SCM_FIND_MODULE_CREATE);
143 Scm_InitStaticClass(&Scm_BsdDbClass, "<bsd-db>",
144 mod, db_slots, 0);
145 Scm_Init_bsddb(mod);
146 }