source: git/Singular/ndbm.cc @ 06c0b3

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