source: git/Singular/ndbm.cc @ 48ce9a

spielwiese
Last change on this file since 48ce9a was 48ce9a, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: removed MWERKS and macintosh (Mac 9) code git-svn-id: file:///usr/local/Singular/svn/trunk@8458 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5//**************************************************************************/
6//
7// $Id: ndbm.cc,v 1.16 2005-07-27 09:59:27 Singular Exp $
8//
9//**************************************************************************/
10// 'ndbm.cc' containes all low-level functions to manipulate dbm-files
11// for the original Copyright of this file and 'ndbm.h' see below .
12//
13// some minor change where needed to compile and run under MacOS/MPW
14//
15//**************************************************************************/
16
17#include "mod2.h"
18#ifdef HAVE_DBM
19#ifndef HPUX_9
20#include <strings.h>
21#endif
22/*
23 * Copyright (c) 1983 Regents of the University of California.
24 * All rights reserved.  The Berkeley software License Agreement
25 * specifies the terms and conditions for redistribution.
26 */
27
28#if defined(LIBC_SCCS) && !defined(lint)
29static char sccsid[] = "@(#)ndbm.c        5.3 (Berkeley) 3/9/86";
30#endif LIBC_SCCS and not lint
31
32//**************************************************************************/
33
34#include <stdio.h>
35/* alternative:
36* #   define EPERM 1
37* #   define ENOMEM 23
38* #   define ENOSPC 28
39* #   define L_SET SEEK_SET
40*/
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <sys/file.h>
44#include <errno.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <fcntl.h>
49#ifndef HAVE_BCOPY
50#   define bcopy(a,b,c) memmove(b,a,c)
51#endif /* not HAVE_BCOPY */
52#include "ndbm.h"
53
54#define BYTESIZ 8
55#undef setbit
56
57static  void dbm_access(register DBM *db, long hash);
58static  int getbit(register DBM *db);
59static  void setbit(register DBM *db);
60static  datum makdatum(char buf[PBLKSIZ], int n);
61static  int finddatum(char buf[PBLKSIZ], datum item);
62static  long hashinc(register DBM *db, long hash);
63static  long dcalchash(datum item);
64static  int delitem(char buf[PBLKSIZ], int n);
65static  int additem(char buf[PBLKSIZ], datum item, datum item1);
66extern  int errno;
67
68DBM *
69dbm_open(char *file, int flags, int mode)
70{
71  struct stat statb;
72  register DBM *db;
73
74  if ((db = (DBM *)malloc(sizeof *db)) == 0) {
75    errno = ENOMEM;
76    return ((DBM *)0);
77  }
78#ifdef MSDOS
79  // default mode of open is ascii, we need binary mode.
80  flags |= O_BINARY;
81#endif
82  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
83  if ((flags & 03) == O_WRONLY)
84    flags = (flags & ~03) | O_RDWR;
85  strcpy(db->dbm_pagbuf, file);
86  strcat(db->dbm_pagbuf, ".pag");
87  db->dbm_pagf = open(db->dbm_pagbuf, flags, mode);
88  if (db->dbm_pagf < 0)
89    goto bad;
90  strcpy(db->dbm_pagbuf, file);
91  strcat(db->dbm_pagbuf, ".dir");
92  db->dbm_dirf = open(db->dbm_pagbuf, flags, mode);
93  if (db->dbm_dirf < 0)
94    goto bad1;
95  fstat(db->dbm_dirf, &statb);
96  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
97  db->dbm_pagbno = db->dbm_dirbno = -1;
98  return (db);
99bad1:
100  (void) close(db->dbm_pagf);
101bad:
102  free((char *)db);
103  return ((DBM *)0);
104}
105
106void
107dbm_close(DBM *db)
108{
109  (void) close(db->dbm_dirf);
110  (void) close(db->dbm_pagf);
111  free((char *)db);
112}
113
114long
115dbm_forder(register DBM *db, datum key)
116{
117  long hash;
118
119  hash = dcalchash(key);
120  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1) {
121    db->dbm_blkno = hash & db->dbm_hmask;
122    db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
123    if (getbit(db) == 0)
124      break;
125  }
126  return (db->dbm_blkno);
127}
128
129datum
130dbm_fetch(register DBM *db, datum key)
131{
132  register int i;
133  datum item;
134
135  if (dbm_error(db))
136    goto err;
137  dbm_access(db, dcalchash(key));
138  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0) {
139    item = makdatum(db->dbm_pagbuf, i+1);
140    if (item.dptr != NULL)
141      return (item);
142  }
143err:
144  item.dptr = NULL;
145  item.dsize = 0;
146  return (item);
147}
148
149int dbm_delete(register DBM *db, datum key)
150{
151  register int i;
152  datum item;
153
154  if (dbm_error(db))
155    return (-1);
156  if (dbm_rdonly(db)) {
157    errno = EPERM;
158    return (-1);
159  }
160  dbm_access(db, dcalchash(key));
161  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
162    return (-1);
163  if (!delitem(db->dbm_pagbuf, i))
164    goto err;
165  db->dbm_pagbno = db->dbm_blkno;
166  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
167  if (write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ) {
168  err:
169    db->dbm_flags |= _DBM_IOERR;
170    return (-1);
171  }
172  return (0);
173}
174
175int dbm_store(register DBM *db, datum key, datum dat, int replace)
176{
177  register int i;
178  int ret;
179  datum item, item1;
180  char ovfbuf[PBLKSIZ];
181
182  if (dbm_error(db))
183    return (-1);
184  if (dbm_rdonly(db)) {
185    errno = EPERM;
186    return (-1);
187  }
188loop:
189  dbm_access(db, dcalchash(key));
190  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0) {
191    if (!replace)
192      return (1);
193    if (!delitem(db->dbm_pagbuf, i)) {
194      db->dbm_flags |= _DBM_IOERR;
195      return (-1);
196    }
197  }
198  if (!additem(db->dbm_pagbuf, key, dat))
199    goto split;
200  db->dbm_pagbno = db->dbm_blkno;
201  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
202  if ( (ret=write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ) {
203    db->dbm_flags |= _DBM_IOERR;
204    return (-1);
205  }
206  return (0);
207
208split:
209  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ) {
210    db->dbm_flags |= _DBM_IOERR;
211    errno = ENOSPC;
212    return (-1);
213  }
214  memset(ovfbuf, 0, PBLKSIZ);
215  for (i=0;;) {
216    item = makdatum(db->dbm_pagbuf, i);
217    if (item.dptr == NULL)
218      break;
219    if (dcalchash(item) & (db->dbm_hmask+1)) {
220      item1 = makdatum(db->dbm_pagbuf, i+1);
221      if (item1.dptr == NULL) {
222        fprintf(stderr, "ndbm: split not paired\n");
223        db->dbm_flags |= _DBM_IOERR;
224        break;
225      }
226      if (!additem(ovfbuf, item, item1) ||
227          !delitem(db->dbm_pagbuf, i)) {
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, L_SET);
237  if (write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ) {
238    db->dbm_flags |= _DBM_IOERR;
239    return (-1);
240  }
241  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, L_SET);
242  if (write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ) {
243    db->dbm_flags |= _DBM_IOERR;
244    return (-1);
245  }
246  setbit(db);
247  goto loop;
248}
249
250datum
251dbm_firstkey(DBM *db)
252{
253
254  db->dbm_blkptr = 0L;
255  db->dbm_keyptr = 0;
256  return (dbm_nextkey(db));
257}
258
259datum
260dbm_nextkey(register DBM *db)
261{
262  struct stat statb;
263  datum item;
264
265  if (dbm_error(db) || fstat(db->dbm_pagf, &statb) < 0)
266                goto err;
267  statb.st_size /= PBLKSIZ;
268  for (;;) {
269    if (db->dbm_blkptr != db->dbm_pagbno) {
270      db->dbm_pagbno = db->dbm_blkptr;
271      (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, L_SET);
272      if (read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
273        memset(db->dbm_pagbuf, 0, PBLKSIZ);
274#ifdef DEBUG
275      else if (chkblk(db->dbm_pagbuf) < 0)
276        db->dbm_flags |= _DBM_IOERR;
277#endif
278    }
279    if (((short *)db->dbm_pagbuf)[0] != 0) {
280      item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
281      if (item.dptr != NULL) {
282        db->dbm_keyptr += 2;
283        return (item);
284      }
285      db->dbm_keyptr = 0;
286    }
287    if (++db->dbm_blkptr >= statb.st_size)
288      break;
289  }
290err:
291  item.dptr = NULL;
292  item.dsize = 0;
293  return (item);
294}
295
296static void
297dbm_access(register DBM *db, long hash)
298{
299  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1) {
300    db->dbm_blkno = hash & db->dbm_hmask;
301    db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
302    if (getbit(db) == 0)
303      break;
304  }
305  if (db->dbm_blkno != db->dbm_pagbno) {
306    db->dbm_pagbno = db->dbm_blkno;
307    (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
308    if (read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
309      memset(db->dbm_pagbuf, 0, PBLKSIZ);
310#ifdef DEBUG
311    else if (chkblk(db->dbm_pagbuf) < 0)
312      db->dbm_flags |= _DBM_IOERR;
313#endif
314  }
315}
316
317static
318int getbit(register DBM *db)
319{
320  long bn;
321  register int b, i, n;
322
323
324  if (db->dbm_bitno > db->dbm_maxbno)
325    return (0);
326  n = db->dbm_bitno % BYTESIZ;
327  bn = db->dbm_bitno / BYTESIZ;
328  i = bn % DBLKSIZ;
329  b = bn / DBLKSIZ;
330  if (b != db->dbm_dirbno) {
331    db->dbm_dirbno = b;
332    (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
333    if (read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
334      memset(db->dbm_dirbuf, 0, DBLKSIZ);
335  }
336  return (db->dbm_dirbuf[i] & (1<<n));
337}
338
339static void
340setbit(register DBM *db)
341{
342  long bn;
343  register int i, n, b;
344
345  if (db->dbm_bitno > db->dbm_maxbno)
346    db->dbm_maxbno = db->dbm_bitno;
347  n = db->dbm_bitno % BYTESIZ;
348  bn = db->dbm_bitno / BYTESIZ;
349  i = bn % DBLKSIZ;
350  b = bn / DBLKSIZ;
351  if (b != db->dbm_dirbno) {
352    db->dbm_dirbno = b;
353    (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
354    if (read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
355      memset(db->dbm_dirbuf, 0, DBLKSIZ);
356  }
357  db->dbm_dirbuf[i] |= 1<<n;
358  db->dbm_dirbno = b;
359  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
360  if (write(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
361    db->dbm_flags |= _DBM_IOERR;
362}
363
364static datum
365makdatum(char buf[PBLKSIZ], int n)
366{
367  register short *sp;
368  register int t;
369  datum item;
370
371  sp = (short *)buf;
372  if ((unsigned)n >= (unsigned)sp[0])
373  {
374    item.dptr = NULL;
375    item.dsize = 0;
376    return (item);
377  }
378  t = PBLKSIZ;
379  if (n > 0)
380    t = sp[n];
381  item.dptr = buf+sp[n+1];
382  item.dsize = t - sp[n+1];
383  return (item);
384}
385
386static
387int finddatum(char buf[PBLKSIZ], datum item)
388{
389  register short *sp;
390  register int i, n, j;
391
392  sp = (short *)buf;
393  n = PBLKSIZ;
394  for (i=0, j=sp[0]; i<j; i+=2, n = sp[i]) {
395    n -= sp[i+1];
396    if (n != item.dsize)
397      continue;
398    if (n == 0 || memcmp(&buf[sp[i+1]], item.dptr, n) == 0)
399      return (i);
400  }
401  return (-1);
402}
403
404static  int hitab[16]
405/* ken's
406{
407        055,043,036,054,063,014,004,005,
408        010,064,077,000,035,027,025,071,
409};
410*/
411 = {    61, 57, 53, 49, 45, 41, 37, 33,
412        29, 25, 21, 17, 13,  9,  5,  1,
413};
414static  long hltab[64]
415 = {
416        06100151277L,06106161736L,06452611562L,05001724107L,
417        02614772546L,04120731531L,04665262210L,07347467531L,
418        06735253126L,06042345173L,03072226605L,01464164730L,
419        03247435524L,07652510057L,01546775256L,05714532133L,
420        06173260402L,07517101630L,02431460343L,01743245566L,
421        00261675137L,02433103631L,03421772437L,04447707466L,
422        04435620103L,03757017115L,03641531772L,06767633246L,
423        02673230344L,00260612216L,04133454451L,00615531516L,
424        06137717526L,02574116560L,02304023373L,07061702261L,
425        05153031405L,05322056705L,07401116734L,06552375715L,
426        06165233473L,05311063631L,01212221723L,01052267235L,
427        06000615237L,01075222665L,06330216006L,04402355630L,
428        01451177262L,02000133436L,06025467062L,07121076461L,
429        03123433522L,01010635225L,01716177066L,05161746527L,
430        01736635071L,06243505026L,03637211610L,01756474365L,
431        04723077174L,03642763134L,05750130273L,03655541561L,
432};
433
434static long
435hashinc(register DBM *db, long hash)
436{
437  long bit;
438
439  hash &= db->dbm_hmask;
440  bit = db->dbm_hmask+1;
441  for (;;) {
442    bit >>= 1;
443    if (bit == 0)
444      return (0L);
445    if ((hash & bit) == 0)
446      return (hash | bit);
447    hash &= ~bit;
448  }
449}
450
451static long
452dcalchash(datum item)
453{
454  register int s, c, j;
455  register char *cp;
456  register long hashl;
457  register int hashi;
458
459  hashl = 0;
460  hashi = 0;
461  for (cp = item.dptr, s=item.dsize; --s >= 0; ) {
462    c = *cp++;
463    for (j=0; j<BYTESIZ; j+=4) {
464      hashi += hitab[c&017];
465      hashl += hltab[hashi&63];
466      c >>= 4;
467    }
468  }
469  return (hashl);
470}
471
472/*
473 * Delete pairs of items (n & n+1).
474 */
475static
476int delitem(char buf[PBLKSIZ], int n)
477{
478  register short *sp, *sp1;
479  register int i1, i2;
480
481  sp = (short *)buf;
482  i2 = sp[0];
483  if ((unsigned)n >= (unsigned)i2 || (n & 1))
484    return (0);
485  if (n == i2-2)
486  {
487    sp[0] -= 2;
488    return (1);
489  }
490  i1 = PBLKSIZ;
491  if (n > 0)
492    i1 = sp[n];
493  i1 -= sp[n+2];
494  if (i1 > 0) {
495    i2 = sp[i2];
496    bcopy(&buf[i2], &buf[i2 + i1], sp[n+2] - i2);
497  }
498  sp[0] -= 2;
499  for (sp1 = sp + sp[0], sp += n+1; sp <= sp1; sp++)
500    sp[0] = sp[2] + i1;
501  return (1);
502}
503
504/*
505 * Add pairs of items (item & item1).
506 */
507static
508int additem(char buf[PBLKSIZ], datum item, datum item1)
509{
510  register short *sp;
511  register int i1, i2, tmp;
512
513  sp = (short *)buf;
514  i1 = PBLKSIZ;
515  i2 = sp[0];
516  if (i2 > 0)
517    i1 = sp[i2];
518  i1 -= item.dsize + item1.dsize;
519  tmp = (i2+3) * sizeof(short);
520  if (i1 <= tmp) return (0);
521  sp[0] += 2;
522  sp[++i2] = i1 + item1.dsize;
523  bcopy(item.dptr, &buf[i1 + item1.dsize], item.dsize);
524  sp[++i2] = i1;
525  bcopy(item1.dptr, &buf[i1], item1.dsize);
526  return (1);
527}
528
529#ifdef DEBUG
530static
531chkblk(char buf[PBLKSIZ])
532{
533  register short *sp;
534  register t, i;
535
536  sp = (short *)buf;
537  t = PBLKSIZ;
538  for (i=0; i<sp[0]; i++) {
539    if (sp[i+1] > t)
540      return (-1);
541    t = sp[i+1];
542  }
543  if (t < (sp[0]+1)*sizeof(short))
544    return (-1);
545  return (0);
546}
547#endif
548
549#endif /* HAVE_DBM */
Note: See TracBrowser for help on using the repository browser.