My Project
Data Structures | Macros | Functions
ndbm.h File Reference

Go to the source code of this file.

Data Structures

struct  DBM
 
struct  datum
 

Macros

#define PBLKSIZ   1024
 
#define DBLKSIZ   4096
 
#define _DBM_RDONLY   0x01 /* data base open read-only */
 
#define _DBM_IOERR   0x02 /* data base I/O error */
 
#define dbm_rdonly(db)   ((db)->dbm_flags & _DBM_RDONLY)
 
#define dbm_error(db)   ((db)->dbm_flags & _DBM_IOERR)
 
#define dbm_clearerr(db)   ((db)->dbm_flags &= ~_DBM_IOERR)
 
#define dbm_dirfno(db)   ((db)->dbm_dirf)
 
#define dbm_pagfno(db)   ((db)->dbm_pagf)
 
#define DBM_INSERT   0
 
#define DBM_REPLACE   1
 

Functions

DBMdbm_open (char *file, int flags, int mode)
 
void dbm_close (DBM *db)
 
datum dbm_fetch (DBM *db, datum key)
 
datum dbm_firstkey (DBM *db)
 
datum dbm_nextkey (DBM *db)
 
long dbm_forder (DBM *db, datum key)
 
int dbm_delete (DBM *db, datum key)
 
int dbm_store (DBM *db, datum key, datum dat, int replace)
 

Data Structure Documentation

◆ DBM

struct DBM

Definition at line 54 of file ndbm.h.

Data Fields
long dbm_bitno
long dbm_blkno
long dbm_blkptr
long dbm_dirbno
char dbm_dirbuf[DBLKSIZ]
int dbm_dirf
int dbm_flags
long dbm_hmask
int dbm_keyptr
long dbm_maxbno
long dbm_pagbno
char dbm_pagbuf[PBLKSIZ]
int dbm_pagf

◆ datum

struct datum

Definition at line 83 of file ndbm.h.

Data Fields
char * dptr
int dsize

Macro Definition Documentation

◆ _DBM_IOERR

#define _DBM_IOERR   0x02 /* data base I/O error */

Definition at line 71 of file ndbm.h.

◆ _DBM_RDONLY

#define _DBM_RDONLY   0x01 /* data base open read-only */

Definition at line 70 of file ndbm.h.

◆ DBLKSIZ

#define DBLKSIZ   4096

Definition at line 52 of file ndbm.h.

◆ dbm_clearerr

#define dbm_clearerr (   db)    ((db)->dbm_flags &= ~_DBM_IOERR)

Definition at line 77 of file ndbm.h.

◆ dbm_dirfno

#define dbm_dirfno (   db)    ((db)->dbm_dirf)

Definition at line 80 of file ndbm.h.

◆ dbm_error

#define dbm_error (   db)    ((db)->dbm_flags & _DBM_IOERR)

Definition at line 75 of file ndbm.h.

◆ DBM_INSERT

#define DBM_INSERT   0

Definition at line 91 of file ndbm.h.

◆ dbm_pagfno

#define dbm_pagfno (   db)    ((db)->dbm_pagf)

Definition at line 81 of file ndbm.h.

◆ dbm_rdonly

#define dbm_rdonly (   db)    ((db)->dbm_flags & _DBM_RDONLY)

Definition at line 73 of file ndbm.h.

◆ DBM_REPLACE

#define DBM_REPLACE   1

Definition at line 92 of file ndbm.h.

◆ PBLKSIZ

#define PBLKSIZ   1024

Definition at line 50 of file ndbm.h.

Function Documentation

◆ dbm_close()

void dbm_close ( DBM db)

Definition at line 97 of file ndbm.cc.

98{
99 (void) si_close(db->dbm_dirf);
100 (void) si_close(db->dbm_pagf);
101 free((char *)db);
102}
int dbm_pagf
Definition: ndbm.h:56
int dbm_dirf
Definition: ndbm.h:55
#define free
Definition: omAllocFunc.c:14

◆ dbm_delete()

int dbm_delete ( DBM db,
datum  key 
)

Definition at line 139 of file ndbm.cc.

140{
141 int i;
142 // datum item;
143
144 if (dbm_error(db))
145 return (-1);
146 if (dbm_rdonly(db))
147 {
148 errno = EPERM;
149 return (-1);
150 }
151 dbm_access(db, dcalchash(key));
152 if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
153 return (-1);
154 if (!delitem(db->dbm_pagbuf, i))
155 goto err;
156 db->dbm_pagbno = db->dbm_blkno;
157 (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
158 if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
159 {
160 err:
161 db->dbm_flags |= _DBM_IOERR;
162 return (-1);
163 }
164 return (0);
165}
int i
Definition: cfEzgcd.cc:132
#define SEEK_SET
Definition: mod2.h:115
static void dbm_access(DBM *db, long hash)
Definition: ndbm.cc:304
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:467
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:394
static long dcalchash(datum item)
Definition: ndbm.cc:442
#define _DBM_IOERR
Definition: ndbm.h:71
#define dbm_rdonly(db)
Definition: ndbm.h:73
#define dbm_error(db)
Definition: ndbm.h:75
long dbm_pagbno
Definition: ndbm.h:64
int dbm_flags
Definition: ndbm.h:57
long dbm_blkno
Definition: ndbm.h:63
#define PBLKSIZ
Definition: ndbm.h:50
char dbm_pagbuf[PBLKSIZ]
Definition: ndbm.h:65

◆ dbm_fetch()

datum dbm_fetch ( DBM db,
datum  key 
)

Definition at line 119 of file ndbm.cc.

120{
121 int i;
122 datum item;
123
124 if (dbm_error(db))
125 goto err;
126 dbm_access(db, dcalchash(key));
127 if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
128 {
129 item = makdatum(db->dbm_pagbuf, i+1);
130 if (item.dptr != NULL)
131 return (item);
132 }
133err:
134 item.dptr = NULL;
135 item.dsize = 0;
136 return (item);
137}
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:373
char * dptr
Definition: ndbm.h:84
int dsize
Definition: ndbm.h:85
Definition: ndbm.h:83
#define NULL
Definition: omList.c:12

◆ dbm_firstkey()

datum dbm_firstkey ( DBM db)

Definition at line 252 of file ndbm.cc.

253{
254
255 db->dbm_blkptr = 0L;
256 db->dbm_keyptr = 0;
257 return (dbm_nextkey(db));
258}
datum dbm_nextkey(DBM *db)
Definition: ndbm.cc:260
int dbm_keyptr
Definition: ndbm.h:62
long dbm_blkptr
Definition: ndbm.h:61

◆ dbm_forder()

long dbm_forder ( DBM db,
datum  key 
)

Definition at line 104 of file ndbm.cc.

105{
106 long hash;
107
108 hash = dcalchash(key);
109 for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
110 {
111 db->dbm_blkno = hash & db->dbm_hmask;
112 db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
113 if (getbit(db) == 0)
114 break;
115 }
116 return (db->dbm_blkno);
117}
static int getbit(DBM *db)
Definition: ndbm.cc:326
long dbm_hmask
Definition: ndbm.h:60
long dbm_bitno
Definition: ndbm.h:59

◆ dbm_nextkey()

datum dbm_nextkey ( DBM db)

Definition at line 260 of file ndbm.cc.

261{
262 struct stat statb;
263 datum item;
264
265 if (dbm_error(db)
266 || singular_fstat(db->dbm_pagf, &statb) < 0
267 )
268 goto err;
269 statb.st_size /= PBLKSIZ;
270 for (;;)
271 {
272 if (db->dbm_blkptr != db->dbm_pagbno)
273 {
274 db->dbm_pagbno = db->dbm_blkptr;
275 (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, SEEK_SET);
276 if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
277 memset(db->dbm_pagbuf, 0, PBLKSIZ);
278#ifdef DEBUG
279 else if (chkblk(db->dbm_pagbuf) < 0)
280 db->dbm_flags |= _DBM_IOERR;
281#endif
282 }
283 short tmp;
284 memcpy(&tmp, db->dbm_pagbuf, sizeof(tmp));
285 if (tmp != 0)
286 {
287 item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
288 if (item.dptr != NULL)
289 {
290 db->dbm_keyptr += 2;
291 return (item);
292 }
293 db->dbm_keyptr = 0;
294 }
295 if (++db->dbm_blkptr >= statb.st_size)
296 break;
297 }
298err:
299 item.dptr = NULL;
300 item.dsize = 0;
301 return (item);
302}
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1082

◆ dbm_open()

DBM * dbm_open ( char *  file,
int  flags,
int  mode 
)

Definition at line 59 of file ndbm.cc.

60{
61 struct stat statb;
62 DBM *db;
63
64 if ((db = (DBM *)malloc(sizeof *db)) == 0)
65 {
66 errno = ENOMEM;
67 return ((DBM *)0);
68 }
69#ifdef MSDOS
70 // default mode of open is ascii, we need binary mode.
71 flags |= O_BINARY;
72#endif
73 db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
74 if ((flags & 03) == O_WRONLY)
75 flags = (flags & ~03) | O_RDWR;
76 strcpy(db->dbm_pagbuf, file);
77 strcat(db->dbm_pagbuf, ".pag");
78 db->dbm_pagf = si_open(db->dbm_pagbuf, flags, mode);
79 if (db->dbm_pagf < 0)
80 goto bad;
81 strcpy(db->dbm_pagbuf, file);
82 strcat(db->dbm_pagbuf, ".dir");
83 db->dbm_dirf = si_open(db->dbm_pagbuf, flags, mode);
84 if (db->dbm_dirf < 0)
85 goto bad1;
86 singular_fstat(db->dbm_dirf, &statb);
87 db->dbm_maxbno = statb.st_size*BYTESIZ-1;
88 db->dbm_pagbno = db->dbm_dirbno = -1;
89 return (db);
90bad1:
91 (void) si_close(db->dbm_pagf);
92bad:
93 free((char *)db);
94 return ((DBM *)0);
95}
bool bad
Definition: facFactorize.cc:64
#define BYTESIZ
Definition: ndbm.cc:46
#define _DBM_RDONLY
Definition: ndbm.h:70
Definition: ndbm.h:54
void * malloc(size_t size)
Definition: omalloc.c:85
#define si_open(...)
int status int void size_t count int const void size_t count const char int flags
Definition: si_signals.h:73

◆ dbm_store()

int dbm_store ( DBM db,
datum  key,
datum  dat,
int  replace 
)

Definition at line 167 of file ndbm.cc.

168{
169 int i;
170 int ret;
171 datum item, item1;
172 char ovfbuf[PBLKSIZ];
173
174 if (dbm_error(db))
175 return (-1);
176 if (dbm_rdonly(db))
177 {
178 errno = EPERM;
179 return (-1);
180 }
181
182_loop:
183 dbm_access(db, dcalchash(key));
184 if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
185 {
186 if (!replace)
187 return (1);
188 if (!delitem(db->dbm_pagbuf, i))
189 {
190 db->dbm_flags |= _DBM_IOERR;
191 return (-1);
192 }
193 }
194 if (!additem(db->dbm_pagbuf, key, dat))
195 goto split;
196 db->dbm_pagbno = db->dbm_blkno;
197 (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
198 if ( (ret=si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ)
199 {
200 db->dbm_flags |= _DBM_IOERR;
201 return (-1);
202 }
203 return (0);
204
205split:
206 if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ)
207 {
208 db->dbm_flags |= _DBM_IOERR;
209 errno = ENOSPC;
210 return (-1);
211 }
212 memset(ovfbuf, 0, PBLKSIZ);
213 for (i=0;;) {
214 item = makdatum(db->dbm_pagbuf, i);
215 if (item.dptr == NULL)
216 break;
217 if (dcalchash(item) & (db->dbm_hmask+1))
218 {
219 item1 = makdatum(db->dbm_pagbuf, i+1);
220 if (item1.dptr == NULL) {
221 fprintf(stderr, "ndbm: split not paired\n");
222 db->dbm_flags |= _DBM_IOERR;
223 break;
224 }
225 if (!additem(ovfbuf, item, item1) ||
226 !delitem(db->dbm_pagbuf, i))
227 {
228 db->dbm_flags |= _DBM_IOERR;
229 return (-1);
230 }
231 continue;
232 }
233 i += 2;
234 }
235 db->dbm_pagbno = db->dbm_blkno;
236 (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
237 if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
238 {
239 db->dbm_flags |= _DBM_IOERR;
240 return (-1);
241 }
242 (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, SEEK_SET);
243 if (si_write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ)
244 {
245 db->dbm_flags |= _DBM_IOERR;
246 return (-1);
247 }
248 setbit(db);
249 goto _loop;
250}
static CFList split(const CanonicalForm &F, const int m, const Variable &x)
Definition: facMul.cc:3469
static int additem(char buf[PBLKSIZ], datum item, datum item1)
Definition: ndbm.cc:499
static void setbit(DBM *db)
Definition: ndbm.cc:348