source: git/Singular/ndbm.cc @ a2dde00

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