source: git/Singular/sing_dbm.cc @ f6b5f0

spielwiese
Last change on this file since f6b5f0 was f6b5f0, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: change Log: and Header: to Id: git-svn-id: file:///usr/local/Singular/svn/trunk@73 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5//**************************************************************************
6//
7// $Id: sing_dbm.cc,v 1.2 1997-03-24 14:25:47 Singular Exp $
8//
9//**************************************************************************
10//  'sing_dbm.cc' containes command to handle dbm-files under
11// Singular. Don't forget to compile Singular with the option -ldbm
12//
13//**************************************************************************
14
15#include "mod2.h"
16
17#ifdef HAVE_DBM
18
19#include <stdio.h>
20#include <fcntl.h>
21#include <errno.h>
22#include "tok.h"
23#include "febase.h"
24#include "mmemory.h"
25#include "ipid.h"
26#include "silink.h"
27#include "sing_dbm.h"
28
29
30static BOOLEAN dbOpened = FALSE;
31
32/* The data and key structure.  This structure is defined for compatibility. */
33typedef struct {
34        char *dptr;
35        int   dsize;
36} datum;
37
38
39/* These are the routines in dbm. */
40
41
42extern "C" {
43extern int dbminit(const char *__file);
44extern int store(datum __key, datum __content);
45extern datum fetch(datum);
46extern datum firstkey(void);
47extern datum nextkey(datum);
48}
49
50//extern char *getenv(char *var);
51extern char *index(char *str, char c);
52
53//**************************************************************************
54BOOLEAN dbOpen(si_link l)
55{
56  if (!dbOpened)
57  {
58    if(dbminit(l->name) == 0)
59    {
60      dbOpened=TRUE;
61      SI_LINK_SET_RW_OPEN_P(l);
62      l->data=NULL;
63      return FALSE;
64    }
65    Werror("dbminit of `%s` failed",l->name);
66  }
67  else
68    Werror("only one DBM link allowed:`%s`",l->name);
69  return TRUE;
70}
71
72//**************************************************************************
73BOOLEAN dbClose(si_link l)
74{
75  dbOpened=FALSE;
76  SI_LINK_SET_CLOSE_P(l);
77  return FALSE;
78}
79
80//**************************************************************************
81static datum d_value;
82leftv dbRead(si_link l, leftv key)
83{
84  leftv v=NULL;
85  if(dbOpened)
86  {
87    datum d_key;
88
89    if(key!=NULL)
90    {
91      if (key->Typ()==STRING_CMD)
92      {
93        d_key.dptr = (char*)key->Data();
94        d_key.dsize = strlen(d_key.dptr)+1;
95        d_value = fetch(d_key);
96        v=(leftv)Alloc0(sizeof(sleftv));
97        if (d_value.dptr!=NULL) v->data=mstrdup(d_value.dptr);
98        else                    v->data=mstrdup("");
99        v->rtyp=STRING_CMD;
100      }
101      else
102      {
103        WerrorS("read(`link`,`string`) expected");
104      }
105    }
106    else
107    {
108      if (l->data==NULL)
109      {
110        d_value = firstkey();
111        l->data=(void *)&d_value;
112      }
113      else
114      {
115        d_value = nextkey(*(datum *)l->data);
116      }
117      v=(leftv)Alloc0(sizeof(sleftv));
118      v->rtyp=STRING_CMD;
119      if (d_value.dptr!=NULL) v->data=mstrdup(d_value.dptr);
120      else                    v->data=mstrdup("");
121    }
122  }
123  else
124  {
125    Werror("DBM-link `%s` not open in read",l->name);
126  }
127  return v;
128}
129leftv dbRead1(si_link l)
130{
131  return dbRead(l,NULL);
132}
133//**************************************************************************
134BOOLEAN dbWrite(si_link l, leftv key)
135{
136  BOOLEAN b=TRUE;
137
138  if(dbOpened)
139  {
140    if((key!=NULL)
141    && (key->Typ()==STRING_CMD)
142    && (key->next!=NULL)
143    && (key->next->Typ()==STRING_CMD))
144    {
145      datum d_key, d_value;
146      d_key.dptr = (char *)key->Data();
147      d_key.dsize = strlen(d_key.dptr)+1;
148      d_value.dptr = (char *)key->next->Data();
149      d_value.dsize = strlen(d_value.dptr)+1;
150      store(d_key, d_value);
151      b=FALSE;
152    }
153    else
154    {
155      WerrorS("write(`DBM link`,`key string`,`data string`) expected");
156    }
157  }
158  else
159  {
160    Werror("DBM-link `%s` not open in write",l->name);
161  }
162  return b;
163}
164//**************************************************************************
165si_link_extension dbInit()
166{
167  si_link_extension s=(si_link_extension)Alloc0(sizeof(*s));
168  s->Init=slInitALink;
169  s->OpenRead=dbOpen;
170  s->OpenWrite=dbOpen;
171  s->Close=dbClose;
172  s->Read=dbRead1;
173  s->Read2=dbRead;
174  s->Write=dbWrite;
175  s->name="DBM";
176  return s;
177}
178#ifdef HAVE_MODULE_DBM
179leftv initDBM(leftv v)
180{
181  slExtensionInit(dbInit());
182  return NULL;
183}
184#endif
185#endif
Note: See TracBrowser for help on using the repository browser.