source: git/Singular/ndbm.cc @ fdc537

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