1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | |
---|
5 | //************************************************************************** |
---|
6 | // |
---|
7 | // $Id: sing_dbm.cc,v 1.4 1997-04-09 12:20:09 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 | |
---|
30 | static BOOLEAN dbOpened = FALSE; |
---|
31 | |
---|
32 | /* The data and key structure. This structure is defined for compatibility. */ |
---|
33 | typedef struct { |
---|
34 | char *dptr; |
---|
35 | int dsize; |
---|
36 | } datum; |
---|
37 | |
---|
38 | |
---|
39 | /* These are the routines in dbm. */ |
---|
40 | |
---|
41 | |
---|
42 | extern "C" { |
---|
43 | extern int dbminit(const char *__file); |
---|
44 | extern int store(datum __key, datum __content); |
---|
45 | extern datum fetch(datum); |
---|
46 | extern datum firstkey(void); |
---|
47 | extern datum nextkey(datum); |
---|
48 | } |
---|
49 | |
---|
50 | //extern char *getenv(char *var); |
---|
51 | extern char *index(char *str, char c); |
---|
52 | |
---|
53 | //************************************************************************** |
---|
54 | BOOLEAN dbOpen(si_link l, short flag) |
---|
55 | { |
---|
56 | if((flag==SI_LINK_OPEN) |
---|
57 | || (flag==SI_LINK_READ) |
---|
58 | || (flag==SI_LINK_WRITE)) |
---|
59 | { |
---|
60 | if (!dbOpened) |
---|
61 | { |
---|
62 | if(dbminit(l->name) == 0) |
---|
63 | { |
---|
64 | dbOpened=TRUE; |
---|
65 | SI_LINK_SET_RW_OPEN_P(l); |
---|
66 | l->data=NULL; |
---|
67 | FreeL(l->mode); |
---|
68 | l->mode=mstrdup("rw"); |
---|
69 | return FALSE; |
---|
70 | } |
---|
71 | Werror("dbminit of `%s` failed",l->name); |
---|
72 | } |
---|
73 | else |
---|
74 | Werror("only one DBM link allowed:`%s`",l->name); |
---|
75 | } |
---|
76 | return TRUE; |
---|
77 | } |
---|
78 | |
---|
79 | //************************************************************************** |
---|
80 | BOOLEAN dbClose(si_link l) |
---|
81 | { |
---|
82 | dbOpened=FALSE; |
---|
83 | SI_LINK_SET_CLOSE_P(l); |
---|
84 | return FALSE; |
---|
85 | } |
---|
86 | |
---|
87 | //************************************************************************** |
---|
88 | static datum d_value; |
---|
89 | leftv dbRead2(si_link l, leftv key) |
---|
90 | { |
---|
91 | leftv v=NULL; |
---|
92 | if(dbOpened) |
---|
93 | { |
---|
94 | datum d_key; |
---|
95 | |
---|
96 | if(key!=NULL) |
---|
97 | { |
---|
98 | if (key->Typ()==STRING_CMD) |
---|
99 | { |
---|
100 | d_key.dptr = (char*)key->Data(); |
---|
101 | d_key.dsize = strlen(d_key.dptr)+1; |
---|
102 | d_value = fetch(d_key); |
---|
103 | v=(leftv)Alloc0(sizeof(sleftv)); |
---|
104 | if (d_value.dptr!=NULL) v->data=mstrdup(d_value.dptr); |
---|
105 | else v->data=mstrdup(""); |
---|
106 | v->rtyp=STRING_CMD; |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | WerrorS("read(`link`,`string`) expected"); |
---|
111 | } |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | if (l->data==NULL) |
---|
116 | { |
---|
117 | d_value = firstkey(); |
---|
118 | l->data=(void *)&d_value; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | d_value = nextkey(*(datum *)l->data); |
---|
123 | } |
---|
124 | v=(leftv)Alloc0(sizeof(sleftv)); |
---|
125 | v->rtyp=STRING_CMD; |
---|
126 | if (d_value.dptr!=NULL) v->data=mstrdup(d_value.dptr); |
---|
127 | else v->data=mstrdup(""); |
---|
128 | } |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | Werror("DBM-link `%s` not open in read",l->name); |
---|
133 | } |
---|
134 | return v; |
---|
135 | } |
---|
136 | leftv dbRead1(si_link l) |
---|
137 | { |
---|
138 | return dbRead2(l,NULL); |
---|
139 | } |
---|
140 | //************************************************************************** |
---|
141 | BOOLEAN dbWrite(si_link l, leftv key) |
---|
142 | { |
---|
143 | BOOLEAN b=TRUE; |
---|
144 | |
---|
145 | if(dbOpened) |
---|
146 | { |
---|
147 | if((key!=NULL) |
---|
148 | && (key->Typ()==STRING_CMD) |
---|
149 | && (key->next!=NULL) |
---|
150 | && (key->next->Typ()==STRING_CMD)) |
---|
151 | { |
---|
152 | datum d_key, d_value; |
---|
153 | d_key.dptr = (char *)key->Data(); |
---|
154 | d_key.dsize = strlen(d_key.dptr)+1; |
---|
155 | d_value.dptr = (char *)key->next->Data(); |
---|
156 | d_value.dsize = strlen(d_value.dptr)+1; |
---|
157 | store(d_key, d_value); |
---|
158 | b=FALSE; |
---|
159 | } |
---|
160 | else |
---|
161 | { |
---|
162 | WerrorS("write(`DBM link`,`key string`,`data string`) expected"); |
---|
163 | } |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | Werror("DBM-link `%s` not open in write",l->name); |
---|
168 | } |
---|
169 | return b; |
---|
170 | } |
---|
171 | //************************************************************************** |
---|
172 | char *dbStatus(si_link l, char *request) |
---|
173 | { |
---|
174 | if ((strcmp(request, "read") == 0) |
---|
175 | || (strcmp(request, "write") == 0)) |
---|
176 | { |
---|
177 | if (SI_LINK_RW_OPEN_P(l)) return "ready"; |
---|
178 | else return "not ready"; |
---|
179 | } |
---|
180 | else return "unknown status request"; |
---|
181 | } |
---|
182 | //************************************************************************** |
---|
183 | si_link_extension slInitDBMExtension(si_link_extension s) |
---|
184 | { |
---|
185 | s->Open=dbOpen; |
---|
186 | s->Close=dbClose; |
---|
187 | s->Read=dbRead1; |
---|
188 | s->Read2=dbRead2; |
---|
189 | s->Write=dbWrite; |
---|
190 | //s->Dump=NULL; |
---|
191 | //s->GetDump=NULL; |
---|
192 | s->Status=dbStatus; |
---|
193 | s->type="DBM"; |
---|
194 | return s; |
---|
195 | } |
---|
196 | #endif |
---|