My Project
Loading...
Searching...
No Matches
sing_dbm.cc
Go to the documentation of this file.
1/****************************************
2* Computer Algebra System SINGULAR *
3****************************************/
4
5//**************************************************************************/
6// 'sing_dbm.cc' containes command to handle dbm-files under
7// Singular.
8//
9//**************************************************************************/
10
11#include "kernel/mod2.h"
12
13# include <fcntl.h>
14
15#ifdef HAVE_DBM
16
17# include "Singular/tok.h"
18# include "Singular/ipid.h"
21
22// #ifdef __CYGWIN__
23// # define USE_GDBM
24// # define BLOCKSIZE 1
25// # define GDBM_STATIC
26// # include <gdbm.h>
27// #endif
28
29#ifdef USE_GDBM
30typedef struct {
31 GDBM_FILE db; // pointer to open database
32 int first; // firstkey to look for?
33 datum actual; // the actual key
34} GDBM_info;
35
36//**************************************************************************/
37LINKAGE BOOLEAN dbOpen(si_link l, short flag, leftv u)
38{
39 char *mode = "r";
40 GDBM_info *db;
41 datum d_key;
42 // int dbm_flags = O_RDONLY | O_CREAT; // open database readonly as default
43 int read_write= GDBM_READER;
44
45 if(flag & SI_LINK_WRITE)
46 {
47 // if((l->mode==NULL)
48// || ((l->mode[0]!='w')&&(l->mode[1]!='w')))
49// {
50// // request w- open, but mode is not "w" nor "rw" => fail
51// return TRUE;
52// }
53 // dbm_flags = O_RDWR | O_CREAT;
54 read_write = GDBM_WRCREAT | GDBM_NOLOCK;
55 mode = "rw";
56 }
57 if(flag & SI_LINK_READ)
58 {
59 if (strcmp(l->mode,"rw")==0) mode="rw";
60 }
61 //if (((db = (DBM_info *)omAlloc(sizeof *db)) != NULL)
62 //&&((db->db = dbm_open(l->name, dbm_flags, 0664 )) != NULL ))
63 db = (GDBM_info *)omAlloc0(sizeof *db);
64 if((db->db = gdbm_open(l->name, BLOCKSIZE, read_write, 0664, 0)) != NULL )
65 {
66// if (db->first) // first created?
67// {
68// db->actual=gdbm_firstkey(db->db);
69// }
70// else
71// {
72// d_key=db->actual;
73// if (d_key.dptr!=NULL)
74// {
75// db->actual=gdbm_nextkey(db->db,db->actual);
76// }
77// else { db->actual=gdbm_firstkey(db->db); }
78// }
79 db->first=1;
80 if(flag & SI_LINK_WRITE)
82 else
84 l->data=(void *)(db);
85 omFreeBinAddr(l->mode);
86 l->mode=omStrDup(mode);
87 return FALSE;
88 }
89 Print("%d/%s",gdbm_errno,gdbm_strerror(gdbm_errno));
90 return TRUE;
91}
92
93//**************************************************************************/
95{
96 GDBM_info *db = (GDBM_info *)l->data;
97 gdbm_sync(db->db);
98 gdbm_close(db->db);
99 omFreeSize((ADDRESS)db,(sizeof *db));
100 l->data=NULL;
102 return FALSE;
103}
104
105//**************************************************************************/
108{
109 GDBM_info *db = (GDBM_info *)l->data;
110 // GDBM_info *db;
111// db = (GDBM_info *)omAlloc0(sizeof *db);
112// db = (GDBM_info *)l->data;
113 leftv v=NULL;
114 datum d_key;
115 int flag;
116
117 if (!SI_LINK_R_OPEN_P(l)) //exceptions
118 // if (strcmp(l->mode,"rw")==0) //rw-mode
119 {
120 if (!SI_LINK_CLOSE_P(l))
121 {
122 if (!dbClose(l)) {PrintS("cannot close link!\n");}
123 }
124 //(SI_LINK_CLOSE_P(l)) automatically
125 if (dbOpen(l, SI_LINK_READ)) return NULL;
126 }
127 if (SI_LINK_RW_OPEN_P(l)) {PrintS("I/O Error!\n");}
128
129 if(key!=NULL)
130 {
131 if (key->Typ()==STRING_CMD)
132 {
133 d_key.dptr = (char*)key->Data();
134 d_key.dsize = strlen(d_key.dptr)+1;
135 d_value = gdbm_fetch(db->db, d_key);
137 if (d_value.dptr!=NULL) v->data=omStrDup(d_value.dptr);
138 else v->data=omStrDup("");
139 v->rtyp=STRING_CMD;
140 }
141 else
142 {
143 WerrorS("read(`GDBM link`,`string`) expected");
144 }
145 }
146 else
147 {
148 if (db->first)
149 {
150 db->first=0;
151 d_key = gdbm_firstkey(db->db);
152 // db->actual=d_key;
153// Print("firstkey:%s\n",d_key.dptr);
154 }
155 else
156 {
157 if (db->actual.dptr==NULL)
158 {
159 db->actual=gdbm_firstkey(db->db);
160 }
161 d_key = gdbm_nextkey(db->db,db->actual);
162 db->actual=d_key;
163 if (d_key.dptr==NULL)
164 {
165 db->first=1;
166 // Print("nextkey:NULL\n");
167 }
168 // else
169// Print("nextkey:%s\n",d_key.dptr);
170 }
171
172 if (d_key.dptr!=NULL)
173 d_value = gdbm_fetch(db->db, d_key);
174 else
176
178 v->rtyp=STRING_CMD;
179 if (d_value.dptr!=NULL)
180 {
181 v->data=omStrDup(d_key.dptr);
182 db->first = 0;
183 }
184 else
185 {
186 v->data=omStrDup("");
187 // db->first = 1;
188 }
189
190 }
191 return v;
192}
194{
195 return dbRead2(l,NULL);
196}
197//**************************************************************************/
199{
200 GDBM_info *db = (GDBM_info *)l->data;
201 // GDBM_info *db;
202// db = (GDBM_info *)omAlloc0(sizeof *db);
203// db = (GDBM_info *)l->data;
204 BOOLEAN b=TRUE;
205 REGISTER int ret;
206
207 if (strcmp(l->mode,"rw")!=0) // r-mode
208 {
209 PrintS("Write error on readonly source\n");
210 }
211 else //rw-mode
212 {
213 if (!SI_LINK_W_OPEN_P(l)) //exceptions
214 {
215 if (!SI_LINK_CLOSE_P(l))
216 {
217 if (!dbClose(l)) {PrintS("close error\n");};
218 }
219 if (!dbOpen(l,SI_LINK_WRITE)) {PrintS("open_for_write error\n");}
220 }
221 }
222
223 if((key!=NULL) && (key->Typ()==STRING_CMD) )
224 {
225 if (key->next!=NULL) // have a second parameter ?
226 {
227 if(key->next->Typ()==STRING_CMD) // replace (key,value)
228 {
229 datum d_key, d_value;
230
231 d_key.dptr = (char *)key->Data();
232 d_key.dsize = strlen(d_key.dptr)+1;
233 d_value.dptr = (char *)key->next->Data();
234 d_value.dsize = strlen(d_value.dptr)+1;
235 ret = gdbm_store(db->db, d_key, d_value, GDBM_REPLACE);
236// db->actual=d_key;
237 if (ret==-1) {PrintS("reader calls gdbm_store!");}
238 if (ret==0)
239 { b=FALSE; }
240 else
241 {
242 // if(gdbm_error(db->db))
243 if (gdbm_errno != 0)
244 {
245 Werror("GDBM link I/O error: '%s' ", gdbm_errno);
246 // Print(gdbm_strerror(gdbm_errno));
247 //dbm_clearerr(db->db);
248 // gdbm_errno=0;
249 }
250 }
251 }
252 }
253 else
254 { // delete (key)
255 datum d_key;
256
257 d_key.dptr = (char *)key->Data();
258 d_key.dsize = strlen(d_key.dptr)+1;
259 // db->actual=gdbm_nextkey(db->db,d_key);
260 gdbm_delete(db->db, d_key);
261 b=FALSE;
262 }
263 }
264 else
265 {
266 WerrorS("write(`GDBM link`,`key string` [,`data string`]) expected");
267 }
268 gdbm_sync(db->db);
269 return b;
270}
271#endif /* USE_GDBM */
272
273#ifndef USE_GDBM
274/* These are the routines in dbm. */
275# include "ndbm.h"
276typedef struct {
277 DBM *db; // pointer to open database
278 int first; // firstkey to look for?
279} DBM_info;
280
281//**************************************************************************/
282LINKAGE BOOLEAN dbOpen(si_link l, short flag, leftv /*u*/)
283{
284 const char *mode = "r";
285 DBM_info *db;
286 int dbm_flags = O_RDONLY | O_CREAT; // open database readonly as default
287
288 if((l->mode!=NULL)
289 && ((l->mode[0]=='w')||(l->mode[1]=='w')))
290 {
291 dbm_flags = O_RDWR | O_CREAT;
292 mode = "rw";
294 }
295 else if(flag & SI_LINK_WRITE)
296 {
297 // request w- open, but mode is not "w" nor "rw" => fail
298 return TRUE;
299 }
300 //if (((db = (DBM_info *)omAlloc(sizeof *db)) != NULL)
301 //&&((db->db = dbm_open(l->name, dbm_flags, 0664 )) != NULL ))
302 db = (DBM_info *)omAlloc(sizeof *db);
303 if((db->db = dbm_open(l->name, dbm_flags, 0664 )) != NULL )
304 {
305 db->first=1;
306 if(flag & SI_LINK_WRITE)
308 else
310 l->data=(void *)(db);
311 omFreeBinAddr(l->mode);
312 l->mode=omStrDup(mode);
313 return FALSE;
314 }
315 return TRUE;
316}
317
318//**************************************************************************/
320{
321 DBM_info *db = (DBM_info *)l->data;
322
323 dbm_close(db->db);
324 omFreeSize((ADDRESS)db,(sizeof *db));
325 l->data=NULL;
327 return FALSE;
328}
329
330//**************************************************************************/
333{
334 DBM_info *db = (DBM_info *)l->data;
335 leftv v=NULL;
336 datum d_key;
337
338 if(key!=NULL)
339 {
340 if (key->Typ()==STRING_CMD)
341 {
342 d_key.dptr = (char*)key->Data();
343 d_key.dsize = strlen(d_key.dptr)+1;
344 d_value = dbm_fetch(db->db, d_key);
346 if (d_value.dptr!=NULL) v->data=omStrDup(d_value.dptr);
347 else v->data=omStrDup("");
348 v->rtyp=STRING_CMD;
349 }
350 else
351 {
352 WerrorS("read(`DBM link`,`string`) expected");
353 }
354 }
355 else
356 {
357 if(db->first)
358 d_value = dbm_firstkey((DBM *)db->db);
359 else
360 d_value = dbm_nextkey((DBM *)db->db);
361
363 v->rtyp=STRING_CMD;
364 if (d_value.dptr!=NULL)
365 {
366 v->data=omStrDup(d_value.dptr);
367 db->first = 0;
368 }
369 else
370 {
371 v->data=omStrDup("");
372 db->first = 1;
373 }
374
375 }
376 return v;
377}
379{
380 return dbRead2(l,NULL);
381}
382//**************************************************************************/
384{
385 DBM_info *db = (DBM_info *)l->data;
386 BOOLEAN b=TRUE;
387 int ret;
388
389 // database is opened
390 if((key!=NULL) && (key->Typ()==STRING_CMD) )
391 {
392 if (key->next!=NULL) // have a second parameter ?
393 {
394 if(key->next->Typ()==STRING_CMD) // replace (key,value)
395 {
396 datum d_key, d_value;
397
398 d_key.dptr = (char *)key->Data();
399 d_key.dsize = strlen(d_key.dptr)+1;
400 d_value.dptr = (char *)key->next->Data();
401 d_value.dsize = strlen(d_value.dptr)+1;
402 ret = dbm_store(db->db, d_key, d_value, DBM_REPLACE);
403 if(!ret )
404 b=FALSE;
405 else
406 {
407 if(dbm_error(db->db))
408 {
409 Werror("DBM link I/O error. Is '%s' readonly?", l->name);
410 dbm_clearerr(db->db);
411 }
412 }
413 }
414 }
415 else
416 { // delete (key)
417 datum d_key;
418
419 d_key.dptr = (char *)key->Data();
420 d_key.dsize = strlen(d_key.dptr)+1;
421 dbm_delete(db->db, d_key);
422 b=FALSE;
423 }
424 }
425 else
426 {
427 WerrorS("write(`DBM link`,`key string` [,`data string`]) expected");
428 }
429 return b;
430}
431//**************************************************************************/
432//char *dbStatus(si_link l, char *request)
433//{
434// if (strcmp(request, "read") == 0)
435// {
436// if (SI_LINK_R_OPEN_P(l))
437// return "ready";
438// else
439// return "not ready";
440// }
441// else if (strcmp(request, "write") == 0)
442// {
443// if (SI_LINK_W_OPEN_P(l))
444// return "ready";
445// else
446// return "not ready";
447// }
448// else return "unknown status request";
449//}
450//**************************************************************************/
451
452#endif /* USE_GDBM */
453#endif /* HAVE_DBM */
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
void * ADDRESS
Definition: auxiliary.h:119
int l
Definition: cfEzgcd.cc:100
CanonicalForm b
Definition: cfModGcd.cc:4103
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int Typ()
Definition: subexpr.cc:1019
void * Data()
Definition: subexpr.cc:1162
leftv next
Definition: subexpr.h:86
#define Print
Definition: emacs.cc:80
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
void WerrorS(const char *s)
Definition: feFopen.cc:24
if(!FE_OPT_NO_SHELL_FLAG)(void) system(sys)
#define STATIC_VAR
Definition: globaldefs.h:7
EXTERN_VAR omBin sleftv_bin
Definition: ipid.h:145
#define LINKAGE
Definition: mod2.h:140
int dbm_store(DBM *db, datum key, datum dat, int replace)
Definition: ndbm.cc:167
datum dbm_fetch(DBM *db, datum key)
Definition: ndbm.cc:119
int dbm_delete(DBM *db, datum key)
Definition: ndbm.cc:139
datum dbm_nextkey(DBM *db)
Definition: ndbm.cc:260
datum dbm_firstkey(DBM *db)
Definition: ndbm.cc:252
DBM * dbm_open(char *file, int flags, int mode)
Definition: ndbm.cc:59
void dbm_close(DBM *db)
Definition: ndbm.cc:97
char * dptr
Definition: ndbm.h:84
#define dbm_clearerr(db)
Definition: ndbm.h:77
#define dbm_error(db)
Definition: ndbm.h:75
#define DBM_REPLACE
Definition: ndbm.h:92
int dsize
Definition: ndbm.h:85
Definition: ndbm.h:54
Definition: ndbm.h:83
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define omFreeBinAddr(addr)
Definition: omAllocDecl.h:258
#define NULL
Definition: omList.c:12
#define REGISTER
Definition: omalloc.h:27
void PrintS(const char *s)
Definition: reporter.cc:284
void Werror(const char *fmt,...)
Definition: reporter.cc:189
LINKAGE BOOLEAN dbClose(si_link l)
Definition: sing_dbm.cc:319
LINKAGE BOOLEAN dbWrite(si_link l, leftv key)
Definition: sing_dbm.cc:383
LINKAGE leftv dbRead1(si_link l)
Definition: sing_dbm.cc:378
LINKAGE BOOLEAN dbOpen(si_link l, short flag, leftv)
Definition: sing_dbm.cc:282
int first
Definition: sing_dbm.cc:278
DBM * db
Definition: sing_dbm.cc:277
LINKAGE leftv dbRead2(si_link l, leftv key)
Definition: sing_dbm.cc:332
STATIC_VAR datum d_value
Definition: sing_dbm.cc:331
sleftv * leftv
Definition: structs.h:57
@ STRING_CMD
Definition: tok.h:185