source: git/Singular/ndbm.cc @ 046f44

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