source: git/Singular/ndbm.cc @ 1fc83c0

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