1 | #include <cstdio> |
---|
2 | #include <cstring> |
---|
3 | #include <memory.h> |
---|
4 | |
---|
5 | #include <iostream> |
---|
6 | |
---|
7 | #include <arbdb.h> |
---|
8 | #include <arbdbt.h> |
---|
9 | |
---|
10 | #include "arbdb++.hxx" |
---|
11 | |
---|
12 | /*********************************** |
---|
13 | *********************************** |
---|
14 | DB MAIN |
---|
15 | ************************************ |
---|
16 | ************************************/ |
---|
17 | |
---|
18 | AD_MAIN::AD_MAIN() { |
---|
19 | gbd = 0; |
---|
20 | species_data = sai_data = presets = 0; |
---|
21 | AD_fast = MAXCACH; |
---|
22 | } |
---|
23 | |
---|
24 | AD_MAIN::~AD_MAIN() |
---|
25 | { |
---|
26 | if (gbd) |
---|
27 | new AD_ERR("AD_MAIN: no close or exit !!"); |
---|
28 | } |
---|
29 | |
---|
30 | /********************************* |
---|
31 | open : oeffnet die Datenbank mit dem namen *path |
---|
32 | default: cach abgeschaltet, AD_fast = MAXCACH; |
---|
33 | cach an MINCACH |
---|
34 | ***********************************/ |
---|
35 | |
---|
36 | AD_ERR *AD_MAIN::open(const char *path) { |
---|
37 | if (AD_fast == MAXCACH) { |
---|
38 | gbd = GB_open(path,"rw"); |
---|
39 | } else { |
---|
40 | gbd = GB_open(path,"rwt"); // tiny speichersparend |
---|
41 | } |
---|
42 | if (gbd) { // DB geoeffnet |
---|
43 | GB_begin_transaction(gbd); // Zeiger initialisieren |
---|
44 | |
---|
45 | species_data = GB_entry(gbd,"species_data"); |
---|
46 | sai_data = GB_entry(gbd,"extended_data"); |
---|
47 | presets = GB_entry(gbd,"presets"); |
---|
48 | |
---|
49 | GB_commit_transaction(gbd); |
---|
50 | |
---|
51 | gbdataptr = gbd; |
---|
52 | return 0; |
---|
53 | } else |
---|
54 | { |
---|
55 | return new AD_ERR("database doesn't exist"); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | AD_ERR *AD_MAIN::open(const char *path,int cach/* = MAXCACH*/) |
---|
60 | { |
---|
61 | AD_fast = cach; |
---|
62 | return open(path); |
---|
63 | } |
---|
64 | |
---|
65 | AD_ERR * AD_MAIN::save(const char *modus) |
---|
66 | { |
---|
67 | // binary format als Voreinstellung |
---|
68 | char *error; |
---|
69 | if (strncmp("ascii",modus,strlen(modus))) { |
---|
70 | error = (char *)GB_save(gbd,0,"b"); } |
---|
71 | else { |
---|
72 | error = (char *)GB_save(gbd,0,"a"); |
---|
73 | } |
---|
74 | if (error) printf("%s\n",error); //return new AD_ERR(error); |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | AD_ERR * AD_MAIN::save_as(const char *modus) |
---|
79 | { |
---|
80 | // binary format als Voreinstellung |
---|
81 | char *error; |
---|
82 | if (strncmp("ascii",modus,strlen(modus))) { |
---|
83 | error = (char *)GB_save_as(gbd,0,"b"); } |
---|
84 | else { |
---|
85 | error = (char *)GB_save_as(gbd,0,"a"); |
---|
86 | } |
---|
87 | if (error) printf("%s\n",error); //return new AD_ERR(error); |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|
91 | AD_ERR * AD_MAIN::save_home(const char *modus) |
---|
92 | { |
---|
93 | // binary format als Voreinstellung |
---|
94 | char *error; |
---|
95 | if (strncmp("ascii",modus,strlen(modus))) { |
---|
96 | error = (char *)GB_save_in_home(gbd,0,"b"); } |
---|
97 | else { |
---|
98 | error = (char *)GB_save_in_home(gbd,0,"a"); |
---|
99 | } |
---|
100 | if (error) printf("%s\n",error); //return new AD_ERR(error); |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | AD_ERR *AD_MAIN::close() |
---|
108 | { |
---|
109 | if (gbd) { GB_close(gbd); } |
---|
110 | gbd = 0; |
---|
111 | return 0; |
---|
112 | } |
---|
113 | AD_ERR *AD_MAIN::push_transaction() |
---|
114 | { |
---|
115 | char *error = 0; |
---|
116 | error = (char *)GB_push_transaction(gbd); |
---|
117 | if (!error) |
---|
118 | return 0; |
---|
119 | return new AD_ERR(error); |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | AD_ERR *AD_MAIN::pop_transaction() |
---|
125 | { |
---|
126 | char *error = 0; |
---|
127 | error = (char *)GB_pop_transaction(gbd); |
---|
128 | if (!error) |
---|
129 | return 0; |
---|
130 | return new AD_ERR(error); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | AD_ERR *AD_MAIN::begin_transaction() |
---|
135 | { |
---|
136 | char *error = 0; |
---|
137 | error = (char *)GB_begin_transaction(gbd); |
---|
138 | if (!error) |
---|
139 | return 0; |
---|
140 | return new AD_ERR(error); |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | AD_ERR *AD_MAIN::commit_transaction() |
---|
146 | { |
---|
147 | char *error = 0; |
---|
148 | error = (char *)GB_commit_transaction(gbd); |
---|
149 | if (!error) |
---|
150 | return 0; |
---|
151 | return new AD_ERR(error); |
---|
152 | } |
---|
153 | |
---|
154 | AD_ERR * AD_MAIN::abort_transaction() |
---|
155 | { |
---|
156 | char* error = 0; |
---|
157 | error = (char *)GB_abort_transaction(gbd); |
---|
158 | if (!error) return 0; |
---|
159 | return new AD_ERR(error); |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | int AD_MAIN::get_cach_flag() |
---|
164 | // liefert das Speicherflag zurueck |
---|
165 | { |
---|
166 | return AD_fast; |
---|
167 | } |
---|
168 | |
---|
169 | int AD_MAIN::time_stamp(void) |
---|
170 | { |
---|
171 | return GB_read_clock(species_data); |
---|
172 | } |
---|
173 | |
---|
174 | AD_ERR * AD_MAIN::change_security_level(int level) { |
---|
175 | GB_ERROR error; |
---|
176 | char passwd='\n'; // not implemented |
---|
177 | error = GB_change_my_security(gbd,level,&passwd); |
---|
178 | if (error == 0) |
---|
179 | return 0; |
---|
180 | return new AD_ERR(error); |
---|
181 | } |
---|
182 | |
---|
183 | /************************************** |
---|
184 | |
---|
185 | AD_READWRITE |
---|
186 | |
---|
187 | *************************/ |
---|
188 | |
---|
189 | char *AD_READWRITE::readstring(char *feld) { |
---|
190 | GBDATA *gbptr = 0; |
---|
191 | GB_TYPES type; |
---|
192 | if (gbdataptr != 0) { |
---|
193 | gbptr = GB_entry(gbdataptr,feld); |
---|
194 | } |
---|
195 | if (gbptr != 0) { |
---|
196 | type = GB_read_type(gbptr); |
---|
197 | if (type == GB_STRING) { |
---|
198 | return (char *)GB_read_string(gbptr); |
---|
199 | } |
---|
200 | |
---|
201 | } |
---|
202 | return 0; // falscher type oder eintrag nicht gefunden; |
---|
203 | |
---|
204 | } |
---|
205 | |
---|
206 | int AD_READWRITE::readint(char *feld) { |
---|
207 | GBDATA *gbptr = 0; |
---|
208 | GB_TYPES type; |
---|
209 | if (gbdataptr != 0) { |
---|
210 | gbptr = GB_entry(gbdataptr,feld); |
---|
211 | } |
---|
212 | if (gbptr != 0) { |
---|
213 | type = GB_read_type(gbptr); |
---|
214 | if (type == GB_INT ) { |
---|
215 | return (int)GB_read_int(gbptr); |
---|
216 | } |
---|
217 | new AD_ERR("readint: no int type!"); |
---|
218 | return 0; |
---|
219 | } |
---|
220 | return 0; // falscher type oder eintrag nicht gefunden; |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | float AD_READWRITE::readfloat(char *feld) { |
---|
225 | GBDATA *gbptr = 0; |
---|
226 | GB_TYPES type; |
---|
227 | if (gbdataptr != 0) { |
---|
228 | gbptr = GB_entry(gbdataptr,feld); |
---|
229 | } |
---|
230 | if (gbptr != 0) { |
---|
231 | type = GB_read_type(gbptr); |
---|
232 | if (type == GB_FLOAT ) { |
---|
233 | return (float)GB_read_float(gbptr); |
---|
234 | } |
---|
235 | new AD_ERR("readfloat: no float type!"); |
---|
236 | return 0; |
---|
237 | } |
---|
238 | return 0; // falscher type oder efloatrag nicht gefunden; |
---|
239 | |
---|
240 | } |
---|
241 | AD_ERR *AD_READWRITE::writestring(char *feld,char *eintrag) { |
---|
242 | GBDATA *gbptr = 0; |
---|
243 | GB_TYPES type; |
---|
244 | GB_ERROR error; |
---|
245 | if (gbdataptr != 0) { |
---|
246 | gbptr = GB_entry(gbdataptr,feld); |
---|
247 | } |
---|
248 | if (gbptr != 0) { |
---|
249 | type = GB_read_type(gbptr); |
---|
250 | if (type == GB_STRING ) { |
---|
251 | error = GB_write_string(gbptr,eintrag); |
---|
252 | if (error == 0) { |
---|
253 | return 0; } |
---|
254 | return new AD_ERR("writestring not possible"); |
---|
255 | } |
---|
256 | return new AD_ERR("writestring on non string entry!"); |
---|
257 | } |
---|
258 | return new AD_ERR("writestring: feld not existing",CORE); |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | AD_ERR *AD_READWRITE::writeint(char *feld,int eintrag) { |
---|
263 | GBDATA *gbptr = 0; |
---|
264 | GB_TYPES type; |
---|
265 | GB_ERROR error; |
---|
266 | if (gbdataptr != 0) { |
---|
267 | gbptr = GB_entry(gbdataptr,feld); |
---|
268 | } |
---|
269 | if (gbptr != 0) { |
---|
270 | type = GB_read_type(gbptr); |
---|
271 | if (type == GB_INT) { |
---|
272 | error = GB_write_int(gbptr,eintrag); |
---|
273 | if (error == 0) { |
---|
274 | return 0; } |
---|
275 | return new AD_ERR("writeint not possible"); |
---|
276 | } |
---|
277 | return new AD_ERR("writeint on non string entry!"); |
---|
278 | //return new AD_ERR("writeint on non string entry!",AD_ERR_WARNING); |
---|
279 | } |
---|
280 | return new AD_ERR("writeint: feld not existing",CORE); |
---|
281 | } |
---|
282 | |
---|
283 | AD_ERR *AD_READWRITE::writefloat(char *feld,float eintrag) { |
---|
284 | GBDATA *gbptr = 0; |
---|
285 | GB_TYPES type; |
---|
286 | GB_ERROR error; |
---|
287 | if (gbdataptr != 0) { |
---|
288 | gbptr = GB_entry(gbdataptr,feld); |
---|
289 | } |
---|
290 | if (gbptr != 0) { |
---|
291 | type = GB_read_type(gbptr); |
---|
292 | if (type == GB_FLOAT) { |
---|
293 | error = GB_write_float(gbptr,eintrag); |
---|
294 | if (error == 0) { |
---|
295 | return 0; } |
---|
296 | return new AD_ERR("writefloat not possible"); |
---|
297 | } |
---|
298 | return new AD_ERR("writefloat on non string entry!"); |
---|
299 | //return new AD_ERR("writefloat on non string entry!",AD_ERR_WARNING); |
---|
300 | } |
---|
301 | return new AD_ERR("writefloat: feld not existing",CORE); |
---|
302 | } |
---|
303 | |
---|
304 | AD_ERR *AD_READWRITE::create_entry(char *key, AD_TYPES type) { |
---|
305 | GBDATA *newentry = 0; |
---|
306 | if (gbdataptr == 0) { |
---|
307 | return new AD_ERR("AD_READWRITE::create_entry : not inited right"); |
---|
308 | } |
---|
309 | // place to check the rights for creation of a new entry |
---|
310 | // not yet implemented |
---|
311 | newentry = GB_create(gbdataptr,key,(GB_TYPES)type); |
---|
312 | // NULL standing for previous GBDATA and is so not used |
---|
313 | if (newentry == 0) { |
---|
314 | return new AD_ERR("AD_READWRITE::create_entry didn't work",CORE); |
---|
315 | } |
---|
316 | return 0; |
---|
317 | } |
---|
318 | |
---|
319 | AD_TYPES AD_READWRITE::read_type(char *key) { |
---|
320 | GBDATA *gbptr =0; |
---|
321 | if (gbdataptr != 0) { |
---|
322 | gbptr = GB_entry(gbdataptr,key); |
---|
323 | } |
---|
324 | if (gbptr != 0) { |
---|
325 | return (AD_TYPES)GB_read_type(gbptr); |
---|
326 | } |
---|
327 | return ad_none; |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | |
---|
332 | /********************************************* |
---|
333 | AD_ERR |
---|
334 | *************************/ |
---|
335 | |
---|
336 | AD_ERR::~AD_ERR() |
---|
337 | { |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | AD_ERR::AD_ERR (const char *pntr) |
---|
342 | // setzt den Fehlertext und zeigt ihn an |
---|
343 | { |
---|
344 | text = (char *)pntr; |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | AD_ERR::AD_ERR (void ) |
---|
349 | // setzt den Fehlertext und zeigt ihn an |
---|
350 | { |
---|
351 | text = 0; |
---|
352 | printf("%c\n",7); |
---|
353 | } |
---|
354 | |
---|
355 | AD_ERR::AD_ERR (const char *pntr, const int core) |
---|
356 | // setzt den Fehlertext |
---|
357 | // bricht ab |
---|
358 | // -> besseres Debugging |
---|
359 | // wird bei flascher Anwendung der AD_~Klassen verwendet |
---|
360 | |
---|
361 | { |
---|
362 | text = (char *)pntr; |
---|
363 | //cout << "ERROR in ARBDB++: \n" << text << "\n"; |
---|
364 | //cout.flush(); |
---|
365 | if (core == CORE) |
---|
366 | ADPP_CORE; // -segmantation Fault |
---|
367 | } |
---|
368 | |
---|
369 | char *AD_ERR::show() |
---|
370 | { |
---|
371 | return text; |
---|
372 | } |
---|