source: git/Singular/sing_dbm.cc @ 457e2d7

spielwiese
Last change on this file since 457e2d7 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: 5.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5//**************************************************************************/
6//
7// $Id: sing_dbm.cc,v 1.7 1997-08-12 17:14:44 Singular Exp $
8//
9//**************************************************************************/
10//  'sing_dbm.cc' containes command to handle dbm-files under
11// Singular.
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/* These are the routines in dbm. */
30#  include "ndbm.h"
31typedef struct {
32  DBM *db;        // pointer to open database
33  int first;      // firstkey to look for?
34} DBM_info;
35
36//**************************************************************************/
37BOOLEAN dbOpen(si_link l, short flag)
38{
39  char *mode = "r";
40  DBM_info *db;
41  int dbm_flags = O_RDONLY | O_CREAT;  // open database readonly as default
42
43  if((flag & SI_LINK_WRITE)
44  || ((l->mode!=NULL)&&
45    ((l->mode[0]=='w')||(l->mode[1]=='w')))
46  )
47  {
48    dbm_flags = O_RDWR | O_CREAT;
49    mode = "rw";
50    flag|=SI_LINK_WRITE;
51  }
52  if (((db = (DBM_info *)Alloc(sizeof *db)) != NULL)
53  &&((db->db = dbm_open(l->name, dbm_flags, 0664 )) != NULL ))
54  {
55    db->first=1;
56    if(flag & SI_LINK_WRITE)
57      SI_LINK_SET_RW_OPEN_P(l);
58    else
59      SI_LINK_SET_R_OPEN_P(l);
60    l->data=(void *)(db);
61    FreeL(l->mode);
62    l->mode=mstrdup(mode);
63    return FALSE;
64  }
65  return TRUE;
66}
67
68//**************************************************************************/
69BOOLEAN dbClose(si_link l)
70{
71  DBM_info *db = (DBM_info *)l->data;
72
73  dbm_close(db->db);
74  Free((ADDRESS)db,(sizeof *db));
75  l->data=NULL; 
76  SI_LINK_SET_CLOSE_P(l);
77  return FALSE;
78}
79
80//**************************************************************************/
81static datum d_value;
82leftv dbRead2(si_link l, leftv key)
83{
84  DBM_info *db = (DBM_info *)l->data;
85  leftv v=NULL;
86  datum d_key;
87
88  if(key!=NULL)
89  {
90    if (key->Typ()==STRING_CMD)
91    {
92      d_key.dptr = (char*)key->Data();
93      d_key.dsize = strlen(d_key.dptr)+1;
94      d_value = dbm_fetch(db->db, d_key);
95      v=(leftv)Alloc0(sizeof(sleftv));
96      if (d_value.dptr!=NULL) v->data=mstrdup(d_value.dptr);
97      else                    v->data=mstrdup("");
98      v->rtyp=STRING_CMD;
99    }
100    else
101    {
102      WerrorS("read(`DBM link`,`string`) expected");
103    }
104  }
105  else
106  {
107    if(db->first)
108      d_value = dbm_firstkey((DBM *)db->db);
109    else
110      d_value = dbm_nextkey((DBM *)db->db);
111
112    v=(leftv)Alloc0(sizeof(sleftv));
113    v->rtyp=STRING_CMD;
114    if (d_value.dptr!=NULL)
115    {
116      v->data=mstrdup(d_value.dptr);
117      db->first = 0;
118    }
119    else
120    {
121      v->data=mstrdup("");
122      db->first = 1;
123    }
124
125  }
126  return v;
127}
128leftv dbRead1(si_link l)
129{
130  return dbRead2(l,NULL);
131}
132//**************************************************************************/
133BOOLEAN dbWrite(si_link l, leftv key)
134{
135  DBM_info *db = (DBM_info *)l->data;
136  BOOLEAN b=TRUE;
137  register int ret;
138
139  // database is opened
140  if((key!=NULL) && (key->Typ()==STRING_CMD) )
141  { 
142    if (key->next!=NULL)                   // have a second parameter ?
143    {
144      if(key->next->Typ()==STRING_CMD)     // replace (key,value)
145      {
146        datum d_key, d_value;
147
148        d_key.dptr = (char *)key->Data();
149        d_key.dsize = strlen(d_key.dptr)+1;
150        d_value.dptr = (char *)key->next->Data();
151        d_value.dsize = strlen(d_value.dptr)+1;
152        ret  = dbm_store(db->db, d_key, d_value, DBM_REPLACE);
153        if(!ret )
154          b=FALSE;
155        else
156        {
157          if(dbm_error(db->db))
158          {
159            Werror("DBM link I/O error. Is '%s' readonly?", l->name);
160            dbm_clearerr(db->db);
161          }
162        }
163      }
164    }
165    else
166    {                               // delete (key)
167      datum d_key;
168
169      d_key.dptr = (char *)key->Data();
170      d_key.dsize = strlen(d_key.dptr)+1;
171      dbm_delete(db->db, d_key);
172      b=FALSE;
173    }
174  }
175  else
176  {
177    WerrorS("write(`DBM link`,`key string` [,`data string`]) expected");
178  }
179  return b;
180}
181//**************************************************************************/
182//char *dbStatus(si_link l, char *request)
183//{
184//  if (strcmp(request, "read") == 0)
185//  {
186//    if (SI_LINK_R_OPEN_P(l))
187//      return "ready";
188//    else
189//      return "not ready";
190//  }
191//  else if (strcmp(request, "write") == 0)
192//  {
193//    if (SI_LINK_W_OPEN_P(l))
194//      return "ready";
195//    else
196//      return "not ready";
197//  }
198//  else return "unknown status request";
199//}
200//**************************************************************************/
201si_link_extension slInitDBMExtension(si_link_extension s)
202{
203  s->Open=dbOpen;
204  s->Close=dbClose;
205  s->Read=dbRead1;
206  s->Read2=dbRead2;
207  s->Write=dbWrite;
208  //s->Dump=NULL;
209  //s->GetDump=NULL;
210  //s->Status=dbStatus;
211  s->Status=slStatusAscii;
212  s->type="DBM";
213  return s;
214}
215#endif /* HAVE_DBM */
Note: See TracBrowser for help on using the repository browser.