source: git/Singular/ndbm.cc @ cc94b0a

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