source: git/Singular/ndbm.cc @ 82716e

spielwiese
Last change on this file since 82716e was 3ff334, checked in by Wilfred Pohl <pohl@…>, 26 years ago
generalisation to Metrowerks with WindowsNT git-svn-id: file:///usr/local/Singular/svn/trunk@1367 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.9 1998-04-09 11:47:13 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 __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 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 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 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 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 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 t;
397  datum item;
398
399  sp = (short *)buf;
400  if ((unsigned)n >= sp[0]) {
401    item.dptr = NULL;
402    item.dsize = 0;
403    return (item);
404  }
405  t = PBLKSIZ;
406  if (n > 0)
407    t = sp[n];
408  item.dptr = buf+sp[n+1];
409  item.dsize = t - sp[n+1];
410  return (item);
411}
412
413static
414int finddatum(char buf[PBLKSIZ], datum item)
415{
416  register short *sp;
417  register int i, n, j;
418
419  sp = (short *)buf;
420  n = PBLKSIZ;
421  for (i=0, j=sp[0]; i<j; i+=2, n = sp[i]) {
422    n -= sp[i+1];
423    if (n != item.dsize)
424      continue;
425    if (n == 0 || memcmp(&buf[sp[i+1]], item.dptr, n) == 0)
426      return (i);
427  }
428  return (-1);
429}
430
431static  int hitab[16]
432/* ken's
433{
434        055,043,036,054,063,014,004,005,
435        010,064,077,000,035,027,025,071,
436};
437*/
438 = {    61, 57, 53, 49, 45, 41, 37, 33,
439        29, 25, 21, 17, 13,  9,  5,  1,
440};
441static  long hltab[64]
442 = {
443        06100151277L,06106161736L,06452611562L,05001724107L,
444        02614772546L,04120731531L,04665262210L,07347467531L,
445        06735253126L,06042345173L,03072226605L,01464164730L,
446        03247435524L,07652510057L,01546775256L,05714532133L,
447        06173260402L,07517101630L,02431460343L,01743245566L,
448        00261675137L,02433103631L,03421772437L,04447707466L,
449        04435620103L,03757017115L,03641531772L,06767633246L,
450        02673230344L,00260612216L,04133454451L,00615531516L,
451        06137717526L,02574116560L,02304023373L,07061702261L,
452        05153031405L,05322056705L,07401116734L,06552375715L,
453        06165233473L,05311063631L,01212221723L,01052267235L,
454        06000615237L,01075222665L,06330216006L,04402355630L,
455        01451177262L,02000133436L,06025467062L,07121076461L,
456        03123433522L,01010635225L,01716177066L,05161746527L,
457        01736635071L,06243505026L,03637211610L,01756474365L,
458        04723077174L,03642763134L,05750130273L,03655541561L,
459};
460
461static long
462hashinc(register DBM *db, long hash)
463{
464  long bit;
465
466  hash &= db->dbm_hmask;
467  bit = db->dbm_hmask+1;
468  for (;;) {
469    bit >>= 1;
470    if (bit == 0)
471      return (0L);
472    if ((hash & bit) == 0)
473      return (hash | bit);
474    hash &= ~bit;
475  }
476}
477
478static long
479dcalchash(datum item)
480{
481  register int s, c, j;
482  register char *cp;
483  register long hashl;
484  register int hashi;
485 
486  hashl = 0;
487  hashi = 0;
488  for (cp = item.dptr, s=item.dsize; --s >= 0; ) {
489    c = *cp++;
490    for (j=0; j<BYTESIZ; j+=4) {
491      hashi += hitab[c&017];
492      hashl += hltab[hashi&63];
493      c >>= 4;
494    }
495  }
496  return (hashl);
497}
498
499/*
500 * Delete pairs of items (n & n+1).
501 */
502static
503int delitem(char buf[PBLKSIZ], int n)
504{
505  register short *sp, *sp1;
506  register i1, i2;
507
508  sp = (short *)buf;
509  i2 = sp[0];
510  if ((unsigned)n >= i2 || (n & 1))
511    return (0);
512  if (n == i2-2) {
513    sp[0] -= 2;
514    return (1);
515  }
516  i1 = PBLKSIZ;
517  if (n > 0)
518    i1 = sp[n];
519  i1 -= sp[n+2];
520  if (i1 > 0) {
521    i2 = sp[i2];
522    bcopy(&buf[i2], &buf[i2 + i1], sp[n+2] - i2);
523  }
524  sp[0] -= 2;
525  for (sp1 = sp + sp[0], sp += n+1; sp <= sp1; sp++)
526    sp[0] = sp[2] + i1;
527  return (1);
528}
529
530/*
531 * Add pairs of items (item & item1).
532 */
533static
534int additem(char buf[PBLKSIZ], datum item, datum item1)
535{
536  register short *sp;
537  register i1, i2, tmp;
538
539  sp = (short *)buf;
540  i1 = PBLKSIZ;
541  i2 = sp[0];
542  if (i2 > 0)
543    i1 = sp[i2];
544  i1 -= item.dsize + item1.dsize;
545  tmp = (i2+3) * sizeof(short);
546  if (i1 <= tmp) return (0);
547  sp[0] += 2;
548  sp[++i2] = i1 + item1.dsize;
549  bcopy(item.dptr, &buf[i1 + item1.dsize], item.dsize);
550  sp[++i2] = i1;
551  bcopy(item1.dptr, &buf[i1], item1.dsize);
552  return (1);
553}
554
555#ifdef DEBUG
556static
557chkblk(char buf[PBLKSIZ])
558{
559  register short *sp;
560  register t, i;
561
562  sp = (short *)buf;
563  t = PBLKSIZ;
564  for (i=0; i<sp[0]; i++) {
565    if (sp[i+1] > t)
566      return (-1);
567    t = sp[i+1];
568  }
569  if (t < (sp[0]+1)*sizeof(short))
570    return (-1);
571  return (0);
572}
573#endif
574
575#endif /* HAVE_DBM */
Note: See TracBrowser for help on using the repository browser.