source: git/Singular/ndbm.cc @ 634dab0

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