1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : arbdb.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #if defined(DARWIN) |
---|
12 | #include <cstdio> |
---|
13 | #endif // DARWIN |
---|
14 | |
---|
15 | #include <rpc/types.h> |
---|
16 | #include <rpc/xdr.h> |
---|
17 | |
---|
18 | #include "ad_hcb.h" |
---|
19 | #include "gb_comm.h" |
---|
20 | #include "gb_compress.h" |
---|
21 | #include "gb_localdata.h" |
---|
22 | #include "gb_ta.h" |
---|
23 | #include "gb_ts.h" |
---|
24 | #include "gb_index.h" |
---|
25 | #include <arb_strarray.h> |
---|
26 | |
---|
27 | #include <glib.h> |
---|
28 | #include <glib/gprintf.h> |
---|
29 | |
---|
30 | gb_local_data *gb_local = 0; |
---|
31 | |
---|
32 | #define INIT_TYPE_NAME(t) GB_TYPES_name[t] = #t |
---|
33 | |
---|
34 | static const char *GB_TYPES_2_name(GB_TYPES type) { |
---|
35 | static const char *GB_TYPES_name[GB_TYPE_MAX]; |
---|
36 | static bool initialized = false; |
---|
37 | |
---|
38 | if (!initialized) { |
---|
39 | memset(GB_TYPES_name, 0, sizeof(GB_TYPES_name)); |
---|
40 | INIT_TYPE_NAME(GB_NONE); |
---|
41 | INIT_TYPE_NAME(GB_BIT); |
---|
42 | INIT_TYPE_NAME(GB_BYTE); |
---|
43 | INIT_TYPE_NAME(GB_INT); |
---|
44 | INIT_TYPE_NAME(GB_FLOAT); |
---|
45 | INIT_TYPE_NAME(GB_POINTER); |
---|
46 | INIT_TYPE_NAME(GB_BITS); |
---|
47 | INIT_TYPE_NAME(GB_BYTES); |
---|
48 | INIT_TYPE_NAME(GB_INTS); |
---|
49 | INIT_TYPE_NAME(GB_FLOATS); |
---|
50 | INIT_TYPE_NAME(GB_LINK); |
---|
51 | INIT_TYPE_NAME(GB_STRING); |
---|
52 | INIT_TYPE_NAME(GB_STRING_SHRT); |
---|
53 | INIT_TYPE_NAME(GB_DB); |
---|
54 | initialized = true; |
---|
55 | } |
---|
56 | |
---|
57 | const char *name = NULL; |
---|
58 | if (type >= 0 && type<GB_TYPE_MAX) name = GB_TYPES_name[type]; |
---|
59 | if (!name) { |
---|
60 | static char *unknownType = 0; |
---|
61 | freeset(unknownType, GBS_global_string_copy("<invalid-type=%i>", type)); |
---|
62 | name = unknownType; |
---|
63 | } |
---|
64 | return name; |
---|
65 | } |
---|
66 | |
---|
67 | const char *GB_get_type_name(GBDATA *gbd) { |
---|
68 | return GB_TYPES_2_name(gbd->type()); |
---|
69 | } |
---|
70 | |
---|
71 | inline GB_ERROR gb_transactable_type(GB_TYPES type, GBDATA *gbd) { |
---|
72 | GB_ERROR error = NULL; |
---|
73 | if (GB_MAIN(gbd)->get_transaction_level() == 0) { |
---|
74 | error = "No transaction running"; |
---|
75 | } |
---|
76 | else if (GB_ARRAY_FLAGS(gbd).changed == GB_DELETED) { |
---|
77 | error = "Entry has been deleted"; |
---|
78 | } |
---|
79 | else { |
---|
80 | GB_TYPES gb_type = gbd->type(); |
---|
81 | if (gb_type != type && (type != GB_STRING || gb_type != GB_LINK)) { |
---|
82 | char *rtype = strdup(GB_TYPES_2_name(type)); |
---|
83 | char *rgb_type = strdup(GB_TYPES_2_name(gb_type)); |
---|
84 | |
---|
85 | error = GBS_global_string("type mismatch (want='%s', got='%s') in '%s'", rtype, rgb_type, GB_get_db_path(gbd)); |
---|
86 | |
---|
87 | free(rgb_type); |
---|
88 | free(rtype); |
---|
89 | } |
---|
90 | } |
---|
91 | if (error) { |
---|
92 | GBK_dump_backtrace(stderr, error); // it's a bug: none of the above errors should ever happen |
---|
93 | gb_assert(0); |
---|
94 | } |
---|
95 | return error; |
---|
96 | } |
---|
97 | |
---|
98 | __ATTR__USERESULT static GB_ERROR gb_security_error(GBDATA *gbd) { |
---|
99 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
100 | const char *error = GBS_global_string("Protection: Attempt to change a level-%i-'%s'-entry,\n" |
---|
101 | "but your current security level is only %i", |
---|
102 | GB_GET_SECURITY_WRITE(gbd), |
---|
103 | GB_read_key_pntr(gbd), |
---|
104 | Main->security_level); |
---|
105 | #if defined(DEBUG) |
---|
106 | fprintf(stderr, "%s\n", error); |
---|
107 | #endif // DEBUG |
---|
108 | return error; |
---|
109 | } |
---|
110 | |
---|
111 | inline GB_ERROR gb_type_writeable_to(GB_TYPES type, GBDATA *gbd) { |
---|
112 | GB_ERROR error = gb_transactable_type(type, gbd); |
---|
113 | if (!error) { |
---|
114 | if (GB_GET_SECURITY_WRITE(gbd) > GB_MAIN(gbd)->security_level) { |
---|
115 | error = gb_security_error(gbd); |
---|
116 | } |
---|
117 | } |
---|
118 | return error; |
---|
119 | } |
---|
120 | inline GB_ERROR gb_type_readable_from(GB_TYPES type, GBDATA *gbd) { |
---|
121 | return gb_transactable_type(type, gbd); |
---|
122 | } |
---|
123 | |
---|
124 | inline GB_ERROR error_with_dbentry(const char *action, GBDATA *gbd, GB_ERROR error) { |
---|
125 | const char *path = GB_get_db_path(gbd); |
---|
126 | return GBS_global_string("Can't %s '%s':\n%s", action, path, error); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | #define RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) \ |
---|
131 | do { \ |
---|
132 | GB_ERROR error = gb_type_writeable_to(type, gbd); \ |
---|
133 | if (error) { \ |
---|
134 | return error_with_dbentry("write", gbd, error); \ |
---|
135 | } \ |
---|
136 | } while(0) |
---|
137 | |
---|
138 | #define EXPORT_ERROR_AND_RETURN_0_IF_NOT_READABLE_AS_TYPE(gbd, type) \ |
---|
139 | do { \ |
---|
140 | GB_ERROR error = gb_type_readable_from(type, gbd); \ |
---|
141 | if (error) { \ |
---|
142 | error = error_with_dbentry("read", gbd, error); \ |
---|
143 | GB_export_error(error); \ |
---|
144 | return 0; \ |
---|
145 | } \ |
---|
146 | } while(0) \ |
---|
147 | |
---|
148 | |
---|
149 | #if defined(WARN_TODO) |
---|
150 | #warning replace GB_TEST_READ / GB_TEST_READ by new names later |
---|
151 | #endif |
---|
152 | |
---|
153 | #define GB_TEST_READ(gbd, type, ignored) EXPORT_ERROR_AND_RETURN_0_IF_NOT_READABLE_AS_TYPE(gbd, type) |
---|
154 | #define GB_TEST_WRITE(gbd, type, ignored) RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) |
---|
155 | |
---|
156 | #define GB_TEST_NON_BUFFER(x, gerror) \ |
---|
157 | do { \ |
---|
158 | if (GB_is_in_buffer(x)) { \ |
---|
159 | GBK_terminatef("%s: you are not allowed to write any data, which you get by pntr", gerror); \ |
---|
160 | } \ |
---|
161 | } while (0) |
---|
162 | |
---|
163 | |
---|
164 | static GB_ERROR GB_safe_atof(const char *str, double *res) { |
---|
165 | GB_ERROR error = NULL; |
---|
166 | char *end; |
---|
167 | *res = strtod(str, &end); |
---|
168 | if (end == str || end[0] != 0) { |
---|
169 | if (!str[0]) { |
---|
170 | *res = 0.0; |
---|
171 | } |
---|
172 | else { |
---|
173 | error = GBS_global_string("cannot convert '%s' to double", str); |
---|
174 | } |
---|
175 | } |
---|
176 | return error; |
---|
177 | } |
---|
178 | |
---|
179 | double GB_atof(const char *str) { |
---|
180 | // convert ASCII to double |
---|
181 | double res = 0; |
---|
182 | GB_ERROR err = GB_safe_atof(str, &res); |
---|
183 | if (err) { |
---|
184 | // expected double in 'str'- better use GB_safe_atof() |
---|
185 | GBK_terminatef("GB_safe_atof(\"%s\", ..) returns error: %s", str, err); |
---|
186 | } |
---|
187 | return res; |
---|
188 | } |
---|
189 | |
---|
190 | // --------------------------- |
---|
191 | // compression tables |
---|
192 | |
---|
193 | const int gb_convert_type_2_compression_flags[] = { |
---|
194 | GB_COMPRESSION_NONE, // GB_NONE 0 |
---|
195 | GB_COMPRESSION_NONE, // GB_BIT 1 |
---|
196 | GB_COMPRESSION_NONE, // GB_BYTE 2 |
---|
197 | GB_COMPRESSION_NONE, // GB_INT 3 |
---|
198 | GB_COMPRESSION_NONE, // GB_FLOAT 4 |
---|
199 | GB_COMPRESSION_NONE, // GB_?? 5 |
---|
200 | GB_COMPRESSION_BITS, // GB_BITS 6 |
---|
201 | GB_COMPRESSION_NONE, // GB_?? 7 |
---|
202 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN, // GB_BYTES 8 |
---|
203 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_INTS 9 |
---|
204 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_FLTS 10 |
---|
205 | GB_COMPRESSION_NONE, // GB_LINK 11 |
---|
206 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_DICTIONARY, // GB_STR 12 |
---|
207 | GB_COMPRESSION_NONE, // GB_STRS 13 |
---|
208 | GB_COMPRESSION_NONE, // GB?? 14 |
---|
209 | GB_COMPRESSION_NONE // GB_DB 15 |
---|
210 | }; |
---|
211 | |
---|
212 | int gb_convert_type_2_sizeof[] = { /* contains the unit-size of data stored in DB, |
---|
213 | * i.e. realsize = unit_size * size() |
---|
214 | */ |
---|
215 | 0, // GB_NONE 0 |
---|
216 | 0, // GB_BIT 1 |
---|
217 | sizeof(char), // GB_BYTE 2 |
---|
218 | sizeof(int), // GB_INT 3 |
---|
219 | sizeof(float), // GB_FLOAT 4 |
---|
220 | 0, // GB_?? 5 |
---|
221 | 0, // GB_BITS 6 |
---|
222 | 0, // GB_?? 7 |
---|
223 | sizeof(char), // GB_BYTES 8 |
---|
224 | sizeof(int), // GB_INTS 9 |
---|
225 | sizeof(float), // GB_FLTS 10 |
---|
226 | sizeof(char), // GB_LINK 11 |
---|
227 | sizeof(char), // GB_STR 12 |
---|
228 | sizeof(char), // GB_STRS 13 |
---|
229 | 0, // GB_?? 14 |
---|
230 | 0, // GB_DB 15 |
---|
231 | }; |
---|
232 | |
---|
233 | int gb_convert_type_2_appendix_size[] = { /* contains the size of the suffix (aka terminator element) |
---|
234 | * size is in bytes |
---|
235 | */ |
---|
236 | |
---|
237 | 0, // GB_NONE 0 |
---|
238 | 0, // GB_BIT 1 |
---|
239 | 0, // GB_BYTE 2 |
---|
240 | 0, // GB_INT 3 |
---|
241 | 0, // GB_FLOAT 4 |
---|
242 | 0, // GB_?? 5 |
---|
243 | 0, // GB_BITS 6 |
---|
244 | 0, // GB_?? 7 |
---|
245 | 0, // GB_BYTES 8 |
---|
246 | 0, // GB_INTS 9 |
---|
247 | 0, // GB_FLTS 10 |
---|
248 | 1, // GB_LINK 11 (zero terminated) |
---|
249 | 1, // GB_STR 12 (zero terminated) |
---|
250 | 1, // GB_STRS 13 (zero terminated) |
---|
251 | 0, // GB_?? 14 |
---|
252 | 0, // GB_DB 15 |
---|
253 | }; |
---|
254 | |
---|
255 | |
---|
256 | // --------------------------------- |
---|
257 | // local buffer management |
---|
258 | |
---|
259 | static void init_buffer(gb_buffer *buf, size_t initial_size) { |
---|
260 | buf->size = initial_size; |
---|
261 | buf->mem = buf->size ? (char*)malloc(buf->size) : NULL; |
---|
262 | } |
---|
263 | |
---|
264 | static char *check_out_buffer(gb_buffer *buf) { |
---|
265 | char *checkOut = buf->mem; |
---|
266 | |
---|
267 | buf->mem = 0; |
---|
268 | buf->size = 0; |
---|
269 | |
---|
270 | return checkOut; |
---|
271 | } |
---|
272 | |
---|
273 | static void alloc_buffer(gb_buffer *buf, size_t size) { |
---|
274 | free(buf->mem); |
---|
275 | buf->size = size; |
---|
276 | #if (MEMORY_TEST==1) |
---|
277 | buf->mem = (char *)malloc(buf->size); |
---|
278 | #else |
---|
279 | buf->mem = (char *)GB_calloc(buf->size, 1); |
---|
280 | #endif |
---|
281 | } |
---|
282 | |
---|
283 | static GB_BUFFER give_buffer(gb_buffer *buf, size_t size) { |
---|
284 | #if (MEMORY_TEST==1) |
---|
285 | alloc_buffer(buf, size); // do NOT reuse buffer if testing memory |
---|
286 | #else |
---|
287 | if (size >= buf->size) { |
---|
288 | alloc_buffer(buf, size); |
---|
289 | } |
---|
290 | #endif |
---|
291 | return buf->mem; |
---|
292 | } |
---|
293 | |
---|
294 | static int is_in_buffer(gb_buffer *buf, GB_CBUFFER ptr) { |
---|
295 | return ptr >= buf->mem && ptr < buf->mem+buf->size; |
---|
296 | } |
---|
297 | |
---|
298 | // ------------------------------ |
---|
299 | |
---|
300 | GB_BUFFER GB_give_buffer(size_t size) { |
---|
301 | // return a pointer to a static piece of memory at least size bytes long |
---|
302 | return give_buffer(&gb_local->buf1, size); |
---|
303 | } |
---|
304 | |
---|
305 | GB_BUFFER GB_increase_buffer(size_t size) { |
---|
306 | if (size < gb_local->buf1.size) { |
---|
307 | char *old_buffer = gb_local->buf1.mem; |
---|
308 | size_t old_size = gb_local->buf1.size; |
---|
309 | |
---|
310 | gb_local->buf1.mem = NULL; |
---|
311 | alloc_buffer(&gb_local->buf1, size); |
---|
312 | memcpy(gb_local->buf1.mem, old_buffer, old_size); |
---|
313 | |
---|
314 | free(old_buffer); |
---|
315 | } |
---|
316 | return gb_local->buf1.mem; |
---|
317 | } |
---|
318 | |
---|
319 | NOT4PERL int GB_give_buffer_size() { |
---|
320 | return gb_local->buf1.size; |
---|
321 | } |
---|
322 | |
---|
323 | GB_BUFFER GB_give_buffer2(long size) { |
---|
324 | return give_buffer(&gb_local->buf2, size); |
---|
325 | } |
---|
326 | |
---|
327 | static int GB_is_in_buffer(GB_CBUFFER ptr) { |
---|
328 | /* returns 1 or 2 if 'ptr' points to gb_local->buf1/buf2 |
---|
329 | * returns 0 otherwise |
---|
330 | */ |
---|
331 | int buffer = 0; |
---|
332 | |
---|
333 | if (is_in_buffer(&gb_local->buf1, ptr)) buffer = 1; |
---|
334 | else if (is_in_buffer(&gb_local->buf2, ptr)) buffer = 2; |
---|
335 | |
---|
336 | return buffer; |
---|
337 | } |
---|
338 | |
---|
339 | char *GB_check_out_buffer(GB_CBUFFER buffer) { |
---|
340 | /* Check a piece of memory out of the buffer management |
---|
341 | * after it is checked out, the user has the full control to use and free it |
---|
342 | * Returns a pointer to the start of the buffer (even if 'buffer' points inside the buffer!) |
---|
343 | */ |
---|
344 | char *old = 0; |
---|
345 | |
---|
346 | if (is_in_buffer(&gb_local->buf1, buffer)) old = check_out_buffer(&gb_local->buf1); |
---|
347 | else if (is_in_buffer(&gb_local->buf2, buffer)) old = check_out_buffer(&gb_local->buf2); |
---|
348 | |
---|
349 | return old; |
---|
350 | } |
---|
351 | |
---|
352 | GB_BUFFER GB_give_other_buffer(GB_CBUFFER buffer, long size) { |
---|
353 | return is_in_buffer(&gb_local->buf1, buffer) |
---|
354 | ? GB_give_buffer2(size) |
---|
355 | : GB_give_buffer(size); |
---|
356 | } |
---|
357 | |
---|
358 | static unsigned char GB_BIT_compress_data[] = { |
---|
359 | 0x1d, GB_CS_OK, 0, 0, |
---|
360 | 0x04, GB_CS_OK, 0, 1, |
---|
361 | 0x0a, GB_CS_OK, 0, 2, |
---|
362 | 0x0b, GB_CS_OK, 0, 3, |
---|
363 | 0x0c, GB_CS_OK, 0, 4, |
---|
364 | 0x1a, GB_CS_OK, 0, 5, |
---|
365 | 0x1b, GB_CS_OK, 0, 6, |
---|
366 | 0x1c, GB_CS_OK, 0, 7, |
---|
367 | 0xf0, GB_CS_OK, 0, 8, |
---|
368 | 0xf1, GB_CS_OK, 0, 9, |
---|
369 | 0xf2, GB_CS_OK, 0, 10, |
---|
370 | 0xf3, GB_CS_OK, 0, 11, |
---|
371 | 0xf4, GB_CS_OK, 0, 12, |
---|
372 | 0xf5, GB_CS_OK, 0, 13, |
---|
373 | 0xf6, GB_CS_OK, 0, 14, |
---|
374 | 0xf7, GB_CS_OK, 0, 15, |
---|
375 | 0xf8, GB_CS_SUB, 0, 16, |
---|
376 | 0xf9, GB_CS_SUB, 0, 32, |
---|
377 | 0xfa, GB_CS_SUB, 0, 48, |
---|
378 | 0xfb, GB_CS_SUB, 0, 64, |
---|
379 | 0xfc, GB_CS_SUB, 0, 128, |
---|
380 | 0xfd, GB_CS_SUB, 1, 0, |
---|
381 | 0xfe, GB_CS_SUB, 2, 0, |
---|
382 | 0xff, GB_CS_SUB, 4, 0, |
---|
383 | 0 |
---|
384 | }; |
---|
385 | |
---|
386 | struct gb_exitfun { |
---|
387 | void (*exitfun)(); |
---|
388 | gb_exitfun *next; |
---|
389 | }; |
---|
390 | |
---|
391 | void GB_atexit(void (*exitfun)()) { |
---|
392 | // called when GB_shell is destroyed (use similar to atexit()) |
---|
393 | // |
---|
394 | // Since the program does not neccessarily terminate, your code calling |
---|
395 | // GB_atexit() may run multiple times. Make sure everything is completely reset by your 'exitfun' |
---|
396 | |
---|
397 | gb_exitfun *fun = new gb_exitfun; |
---|
398 | fun->exitfun = exitfun; |
---|
399 | |
---|
400 | fun->next = gb_local->atgbexit; |
---|
401 | gb_local->atgbexit = fun; |
---|
402 | } |
---|
403 | |
---|
404 | static void run_and_destroy_exit_functions(gb_exitfun *fun) { |
---|
405 | if (fun) { |
---|
406 | fun->exitfun(); |
---|
407 | run_and_destroy_exit_functions(fun->next); |
---|
408 | delete fun; |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | static void GB_exit_gb() { |
---|
413 | GB_shell::ensure_inside(); |
---|
414 | |
---|
415 | if (gb_local) { |
---|
416 | gb_local->~gb_local_data(); // inplace-dtor |
---|
417 | gbm_free_mem(gb_local, sizeof(*gb_local), 0); |
---|
418 | gb_local = NULL; |
---|
419 | gbm_flush_mem(); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | gb_local_data::~gb_local_data() { |
---|
424 | gb_assert(openedDBs == closedDBs); |
---|
425 | |
---|
426 | run_and_destroy_exit_functions(atgbexit); |
---|
427 | |
---|
428 | free(bitcompress); |
---|
429 | gb_free_compress_tree(bituncompress); |
---|
430 | free(write_buffer); |
---|
431 | |
---|
432 | free(check_out_buffer(&buf2)); |
---|
433 | free(check_out_buffer(&buf1)); |
---|
434 | free(open_gb_mains); |
---|
435 | } |
---|
436 | |
---|
437 | // ----------------- |
---|
438 | // GB_shell |
---|
439 | |
---|
440 | |
---|
441 | static GB_shell *inside_shell = NULL; |
---|
442 | |
---|
443 | GB_shell::GB_shell() { |
---|
444 | if (inside_shell) GBK_terminate("only one GB_shell allowed"); |
---|
445 | inside_shell = this; |
---|
446 | } |
---|
447 | GB_shell::~GB_shell() { |
---|
448 | gb_assert(inside_shell == this); |
---|
449 | GB_exit_gb(); |
---|
450 | inside_shell = NULL; |
---|
451 | } |
---|
452 | void GB_shell::ensure_inside() { if (!inside_shell) GBK_terminate("Not inside GB_shell"); } |
---|
453 | |
---|
454 | bool GB_shell::in_shell() { // used by code based on ARBDB (Kai IIRC) |
---|
455 | return inside_shell; |
---|
456 | } |
---|
457 | |
---|
458 | struct GB_test_shell_closed { |
---|
459 | ~GB_test_shell_closed() { |
---|
460 | if (GB_shell::in_shell()) { // leave that call |
---|
461 | inside_shell->~GB_shell(); // call dtor |
---|
462 | } |
---|
463 | } |
---|
464 | }; |
---|
465 | static GB_test_shell_closed test; |
---|
466 | |
---|
467 | #if defined(UNIT_TESTS) |
---|
468 | static bool closed_open_shell_for_unit_tests() { |
---|
469 | bool was_open = inside_shell; |
---|
470 | if (was_open) { |
---|
471 | if (gb_local) gb_local->fake_closed_DBs(); |
---|
472 | inside_shell->~GB_shell(); // just call dtor (not delete) |
---|
473 | } |
---|
474 | return was_open; |
---|
475 | } |
---|
476 | #endif |
---|
477 | |
---|
478 | void GB_init_gb() { |
---|
479 | GB_shell::ensure_inside(); |
---|
480 | if (!gb_local) { |
---|
481 | GBK_install_SIGSEGV_handler(true); // never uninstalled |
---|
482 | gbm_init_mem(); |
---|
483 | gb_local = (gb_local_data *)gbm_get_mem(sizeof(gb_local_data), 0); |
---|
484 | ::new(gb_local) gb_local_data(); // inplace-ctor |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | int GB_open_DBs() { return gb_local ? gb_local->open_dbs() : 0; } |
---|
489 | |
---|
490 | gb_local_data::gb_local_data() |
---|
491 | { |
---|
492 | init_buffer(&buf1, 4000); |
---|
493 | init_buffer(&buf2, 4000); |
---|
494 | |
---|
495 | write_bufsize = GBCM_BUFFER; |
---|
496 | write_buffer = (char *)malloc((size_t)write_bufsize); |
---|
497 | write_ptr = write_buffer; |
---|
498 | write_free = write_bufsize; |
---|
499 | |
---|
500 | bituncompress = gb_build_uncompress_tree(GB_BIT_compress_data, 1, 0); |
---|
501 | bitcompress = gb_build_compress_list(GB_BIT_compress_data, 1, &(bc_size)); |
---|
502 | |
---|
503 | openedDBs = 0; |
---|
504 | closedDBs = 0; |
---|
505 | |
---|
506 | open_gb_mains = NULL; |
---|
507 | open_gb_alloc = 0; |
---|
508 | |
---|
509 | atgbexit = NULL; |
---|
510 | |
---|
511 | iamclient = false; |
---|
512 | search_system_folder = false; |
---|
513 | running_client_transaction = ARB_NO_TRANS; |
---|
514 | } |
---|
515 | |
---|
516 | void gb_local_data::announce_db_open(GB_MAIN_TYPE *Main) { |
---|
517 | gb_assert(Main); |
---|
518 | int idx = open_dbs(); |
---|
519 | if (idx >= open_gb_alloc) { |
---|
520 | int new_alloc = open_gb_alloc+10; |
---|
521 | GB_MAIN_TYPE **new_mains = (GB_MAIN_TYPE**)realloc(open_gb_mains, new_alloc*sizeof(*new_mains)); |
---|
522 | memset(new_mains+open_gb_alloc, 0, 10*sizeof(*new_mains)); |
---|
523 | open_gb_alloc = new_alloc; |
---|
524 | open_gb_mains = new_mains; |
---|
525 | } |
---|
526 | open_gb_mains[idx] = Main; |
---|
527 | openedDBs++; |
---|
528 | } |
---|
529 | |
---|
530 | void gb_local_data::announce_db_close(GB_MAIN_TYPE *Main) { |
---|
531 | gb_assert(Main); |
---|
532 | int open = open_dbs(); |
---|
533 | int idx; |
---|
534 | for (idx = 0; idx<open; ++idx) if (open_gb_mains[idx] == Main) break; |
---|
535 | |
---|
536 | gb_assert(idx<open); // passed gb_main is unknown |
---|
537 | if (idx<open) { |
---|
538 | if (idx<(open-1)) { // not last |
---|
539 | open_gb_mains[idx] = open_gb_mains[open-1]; |
---|
540 | } |
---|
541 | closedDBs++; |
---|
542 | } |
---|
543 | if (closedDBs == openedDBs) { |
---|
544 | GB_exit_gb(); // free most memory allocated by ARBDB library |
---|
545 | // Caution: calling GB_exit_gb() frees 'this'! |
---|
546 | } |
---|
547 | } |
---|
548 | |
---|
549 | static GBDATA *gb_remembered_db() { |
---|
550 | GB_MAIN_TYPE *Main = gb_local ? gb_local->get_any_open_db() : NULL; |
---|
551 | return Main ? Main->gb_main() : NULL; |
---|
552 | } |
---|
553 | |
---|
554 | GB_ERROR gb_unfold(GBCONTAINER *gbc, long deep, int index_pos) { |
---|
555 | /*! get data from server. |
---|
556 | * |
---|
557 | * @param gbc container to unfold |
---|
558 | * @param deep if != 0, then get subitems too. |
---|
559 | * @param index_pos |
---|
560 | * - >= 0, get indexed item from server |
---|
561 | * - <0, get all items |
---|
562 | * |
---|
563 | * @return error on failure |
---|
564 | */ |
---|
565 | |
---|
566 | GB_ERROR error; |
---|
567 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
---|
568 | |
---|
569 | if (!gbc->flags2.folded_container) return 0; |
---|
570 | if (index_pos> gbc->d.nheader) gb_create_header_array(gbc, index_pos + 1); |
---|
571 | if (index_pos >= 0 && GB_HEADER_LIST_GBD(header[index_pos])) return 0; |
---|
572 | |
---|
573 | if (GBCONTAINER_MAIN(gbc)->is_server()) { |
---|
574 | GB_internal_error("Cannot unfold in server"); |
---|
575 | return 0; |
---|
576 | } |
---|
577 | |
---|
578 | do { |
---|
579 | if (index_pos<0) break; |
---|
580 | if (index_pos >= gbc->d.nheader) break; |
---|
581 | if (header[index_pos].flags.changed >= GB_DELETED) { |
---|
582 | GB_internal_error("Tried to unfold a deleted item"); |
---|
583 | return 0; |
---|
584 | } |
---|
585 | if (GB_HEADER_LIST_GBD(header[index_pos])) return 0; // already unfolded |
---|
586 | } while (0); |
---|
587 | |
---|
588 | error = gbcm_unfold_client(gbc, deep, index_pos); |
---|
589 | if (error) { |
---|
590 | GB_print_error(); |
---|
591 | return error; |
---|
592 | } |
---|
593 | |
---|
594 | if (index_pos<0) { |
---|
595 | gb_untouch_children(gbc); |
---|
596 | gbc->flags2.folded_container = 0; |
---|
597 | } |
---|
598 | else { |
---|
599 | GBDATA *gb2 = GBCONTAINER_ELEM(gbc, index_pos); |
---|
600 | if (gb2) { |
---|
601 | if (gb2->is_container()) { |
---|
602 | gb_untouch_children_and_me(gb2->as_container()); |
---|
603 | } |
---|
604 | else { |
---|
605 | gb_untouch_me(gb2->as_entry()); |
---|
606 | } |
---|
607 | } |
---|
608 | } |
---|
609 | return 0; |
---|
610 | } |
---|
611 | |
---|
612 | // ----------------------- |
---|
613 | // close database |
---|
614 | |
---|
615 | typedef void (*gb_close_callback)(GBDATA *gb_main, void *client_data); |
---|
616 | |
---|
617 | struct gb_close_callback_list { |
---|
618 | gb_close_callback_list *next; |
---|
619 | gb_close_callback cb; |
---|
620 | void *client_data; |
---|
621 | }; |
---|
622 | |
---|
623 | #if defined(ASSERTION_USED) |
---|
624 | static bool atclose_cb_exists(gb_close_callback_list *gccs, gb_close_callback cb) { |
---|
625 | return gccs && (gccs->cb == cb || atclose_cb_exists(gccs->next, cb)); |
---|
626 | } |
---|
627 | #endif // ASSERTION_USED |
---|
628 | |
---|
629 | void GB_atclose(GBDATA *gbd, void (*fun)(GBDATA *gb_main, void *client_data), void *client_data) { |
---|
630 | /*! Add a callback, which gets called directly before GB_close destroys all data. |
---|
631 | * This is the recommended way to remove all callbacks from DB elements. |
---|
632 | */ |
---|
633 | |
---|
634 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
635 | |
---|
636 | gb_assert(!atclose_cb_exists(Main->close_callbacks, fun)); // each close callback should only exist once |
---|
637 | |
---|
638 | gb_close_callback_list *gccs = (gb_close_callback_list *)malloc(sizeof(*gccs)); |
---|
639 | |
---|
640 | gccs->next = Main->close_callbacks; |
---|
641 | gccs->cb = fun; |
---|
642 | gccs->client_data = client_data; |
---|
643 | |
---|
644 | Main->close_callbacks = gccs; |
---|
645 | } |
---|
646 | |
---|
647 | static void run_close_callbacks(GBDATA *gb_main, gb_close_callback_list *gccs) { |
---|
648 | while (gccs) { |
---|
649 | gccs->cb(gb_main, gccs->client_data); |
---|
650 | gb_close_callback_list *next = gccs->next; |
---|
651 | free(gccs); |
---|
652 | gccs = next; |
---|
653 | } |
---|
654 | } |
---|
655 | |
---|
656 | static gb_triggered_callback *currently_called_back = NULL; // points to callback during callback; NULL otherwise |
---|
657 | static GB_MAIN_TYPE *inside_callback_main = NULL; // points to DB root during callback; NULL otherwise |
---|
658 | |
---|
659 | void gb_pending_callbacks::call_and_forget(GB_CB_TYPE allowedTypes) { |
---|
660 | #if defined(ASSERTION_USED) |
---|
661 | const gb_triggered_callback *tail = get_tail(); |
---|
662 | #endif |
---|
663 | |
---|
664 | for (itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
665 | currently_called_back = &*cb; |
---|
666 | gb_assert(currently_called_back); |
---|
667 | currently_called_back->spec(cb->gbd, allowedTypes); |
---|
668 | currently_called_back = NULL; |
---|
669 | } |
---|
670 | |
---|
671 | gb_assert(tail == get_tail()); |
---|
672 | |
---|
673 | callbacks.clear(); |
---|
674 | } |
---|
675 | |
---|
676 | void GB_MAIN_TYPE::call_pending_callbacks() { |
---|
677 | inside_callback_main = this; |
---|
678 | |
---|
679 | deleteCBs.pending.call_and_forget(GB_CB_DELETE); // first all delete callbacks: |
---|
680 | changeCBs.pending.call_and_forget(GB_CB_ALL_BUT_DELETE); // then all change callbacks: |
---|
681 | |
---|
682 | inside_callback_main = NULL; |
---|
683 | } |
---|
684 | |
---|
685 | inline void GB_MAIN_TYPE::callback_group::forget_hcbs() { |
---|
686 | delete hierarchy_cbs; |
---|
687 | hierarchy_cbs = NULL; |
---|
688 | } |
---|
689 | |
---|
690 | void GB_MAIN_TYPE::forget_hierarchy_cbs() { |
---|
691 | changeCBs.forget_hcbs(); |
---|
692 | deleteCBs.forget_hcbs(); |
---|
693 | } |
---|
694 | |
---|
695 | void GB_close(GBDATA *gbd) { |
---|
696 | GB_ERROR error = NULL; |
---|
697 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
698 | |
---|
699 | gb_assert(Main->get_transaction_level() <= 0); // transaction running - you can't close DB yet! |
---|
700 | |
---|
701 | Main->forget_hierarchy_cbs(); |
---|
702 | |
---|
703 | gb_assert(Main->gb_main() == gbd); |
---|
704 | run_close_callbacks(gbd, Main->close_callbacks); |
---|
705 | Main->close_callbacks = 0; |
---|
706 | |
---|
707 | if (Main->is_client()) { |
---|
708 | long result = gbcmc_close(Main->c_link); |
---|
709 | if (result != 0) error = GBS_global_string("gbcmc_close returns %li", result); |
---|
710 | } |
---|
711 | |
---|
712 | gbcm_logout(Main, NULL); // logout default user |
---|
713 | |
---|
714 | if (!error) { |
---|
715 | gb_assert(Main->close_callbacks == 0); |
---|
716 | |
---|
717 | gb_delete_dummy_father(Main->dummy_father); |
---|
718 | Main->root_container = NULL; |
---|
719 | |
---|
720 | /* ARBDB applications using awars easily crash in call_pending_callbacks(), |
---|
721 | * if AWARs are still bound to elements in the closed database. |
---|
722 | * |
---|
723 | * To unlink awars call AW_root::unlink_awars_from_DB(). |
---|
724 | * If that doesn't help, test Main->data (often aka as GLOBAL_gb_main) |
---|
725 | */ |
---|
726 | Main->call_pending_callbacks(); // do all callbacks |
---|
727 | delete Main; |
---|
728 | } |
---|
729 | |
---|
730 | if (error) { |
---|
731 | GB_warningf("Error in GB_close: %s", error); |
---|
732 | } |
---|
733 | } |
---|
734 | |
---|
735 | void gb_close_unclosed_DBs() { |
---|
736 | GBDATA *gb_main; |
---|
737 | while ((gb_main = gb_remembered_db())) { |
---|
738 | GB_close(gb_main); |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | // ------------------ |
---|
743 | // read data |
---|
744 | |
---|
745 | long GB_read_int(GBDATA *gbd) |
---|
746 | { |
---|
747 | GB_TEST_READ(gbd, GB_INT, "GB_read_int"); |
---|
748 | return gbd->as_entry()->info.i; |
---|
749 | } |
---|
750 | |
---|
751 | int GB_read_byte(GBDATA *gbd) |
---|
752 | { |
---|
753 | GB_TEST_READ(gbd, GB_BYTE, "GB_read_byte"); |
---|
754 | return gbd->as_entry()->info.i; |
---|
755 | } |
---|
756 | |
---|
757 | GBDATA *GB_read_pointer(GBDATA *gbd) { |
---|
758 | GB_TEST_READ(gbd, GB_POINTER, "GB_read_pointer"); |
---|
759 | return gbd->as_entry()->info.ptr; |
---|
760 | } |
---|
761 | |
---|
762 | double GB_read_float(GBDATA *gbd) // @@@ change return type to float (that's what's stored in DB) |
---|
763 | { |
---|
764 | XDR xdrs; |
---|
765 | static float f; // @@@ why static? |
---|
766 | |
---|
767 | GB_TEST_READ(gbd, GB_FLOAT, "GB_read_float"); |
---|
768 | xdrmem_create(&xdrs, &gbd->as_entry()->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
---|
769 | xdr_float(&xdrs, &f); |
---|
770 | xdr_destroy(&xdrs); |
---|
771 | |
---|
772 | gb_assert(f == f); // !nan |
---|
773 | |
---|
774 | return (double)f; |
---|
775 | } |
---|
776 | |
---|
777 | long GB_read_count(GBDATA *gbd) { |
---|
778 | return gbd->as_entry()->size(); |
---|
779 | } |
---|
780 | |
---|
781 | long GB_read_memuse(GBDATA *gbd) { |
---|
782 | return gbd->as_entry()->memsize(); |
---|
783 | } |
---|
784 | |
---|
785 | #if defined(DEBUG) |
---|
786 | |
---|
787 | #define MIN_CBLISTNODE_SIZE 48 // minimum (found) callbacklist-elementsize |
---|
788 | |
---|
789 | #if defined(DARWIN) |
---|
790 | |
---|
791 | #define CBLISTNODE_SIZE MIN_CBLISTNODE_SIZE // assume known minimum (doesnt really matter; only used in db-browser) |
---|
792 | |
---|
793 | #else // linux: |
---|
794 | |
---|
795 | typedef std::_List_node<gb_callback_list::cbtype> CBLISTNODE_TYPE; |
---|
796 | const size_t CBLISTNODE_SIZE = sizeof(CBLISTNODE_TYPE); |
---|
797 | |
---|
798 | #if defined(ARB_64) |
---|
799 | // ignore smaller 32-bit implementations |
---|
800 | STATIC_ASSERT_ANNOTATED(MIN_CBLISTNODE_SIZE<=CBLISTNODE_SIZE, "MIN_CBLISTNODE_SIZE too big (smaller implementation detected)"); |
---|
801 | #endif |
---|
802 | |
---|
803 | #endif |
---|
804 | |
---|
805 | inline long calc_size(gb_callback_list *gbcbl) { |
---|
806 | return gbcbl |
---|
807 | ? sizeof(*gbcbl) + gbcbl->callbacks.size()* CBLISTNODE_SIZE |
---|
808 | : 0; |
---|
809 | } |
---|
810 | inline long calc_size(gb_transaction_save *gbts) { |
---|
811 | return gbts |
---|
812 | ? sizeof(*gbts) |
---|
813 | : 0; |
---|
814 | } |
---|
815 | inline long calc_size(gb_if_entries *gbie) { |
---|
816 | return gbie |
---|
817 | ? sizeof(*gbie) + calc_size(GB_IF_ENTRIES_NEXT(gbie)) |
---|
818 | : 0; |
---|
819 | } |
---|
820 | inline long calc_size(GB_REL_IFES *gbri, int table_size) { |
---|
821 | long size = 0; |
---|
822 | |
---|
823 | gb_if_entries *ifes; |
---|
824 | for (int idx = 0; idx<table_size; ++idx) { |
---|
825 | for (ifes = GB_ENTRIES_ENTRY(gbri, idx); |
---|
826 | ifes; |
---|
827 | ifes = GB_IF_ENTRIES_NEXT(ifes)) |
---|
828 | { |
---|
829 | size += calc_size(ifes); |
---|
830 | } |
---|
831 | } |
---|
832 | return size; |
---|
833 | } |
---|
834 | inline long calc_size(gb_index_files *gbif) { |
---|
835 | return gbif |
---|
836 | ? sizeof(*gbif) + calc_size(GB_INDEX_FILES_NEXT(gbif)) + calc_size(GB_INDEX_FILES_ENTRIES(gbif), gbif->hash_table_size) |
---|
837 | : 0; |
---|
838 | } |
---|
839 | inline long calc_size(gb_db_extended *gbe) { |
---|
840 | return gbe |
---|
841 | ? sizeof(*gbe) + calc_size(gbe->callback) + calc_size(gbe->old) |
---|
842 | : 0; |
---|
843 | } |
---|
844 | inline long calc_size(GBENTRY *gbe) { |
---|
845 | return gbe |
---|
846 | ? sizeof(*gbe) + calc_size(gbe->ext) |
---|
847 | : 0; |
---|
848 | } |
---|
849 | inline long calc_size(GBCONTAINER *gbc) { |
---|
850 | return gbc |
---|
851 | ? sizeof(*gbc) + calc_size(gbc->ext) + calc_size(GBCONTAINER_IFS(gbc)) |
---|
852 | : 0; |
---|
853 | } |
---|
854 | |
---|
855 | long GB_calc_structure_size(GBDATA *gbd) { |
---|
856 | long size = 0; |
---|
857 | if (gbd->is_container()) { |
---|
858 | size = calc_size(gbd->as_container()); |
---|
859 | } |
---|
860 | else { |
---|
861 | size = calc_size(gbd->as_entry()); |
---|
862 | } |
---|
863 | return size; |
---|
864 | } |
---|
865 | |
---|
866 | void GB_SizeInfo::collect(GBDATA *gbd) { |
---|
867 | if (gbd->is_container()) { |
---|
868 | ++containers; |
---|
869 | for (GBDATA *gb_child = GB_child(gbd); gb_child; gb_child = GB_nextChild(gb_child)) { |
---|
870 | collect(gb_child); |
---|
871 | } |
---|
872 | } |
---|
873 | else { |
---|
874 | ++terminals; |
---|
875 | mem += GB_read_memuse(gbd); |
---|
876 | |
---|
877 | long size; |
---|
878 | switch (gbd->type()) { |
---|
879 | case GB_INT: size = sizeof(int); break; |
---|
880 | case GB_FLOAT: size = sizeof(float); break; |
---|
881 | case GB_BYTE: size = sizeof(char); break; |
---|
882 | case GB_POINTER: size = sizeof(GBDATA*); break; |
---|
883 | case GB_STRING: size = GB_read_count(gbd); break; // accept 0 sized data for strings |
---|
884 | |
---|
885 | default: |
---|
886 | size = GB_read_count(gbd); |
---|
887 | gb_assert(size>0); // terminal w/o data - really? |
---|
888 | break; |
---|
889 | } |
---|
890 | data += size; |
---|
891 | } |
---|
892 | structure += GB_calc_structure_size(gbd); |
---|
893 | } |
---|
894 | #endif |
---|
895 | |
---|
896 | GB_CSTR GB_read_pntr(GBDATA *gbd) { |
---|
897 | GBENTRY *gbe = gbd->as_entry(); |
---|
898 | const char *data = gbe->data(); |
---|
899 | |
---|
900 | if (data) { |
---|
901 | if (gbe->flags.compressed_data) { // uncompressed data return pntr to database entry |
---|
902 | char *ca = gb_read_cache(gbe); |
---|
903 | |
---|
904 | if (!ca) { |
---|
905 | size_t size = gbe->uncompressed_size(); |
---|
906 | const char *da = gb_uncompress_data(gbe, data, size); |
---|
907 | |
---|
908 | if (da) { |
---|
909 | ca = gb_alloc_cache_index(gbe, size); |
---|
910 | memcpy(ca, da, size); |
---|
911 | } |
---|
912 | } |
---|
913 | data = ca; |
---|
914 | } |
---|
915 | } |
---|
916 | return data; |
---|
917 | } |
---|
918 | |
---|
919 | int gb_read_nr(GBDATA *gbd) { |
---|
920 | return gbd->index; |
---|
921 | } |
---|
922 | |
---|
923 | GB_CSTR GB_read_char_pntr(GBDATA *gbd) { |
---|
924 | GB_TEST_READ(gbd, GB_STRING, "GB_read_char_pntr"); |
---|
925 | return GB_read_pntr(gbd); |
---|
926 | } |
---|
927 | |
---|
928 | char *GB_read_string(GBDATA *gbd) { |
---|
929 | GB_TEST_READ(gbd, GB_STRING, "GB_read_string"); |
---|
930 | const char *d = GB_read_pntr(gbd); |
---|
931 | if (!d) return NULL; |
---|
932 | return GB_memdup(d, gbd->as_entry()->size()+1); |
---|
933 | } |
---|
934 | |
---|
935 | size_t GB_read_string_count(GBDATA *gbd) { |
---|
936 | GB_TEST_READ(gbd, GB_STRING, "GB_read_string_count"); |
---|
937 | return gbd->as_entry()->size(); |
---|
938 | } |
---|
939 | |
---|
940 | GB_CSTR GB_read_link_pntr(GBDATA *gbd) { |
---|
941 | GB_TEST_READ(gbd, GB_LINK, "GB_read_link_pntr"); |
---|
942 | return GB_read_pntr(gbd); |
---|
943 | } |
---|
944 | |
---|
945 | static char *GB_read_link(GBDATA *gbd) { |
---|
946 | const char *d; |
---|
947 | GB_TEST_READ(gbd, GB_LINK, "GB_read_link_pntr"); |
---|
948 | d = GB_read_pntr(gbd); |
---|
949 | if (!d) return NULL; |
---|
950 | return GB_memdup(d, gbd->as_entry()->size()+1); |
---|
951 | } |
---|
952 | |
---|
953 | long GB_read_bits_count(GBDATA *gbd) { |
---|
954 | GB_TEST_READ(gbd, GB_BITS, "GB_read_bits_count"); |
---|
955 | return gbd->as_entry()->size(); |
---|
956 | } |
---|
957 | |
---|
958 | GB_CSTR GB_read_bits_pntr(GBDATA *gbd, char c_0, char c_1) { |
---|
959 | GB_TEST_READ(gbd, GB_BITS, "GB_read_bits_pntr"); |
---|
960 | GBENTRY *gbe = gbd->as_entry(); |
---|
961 | long size = gbe->size(); |
---|
962 | if (size) { |
---|
963 | char *ca = gb_read_cache(gbe); |
---|
964 | if (ca) return ca; |
---|
965 | |
---|
966 | ca = gb_alloc_cache_index(gbe, size+1); |
---|
967 | const char *data = gbe->data(); |
---|
968 | char *da = gb_uncompress_bits(data, size, c_0, c_1); |
---|
969 | if (ca) { |
---|
970 | memcpy(ca, da, size+1); |
---|
971 | return ca; |
---|
972 | } |
---|
973 | return da; |
---|
974 | } |
---|
975 | return 0; |
---|
976 | } |
---|
977 | |
---|
978 | char *GB_read_bits(GBDATA *gbd, char c_0, char c_1) { |
---|
979 | GB_CSTR d = GB_read_bits_pntr(gbd, c_0, c_1); |
---|
980 | return d ? GB_memdup(d, gbd->as_entry()->size()+1) : 0; |
---|
981 | } |
---|
982 | |
---|
983 | |
---|
984 | GB_CSTR GB_read_bytes_pntr(GBDATA *gbd) |
---|
985 | { |
---|
986 | GB_TEST_READ(gbd, GB_BYTES, "GB_read_bytes_pntr"); |
---|
987 | return GB_read_pntr(gbd); |
---|
988 | } |
---|
989 | |
---|
990 | long GB_read_bytes_count(GBDATA *gbd) |
---|
991 | { |
---|
992 | GB_TEST_READ(gbd, GB_BYTES, "GB_read_bytes_count"); |
---|
993 | return gbd->as_entry()->size(); |
---|
994 | } |
---|
995 | |
---|
996 | char *GB_read_bytes(GBDATA *gbd) { |
---|
997 | GB_CSTR d = GB_read_bytes_pntr(gbd); |
---|
998 | return d ? GB_memdup(d, gbd->as_entry()->size()) : 0; |
---|
999 | } |
---|
1000 | |
---|
1001 | GB_CUINT4 *GB_read_ints_pntr(GBDATA *gbd) |
---|
1002 | { |
---|
1003 | GB_TEST_READ(gbd, GB_INTS, "GB_read_ints_pntr"); |
---|
1004 | GBENTRY *gbe = gbd->as_entry(); |
---|
1005 | |
---|
1006 | GB_UINT4 *res; |
---|
1007 | if (gbe->flags.compressed_data) { |
---|
1008 | res = (GB_UINT4 *)GB_read_pntr(gbe); |
---|
1009 | } |
---|
1010 | else { |
---|
1011 | res = (GB_UINT4 *)gbe->data(); |
---|
1012 | } |
---|
1013 | if (!res) return NULL; |
---|
1014 | |
---|
1015 | if (0x01020304U == htonl(0x01020304U)) { |
---|
1016 | return res; |
---|
1017 | } |
---|
1018 | else { |
---|
1019 | int size = gbe->size(); |
---|
1020 | char *buf2 = GB_give_other_buffer((char *)res, size<<2); |
---|
1021 | GB_UINT4 *s = (GB_UINT4 *)res; |
---|
1022 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
---|
1023 | |
---|
1024 | for (long i=size; i; i--) { |
---|
1025 | *(d++) = htonl(*(s++)); |
---|
1026 | } |
---|
1027 | return (GB_UINT4 *)buf2; |
---|
1028 | } |
---|
1029 | } |
---|
1030 | |
---|
1031 | long GB_read_ints_count(GBDATA *gbd) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_ints_count |
---|
1032 | GB_TEST_READ(gbd, GB_INTS, "GB_read_ints_count"); |
---|
1033 | return gbd->as_entry()->size(); |
---|
1034 | } |
---|
1035 | |
---|
1036 | GB_UINT4 *GB_read_ints(GBDATA *gbd) |
---|
1037 | { |
---|
1038 | GB_CUINT4 *i = GB_read_ints_pntr(gbd); |
---|
1039 | if (!i) return NULL; |
---|
1040 | return (GB_UINT4 *)GB_memdup((char *)i, gbd->as_entry()->size()*sizeof(GB_UINT4)); |
---|
1041 | } |
---|
1042 | |
---|
1043 | GB_CFLOAT *GB_read_floats_pntr(GBDATA *gbd) |
---|
1044 | { |
---|
1045 | GB_TEST_READ(gbd, GB_FLOATS, "GB_read_floats_pntr"); |
---|
1046 | GBENTRY *gbe = gbd->as_entry(); |
---|
1047 | char *res; |
---|
1048 | if (gbe->flags.compressed_data) { |
---|
1049 | res = (char *)GB_read_pntr(gbe); |
---|
1050 | } |
---|
1051 | else { |
---|
1052 | res = (char *)gbe->data(); |
---|
1053 | } |
---|
1054 | if (res) { |
---|
1055 | long size = gbe->size(); |
---|
1056 | long full_size = size*sizeof(float); |
---|
1057 | |
---|
1058 | XDR xdrs; |
---|
1059 | xdrmem_create(&xdrs, res, (int)(full_size), XDR_DECODE); |
---|
1060 | |
---|
1061 | char *buf2 = GB_give_other_buffer(res, full_size); |
---|
1062 | float *d = (float *)(void*)buf2; |
---|
1063 | for (long i=size; i; i--) { |
---|
1064 | xdr_float(&xdrs, d); |
---|
1065 | d++; |
---|
1066 | } |
---|
1067 | xdr_destroy(&xdrs); |
---|
1068 | return (float *)(void*)buf2; |
---|
1069 | } |
---|
1070 | return NULL; |
---|
1071 | } |
---|
1072 | |
---|
1073 | static long GB_read_floats_count(GBDATA *gbd) |
---|
1074 | { |
---|
1075 | GB_TEST_READ(gbd, GB_FLOATS, "GB_read_floats_count"); |
---|
1076 | return gbd->as_entry()->size(); |
---|
1077 | } |
---|
1078 | |
---|
1079 | float *GB_read_floats(GBDATA *gbd) { // @@@ only used in unittest - check usage of floats |
---|
1080 | GB_CFLOAT *f; |
---|
1081 | f = GB_read_floats_pntr(gbd); |
---|
1082 | if (!f) return NULL; |
---|
1083 | return (float *)GB_memdup((char *)f, gbd->as_entry()->size()*sizeof(float)); |
---|
1084 | } |
---|
1085 | |
---|
1086 | char *GB_read_as_string(GBDATA *gbd) { |
---|
1087 | switch (gbd->type()) { |
---|
1088 | case GB_STRING: return GB_read_string(gbd); |
---|
1089 | case GB_LINK: return GB_read_link(gbd); |
---|
1090 | case GB_BYTE: return GBS_global_string_copy("%i", GB_read_byte(gbd)); |
---|
1091 | case GB_INT: return GBS_global_string_copy("%li", GB_read_int(gbd)); |
---|
1092 | case GB_FLOAT: return GBS_global_string_copy("%g", GB_read_float(gbd)); |
---|
1093 | case GB_BITS: return GB_read_bits(gbd, '0', '1'); |
---|
1094 | /* Be careful : When adding new types here, you have to make sure that |
---|
1095 | * GB_write_as_string is able to write them back and that this makes sense. |
---|
1096 | */ |
---|
1097 | default: return NULL; |
---|
1098 | } |
---|
1099 | } |
---|
1100 | |
---|
1101 | // ------------------------------------------------------------ |
---|
1102 | // array type access functions (intended for perl use) |
---|
1103 | |
---|
1104 | long GB_read_from_ints(GBDATA *gbd, long index) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_from_ints |
---|
1105 | static GBDATA *last_gbd = 0; |
---|
1106 | static long count = 0; |
---|
1107 | static GB_CUINT4 *i = 0; |
---|
1108 | |
---|
1109 | if (gbd != last_gbd) { |
---|
1110 | count = GB_read_ints_count(gbd); |
---|
1111 | i = GB_read_ints_pntr(gbd); |
---|
1112 | last_gbd = gbd; |
---|
1113 | } |
---|
1114 | |
---|
1115 | if (index >= 0 && index < count) { |
---|
1116 | return i[index]; |
---|
1117 | } |
---|
1118 | return -1; |
---|
1119 | } |
---|
1120 | |
---|
1121 | double GB_read_from_floats(GBDATA *gbd, long index) { // @@@ unused |
---|
1122 | static GBDATA *last_gbd = 0; |
---|
1123 | static long count = 0; |
---|
1124 | static GB_CFLOAT *f = 0; |
---|
1125 | |
---|
1126 | if (gbd != last_gbd) { |
---|
1127 | count = GB_read_floats_count(gbd); |
---|
1128 | f = GB_read_floats_pntr(gbd); |
---|
1129 | last_gbd = gbd; |
---|
1130 | } |
---|
1131 | |
---|
1132 | if (index >= 0 && index < count) { |
---|
1133 | return f[index]; |
---|
1134 | } |
---|
1135 | return -1; |
---|
1136 | } |
---|
1137 | |
---|
1138 | // ------------------- |
---|
1139 | // write data |
---|
1140 | |
---|
1141 | static void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd); |
---|
1142 | |
---|
1143 | static void gb_do_callbacks(GBDATA *gbd) { |
---|
1144 | gb_assert(GB_MAIN(gbd)->get_transaction_level() < 0); // only use in NO_TRANSACTION_MODE! |
---|
1145 | |
---|
1146 | while (gbd) { |
---|
1147 | GBDATA *gbdn = GB_get_father(gbd); |
---|
1148 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
1149 | if (cbl && cbl->call(gbd, GB_CB_CHANGED)) { |
---|
1150 | gb_remove_callbacks_marked_for_deletion(gbd); |
---|
1151 | } |
---|
1152 | gbd = gbdn; |
---|
1153 | } |
---|
1154 | } |
---|
1155 | |
---|
1156 | #define GB_DO_CALLBACKS(gbd) do { if (GB_MAIN(gbd)->get_transaction_level() < 0) gb_do_callbacks(gbd); } while (0) |
---|
1157 | |
---|
1158 | GB_ERROR GB_write_byte(GBDATA *gbd, int i) |
---|
1159 | { |
---|
1160 | GB_TEST_WRITE(gbd, GB_BYTE, "GB_write_byte"); |
---|
1161 | GBENTRY *gbe = gbd->as_entry(); |
---|
1162 | if (gbe->info.i != i) { |
---|
1163 | gb_save_extern_data_in_ts(gbe); |
---|
1164 | gbe->info.i = i & 0xff; |
---|
1165 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1166 | GB_DO_CALLBACKS(gbe); |
---|
1167 | } |
---|
1168 | return 0; |
---|
1169 | } |
---|
1170 | |
---|
1171 | GB_ERROR GB_write_int(GBDATA *gbd, long i) { |
---|
1172 | #if defined(ARB_64) |
---|
1173 | #if defined(WARN_TODO) |
---|
1174 | #warning GB_write_int should be GB_ERROR GB_write_int(GBDATA *gbd,int32_t i) |
---|
1175 | #endif |
---|
1176 | #endif |
---|
1177 | |
---|
1178 | GB_TEST_WRITE(gbd, GB_INT, "GB_write_int"); |
---|
1179 | if ((long)((int32_t)i) != i) { |
---|
1180 | gb_assert(0); |
---|
1181 | GB_warningf("Warning: 64bit incompatibility detected\nNo data written to '%s'\n", GB_get_db_path(gbd)); |
---|
1182 | return "GB_INT out of range (signed, 32bit)"; |
---|
1183 | } |
---|
1184 | GBENTRY *gbe = gbd->as_entry(); |
---|
1185 | if (gbe->info.i != (int32_t)i) { |
---|
1186 | gb_save_extern_data_in_ts(gbe); |
---|
1187 | gbe->info.i = i; |
---|
1188 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1189 | GB_DO_CALLBACKS(gbe); |
---|
1190 | } |
---|
1191 | return 0; |
---|
1192 | } |
---|
1193 | |
---|
1194 | GB_ERROR GB_write_pointer(GBDATA *gbd, GBDATA *pointer) { |
---|
1195 | GB_TEST_WRITE(gbd, GB_POINTER, "GB_write_pointer"); |
---|
1196 | GBENTRY *gbe = gbd->as_entry(); |
---|
1197 | if (gbe->info.ptr != pointer) { |
---|
1198 | gb_save_extern_data_in_ts(gbe); |
---|
1199 | gbe->info.ptr = pointer; |
---|
1200 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1201 | GB_DO_CALLBACKS(gbe); |
---|
1202 | } |
---|
1203 | return 0; |
---|
1204 | } |
---|
1205 | |
---|
1206 | GB_ERROR GB_write_float(GBDATA *gbd, double f) |
---|
1207 | { |
---|
1208 | gb_assert(f == f); // !nan |
---|
1209 | |
---|
1210 | XDR xdrs; |
---|
1211 | static float f2; |
---|
1212 | |
---|
1213 | GB_TEST_WRITE(gbd, GB_FLOAT, "GB_write_float"); |
---|
1214 | |
---|
1215 | #if defined(WARN_TODO) |
---|
1216 | #warning call GB_read_float here! |
---|
1217 | #endif |
---|
1218 | GB_TEST_READ(gbd, GB_FLOAT, "GB_read_float"); |
---|
1219 | |
---|
1220 | GBENTRY *gbe = gbd->as_entry(); |
---|
1221 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
---|
1222 | xdr_float(&xdrs, &f2); |
---|
1223 | xdr_destroy(&xdrs); |
---|
1224 | |
---|
1225 | if (f2 != f) { |
---|
1226 | f2 = f; |
---|
1227 | gb_save_extern_data_in_ts(gbe); |
---|
1228 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_ENCODE); |
---|
1229 | xdr_float(&xdrs, &f2); |
---|
1230 | xdr_destroy(&xdrs); |
---|
1231 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1232 | GB_DO_CALLBACKS(gbe); |
---|
1233 | } |
---|
1234 | xdr_destroy(&xdrs); |
---|
1235 | return 0; |
---|
1236 | } |
---|
1237 | |
---|
1238 | GB_ERROR gb_write_compressed_pntr(GBENTRY *gbe, const char *s, long memsize, long stored_size) { |
---|
1239 | gb_uncache(gbe); |
---|
1240 | gb_save_extern_data_in_ts(gbe); |
---|
1241 | gbe->flags.compressed_data = 1; |
---|
1242 | gbe->insert_data((char *)s, stored_size, (size_t)memsize); |
---|
1243 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1244 | |
---|
1245 | return 0; |
---|
1246 | } |
---|
1247 | |
---|
1248 | int gb_get_compression_mask(GB_MAIN_TYPE *Main, GBQUARK key, int gb_type) { |
---|
1249 | gb_Key *ks = &Main->keys[key]; |
---|
1250 | int compression_mask; |
---|
1251 | |
---|
1252 | if (ks->gb_key_disabled) { |
---|
1253 | compression_mask = 0; |
---|
1254 | } |
---|
1255 | else { |
---|
1256 | if (!ks->gb_key) gb_load_single_key_data(Main->gb_main(), key); |
---|
1257 | compression_mask = gb_convert_type_2_compression_flags[gb_type] & ks->compression_mask; |
---|
1258 | } |
---|
1259 | |
---|
1260 | return compression_mask; |
---|
1261 | } |
---|
1262 | |
---|
1263 | GB_ERROR GB_write_pntr(GBDATA *gbd, const char *s, size_t bytes_size, size_t stored_size) |
---|
1264 | { |
---|
1265 | // 'bytes_size' is the size of what 's' points to. |
---|
1266 | // 'stored_size' is the size-information written into the DB |
---|
1267 | // |
---|
1268 | // e.g. for strings : stored_size = bytes_size-1, cause stored_size is string len, |
---|
1269 | // but bytes_size includes zero byte. |
---|
1270 | |
---|
1271 | GBENTRY *gbe = gbd->as_entry(); |
---|
1272 | GB_MAIN_TYPE *Main = GB_MAIN(gbe); |
---|
1273 | GBQUARK key = GB_KEY_QUARK(gbe); |
---|
1274 | GB_TYPES type = gbe->type(); |
---|
1275 | |
---|
1276 | gb_assert(implicated(type == GB_STRING, stored_size == bytes_size-1)); // size constraint for strings not fulfilled! |
---|
1277 | |
---|
1278 | gb_uncache(gbe); |
---|
1279 | gb_save_extern_data_in_ts(gbe); |
---|
1280 | |
---|
1281 | int compression_mask = gb_get_compression_mask(Main, key, type); |
---|
1282 | |
---|
1283 | const char *d; |
---|
1284 | size_t memsize; |
---|
1285 | if (compression_mask) { |
---|
1286 | d = gb_compress_data(gbe, key, s, bytes_size, &memsize, compression_mask, false); |
---|
1287 | } |
---|
1288 | else { |
---|
1289 | d = NULL; |
---|
1290 | } |
---|
1291 | if (d) { |
---|
1292 | gbe->flags.compressed_data = 1; |
---|
1293 | } |
---|
1294 | else { |
---|
1295 | d = s; |
---|
1296 | gbe->flags.compressed_data = 0; |
---|
1297 | memsize = bytes_size; |
---|
1298 | } |
---|
1299 | |
---|
1300 | gbe->insert_data(d, stored_size, memsize); |
---|
1301 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1302 | GB_DO_CALLBACKS(gbe); |
---|
1303 | |
---|
1304 | return 0; |
---|
1305 | } |
---|
1306 | |
---|
1307 | GB_ERROR GB_write_string(GBDATA *gbd, const char *s) { |
---|
1308 | GBENTRY *gbe = gbd->as_entry(); |
---|
1309 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_string"); |
---|
1310 | GB_TEST_NON_BUFFER(s, "GB_write_string"); // compress would destroy the other buffer |
---|
1311 | |
---|
1312 | if (!s) s = ""; |
---|
1313 | size_t size = strlen(s); |
---|
1314 | |
---|
1315 | // no zero len strings allowed |
---|
1316 | if (gbe->memsize() && (size == gbe->size())) |
---|
1317 | { |
---|
1318 | if (!strcmp(s, GB_read_pntr(gbe))) |
---|
1319 | return 0; |
---|
1320 | } |
---|
1321 | #if defined(DEBUG) && 0 |
---|
1322 | // check for error (in compression) |
---|
1323 | { |
---|
1324 | GB_ERROR error = GB_write_pntr(gbe, s, size+1, size); |
---|
1325 | if (!error) { |
---|
1326 | char *check = GB_read_string(gbe); |
---|
1327 | |
---|
1328 | gb_assert(check); |
---|
1329 | gb_assert(strcmp(check, s) == 0); |
---|
1330 | |
---|
1331 | free(check); |
---|
1332 | } |
---|
1333 | return error; |
---|
1334 | } |
---|
1335 | #else |
---|
1336 | return GB_write_pntr(gbe, s, size+1, size); |
---|
1337 | #endif // DEBUG |
---|
1338 | } |
---|
1339 | |
---|
1340 | GB_ERROR GB_write_link(GBDATA *gbd, const char *s) |
---|
1341 | { |
---|
1342 | GBENTRY *gbe = gbd->as_entry(); |
---|
1343 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_link"); |
---|
1344 | GB_TEST_NON_BUFFER(s, "GB_write_link"); // compress would destroy the other buffer |
---|
1345 | |
---|
1346 | if (!s) s = ""; |
---|
1347 | size_t size = strlen(s); |
---|
1348 | |
---|
1349 | // no zero len strings allowed |
---|
1350 | if (gbe->memsize() && (size == gbe->size())) |
---|
1351 | { |
---|
1352 | if (!strcmp(s, GB_read_pntr(gbe))) |
---|
1353 | return 0; |
---|
1354 | } |
---|
1355 | return GB_write_pntr(gbe, s, size+1, size); |
---|
1356 | } |
---|
1357 | |
---|
1358 | |
---|
1359 | GB_ERROR GB_write_bits(GBDATA *gbd, const char *bits, long size, const char *c_0) |
---|
1360 | { |
---|
1361 | GBENTRY *gbe = gbd->as_entry(); |
---|
1362 | GB_TEST_WRITE(gbe, GB_BITS, "GB_write_bits"); |
---|
1363 | GB_TEST_NON_BUFFER(bits, "GB_write_bits"); // compress would destroy the other buffer |
---|
1364 | gb_save_extern_data_in_ts(gbe); |
---|
1365 | |
---|
1366 | long memsize; |
---|
1367 | char *d = gb_compress_bits(bits, size, (const unsigned char *)c_0, &memsize); |
---|
1368 | |
---|
1369 | gbe->flags.compressed_data = 1; |
---|
1370 | gbe->insert_data(d, size, memsize); |
---|
1371 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1372 | GB_DO_CALLBACKS(gbe); |
---|
1373 | return 0; |
---|
1374 | } |
---|
1375 | |
---|
1376 | GB_ERROR GB_write_bytes(GBDATA *gbd, const char *s, long size) |
---|
1377 | { |
---|
1378 | GB_TEST_WRITE(gbd, GB_BYTES, "GB_write_bytes"); |
---|
1379 | return GB_write_pntr(gbd, s, size, size); |
---|
1380 | } |
---|
1381 | |
---|
1382 | GB_ERROR GB_write_ints(GBDATA *gbd, const GB_UINT4 *i, long size) |
---|
1383 | { |
---|
1384 | |
---|
1385 | GB_TEST_WRITE(gbd, GB_INTS, "GB_write_ints"); |
---|
1386 | GB_TEST_NON_BUFFER((char *)i, "GB_write_ints"); // compress would destroy the other buffer |
---|
1387 | |
---|
1388 | if (0x01020304 != htonl((GB_UINT4)0x01020304)) { |
---|
1389 | long j; |
---|
1390 | char *buf2 = GB_give_other_buffer((char *)i, size<<2); |
---|
1391 | GB_UINT4 *s = (GB_UINT4 *)i; |
---|
1392 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
---|
1393 | |
---|
1394 | for (j=size; j; j--) { |
---|
1395 | *(d++) = htonl(*(s++)); |
---|
1396 | } |
---|
1397 | i = (GB_UINT4 *)buf2; |
---|
1398 | } |
---|
1399 | return GB_write_pntr(gbd, (char *)i, size* 4 /* sizeof(long4) */, size); |
---|
1400 | } |
---|
1401 | |
---|
1402 | GB_ERROR GB_write_floats(GBDATA *gbd, const float *f, long size) |
---|
1403 | { |
---|
1404 | long fullsize = size * sizeof(float); |
---|
1405 | GB_TEST_WRITE(gbd, GB_FLOATS, "GB_write_floats"); |
---|
1406 | GB_TEST_NON_BUFFER((char *)f, "GB_write_floats"); // compress would destroy the other buffer |
---|
1407 | |
---|
1408 | { |
---|
1409 | XDR xdrs; |
---|
1410 | long i; |
---|
1411 | char *buf2 = GB_give_other_buffer((char *)f, fullsize); |
---|
1412 | float *s = (float *)f; |
---|
1413 | |
---|
1414 | xdrmem_create(&xdrs, buf2, (int)fullsize, XDR_ENCODE); |
---|
1415 | for (i=size; i; i--) { |
---|
1416 | xdr_float(&xdrs, s); |
---|
1417 | s++; |
---|
1418 | } |
---|
1419 | xdr_destroy (&xdrs); |
---|
1420 | f = (float*)(void*)buf2; |
---|
1421 | } |
---|
1422 | return GB_write_pntr(gbd, (char *)f, size*sizeof(float), size); |
---|
1423 | } |
---|
1424 | |
---|
1425 | GB_ERROR GB_write_as_string(GBDATA *gbd, const char *val) { |
---|
1426 | switch (gbd->type()) { |
---|
1427 | case GB_STRING: return GB_write_string(gbd, val); |
---|
1428 | case GB_LINK: return GB_write_link(gbd, val); |
---|
1429 | case GB_BYTE: return GB_write_byte(gbd, atoi(val)); |
---|
1430 | case GB_INT: return GB_write_int(gbd, atoi(val)); |
---|
1431 | case GB_FLOAT: return GB_write_float(gbd, GB_atof(val)); |
---|
1432 | case GB_BITS: return GB_write_bits(gbd, val, strlen(val), "0"); |
---|
1433 | default: return GB_export_errorf("Error: You cannot use GB_write_as_string on this type of entry (%s)", GB_read_key_pntr(gbd)); |
---|
1434 | } |
---|
1435 | } |
---|
1436 | |
---|
1437 | // --------------------------- |
---|
1438 | // security functions |
---|
1439 | |
---|
1440 | int GB_read_security_write(GBDATA *gbd) { |
---|
1441 | GB_test_transaction(gbd); |
---|
1442 | return GB_GET_SECURITY_WRITE(gbd); |
---|
1443 | } |
---|
1444 | int GB_read_security_read(GBDATA *gbd) { |
---|
1445 | GB_test_transaction(gbd); |
---|
1446 | return GB_GET_SECURITY_READ(gbd); |
---|
1447 | } |
---|
1448 | int GB_read_security_delete(GBDATA *gbd) { |
---|
1449 | GB_test_transaction(gbd); |
---|
1450 | return GB_GET_SECURITY_DELETE(gbd); |
---|
1451 | } |
---|
1452 | GB_ERROR GB_write_security_write(GBDATA *gbd, unsigned long level) |
---|
1453 | { |
---|
1454 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1455 | GB_test_transaction(Main); |
---|
1456 | |
---|
1457 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1458 | return gb_security_error(gbd); |
---|
1459 | if (GB_GET_SECURITY_WRITE(gbd) == level) return 0; |
---|
1460 | GB_PUT_SECURITY_WRITE(gbd, level); |
---|
1461 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1462 | GB_DO_CALLBACKS(gbd); |
---|
1463 | return 0; |
---|
1464 | } |
---|
1465 | GB_ERROR GB_write_security_read(GBDATA *gbd, unsigned long level) // @@@ unused |
---|
1466 | { |
---|
1467 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1468 | GB_test_transaction(Main); |
---|
1469 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1470 | return gb_security_error(gbd); |
---|
1471 | if (GB_GET_SECURITY_READ(gbd) == level) return 0; |
---|
1472 | GB_PUT_SECURITY_READ(gbd, level); |
---|
1473 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1474 | GB_DO_CALLBACKS(gbd); |
---|
1475 | return 0; |
---|
1476 | } |
---|
1477 | GB_ERROR GB_write_security_delete(GBDATA *gbd, unsigned long level) |
---|
1478 | { |
---|
1479 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1480 | GB_test_transaction(Main); |
---|
1481 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1482 | return gb_security_error(gbd); |
---|
1483 | if (GB_GET_SECURITY_DELETE(gbd) == level) return 0; |
---|
1484 | GB_PUT_SECURITY_DELETE(gbd, level); |
---|
1485 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1486 | GB_DO_CALLBACKS(gbd); |
---|
1487 | return 0; |
---|
1488 | } |
---|
1489 | GB_ERROR GB_write_security_levels(GBDATA *gbd, unsigned long readlevel, unsigned long writelevel, unsigned long deletelevel) |
---|
1490 | { |
---|
1491 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1492 | GB_test_transaction(Main); |
---|
1493 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1494 | return gb_security_error(gbd); |
---|
1495 | GB_PUT_SECURITY_WRITE(gbd, writelevel); |
---|
1496 | GB_PUT_SECURITY_READ(gbd, readlevel); |
---|
1497 | GB_PUT_SECURITY_DELETE(gbd, deletelevel); |
---|
1498 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1499 | GB_DO_CALLBACKS(gbd); |
---|
1500 | return 0; |
---|
1501 | } |
---|
1502 | |
---|
1503 | void GB_change_my_security(GBDATA *gbd, int level) { |
---|
1504 | GB_MAIN(gbd)->security_level = level<0 ? 0 : (level>7 ? 7 : level); |
---|
1505 | } |
---|
1506 | |
---|
1507 | // For internal use only |
---|
1508 | void GB_push_my_security(GBDATA *gbd) |
---|
1509 | { |
---|
1510 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1511 | Main->pushed_security_level++; |
---|
1512 | if (Main->pushed_security_level <= 1) { |
---|
1513 | Main->old_security_level = Main->security_level; |
---|
1514 | Main->security_level = 7; |
---|
1515 | } |
---|
1516 | } |
---|
1517 | |
---|
1518 | void GB_pop_my_security(GBDATA *gbd) { |
---|
1519 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1520 | Main->pushed_security_level--; |
---|
1521 | if (Main->pushed_security_level <= 0) { |
---|
1522 | Main->security_level = Main->old_security_level; |
---|
1523 | } |
---|
1524 | } |
---|
1525 | |
---|
1526 | |
---|
1527 | // ------------------------ |
---|
1528 | // Key information |
---|
1529 | |
---|
1530 | GB_TYPES GB_read_type(GBDATA *gbd) { |
---|
1531 | GB_test_transaction(gbd); |
---|
1532 | return gbd->type(); |
---|
1533 | } |
---|
1534 | |
---|
1535 | bool GB_is_container(GBDATA *gbd) { |
---|
1536 | return gbd && gbd->is_container(); |
---|
1537 | } |
---|
1538 | |
---|
1539 | char *GB_read_key(GBDATA *gbd) { |
---|
1540 | return strdup(GB_read_key_pntr(gbd)); |
---|
1541 | } |
---|
1542 | |
---|
1543 | GB_CSTR GB_read_key_pntr(GBDATA *gbd) { |
---|
1544 | GB_CSTR k; |
---|
1545 | GB_test_transaction(gbd); |
---|
1546 | k = GB_KEY(gbd); |
---|
1547 | if (!k) k = GBS_global_string("<invalid key (quark=%i)>", GB_KEY_QUARK(gbd)); |
---|
1548 | return k; |
---|
1549 | } |
---|
1550 | |
---|
1551 | GB_CSTR gb_read_key_pntr(GBDATA *gbd) { |
---|
1552 | return GB_KEY(gbd); |
---|
1553 | } |
---|
1554 | |
---|
1555 | |
---|
1556 | GBQUARK gb_find_existing_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1557 | //! @return existing quark for 'key' (-1 if key == NULL, 0 if key is unknown) |
---|
1558 | return key ? GBS_read_hash(Main->key_2_index_hash, key) : -1; |
---|
1559 | } |
---|
1560 | |
---|
1561 | GBQUARK gb_find_or_create_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1562 | //! @return existing or newly created quark for 'key' |
---|
1563 | GBQUARK quark = gb_find_existing_quark(Main, key); |
---|
1564 | if (!quark) quark = gb_create_key(Main, key, true); |
---|
1565 | return quark; |
---|
1566 | } |
---|
1567 | |
---|
1568 | GBQUARK gb_find_or_create_NULL_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1569 | // similar to gb_find_or_create_quark, |
---|
1570 | // but if 'key' is NULL, quark 0 will be returned. |
---|
1571 | // |
---|
1572 | // Use this function with care. |
---|
1573 | // |
---|
1574 | // Known good use: |
---|
1575 | // - create main entry and its dummy father via gb_make_container() |
---|
1576 | |
---|
1577 | return key ? gb_find_or_create_quark(Main, key) : 0; |
---|
1578 | } |
---|
1579 | |
---|
1580 | GBQUARK GB_find_existing_quark(GBDATA *gbd, const char *key) { |
---|
1581 | //! @return existing quark for 'key' (-1 if key == NULL, 0 if key is unknown) |
---|
1582 | return gb_find_existing_quark(GB_MAIN(gbd), key); |
---|
1583 | } |
---|
1584 | |
---|
1585 | GBQUARK GB_find_or_create_quark(GBDATA *gbd, const char *key) { |
---|
1586 | //! @return existing or newly created quark for 'key' |
---|
1587 | return gb_find_or_create_quark(GB_MAIN(gbd), key); |
---|
1588 | } |
---|
1589 | |
---|
1590 | |
---|
1591 | // --------------------------------------------- |
---|
1592 | |
---|
1593 | GBQUARK GB_get_quark(GBDATA *gbd) { |
---|
1594 | return GB_KEY_QUARK(gbd); |
---|
1595 | } |
---|
1596 | |
---|
1597 | bool GB_has_key(GBDATA *gbd, const char *key) { |
---|
1598 | GBQUARK quark = GB_find_existing_quark(gbd, key); |
---|
1599 | return quark && (quark == GB_get_quark(gbd)); |
---|
1600 | } |
---|
1601 | |
---|
1602 | // --------------------------------------------- |
---|
1603 | |
---|
1604 | long GB_read_clock(GBDATA *gbd) { |
---|
1605 | if (GB_ARRAY_FLAGS(gbd).changed) return GB_MAIN(gbd)->clock; |
---|
1606 | return gbd->update_date(); |
---|
1607 | } |
---|
1608 | |
---|
1609 | // --------------------------------------------- |
---|
1610 | // Get and check the database hierarchy |
---|
1611 | |
---|
1612 | GBDATA *GB_get_father(GBDATA *gbd) { |
---|
1613 | // Get the father of an entry |
---|
1614 | GB_test_transaction(gbd); |
---|
1615 | return gbd->get_father(); |
---|
1616 | } |
---|
1617 | |
---|
1618 | GBDATA *GB_get_grandfather(GBDATA *gbd) { |
---|
1619 | GB_test_transaction(gbd); |
---|
1620 | |
---|
1621 | GBDATA *gb_grandpa = GB_FATHER(gbd); |
---|
1622 | if (gb_grandpa) { |
---|
1623 | gb_grandpa = GB_FATHER(gb_grandpa); |
---|
1624 | if (gb_grandpa && !GB_FATHER(gb_grandpa)) gb_grandpa = NULL; // never return dummy_father of root container |
---|
1625 | } |
---|
1626 | return gb_grandpa; |
---|
1627 | } |
---|
1628 | |
---|
1629 | // Get the root entry (gb_main) |
---|
1630 | GBDATA *GB_get_root(GBDATA *gbd) { return GB_MAIN(gbd)->gb_main(); } |
---|
1631 | GBCONTAINER *gb_get_root(GBENTRY *gbe) { return GB_MAIN(gbe)->root_container; } |
---|
1632 | GBCONTAINER *gb_get_root(GBCONTAINER *gbc) { return GB_MAIN(gbc)->root_container; } |
---|
1633 | |
---|
1634 | bool GB_check_father(GBDATA *gbd, GBDATA *gb_maybefather) { |
---|
1635 | // Test whether an entry is a subentry of another |
---|
1636 | GBDATA *gbfather; |
---|
1637 | for (gbfather = GB_get_father(gbd); |
---|
1638 | gbfather; |
---|
1639 | gbfather = GB_get_father(gbfather)) |
---|
1640 | { |
---|
1641 | if (gbfather == gb_maybefather) return true; |
---|
1642 | } |
---|
1643 | return false; |
---|
1644 | } |
---|
1645 | |
---|
1646 | // -------------------------- |
---|
1647 | // create and rename |
---|
1648 | |
---|
1649 | GBENTRY *gb_create(GBCONTAINER *father, const char *key, GB_TYPES type) { |
---|
1650 | GBENTRY *gbe = gb_make_entry(father, key, -1, 0, type); |
---|
1651 | gb_touch_header(GB_FATHER(gbe)); |
---|
1652 | gb_touch_entry(gbe, GB_CREATED); |
---|
1653 | |
---|
1654 | gb_assert(GB_ARRAY_FLAGS(gbe).changed < GB_DELETED); // happens sometimes -> needs debugging |
---|
1655 | |
---|
1656 | return gbe; |
---|
1657 | } |
---|
1658 | |
---|
1659 | GBCONTAINER *gb_create_container(GBCONTAINER *father, const char *key) { |
---|
1660 | // Create a container, do not check anything |
---|
1661 | GBCONTAINER *gbc = gb_make_container(father, key, -1, 0); |
---|
1662 | gb_touch_header(GB_FATHER(gbc)); |
---|
1663 | gb_touch_entry(gbc, GB_CREATED); |
---|
1664 | return gbc; |
---|
1665 | } |
---|
1666 | |
---|
1667 | GBDATA *GB_create(GBDATA *father, const char *key, GB_TYPES type) { |
---|
1668 | /*! Create a DB entry |
---|
1669 | * |
---|
1670 | * @param father container to create DB field in |
---|
1671 | * @param key name of field |
---|
1672 | * @param type field type |
---|
1673 | * |
---|
1674 | * @return |
---|
1675 | * - created DB entry |
---|
1676 | * - NULL on failure (error is exported then) |
---|
1677 | * |
---|
1678 | * @see GB_create_container() |
---|
1679 | */ |
---|
1680 | |
---|
1681 | if (GB_check_key(key)) { |
---|
1682 | GB_print_error(); |
---|
1683 | return NULL; |
---|
1684 | } |
---|
1685 | |
---|
1686 | if (type == GB_DB) { |
---|
1687 | gb_assert(type != GB_DB); // you like to use GB_create_container! |
---|
1688 | GB_export_error("GB_create error: can't create containers"); |
---|
1689 | return NULL; |
---|
1690 | } |
---|
1691 | |
---|
1692 | if (!father) { |
---|
1693 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
---|
1694 | return NULL; |
---|
1695 | } |
---|
1696 | GB_test_transaction(father); |
---|
1697 | if (father->is_entry()) { |
---|
1698 | GB_export_errorf("GB_create: father (%s) is not of GB_DB type (%i) (creating '%s')", |
---|
1699 | GB_read_key_pntr(father), father->type(), key); |
---|
1700 | return NULL; |
---|
1701 | } |
---|
1702 | |
---|
1703 | if (type == GB_POINTER) { |
---|
1704 | if (!GB_in_temporary_branch(father)) { |
---|
1705 | GB_export_error("GB_create: pointers only allowed in temporary branches"); |
---|
1706 | return NULL; |
---|
1707 | } |
---|
1708 | } |
---|
1709 | |
---|
1710 | return gb_create(father->expect_container(), key, type); |
---|
1711 | } |
---|
1712 | |
---|
1713 | GBDATA *GB_create_container(GBDATA *father, const char *key) { |
---|
1714 | /*! Create a new DB container |
---|
1715 | * |
---|
1716 | * @param father parent container |
---|
1717 | * @param key name of created container |
---|
1718 | * |
---|
1719 | * @return |
---|
1720 | * - created container |
---|
1721 | * - NULL on failure (error is exported then) |
---|
1722 | * |
---|
1723 | * @see GB_create() |
---|
1724 | */ |
---|
1725 | |
---|
1726 | if (GB_check_key(key)) { |
---|
1727 | GB_print_error(); |
---|
1728 | return NULL; |
---|
1729 | } |
---|
1730 | |
---|
1731 | if ((*key == '\0')) { |
---|
1732 | GB_export_error("GB_create error: empty key"); |
---|
1733 | return NULL; |
---|
1734 | } |
---|
1735 | if (!father) { |
---|
1736 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
---|
1737 | return NULL; |
---|
1738 | } |
---|
1739 | |
---|
1740 | GB_test_transaction(father); |
---|
1741 | return gb_create_container(father->expect_container(), key); |
---|
1742 | } |
---|
1743 | |
---|
1744 | // ---------------------- |
---|
1745 | // recompression |
---|
1746 | |
---|
1747 | #if defined(WARN_TODO) |
---|
1748 | #warning rename gb_set_compression into gb_recompress (misleading name) |
---|
1749 | #endif |
---|
1750 | |
---|
1751 | static GB_ERROR gb_set_compression(GBDATA *source) { |
---|
1752 | GB_ERROR error = 0; |
---|
1753 | GB_test_transaction(source); |
---|
1754 | |
---|
1755 | switch (source->type()) { |
---|
1756 | case GB_STRING: { |
---|
1757 | char *str = GB_read_string(source); |
---|
1758 | GB_write_string(source, ""); |
---|
1759 | GB_write_string(source, str); |
---|
1760 | free(str); |
---|
1761 | break; |
---|
1762 | } |
---|
1763 | case GB_BITS: |
---|
1764 | case GB_BYTES: |
---|
1765 | case GB_INTS: |
---|
1766 | case GB_FLOATS: |
---|
1767 | break; |
---|
1768 | case GB_DB: |
---|
1769 | for (GBDATA *gb_p = GB_child(source); gb_p && !error; gb_p = GB_nextChild(gb_p)) { |
---|
1770 | error = gb_set_compression(gb_p); |
---|
1771 | } |
---|
1772 | break; |
---|
1773 | default: |
---|
1774 | break; |
---|
1775 | } |
---|
1776 | return error; |
---|
1777 | } |
---|
1778 | |
---|
1779 | bool GB_allow_compression(GBDATA *gb_main, bool allow_compression) { |
---|
1780 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
1781 | int prev_mask = Main->compression_mask; |
---|
1782 | Main->compression_mask = allow_compression ? -1 : 0; |
---|
1783 | |
---|
1784 | return prev_mask == 0 ? false : true; |
---|
1785 | } |
---|
1786 | |
---|
1787 | |
---|
1788 | GB_ERROR GB_delete(GBDATA*& source) { |
---|
1789 | GBDATA *gb_main; |
---|
1790 | |
---|
1791 | GB_test_transaction(source); |
---|
1792 | if (GB_GET_SECURITY_DELETE(source)>GB_MAIN(source)->security_level) { |
---|
1793 | return GBS_global_string("Security error: deleting entry '%s' not permitted", GB_read_key_pntr(source)); |
---|
1794 | } |
---|
1795 | |
---|
1796 | gb_main = GB_get_root(source); |
---|
1797 | |
---|
1798 | if (source->flags.compressed_data) { |
---|
1799 | bool was_allowed = GB_allow_compression(gb_main, false); |
---|
1800 | gb_set_compression(source); // write data w/o compression (otherwise GB_read_old_value... won't work) |
---|
1801 | GB_allow_compression(gb_main, was_allowed); |
---|
1802 | } |
---|
1803 | |
---|
1804 | { |
---|
1805 | GB_MAIN_TYPE *Main = GB_MAIN(source); |
---|
1806 | if (Main->get_transaction_level() < 0) { // no transaction mode |
---|
1807 | gb_delete_entry(source); |
---|
1808 | Main->call_pending_callbacks(); |
---|
1809 | } |
---|
1810 | else { |
---|
1811 | gb_touch_entry(source, GB_DELETED); |
---|
1812 | } |
---|
1813 | } |
---|
1814 | return 0; |
---|
1815 | } |
---|
1816 | |
---|
1817 | GB_ERROR gb_delete_force(GBDATA *source) // delete always |
---|
1818 | { |
---|
1819 | gb_touch_entry(source, GB_DELETED); |
---|
1820 | return 0; |
---|
1821 | } |
---|
1822 | |
---|
1823 | |
---|
1824 | // ------------------ |
---|
1825 | // Copy data |
---|
1826 | |
---|
1827 | #if defined(WARN_TODO) |
---|
1828 | #warning replace GB_copy with GB_copy_with_protection after release |
---|
1829 | #endif |
---|
1830 | |
---|
1831 | GB_ERROR GB_copy(GBDATA *dest, GBDATA *source) { |
---|
1832 | return GB_copy_with_protection(dest, source, false); |
---|
1833 | } |
---|
1834 | |
---|
1835 | GB_ERROR GB_copy_with_protection(GBDATA *dest, GBDATA *source, bool copy_all_protections) { |
---|
1836 | GB_ERROR error = 0; |
---|
1837 | GB_test_transaction(source); |
---|
1838 | |
---|
1839 | GB_TYPES type = source->type(); |
---|
1840 | if (dest->type() != type) { |
---|
1841 | return GB_export_errorf("incompatible types in GB_copy (source %s:%u != %s:%u", |
---|
1842 | GB_read_key_pntr(source), type, GB_read_key_pntr(dest), dest->type()); |
---|
1843 | } |
---|
1844 | |
---|
1845 | switch (type) { |
---|
1846 | case GB_INT: |
---|
1847 | error = GB_write_int(dest, GB_read_int(source)); |
---|
1848 | break; |
---|
1849 | case GB_FLOAT: |
---|
1850 | error = GB_write_float(dest, GB_read_float(source)); |
---|
1851 | break; |
---|
1852 | case GB_BYTE: |
---|
1853 | error = GB_write_byte(dest, GB_read_byte(source)); |
---|
1854 | break; |
---|
1855 | case GB_STRING: // No local compression |
---|
1856 | error = GB_write_string(dest, GB_read_char_pntr(source)); |
---|
1857 | break; |
---|
1858 | case GB_LINK: // No local compression |
---|
1859 | error = GB_write_link(dest, GB_read_link_pntr(source)); |
---|
1860 | break; |
---|
1861 | case GB_BITS: // only local compressions for the following types |
---|
1862 | case GB_BYTES: |
---|
1863 | case GB_INTS: |
---|
1864 | case GB_FLOATS: { |
---|
1865 | GBENTRY *source_entry = source->as_entry(); |
---|
1866 | GBENTRY *dest_entry = dest->as_entry(); |
---|
1867 | |
---|
1868 | gb_save_extern_data_in_ts(dest_entry); |
---|
1869 | dest_entry->insert_data(source_entry->data(), source_entry->size(), source_entry->memsize()); |
---|
1870 | |
---|
1871 | dest->flags.compressed_data = source->flags.compressed_data; |
---|
1872 | break; |
---|
1873 | } |
---|
1874 | case GB_DB: { |
---|
1875 | if (!dest->is_container()) { |
---|
1876 | GB_ERROR err = GB_export_errorf("GB_COPY Type conflict %s:%i != %s:%i", |
---|
1877 | GB_read_key_pntr(dest), dest->type(), GB_read_key_pntr(source), GB_DB); |
---|
1878 | GB_internal_error(err); |
---|
1879 | return err; |
---|
1880 | } |
---|
1881 | |
---|
1882 | GBCONTAINER *destc = dest->as_container(); |
---|
1883 | GBCONTAINER *sourcec = source->as_container(); |
---|
1884 | |
---|
1885 | if (sourcec->flags2.folded_container) gb_unfold(sourcec, -1, -1); |
---|
1886 | if (destc->flags2.folded_container) gb_unfold(destc, 0, -1); |
---|
1887 | |
---|
1888 | for (GBDATA *gb_p = GB_child(sourcec); gb_p; gb_p = GB_nextChild(gb_p)) { |
---|
1889 | const char *key = GB_read_key_pntr(gb_p); |
---|
1890 | GBDATA *gb_d; |
---|
1891 | |
---|
1892 | if (gb_p->is_container()) { |
---|
1893 | gb_d = GB_create_container(destc, key); |
---|
1894 | gb_create_header_array(gb_d->as_container(), gb_p->as_container()->d.size); |
---|
1895 | } |
---|
1896 | else { |
---|
1897 | gb_d = GB_create(destc, key, gb_p->type()); |
---|
1898 | } |
---|
1899 | |
---|
1900 | if (!gb_d) error = GB_await_error(); |
---|
1901 | else error = GB_copy_with_protection(gb_d, gb_p, copy_all_protections); |
---|
1902 | |
---|
1903 | if (error) break; |
---|
1904 | } |
---|
1905 | |
---|
1906 | destc->flags3 = sourcec->flags3; |
---|
1907 | break; |
---|
1908 | } |
---|
1909 | default: |
---|
1910 | error = GB_export_error("GB_copy-error: unhandled type"); |
---|
1911 | } |
---|
1912 | if (error) return error; |
---|
1913 | |
---|
1914 | gb_touch_entry(dest, GB_NORMAL_CHANGE); |
---|
1915 | |
---|
1916 | dest->flags.security_read = source->flags.security_read; |
---|
1917 | if (copy_all_protections) { |
---|
1918 | dest->flags.security_write = source->flags.security_write; |
---|
1919 | dest->flags.security_delete = source->flags.security_delete; |
---|
1920 | } |
---|
1921 | |
---|
1922 | return 0; |
---|
1923 | } |
---|
1924 | |
---|
1925 | |
---|
1926 | static char *gb_stpcpy(char *dest, const char *source) |
---|
1927 | { |
---|
1928 | while ((*dest++=*source++)) ; |
---|
1929 | return dest-1; // return pointer to last copied character (which is \0) |
---|
1930 | } |
---|
1931 | |
---|
1932 | char* GB_get_subfields(GBDATA *gbd) { |
---|
1933 | /*! Get all subfield names |
---|
1934 | * |
---|
1935 | * @return all subfields of 'gbd' as ';'-separated heap-copy |
---|
1936 | * (first and last char of result is a ';') |
---|
1937 | */ |
---|
1938 | GB_test_transaction(gbd); |
---|
1939 | |
---|
1940 | char *result = 0; |
---|
1941 | if (gbd->is_container()) { |
---|
1942 | GBCONTAINER *gbc = gbd->as_container(); |
---|
1943 | int result_length = 0; |
---|
1944 | |
---|
1945 | if (gbc->flags2.folded_container) { |
---|
1946 | gb_unfold(gbc, -1, -1); |
---|
1947 | } |
---|
1948 | |
---|
1949 | for (GBDATA *gbp = GB_child(gbd); gbp; gbp = GB_nextChild(gbp)) { |
---|
1950 | const char *key = GB_read_key_pntr(gbp); |
---|
1951 | int keylen = strlen(key); |
---|
1952 | |
---|
1953 | if (result) { |
---|
1954 | char *neu_result = (char*)malloc(result_length+keylen+1+1); |
---|
1955 | |
---|
1956 | if (neu_result) { |
---|
1957 | char *p = gb_stpcpy(neu_result, result); |
---|
1958 | p = gb_stpcpy(p, key); |
---|
1959 | *p++ = ';'; |
---|
1960 | p[0] = 0; |
---|
1961 | |
---|
1962 | freeset(result, neu_result); |
---|
1963 | result_length += keylen+1; |
---|
1964 | } |
---|
1965 | else { |
---|
1966 | gb_assert(0); |
---|
1967 | } |
---|
1968 | } |
---|
1969 | else { |
---|
1970 | result = (char*)malloc(1+keylen+1+1); |
---|
1971 | result[0] = ';'; |
---|
1972 | strcpy(result+1, key); |
---|
1973 | result[keylen+1] = ';'; |
---|
1974 | result[keylen+2] = 0; |
---|
1975 | result_length = keylen+2; |
---|
1976 | } |
---|
1977 | } |
---|
1978 | } |
---|
1979 | else { |
---|
1980 | result = strdup(";"); |
---|
1981 | } |
---|
1982 | |
---|
1983 | return result; |
---|
1984 | } |
---|
1985 | |
---|
1986 | // -------------------------- |
---|
1987 | // temporary entries |
---|
1988 | |
---|
1989 | GB_ERROR GB_set_temporary(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
---|
1990 | /*! if the temporary flag is set, then that entry (including all subentries) will not be saved |
---|
1991 | * @see GB_clear_temporary() and GB_is_temporary() |
---|
1992 | */ |
---|
1993 | |
---|
1994 | GB_ERROR error = NULL; |
---|
1995 | GB_test_transaction(gbd); |
---|
1996 | |
---|
1997 | if (GB_GET_SECURITY_DELETE(gbd)>GB_MAIN(gbd)->security_level) { |
---|
1998 | error = GBS_global_string("Security error in GB_set_temporary: %s", GB_read_key_pntr(gbd)); |
---|
1999 | } |
---|
2000 | else { |
---|
2001 | gbd->flags.temporary = 1; |
---|
2002 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2003 | } |
---|
2004 | RETURN_ERROR(error); |
---|
2005 | } |
---|
2006 | |
---|
2007 | GB_ERROR GB_clear_temporary(GBDATA *gbd) { // @@@ used in ptpan branch - do not remove |
---|
2008 | //! undo effect of GB_set_temporary() |
---|
2009 | |
---|
2010 | GB_test_transaction(gbd); |
---|
2011 | gbd->flags.temporary = 0; |
---|
2012 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2013 | return 0; |
---|
2014 | } |
---|
2015 | |
---|
2016 | bool GB_is_temporary(GBDATA *gbd) { |
---|
2017 | //! @see GB_set_temporary() and GB_in_temporary_branch() |
---|
2018 | GB_test_transaction(gbd); |
---|
2019 | return (long)gbd->flags.temporary; |
---|
2020 | } |
---|
2021 | |
---|
2022 | bool GB_in_temporary_branch(GBDATA *gbd) { |
---|
2023 | /*! @return true, if 'gbd' is member of a temporary subtree, |
---|
2024 | * i.e. if GB_is_temporary(itself or any parent) |
---|
2025 | */ |
---|
2026 | |
---|
2027 | if (GB_is_temporary(gbd)) return true; |
---|
2028 | |
---|
2029 | GBDATA *gb_parent = GB_get_father(gbd); |
---|
2030 | if (!gb_parent) return false; |
---|
2031 | |
---|
2032 | return GB_in_temporary_branch(gb_parent); |
---|
2033 | } |
---|
2034 | |
---|
2035 | // --------------------- |
---|
2036 | // transactions |
---|
2037 | |
---|
2038 | GB_ERROR GB_MAIN_TYPE::initial_client_transaction() { |
---|
2039 | // the first client transaction ever |
---|
2040 | transaction_level = 1; |
---|
2041 | GB_ERROR error = gbcmc_init_transaction(root_container); |
---|
2042 | if (!error) ++clock; |
---|
2043 | return error; |
---|
2044 | } |
---|
2045 | |
---|
2046 | inline GB_ERROR GB_MAIN_TYPE::start_transaction() { |
---|
2047 | gb_assert(transaction_level == 0); |
---|
2048 | |
---|
2049 | transaction_level = 1; |
---|
2050 | aborted_transaction = 0; |
---|
2051 | |
---|
2052 | GB_ERROR error = NULL; |
---|
2053 | if (is_client()) { |
---|
2054 | error = gbcmc_begin_transaction(gb_main()); |
---|
2055 | if (!error) { |
---|
2056 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, 0); // init structures |
---|
2057 | gb_untouch_children_and_me(root_container); |
---|
2058 | } |
---|
2059 | } |
---|
2060 | |
---|
2061 | if (!error) { |
---|
2062 | /* do all callbacks |
---|
2063 | * cb that change the db are no problem, because it's the beginning of a ta |
---|
2064 | */ |
---|
2065 | call_pending_callbacks(); |
---|
2066 | ++clock; |
---|
2067 | } |
---|
2068 | return error; |
---|
2069 | } |
---|
2070 | |
---|
2071 | inline GB_ERROR GB_MAIN_TYPE::begin_transaction() { |
---|
2072 | if (transaction_level>0) return GBS_global_string("attempt to start a NEW transaction (at transaction level %i)", transaction_level); |
---|
2073 | if (transaction_level == 0) return start_transaction(); |
---|
2074 | return NULL; // NO_TRANSACTION_MODE |
---|
2075 | } |
---|
2076 | |
---|
2077 | inline GB_ERROR GB_MAIN_TYPE::abort_transaction() { |
---|
2078 | if (transaction_level<=0) { |
---|
2079 | if (transaction_level<0) return "GB_abort_transaction: Attempt to abort transaction in no-transaction-mode"; |
---|
2080 | return "GB_abort_transaction: No transaction running"; |
---|
2081 | } |
---|
2082 | if (transaction_level>1) { |
---|
2083 | aborted_transaction = 1; |
---|
2084 | return pop_transaction(); |
---|
2085 | } |
---|
2086 | |
---|
2087 | gb_abort_transaction_local_rek(gb_main_ref()); |
---|
2088 | if (is_client()) { |
---|
2089 | GB_ERROR error = gbcmc_abort_transaction(gb_main()); |
---|
2090 | if (error) return error; |
---|
2091 | } |
---|
2092 | clock--; |
---|
2093 | call_pending_callbacks(); |
---|
2094 | transaction_level = 0; |
---|
2095 | gb_untouch_children_and_me(root_container); |
---|
2096 | return 0; |
---|
2097 | } |
---|
2098 | |
---|
2099 | inline GB_ERROR GB_MAIN_TYPE::commit_transaction() { |
---|
2100 | GB_ERROR error = 0; |
---|
2101 | GB_CHANGE flag; |
---|
2102 | |
---|
2103 | if (!transaction_level) { |
---|
2104 | return "commit_transaction: No transaction running"; |
---|
2105 | } |
---|
2106 | if (transaction_level>1) { |
---|
2107 | return GBS_global_string("attempt to commit at transaction level %i", transaction_level); |
---|
2108 | } |
---|
2109 | if (aborted_transaction) { |
---|
2110 | aborted_transaction = 0; |
---|
2111 | return abort_transaction(); |
---|
2112 | } |
---|
2113 | if (is_server()) { |
---|
2114 | char *error1 = gb_set_undo_sync(gb_main()); |
---|
2115 | while (1) { |
---|
2116 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
---|
2117 | if (!flag) break; // nothing to do |
---|
2118 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, 0); |
---|
2119 | gb_untouch_children_and_me(root_container); |
---|
2120 | if (error) break; |
---|
2121 | call_pending_callbacks(); |
---|
2122 | } |
---|
2123 | gb_disable_undo(gb_main()); |
---|
2124 | if (error1) { |
---|
2125 | transaction_level = 0; |
---|
2126 | gb_assert(error); // maybe return error1? |
---|
2127 | return error; // @@@ huh? why not return error1 |
---|
2128 | } |
---|
2129 | } |
---|
2130 | else { |
---|
2131 | gb_disable_undo(gb_main()); |
---|
2132 | while (1) { |
---|
2133 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
---|
2134 | if (!flag) break; // nothing to do |
---|
2135 | |
---|
2136 | error = gbcmc_begin_sendupdate(gb_main()); if (error) break; |
---|
2137 | error = gb_commit_transaction_local_rek(gb_main_ref(), 1, 0); if (error) break; |
---|
2138 | error = gbcmc_end_sendupdate(gb_main()); if (error) break; |
---|
2139 | |
---|
2140 | gb_untouch_children_and_me(root_container); |
---|
2141 | call_pending_callbacks(); |
---|
2142 | } |
---|
2143 | if (!error) error = gbcmc_commit_transaction(gb_main()); |
---|
2144 | |
---|
2145 | } |
---|
2146 | transaction_level = 0; |
---|
2147 | return error; |
---|
2148 | } |
---|
2149 | |
---|
2150 | inline GB_ERROR GB_MAIN_TYPE::push_transaction() { |
---|
2151 | if (transaction_level == 0) return start_transaction(); |
---|
2152 | if (transaction_level>0) ++transaction_level; |
---|
2153 | // transaction<0 is NO_TRANSACTION_MODE |
---|
2154 | return NULL; |
---|
2155 | } |
---|
2156 | |
---|
2157 | inline GB_ERROR GB_MAIN_TYPE::pop_transaction() { |
---|
2158 | if (transaction_level==0) return "attempt to pop nested transaction while none running"; |
---|
2159 | if (transaction_level<0) return 0; // NO_TRANSACTION_MODE |
---|
2160 | if (transaction_level==1) return commit_transaction(); |
---|
2161 | transaction_level--; |
---|
2162 | return NULL; |
---|
2163 | } |
---|
2164 | |
---|
2165 | inline GB_ERROR GB_MAIN_TYPE::no_transaction() { |
---|
2166 | if (is_client()) return "Tried to disable transactions in a client"; |
---|
2167 | transaction_level = -1; |
---|
2168 | return NULL; |
---|
2169 | } |
---|
2170 | |
---|
2171 | GB_ERROR GB_MAIN_TYPE::send_update_to_server(GBDATA *gbd) { |
---|
2172 | GB_ERROR error = NULL; |
---|
2173 | |
---|
2174 | if (!transaction_level) error = "send_update_to_server: no transaction running"; |
---|
2175 | else if (is_server()) error = "send_update_to_server: only possible from clients (not from server itself)"; |
---|
2176 | else { |
---|
2177 | const gb_triggered_callback *chg_cbl_old = changeCBs.pending.get_tail(); |
---|
2178 | const gb_triggered_callback *del_cbl_old = deleteCBs.pending.get_tail(); |
---|
2179 | |
---|
2180 | error = gbcmc_begin_sendupdate(gb_main()); |
---|
2181 | if (!error) error = gb_commit_transaction_local_rek(gbd, 2, 0); |
---|
2182 | if (!error) error = gbcmc_end_sendupdate(gb_main()); |
---|
2183 | |
---|
2184 | if (!error && |
---|
2185 | (chg_cbl_old != changeCBs.pending.get_tail() || |
---|
2186 | del_cbl_old != deleteCBs.pending.get_tail())) |
---|
2187 | { |
---|
2188 | error = "send_update_to_server triggered a callback (this is not allowed)"; |
---|
2189 | } |
---|
2190 | } |
---|
2191 | return error; |
---|
2192 | } |
---|
2193 | |
---|
2194 | // -------------------------------------- |
---|
2195 | // client transaction interface |
---|
2196 | |
---|
2197 | GB_ERROR GB_push_transaction(GBDATA *gbd) { |
---|
2198 | /*! start a transaction if no transaction is running. |
---|
2199 | * (otherwise only trace nested transactions) |
---|
2200 | * |
---|
2201 | * recommended transaction usage: |
---|
2202 | * |
---|
2203 | * \code |
---|
2204 | * GB_ERROR myFunc() { |
---|
2205 | * GB_ERROR error = GB_push_transaction(gbd); |
---|
2206 | * if (!error) { |
---|
2207 | * error = ...; |
---|
2208 | * } |
---|
2209 | * return GB_end_transaction(gbd, error); |
---|
2210 | * } |
---|
2211 | * |
---|
2212 | * void myFunc() { |
---|
2213 | * GB_ERROR error = GB_push_transaction(gbd); |
---|
2214 | * if (!error) { |
---|
2215 | * error = ...; |
---|
2216 | * } |
---|
2217 | * GB_end_transaction_show_error(gbd, error, aw_message); |
---|
2218 | * } |
---|
2219 | * \endcode |
---|
2220 | * |
---|
2221 | * @see GB_pop_transaction(), GB_end_transaction(), GB_begin_transaction() |
---|
2222 | */ |
---|
2223 | |
---|
2224 | return GB_MAIN(gbd)->push_transaction(); |
---|
2225 | } |
---|
2226 | |
---|
2227 | GB_ERROR GB_pop_transaction(GBDATA *gbd) { |
---|
2228 | //! commit a transaction started with GB_push_transaction() |
---|
2229 | return GB_MAIN(gbd)->pop_transaction(); |
---|
2230 | } |
---|
2231 | GB_ERROR GB_begin_transaction(GBDATA *gbd) { |
---|
2232 | /*! like GB_push_transaction(), |
---|
2233 | * but fails if there is already an transaction running. |
---|
2234 | * @see GB_commit_transaction() and GB_abort_transaction() |
---|
2235 | */ |
---|
2236 | return GB_MAIN(gbd)->begin_transaction(); |
---|
2237 | } |
---|
2238 | GB_ERROR GB_no_transaction(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
---|
2239 | return GB_MAIN(gbd)->no_transaction(); |
---|
2240 | } |
---|
2241 | |
---|
2242 | GB_ERROR GB_abort_transaction(GBDATA *gbd) { |
---|
2243 | /*! abort a running transaction, |
---|
2244 | * i.e. forget all changes made to DB inside the current transaction. |
---|
2245 | * |
---|
2246 | * May be called instead of GB_pop_transaction() or GB_commit_transaction() |
---|
2247 | * |
---|
2248 | * If a nested transactions got aborted, |
---|
2249 | * committing a surrounding transaction will silently abort it as well. |
---|
2250 | */ |
---|
2251 | return GB_MAIN(gbd)->abort_transaction(); |
---|
2252 | } |
---|
2253 | |
---|
2254 | GB_ERROR GB_commit_transaction(GBDATA *gbd) { |
---|
2255 | /*! commit a transaction started with GB_begin_transaction() |
---|
2256 | * |
---|
2257 | * commit changes made to DB. |
---|
2258 | * |
---|
2259 | * in case of nested transactions, this is equal to GB_pop_transaction() |
---|
2260 | */ |
---|
2261 | return GB_MAIN(gbd)->commit_transaction(); |
---|
2262 | } |
---|
2263 | |
---|
2264 | GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error) { |
---|
2265 | /*! abort or commit transaction |
---|
2266 | * |
---|
2267 | * @ param error |
---|
2268 | * - if NULL commit transaction |
---|
2269 | * - else abort transaction |
---|
2270 | * |
---|
2271 | * always commits in no-transaction-mode |
---|
2272 | * |
---|
2273 | * @return error or transaction error |
---|
2274 | * @see GB_push_transaction() for example |
---|
2275 | */ |
---|
2276 | |
---|
2277 | if (GB_get_transaction_level(gbd)<0) { |
---|
2278 | ASSERT_RESULT(GB_ERROR, NULL, GB_pop_transaction(gbd)); |
---|
2279 | } |
---|
2280 | else { |
---|
2281 | if (error) GB_abort_transaction(gbd); |
---|
2282 | else error = GB_pop_transaction(gbd); |
---|
2283 | } |
---|
2284 | return error; |
---|
2285 | } |
---|
2286 | |
---|
2287 | void GB_end_transaction_show_error(GBDATA *gbd, GB_ERROR error, void (*error_handler)(GB_ERROR)) { |
---|
2288 | //! like GB_end_transaction(), but show error using 'error_handler' |
---|
2289 | error = GB_end_transaction(gbd, error); |
---|
2290 | if (error) error_handler(error); |
---|
2291 | } |
---|
2292 | |
---|
2293 | int GB_get_transaction_level(GBDATA *gbd) { |
---|
2294 | /*! @return transaction level |
---|
2295 | * <0 -> in no-transaction-mode (abort is impossible) |
---|
2296 | * 0 -> not in transaction |
---|
2297 | * 1 -> one single transaction |
---|
2298 | * 2, ... -> nested transactions |
---|
2299 | */ |
---|
2300 | return GB_MAIN(gbd)->get_transaction_level(); |
---|
2301 | } |
---|
2302 | |
---|
2303 | // ------------------ |
---|
2304 | // callbacks |
---|
2305 | |
---|
2306 | static void dummy_db_cb(GBDATA*, GB_CB_TYPE) { gb_assert(0); } // used as marker for deleted callbacks |
---|
2307 | DatabaseCallback TypedDatabaseCallback::MARKED_DELETED = makeDatabaseCallback(dummy_db_cb); |
---|
2308 | |
---|
2309 | GB_MAIN_TYPE *gb_get_main_during_cb() { |
---|
2310 | /* if inside a callback, return the DB root of the DB element, the callback was called for. |
---|
2311 | * if not inside a callback, return NULL. |
---|
2312 | */ |
---|
2313 | return inside_callback_main; |
---|
2314 | } |
---|
2315 | |
---|
2316 | NOT4PERL bool GB_inside_callback(GBDATA *of_gbd, GB_CB_TYPE cbtype) { |
---|
2317 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
---|
2318 | bool inside = false; |
---|
2319 | |
---|
2320 | if (Main) { // inside a callback |
---|
2321 | gb_assert(currently_called_back); |
---|
2322 | if (currently_called_back->gbd == of_gbd) { |
---|
2323 | GB_CB_TYPE curr_cbtype; |
---|
2324 | if (Main->has_pending_delete_callback()) { // delete callbacks were not all performed yet |
---|
2325 | // => current callback is a delete callback |
---|
2326 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & GB_CB_DELETE); |
---|
2327 | } |
---|
2328 | else { |
---|
2329 | gb_assert(Main->has_pending_change_callback()); |
---|
2330 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & (GB_CB_ALL-GB_CB_DELETE)); |
---|
2331 | } |
---|
2332 | gb_assert(curr_cbtype != GB_CB_NONE); // wtf!? are we inside callback or not? |
---|
2333 | |
---|
2334 | if ((cbtype&curr_cbtype) != GB_CB_NONE) { |
---|
2335 | inside = true; |
---|
2336 | } |
---|
2337 | } |
---|
2338 | } |
---|
2339 | |
---|
2340 | return inside; |
---|
2341 | } |
---|
2342 | |
---|
2343 | GBDATA *GB_get_gb_main_during_cb() { |
---|
2344 | GBDATA *gb_main = NULL; |
---|
2345 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
---|
2346 | |
---|
2347 | if (Main) { // inside callback |
---|
2348 | if (!GB_inside_callback(Main->gb_main(), GB_CB_DELETE)) { // main is not deleted |
---|
2349 | gb_main = Main->gb_main(); |
---|
2350 | } |
---|
2351 | } |
---|
2352 | return gb_main; |
---|
2353 | } |
---|
2354 | |
---|
2355 | |
---|
2356 | |
---|
2357 | static GB_CSTR gb_read_pntr_ts(GBDATA *gbd, gb_transaction_save *ts) { |
---|
2358 | int type = GB_TYPE_TS(ts); |
---|
2359 | const char *data = GB_GETDATA_TS(ts); |
---|
2360 | if (data) { |
---|
2361 | if (ts->flags.compressed_data) { // uncompressed data return pntr to database entry |
---|
2362 | long size = GB_GETSIZE_TS(ts) * gb_convert_type_2_sizeof[type] + gb_convert_type_2_appendix_size[type]; |
---|
2363 | data = gb_uncompress_data(gbd, data, size); |
---|
2364 | } |
---|
2365 | } |
---|
2366 | return data; |
---|
2367 | } |
---|
2368 | |
---|
2369 | // get last array value in callbacks |
---|
2370 | NOT4PERL const void *GB_read_old_value() { |
---|
2371 | char *data; |
---|
2372 | |
---|
2373 | if (!currently_called_back) { |
---|
2374 | GB_export_error("You cannot call GB_read_old_value outside a ARBDB callback"); |
---|
2375 | return NULL; |
---|
2376 | } |
---|
2377 | if (!currently_called_back->old) { |
---|
2378 | GB_export_error("No old value available in GB_read_old_value"); |
---|
2379 | return NULL; |
---|
2380 | } |
---|
2381 | data = GB_GETDATA_TS(currently_called_back->old); |
---|
2382 | if (!data) return NULL; |
---|
2383 | |
---|
2384 | return gb_read_pntr_ts(currently_called_back->gbd, currently_called_back->old); |
---|
2385 | } |
---|
2386 | // same for size |
---|
2387 | long GB_read_old_size() { |
---|
2388 | if (!currently_called_back) { |
---|
2389 | GB_export_error("You cannot call GB_read_old_size outside a ARBDB callback"); |
---|
2390 | return -1; |
---|
2391 | } |
---|
2392 | if (!currently_called_back->old) { |
---|
2393 | GB_export_error("No old value available in GB_read_old_size"); |
---|
2394 | return -1; |
---|
2395 | } |
---|
2396 | return GB_GETSIZE_TS(currently_called_back->old); |
---|
2397 | } |
---|
2398 | |
---|
2399 | inline char *cbtype2readable(GB_CB_TYPE type) { |
---|
2400 | ConstStrArray septype; |
---|
2401 | |
---|
2402 | #define appendcbtype(cbt) do { \ |
---|
2403 | if (type&cbt) { \ |
---|
2404 | type = GB_CB_TYPE(type-cbt); \ |
---|
2405 | septype.put(#cbt); \ |
---|
2406 | } \ |
---|
2407 | } while(0) |
---|
2408 | |
---|
2409 | appendcbtype(GB_CB_DELETE); |
---|
2410 | appendcbtype(GB_CB_CHANGED); |
---|
2411 | appendcbtype(GB_CB_SON_CREATED); |
---|
2412 | |
---|
2413 | gb_assert(type == GB_CB_NONE); |
---|
2414 | |
---|
2415 | return GBT_join_names(septype, '|'); |
---|
2416 | } |
---|
2417 | |
---|
2418 | char *TypedDatabaseCallback::get_info() const { |
---|
2419 | const char *readable_fun = GBS_funptr2readable((void*)dbcb.callee(), true); |
---|
2420 | char *readable_cbtype = cbtype2readable((GB_CB_TYPE)dbcb.inspect_CD2()); |
---|
2421 | char *result = GBS_global_string_copy("func='%s' type=%s clientdata=%p", |
---|
2422 | readable_fun, readable_cbtype, (void*)dbcb.inspect_CD1()); |
---|
2423 | |
---|
2424 | free(readable_cbtype); |
---|
2425 | |
---|
2426 | return result; |
---|
2427 | } |
---|
2428 | |
---|
2429 | char *GB_get_callback_info(GBDATA *gbd) { |
---|
2430 | // returns human-readable information about callbacks of 'gbd' or 0 |
---|
2431 | char *result = 0; |
---|
2432 | if (gbd->ext) { |
---|
2433 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
2434 | if (cbl) { |
---|
2435 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
---|
2436 | char *cb_info = cb->spec.get_info(); |
---|
2437 | if (result) { |
---|
2438 | char *new_result = GBS_global_string_copy("%s\n%s", result, cb_info); |
---|
2439 | free(result); |
---|
2440 | free(cb_info); |
---|
2441 | result = new_result; |
---|
2442 | } |
---|
2443 | else { |
---|
2444 | result = cb_info; |
---|
2445 | } |
---|
2446 | } |
---|
2447 | } |
---|
2448 | } |
---|
2449 | |
---|
2450 | return result; |
---|
2451 | } |
---|
2452 | |
---|
2453 | #if defined(ASSERTION_USED) |
---|
2454 | template<typename CB> |
---|
2455 | bool CallbackList<CB>::contains_unremoved_callback(const CB& like) const { |
---|
2456 | for (const_itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
2457 | if (cb->spec.is_equal_to(like.spec) && !cb->spec.is_marked_for_removal()) { |
---|
2458 | return true; |
---|
2459 | } |
---|
2460 | } |
---|
2461 | return false; |
---|
2462 | } |
---|
2463 | #endif |
---|
2464 | |
---|
2465 | inline void add_to_callback_chain(gb_callback_list*& head, const TypedDatabaseCallback& cbs) { |
---|
2466 | if (!head) head = new gb_callback_list; |
---|
2467 | head->add(gb_callback(cbs)); |
---|
2468 | } |
---|
2469 | inline void add_to_callback_chain(gb_hierarchy_callback_list*& head, const TypedDatabaseCallback& cbs, GBDATA *gb_representative) { |
---|
2470 | if (!head) head = new gb_hierarchy_callback_list; |
---|
2471 | head->add(gb_hierarchy_callback(cbs, gb_representative)); |
---|
2472 | } |
---|
2473 | |
---|
2474 | inline GB_ERROR gb_add_callback(GBDATA *gbd, const TypedDatabaseCallback& cbs) { |
---|
2475 | /* Adds a callback to a DB entry. |
---|
2476 | * |
---|
2477 | * Be careful when writing GB_CB_DELETE callbacks, there is a severe restriction: |
---|
2478 | * |
---|
2479 | * - the DB element may already be freed. The pointer is still pointing to the original |
---|
2480 | * location, so you can use it to identify the DB element, but you cannot dereference |
---|
2481 | * it under all circumstances. |
---|
2482 | * |
---|
2483 | * ARBDB internal delete-callbacks may use gb_get_main_during_cb() to access the DB root. |
---|
2484 | * See also: GB_get_gb_main_during_cb() |
---|
2485 | */ |
---|
2486 | |
---|
2487 | #if defined(DEBUG) |
---|
2488 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
---|
2489 | printf("Warning: add_priority_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
---|
2490 | #if defined(DEVEL_RALF) |
---|
2491 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
---|
2492 | #endif // DEVEL_RALF |
---|
2493 | } |
---|
2494 | #endif // DEBUG |
---|
2495 | |
---|
2496 | GB_test_transaction(gbd); // may return error |
---|
2497 | gbd->create_extended(); |
---|
2498 | add_to_callback_chain(gbd->ext->callback, cbs); |
---|
2499 | return 0; |
---|
2500 | } |
---|
2501 | |
---|
2502 | GB_ERROR GB_add_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
2503 | return gb_add_callback(gbd, TypedDatabaseCallback(dbcb, type)); |
---|
2504 | } |
---|
2505 | |
---|
2506 | inline void GB_MAIN_TYPE::callback_group::add_hcb(GBDATA *gb_representative, const TypedDatabaseCallback& dbcb) { |
---|
2507 | add_to_callback_chain(hierarchy_cbs, dbcb, gb_representative); |
---|
2508 | } |
---|
2509 | |
---|
2510 | GB_ERROR GB_MAIN_TYPE::add_hierarchy_cb(GBDATA *gb_representative, const TypedDatabaseCallback& dbcb) { |
---|
2511 | GB_CB_TYPE type = dbcb.get_type(); |
---|
2512 | if (type & GB_CB_DELETE) { |
---|
2513 | deleteCBs.add_hcb(gb_representative, dbcb.with_type_changed_to(GB_CB_DELETE)); |
---|
2514 | } |
---|
2515 | if (type & GB_CB_ALL_BUT_DELETE) { |
---|
2516 | changeCBs.add_hcb(gb_representative, dbcb.with_type_changed_to(GB_CB_TYPE(type&GB_CB_ALL_BUT_DELETE))); |
---|
2517 | } |
---|
2518 | return NULL; |
---|
2519 | } |
---|
2520 | |
---|
2521 | GB_ERROR GB_add_hierarchy_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { // @@@ needed to impl #418 |
---|
2522 | /*! bind callback to ALL entries which are at the same DB-hierarchy as 'gbd'. |
---|
2523 | * |
---|
2524 | * Hierarchy callbacks are triggered before normal callbacks (added by GB_add_callback or GB_ensure_callback). |
---|
2525 | * Nevertheless delete callbacks take precedence over change callbacks |
---|
2526 | * (i.e. a normal delete callback is triggered before a hierarchical change callback). |
---|
2527 | * |
---|
2528 | * Hierarchy callbacks are cannot be installed and will NOT be triggered in NO_TRANSACTION_MODE |
---|
2529 | * (i.e. it will not work in ARBs property DBs) |
---|
2530 | */ |
---|
2531 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
2532 | gb_assert(Main->get_transaction_level()>=0); // hierarchical callbacks are not supported in NO_TRANSACTION_MODE |
---|
2533 | return Main->add_hierarchy_cb(gbd, TypedDatabaseCallback(dbcb, type)); |
---|
2534 | } |
---|
2535 | |
---|
2536 | template <typename PRED> |
---|
2537 | inline void gb_remove_callbacks_that(GBDATA *gbd, PRED shallRemove) { |
---|
2538 | #if defined(ASSERTION_USED) |
---|
2539 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
---|
2540 | printf("Warning: gb_remove_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
---|
2541 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
---|
2542 | return; |
---|
2543 | } |
---|
2544 | #endif // DEBUG |
---|
2545 | |
---|
2546 | if (gbd->ext) { |
---|
2547 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
2548 | if (cbl) { |
---|
2549 | bool prev_running = false; |
---|
2550 | |
---|
2551 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ) { |
---|
2552 | bool this_running = cb->running; |
---|
2553 | |
---|
2554 | if (shallRemove(*cb)) { |
---|
2555 | if (prev_running || this_running) { |
---|
2556 | cb->spec.mark_for_removal(); |
---|
2557 | ++cb; |
---|
2558 | } |
---|
2559 | else { |
---|
2560 | cb = cbl->callbacks.erase(cb); |
---|
2561 | } |
---|
2562 | } |
---|
2563 | else { |
---|
2564 | ++cb; |
---|
2565 | } |
---|
2566 | prev_running = this_running; |
---|
2567 | } |
---|
2568 | } |
---|
2569 | } |
---|
2570 | } |
---|
2571 | |
---|
2572 | struct ShallBeDeleted { |
---|
2573 | bool operator()(const gb_callback& cb) const { return cb.spec.is_marked_for_removal(); } |
---|
2574 | }; |
---|
2575 | static void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd) { |
---|
2576 | gb_remove_callbacks_that(gbd, ShallBeDeleted()); |
---|
2577 | } |
---|
2578 | |
---|
2579 | struct IsCallback : private TypedDatabaseCallback { |
---|
2580 | IsCallback(GB_CB func_, GB_CB_TYPE type_) : TypedDatabaseCallback(makeDatabaseCallback((GB_CB)func_, (int*)NULL), type_) {} |
---|
2581 | bool operator()(const gb_callback& cb) const { return sig_is_equal_to(cb.spec); } |
---|
2582 | }; |
---|
2583 | struct IsSpecificCallback : private TypedDatabaseCallback { |
---|
2584 | IsSpecificCallback(const TypedDatabaseCallback& cb) : TypedDatabaseCallback(cb) {} |
---|
2585 | bool operator()(const gb_callback& cb) const { return is_equal_to(cb.spec); } |
---|
2586 | }; |
---|
2587 | |
---|
2588 | void GB_remove_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
2589 | // remove specific callback; 'type' and 'dbcb' have to match |
---|
2590 | gb_remove_callbacks_that(gbd, IsSpecificCallback(TypedDatabaseCallback(dbcb, type))); |
---|
2591 | } |
---|
2592 | void GB_remove_all_callbacks_to(GBDATA *gbd, GB_CB_TYPE type, GB_CB func) { |
---|
2593 | // removes all callbacks 'func' bound to 'gbd' with 'type' |
---|
2594 | gb_remove_callbacks_that(gbd, IsCallback(func, type)); |
---|
2595 | } |
---|
2596 | |
---|
2597 | GB_ERROR GB_ensure_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
2598 | TypedDatabaseCallback newcb(dbcb, type); |
---|
2599 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
2600 | if (cbl) { |
---|
2601 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
---|
2602 | if (cb->spec.is_equal_to(newcb) && !cb->spec.is_marked_for_removal()) { |
---|
2603 | return NULL; // already in cb list |
---|
2604 | } |
---|
2605 | } |
---|
2606 | } |
---|
2607 | return gb_add_callback(gbd, newcb); |
---|
2608 | } |
---|
2609 | |
---|
2610 | int GB_nsons(GBDATA *gbd) { |
---|
2611 | /*! return number of child entries |
---|
2612 | * |
---|
2613 | * @@@ does this work in clients ? |
---|
2614 | */ |
---|
2615 | |
---|
2616 | return gbd->is_container() |
---|
2617 | ? gbd->as_container()->d.size |
---|
2618 | : 0; |
---|
2619 | } |
---|
2620 | |
---|
2621 | void GB_disable_quicksave(GBDATA *gbd, const char *reason) { |
---|
2622 | /*! Disable quicksaving database |
---|
2623 | * @param gbd any DB node |
---|
2624 | * @param reason why quicksaving is not allowed |
---|
2625 | */ |
---|
2626 | freedup(GB_MAIN(gbd)->qs.quick_save_disabled, reason); |
---|
2627 | } |
---|
2628 | |
---|
2629 | GB_ERROR GB_resort_data_base(GBDATA *gb_main, GBDATA **new_order_list, long listsize) { |
---|
2630 | { |
---|
2631 | long client_count = GB_read_clients(gb_main); |
---|
2632 | if (client_count<0) |
---|
2633 | return "Sorry: this program is not the arbdb server, you cannot resort your data"; |
---|
2634 | |
---|
2635 | if (client_count>0) |
---|
2636 | return GBS_global_string("There are %li clients (editors, tree programs) connected to this server.\n" |
---|
2637 | "You need to these close clients before you can run this operation.", |
---|
2638 | client_count); |
---|
2639 | } |
---|
2640 | |
---|
2641 | if (listsize <= 0) return 0; |
---|
2642 | |
---|
2643 | GBCONTAINER *father = GB_FATHER(new_order_list[0]); |
---|
2644 | GB_disable_quicksave(gb_main, "some entries in the database got a new order"); |
---|
2645 | |
---|
2646 | gb_header_list *hl = GB_DATA_LIST_HEADER(father->d); |
---|
2647 | for (long new_index = 0; new_index< listsize; new_index++) { |
---|
2648 | long old_index = new_order_list[new_index]->index; |
---|
2649 | |
---|
2650 | if (old_index < new_index) { |
---|
2651 | GB_warningf("Warning at resort database: entry exists twice: %li and %li", |
---|
2652 | old_index, new_index); |
---|
2653 | } |
---|
2654 | else { |
---|
2655 | GBDATA *ogb = GB_HEADER_LIST_GBD(hl[old_index]); |
---|
2656 | GBDATA *ngb = GB_HEADER_LIST_GBD(hl[new_index]); |
---|
2657 | |
---|
2658 | gb_header_list h = hl[new_index]; |
---|
2659 | hl[new_index] = hl[old_index]; |
---|
2660 | hl[old_index] = h; // Warning: Relative Pointers are incorrect !!! |
---|
2661 | |
---|
2662 | SET_GB_HEADER_LIST_GBD(hl[old_index], ngb); |
---|
2663 | SET_GB_HEADER_LIST_GBD(hl[new_index], ogb); |
---|
2664 | |
---|
2665 | if (ngb) ngb->index = old_index; |
---|
2666 | if (ogb) ogb->index = new_index; |
---|
2667 | } |
---|
2668 | } |
---|
2669 | |
---|
2670 | gb_touch_entry(father, GB_NORMAL_CHANGE); |
---|
2671 | return 0; |
---|
2672 | } |
---|
2673 | |
---|
2674 | GB_ERROR gb_resort_system_folder_to_top(GBCONTAINER *gb_main) { |
---|
2675 | GBDATA *gb_system = GB_entry(gb_main, GB_SYSTEM_FOLDER); |
---|
2676 | GBDATA *gb_first = GB_child(gb_main); |
---|
2677 | GBDATA **new_order_list; |
---|
2678 | GB_ERROR error = 0; |
---|
2679 | int i, len; |
---|
2680 | if (GB_read_clients(gb_main)<0) return 0; // we are not server |
---|
2681 | if (!gb_system) { |
---|
2682 | return GB_export_error("System databaseentry does not exist"); |
---|
2683 | } |
---|
2684 | if (gb_first == gb_system) return 0; |
---|
2685 | len = GB_number_of_subentries(gb_main); |
---|
2686 | new_order_list = (GBDATA **)GB_calloc(sizeof(GBDATA *), len); |
---|
2687 | new_order_list[0] = gb_system; |
---|
2688 | for (i=1; i<len; i++) { |
---|
2689 | new_order_list[i] = gb_first; |
---|
2690 | do { |
---|
2691 | gb_first = GB_nextChild(gb_first); |
---|
2692 | } while (gb_first == gb_system); |
---|
2693 | } |
---|
2694 | error = GB_resort_data_base(gb_main, new_order_list, len); |
---|
2695 | free(new_order_list); |
---|
2696 | return error; |
---|
2697 | } |
---|
2698 | |
---|
2699 | // ------------------------------ |
---|
2700 | // private(?) user flags |
---|
2701 | |
---|
2702 | STATIC_ASSERT_ANNOTATED(((GB_USERFLAG_ANY+1)&GB_USERFLAG_ANY) == 0, "not all bits set in GB_USERFLAG_ANY"); |
---|
2703 | |
---|
2704 | #if defined(ASSERTION_USED) |
---|
2705 | inline bool legal_user_bitmask(unsigned char bitmask) { |
---|
2706 | return bitmask>0 && bitmask<=GB_USERFLAG_ANY; |
---|
2707 | } |
---|
2708 | #endif |
---|
2709 | |
---|
2710 | inline gb_flag_types2& get_user_flags(GBDATA *gbd) { |
---|
2711 | return gbd->expect_container()->flags2; |
---|
2712 | } |
---|
2713 | |
---|
2714 | bool GB_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2715 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2716 | return get_user_flags(gbd).user_bits & user_bit; |
---|
2717 | } |
---|
2718 | void GB_set_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2719 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2720 | gb_flag_types2& flags = get_user_flags(gbd); |
---|
2721 | flags.user_bits |= user_bit; |
---|
2722 | } |
---|
2723 | |
---|
2724 | void GB_clear_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2725 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2726 | gb_flag_types2& flags = get_user_flags(gbd); |
---|
2727 | flags.user_bits &= (user_bit^GB_USERFLAG_ANY); |
---|
2728 | } |
---|
2729 | |
---|
2730 | // ------------------------ |
---|
2731 | // mark DB entries |
---|
2732 | |
---|
2733 | void GB_write_flag(GBDATA *gbd, long flag) { |
---|
2734 | GBCONTAINER *gbc = gbd->expect_container(); |
---|
2735 | GB_MAIN_TYPE *Main = GB_MAIN(gbc); |
---|
2736 | |
---|
2737 | GB_test_transaction(Main); |
---|
2738 | |
---|
2739 | int ubit = Main->users[0]->userbit; |
---|
2740 | int prev = GB_ARRAY_FLAGS(gbc).flags; |
---|
2741 | gbc->flags.saved_flags = prev; |
---|
2742 | |
---|
2743 | if (flag) { |
---|
2744 | GB_ARRAY_FLAGS(gbc).flags |= ubit; |
---|
2745 | } |
---|
2746 | else { |
---|
2747 | GB_ARRAY_FLAGS(gbc).flags &= ~ubit; |
---|
2748 | } |
---|
2749 | if (prev != (int)GB_ARRAY_FLAGS(gbc).flags) { |
---|
2750 | gb_touch_entry(gbc, GB_NORMAL_CHANGE); |
---|
2751 | gb_touch_header(GB_FATHER(gbc)); |
---|
2752 | GB_DO_CALLBACKS(gbc); |
---|
2753 | } |
---|
2754 | } |
---|
2755 | |
---|
2756 | int GB_read_flag(GBDATA *gbd) { |
---|
2757 | GB_test_transaction(gbd); |
---|
2758 | if (GB_ARRAY_FLAGS(gbd).flags & GB_MAIN(gbd)->users[0]->userbit) return 1; |
---|
2759 | else return 0; |
---|
2760 | } |
---|
2761 | |
---|
2762 | void GB_touch(GBDATA *gbd) { |
---|
2763 | GB_test_transaction(gbd); |
---|
2764 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2765 | GB_DO_CALLBACKS(gbd); |
---|
2766 | } |
---|
2767 | |
---|
2768 | |
---|
2769 | char GB_type_2_char(GB_TYPES type) { |
---|
2770 | const char *type2char = "-bcif-B-CIFlSS-%"; |
---|
2771 | return type2char[type]; |
---|
2772 | } |
---|
2773 | |
---|
2774 | GB_ERROR GB_print_debug_information(void */*dummy_AW_root*/, GBDATA *gb_main) { |
---|
2775 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
2776 | GB_push_transaction(gb_main); |
---|
2777 | for (int i=0; i<Main->keycnt; i++) { |
---|
2778 | if (Main->keys[i].key) { |
---|
2779 | printf("%3i %20s nref %i\n", i, Main->keys[i].key, (int)Main->keys[i].nref); |
---|
2780 | } |
---|
2781 | else { |
---|
2782 | printf(" %3i unused key, next free key = %li\n", i, Main->keys[i].next_free_key); |
---|
2783 | } |
---|
2784 | } |
---|
2785 | gbm_debug_mem(); |
---|
2786 | GB_pop_transaction(gb_main); |
---|
2787 | return 0; |
---|
2788 | } |
---|
2789 | |
---|
2790 | static int GB_info_deep = 15; |
---|
2791 | |
---|
2792 | |
---|
2793 | static int gb_info(GBDATA *gbd, int deep) { |
---|
2794 | if (gbd==NULL) { printf("NULL\n"); return -1; } |
---|
2795 | GB_push_transaction(gbd); |
---|
2796 | |
---|
2797 | GB_TYPES type = gbd->type(); |
---|
2798 | |
---|
2799 | if (deep) { |
---|
2800 | printf(" "); |
---|
2801 | } |
---|
2802 | |
---|
2803 | printf("(GBDATA*)0x%lx (GBCONTAINER*)0x%lx ", (long)gbd, (long)gbd); |
---|
2804 | |
---|
2805 | if (gbd->rel_father==0) { printf("father=NULL\n"); return -1; } |
---|
2806 | |
---|
2807 | GBCONTAINER *gbc; |
---|
2808 | GB_MAIN_TYPE *Main; |
---|
2809 | if (type==GB_DB) { gbc = gbd->as_container(); Main = GBCONTAINER_MAIN(gbc); } |
---|
2810 | else { gbc = NULL; Main = GB_MAIN(gbd); } |
---|
2811 | |
---|
2812 | if (!Main) { printf("Oops - I have no main entry!!!\n"); return -1; } |
---|
2813 | if (gbd==Main->dummy_father) { printf("dummy_father!\n"); return -1; } |
---|
2814 | |
---|
2815 | printf("%10s Type '%c' ", GB_read_key_pntr(gbd), GB_type_2_char(type)); |
---|
2816 | |
---|
2817 | switch (type) { |
---|
2818 | case GB_DB: { |
---|
2819 | int size = gbc->d.size; |
---|
2820 | printf("Size %i nheader %i hmemsize %i", gbc->d.size, gbc->d.nheader, gbc->d.headermemsize); |
---|
2821 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
---|
2822 | if (size < GB_info_deep) { |
---|
2823 | int index; |
---|
2824 | gb_header_list *header; |
---|
2825 | |
---|
2826 | header = GB_DATA_LIST_HEADER(gbc->d); |
---|
2827 | for (index = 0; index < gbc->d.nheader; index++) { |
---|
2828 | GBDATA *gb_sub = GB_HEADER_LIST_GBD(header[index]); |
---|
2829 | printf("\t\t%10s (GBDATA*)0x%lx (GBCONTAINER*)0x%lx\n", Main->keys[header[index].flags.key_quark].key, (long)gb_sub, (long)gb_sub); |
---|
2830 | } |
---|
2831 | } |
---|
2832 | break; |
---|
2833 | } |
---|
2834 | default: { |
---|
2835 | char *data = GB_read_as_string(gbd); |
---|
2836 | if (data) { printf("%s", data); free(data); } |
---|
2837 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
---|
2838 | } |
---|
2839 | } |
---|
2840 | |
---|
2841 | |
---|
2842 | GB_pop_transaction(gbd); |
---|
2843 | |
---|
2844 | return 0; |
---|
2845 | } |
---|
2846 | |
---|
2847 | int GB_info(GBDATA *gbd) { // unused - intended to be used in debugger |
---|
2848 | return gb_info(gbd, 0); |
---|
2849 | } |
---|
2850 | |
---|
2851 | long GB_number_of_subentries(GBDATA *gbd) { |
---|
2852 | GBCONTAINER *gbc = gbd->expect_container(); |
---|
2853 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
---|
2854 | |
---|
2855 | long subentries = 0; |
---|
2856 | int end = gbc->d.nheader; |
---|
2857 | |
---|
2858 | for (int index = 0; index<end; index++) { |
---|
2859 | if (header[index].flags.changed < GB_DELETED) subentries++; |
---|
2860 | } |
---|
2861 | return subentries; |
---|
2862 | } |
---|
2863 | |
---|
2864 | // -------------------------------------------------------------------------------- |
---|
2865 | |
---|
2866 | #ifdef UNIT_TESTS |
---|
2867 | |
---|
2868 | #include <test_unit.h> |
---|
2869 | #include <locale.h> |
---|
2870 | |
---|
2871 | void TEST_GB_atof() { |
---|
2872 | // startup of ARB (gtk_only@11651) is failing on ubuntu 13.10 (in GBT_read_tree) |
---|
2873 | // (failed with "Error: 'GB_safe_atof("0.0810811", ..) returns error: cannot convert '0.0810811' to double'") |
---|
2874 | // Reason: LANG[UAGE] or LC_NUMERIC set to "de_DE..." |
---|
2875 | // |
---|
2876 | // Notes: |
---|
2877 | // * gtk apparently calls 'setlocale(LC_ALL, "");', motif doesnt |
---|
2878 | |
---|
2879 | TEST_EXPECT_SIMILAR(GB_atof("0.031"), 0.031, 0.0001); // @@@ make this fail, then fix it |
---|
2880 | } |
---|
2881 | |
---|
2882 | void TEST_999_strtod_replacement() { |
---|
2883 | // caution: if it fails -> locale is not reset (therefore call with low priority 999) |
---|
2884 | const char *old = setlocale(LC_NUMERIC, "de_DE.UTF-8"); |
---|
2885 | { |
---|
2886 | // TEST_EXPECT_SIMILAR__BROKEN(strtod("0.031", NULL), 0.031, 0.0001); |
---|
2887 | TEST_EXPECT_SIMILAR(g_ascii_strtod("0.031", NULL), 0.031, 0.0001); |
---|
2888 | } |
---|
2889 | setlocale(LC_NUMERIC, old); |
---|
2890 | } |
---|
2891 | |
---|
2892 | static void test_another_shell() { delete new GB_shell; } |
---|
2893 | static void test_opendb() { GB_close(GB_open("no.arb", "c")); } |
---|
2894 | |
---|
2895 | void TEST_GB_shell() { |
---|
2896 | { |
---|
2897 | GB_shell *shell = new GB_shell; |
---|
2898 | TEST_EXPECT_SEGFAULT(test_another_shell); |
---|
2899 | test_opendb(); // no SEGV here |
---|
2900 | delete shell; |
---|
2901 | } |
---|
2902 | |
---|
2903 | TEST_EXPECT_SEGFAULT(test_opendb); // should be impossible to open db w/o shell |
---|
2904 | } |
---|
2905 | |
---|
2906 | void TEST_GB_number_of_subentries() { |
---|
2907 | GB_shell shell; |
---|
2908 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
2909 | |
---|
2910 | { |
---|
2911 | GB_transaction ta(gb_main); |
---|
2912 | |
---|
2913 | GBDATA *gb_cont = GB_create_container(gb_main, "container"); |
---|
2914 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 0); |
---|
2915 | |
---|
2916 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
---|
2917 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
---|
2918 | |
---|
2919 | { |
---|
2920 | GBDATA *gb_entry; |
---|
2921 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_entry = GB_create(gb_cont, "entry", GB_STRING)); |
---|
2922 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
---|
2923 | |
---|
2924 | TEST_EXPECT_NO_ERROR(GB_delete(gb_entry)); |
---|
2925 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
---|
2926 | } |
---|
2927 | |
---|
2928 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
---|
2929 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
---|
2930 | } |
---|
2931 | |
---|
2932 | // TEST_REJECT(true); // @@@ fail while GB_shell open |
---|
2933 | |
---|
2934 | GB_close(gb_main); |
---|
2935 | } |
---|
2936 | |
---|
2937 | static void test_count_cb(GBDATA *, int *counter) { |
---|
2938 | fprintf(stderr, "test_count_cb: var.add=%p old.val=%i ", counter, *counter); |
---|
2939 | (*counter)++; |
---|
2940 | fprintf(stderr, "new.val=%i\n", *counter); |
---|
2941 | fflush(stderr); |
---|
2942 | } |
---|
2943 | |
---|
2944 | static void remove_self_cb(GBDATA *gbe, GB_CB_TYPE cbtype) { |
---|
2945 | GB_remove_callback(gbe, cbtype, makeDatabaseCallback(remove_self_cb)); |
---|
2946 | } |
---|
2947 | |
---|
2948 | static void re_add_self_cb(GBDATA *gbe, int *calledCounter, GB_CB_TYPE cbtype) { |
---|
2949 | ++(*calledCounter); |
---|
2950 | |
---|
2951 | DatabaseCallback dbcb = makeDatabaseCallback(re_add_self_cb, calledCounter); |
---|
2952 | GB_remove_callback(gbe, cbtype, dbcb); |
---|
2953 | |
---|
2954 | GB_ERROR error = GB_add_callback(gbe, cbtype, dbcb); |
---|
2955 | gb_assert(!error); |
---|
2956 | } |
---|
2957 | |
---|
2958 | void TEST_db_callbacks_ta_nota() { |
---|
2959 | GB_shell shell; |
---|
2960 | |
---|
2961 | enum TAmode { |
---|
2962 | NO_TA = 1, // no transaction mode |
---|
2963 | WITH_TA = 2, // transaction mode |
---|
2964 | |
---|
2965 | BOTH_TA_MODES = (NO_TA|WITH_TA) |
---|
2966 | }; |
---|
2967 | |
---|
2968 | for (TAmode ta_mode = NO_TA; ta_mode <= WITH_TA; ta_mode = TAmode(ta_mode+1)) { |
---|
2969 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
2970 | GB_ERROR error; |
---|
2971 | |
---|
2972 | TEST_ANNOTATE(ta_mode == NO_TA ? "NO_TA" : "WITH_TA"); |
---|
2973 | if (ta_mode == NO_TA) { |
---|
2974 | error = GB_no_transaction(gb_main); TEST_EXPECT_NO_ERROR(error); |
---|
2975 | } |
---|
2976 | |
---|
2977 | // create some DB entries |
---|
2978 | GBDATA *gbc; |
---|
2979 | GBDATA *gbe1; |
---|
2980 | GBDATA *gbe2; |
---|
2981 | GBDATA *gbe3; |
---|
2982 | { |
---|
2983 | GB_transaction ta(gb_main); |
---|
2984 | |
---|
2985 | gbc = GB_create_container(gb_main, "cont"); |
---|
2986 | gbe1 = GB_create(gbc, "entry", GB_STRING); |
---|
2987 | gbe2 = GB_create(gb_main, "entry", GB_INT); |
---|
2988 | } |
---|
2989 | |
---|
2990 | // counters to detect called callbacks |
---|
2991 | int e1_changed = 0; |
---|
2992 | int e2_changed = 0; |
---|
2993 | int c_changed = 0; |
---|
2994 | int c_son_created = 0; |
---|
2995 | |
---|
2996 | int e1_deleted = 0; |
---|
2997 | int e2_deleted = 0; |
---|
2998 | int e3_deleted = 0; |
---|
2999 | int c_deleted = 0; |
---|
3000 | |
---|
3001 | #define CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc) \ |
---|
3002 | that(e1_changed).is_equal_to(e1c), \ |
---|
3003 | that(e2_changed).is_equal_to(e2c), \ |
---|
3004 | that(c_changed).is_equal_to(cc), \ |
---|
3005 | that(c_son_created).is_equal_to(csc) |
---|
3006 | |
---|
3007 | #define DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd) \ |
---|
3008 | that(e1_deleted).is_equal_to(e1d), \ |
---|
3009 | that(e2_deleted).is_equal_to(e2d), \ |
---|
3010 | that(e3_deleted).is_equal_to(e3d), \ |
---|
3011 | that(c_deleted).is_equal_to(cd) |
---|
3012 | |
---|
3013 | #define TEST_EXPECT_CHCB_COUNTERS(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
---|
3014 | #define TEST_EXPECT_CHCB___WANTED(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
---|
3015 | |
---|
3016 | #define TEST_EXPECT_DLCB_COUNTERS(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
---|
3017 | #define TEST_EXPECT_DLCB___WANTED(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
---|
3018 | |
---|
3019 | #define TEST_EXPECT_COUNTER(tam,cnt,expected) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL(cnt, expected); }while(0) |
---|
3020 | #define TEST_EXPECT_COUNTER__BROKEN(tam,cnt,expected,got) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL__BROKEN(cnt, expected, got); }while(0) |
---|
3021 | |
---|
3022 | #define RESET_CHCB_COUNTERS() do{ e1_changed = e2_changed = c_changed = c_son_created = 0; }while(0) |
---|
3023 | #define RESET_DLCB_COUNTERS() do{ e1_deleted = e2_deleted = e3_deleted = c_deleted = 0; }while(0) |
---|
3024 | #define RESET_ALL_CB_COUNTERS() do{ RESET_CHCB_COUNTERS(); RESET_DLCB_COUNTERS(); }while(0) |
---|
3025 | |
---|
3026 | // install some DB callbacks |
---|
3027 | { |
---|
3028 | GB_transaction ta(gb_main); |
---|
3029 | GB_add_callback(gbe1, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e1_changed)); |
---|
3030 | GB_add_callback(gbe2, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e2_changed)); |
---|
3031 | GB_add_callback(gbc, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &c_changed)); |
---|
3032 | GB_add_callback(gbc, GB_CB_SON_CREATED, makeDatabaseCallback(test_count_cb, &c_son_created)); |
---|
3033 | } |
---|
3034 | |
---|
3035 | // check callbacks were not called yet |
---|
3036 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); |
---|
3037 | |
---|
3038 | // trigger callbacks |
---|
3039 | { |
---|
3040 | GB_transaction ta(gb_main); |
---|
3041 | |
---|
3042 | error = GB_write_string(gbe1, "hi"); TEST_EXPECT_NO_ERROR(error); |
---|
3043 | error = GB_write_int(gbe2, 666); TEST_EXPECT_NO_ERROR(error); |
---|
3044 | |
---|
3045 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); // callbacks triggered instantly in NO_TA mode |
---|
3046 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, WITH_TA); // callbacks delayed until transaction is committed |
---|
3047 | |
---|
3048 | } // [Note: callbacks happen here in ta_mode] |
---|
3049 | |
---|
3050 | // test that GB_CB_SON_CREATED is not triggered here: |
---|
3051 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); |
---|
3052 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, WITH_TA); |
---|
3053 | |
---|
3054 | // really create a son |
---|
3055 | RESET_CHCB_COUNTERS(); |
---|
3056 | { |
---|
3057 | GB_transaction ta(gb_main); |
---|
3058 | gbe3 = GB_create(gbc, "e3", GB_STRING); |
---|
3059 | } |
---|
3060 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // broken |
---|
3061 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
---|
3062 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 1, WITH_TA); |
---|
3063 | |
---|
3064 | // change that son |
---|
3065 | RESET_CHCB_COUNTERS(); |
---|
3066 | { |
---|
3067 | GB_transaction ta(gb_main); |
---|
3068 | error = GB_write_string(gbe3, "bla"); TEST_EXPECT_NO_ERROR(error); |
---|
3069 | } |
---|
3070 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, BOTH_TA_MODES); |
---|
3071 | |
---|
3072 | |
---|
3073 | // test delete callbacks |
---|
3074 | RESET_CHCB_COUNTERS(); |
---|
3075 | { |
---|
3076 | GB_transaction ta(gb_main); |
---|
3077 | |
---|
3078 | GB_add_callback(gbe1, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e1_deleted)); |
---|
3079 | GB_add_callback(gbe2, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e2_deleted)); |
---|
3080 | GB_add_callback(gbe3, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e3_deleted)); |
---|
3081 | GB_add_callback(gbc, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &c_deleted)); |
---|
3082 | } |
---|
3083 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // adding callbacks does not trigger existing change-callbacks |
---|
3084 | { |
---|
3085 | GB_transaction ta(gb_main); |
---|
3086 | |
---|
3087 | error = GB_delete(gbe3); TEST_EXPECT_NO_ERROR(error); |
---|
3088 | error = GB_delete(gbe2); TEST_EXPECT_NO_ERROR(error); |
---|
3089 | |
---|
3090 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, NO_TA); |
---|
3091 | TEST_EXPECT_DLCB_COUNTERS(0, 0, 0, 0, WITH_TA); |
---|
3092 | } |
---|
3093 | |
---|
3094 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, WITH_TA); // container changed by deleting a son (gbe3); no longer triggers via GB_SON_CHANGED |
---|
3095 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // change is not triggered in NO_TA mode (error?) |
---|
3096 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
---|
3097 | |
---|
3098 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, BOTH_TA_MODES); |
---|
3099 | |
---|
3100 | RESET_ALL_CB_COUNTERS(); |
---|
3101 | { |
---|
3102 | GB_transaction ta(gb_main); |
---|
3103 | error = GB_delete(gbc); TEST_EXPECT_NO_ERROR(error); // delete the container containing gbe1 and gbe3 (gbe3 alreay deleted) |
---|
3104 | } |
---|
3105 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // deleting the container does not trigger any change callbacks |
---|
3106 | TEST_EXPECT_DLCB_COUNTERS(1, 0, 0, 1, BOTH_TA_MODES); // deleting the container does also trigger the delete callback for gbe1 |
---|
3107 | |
---|
3108 | // -------------------------------------------------------------------------------- |
---|
3109 | // document that a callback now can be removed while it is running |
---|
3110 | // (in NO_TA mode; always worked in WITH_TA mode) |
---|
3111 | { |
---|
3112 | GBDATA *gbe; |
---|
3113 | { |
---|
3114 | GB_transaction ta(gb_main); |
---|
3115 | gbe = GB_create(gb_main, "new_e1", GB_INT); // recreate |
---|
3116 | GB_add_callback(gbe, GB_CB_CHANGED, makeDatabaseCallback(remove_self_cb)); |
---|
3117 | } |
---|
3118 | { GB_transaction ta(gb_main); GB_touch(gbe); } |
---|
3119 | } |
---|
3120 | |
---|
3121 | // test that a callback may remove and re-add itself |
---|
3122 | { |
---|
3123 | GBDATA *gbn1; |
---|
3124 | GBDATA *gbn2; |
---|
3125 | |
---|
3126 | int counter1 = 0; |
---|
3127 | int counter2 = 0; |
---|
3128 | |
---|
3129 | { |
---|
3130 | GB_transaction ta(gb_main); |
---|
3131 | gbn1 = GB_create(gb_main, "new_e1", GB_INT); |
---|
3132 | gbn2 = GB_create(gb_main, "new_e2", GB_INT); |
---|
3133 | GB_add_callback(gbn1, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter1)); |
---|
3134 | } |
---|
3135 | |
---|
3136 | TEST_EXPECT_COUNTER(NO_TA, counter1, 0); // no callback triggered (trigger happens BEFORE call to GB_add_callback in NO_TA mode!) |
---|
3137 | TEST_EXPECT_COUNTER(WITH_TA, counter1, 1); // callback gets triggered by GB_create |
---|
3138 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
3139 | |
---|
3140 | counter1 = 0; counter2 = 0; |
---|
3141 | |
---|
3142 | // test no callback is triggered by just adding a callback |
---|
3143 | { |
---|
3144 | GB_transaction ta(gb_main); |
---|
3145 | GB_add_callback(gbn2, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter2)); |
---|
3146 | } |
---|
3147 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 0); |
---|
3148 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
3149 | |
---|
3150 | { GB_transaction ta(gb_main); GB_touch(gbn1); } |
---|
3151 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
3152 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
3153 | |
---|
3154 | { GB_transaction ta(gb_main); GB_touch(gbn2); } |
---|
3155 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
3156 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
---|
3157 | |
---|
3158 | { GB_transaction ta(gb_main); } |
---|
3159 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
3160 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
---|
3161 | } |
---|
3162 | |
---|
3163 | GB_close(gb_main); |
---|
3164 | } |
---|
3165 | } |
---|
3166 | |
---|
3167 | // ----------------------- |
---|
3168 | // test callbacks |
---|
3169 | |
---|
3170 | struct calledWith { |
---|
3171 | GBDATA *gbd; |
---|
3172 | GB_CB_TYPE type; |
---|
3173 | int time_called; |
---|
3174 | |
---|
3175 | static int timer; |
---|
3176 | |
---|
3177 | calledWith(GBDATA *gbd_, GB_CB_TYPE type_) : gbd(gbd_), type(type_), time_called(++timer) {} |
---|
3178 | calledWith(const calledWith& other) : gbd(other.gbd), type(other.type), time_called(other.time_called) {} |
---|
3179 | DECLARE_ASSIGNMENT_OPERATOR(calledWith); |
---|
3180 | }; |
---|
3181 | |
---|
3182 | int calledWith::timer = 0; |
---|
3183 | |
---|
3184 | class callback_trace { |
---|
3185 | typedef std::list<calledWith> calledList; |
---|
3186 | typedef calledList::iterator calledIter; |
---|
3187 | |
---|
3188 | calledList called; |
---|
3189 | |
---|
3190 | calledIter find(GBDATA *gbd) { |
---|
3191 | calledIter c = called.begin(); |
---|
3192 | while (c != called.end()) { |
---|
3193 | if (c->gbd == gbd) break; |
---|
3194 | ++c; |
---|
3195 | } |
---|
3196 | return c; |
---|
3197 | } |
---|
3198 | calledIter find(GB_CB_TYPE exp_type) { |
---|
3199 | calledIter c = called.begin(); |
---|
3200 | while (c != called.end()) { |
---|
3201 | if (c->type&exp_type) break; |
---|
3202 | ++c; |
---|
3203 | } |
---|
3204 | return c; |
---|
3205 | } |
---|
3206 | calledIter find(GBDATA *gbd, GB_CB_TYPE exp_type) { |
---|
3207 | calledIter c = called.begin(); |
---|
3208 | while (c != called.end()) { |
---|
3209 | if (c->gbd == gbd && (c->type&exp_type)) break; |
---|
3210 | ++c; |
---|
3211 | } |
---|
3212 | return c; |
---|
3213 | } |
---|
3214 | |
---|
3215 | bool removed(calledIter c) { |
---|
3216 | if (c == called.end()) return false; |
---|
3217 | called.erase(c); |
---|
3218 | return true; |
---|
3219 | } |
---|
3220 | |
---|
3221 | public: |
---|
3222 | callback_trace() { called.clear(); } |
---|
3223 | |
---|
3224 | void set_called_by(GBDATA *gbd, GB_CB_TYPE type) { called.push_back(calledWith(gbd, type)); } |
---|
3225 | |
---|
3226 | bool was_called_by(GBDATA *gbd) { return removed(find(gbd)); } |
---|
3227 | bool was_called_by(GB_CB_TYPE exp_type) { return removed(find(exp_type)); } |
---|
3228 | bool was_called_by(GBDATA *gbd, GB_CB_TYPE exp_type) { return removed(find(gbd, exp_type)); } |
---|
3229 | |
---|
3230 | int call_time(GBDATA *gbd, GB_CB_TYPE exp_type) { |
---|
3231 | calledIter found = find(gbd, exp_type); |
---|
3232 | if (found == called.end()) return -1; |
---|
3233 | |
---|
3234 | int t = found->time_called; |
---|
3235 | removed(found); |
---|
3236 | return t; |
---|
3237 | } |
---|
3238 | |
---|
3239 | bool was_not_called() const { return called.empty(); } |
---|
3240 | bool was_called() const { return !was_not_called(); } |
---|
3241 | }; |
---|
3242 | |
---|
3243 | static void some_cb(GBDATA *gbd, callback_trace *trace, GB_CB_TYPE cbtype) { |
---|
3244 | trace->set_called_by(gbd, cbtype); |
---|
3245 | } |
---|
3246 | |
---|
3247 | #define TRACESTRUCT(ELEM,FLAVOR) trace_##ELEM##_##FLAVOR |
---|
3248 | #define HIERARCHY_TRACESTRUCT(ELEM,FLAVOR) traceHier_##ELEM##_##FLAVOR |
---|
3249 | |
---|
3250 | #define ADD_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
---|
3251 | #define ADD_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
---|
3252 | #define ADD_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
---|
3253 | |
---|
3254 | #define ADD_CHANGED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,changed)))) |
---|
3255 | #define ADD_DELETED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,deleted)))) |
---|
3256 | #define ADD_NWCHILD_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,newchild)))) |
---|
3257 | |
---|
3258 | #define ENSURE_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
---|
3259 | #define ENSURE_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
---|
3260 | #define ENSURE_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
---|
3261 | |
---|
3262 | #define REMOVE_CHANGED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed))) |
---|
3263 | #define REMOVE_DELETED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted))) |
---|
3264 | #define REMOVE_NWCHILD_CALLBACK(elem) GB_remove_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild))) |
---|
3265 | |
---|
3266 | #define INIT_CHANGED_CALLBACK(elem) callback_trace TRACESTRUCT(elem,changed); ADD_CHANGED_CALLBACK(elem) |
---|
3267 | #define INIT_DELETED_CALLBACK(elem) callback_trace TRACESTRUCT(elem,deleted); ADD_DELETED_CALLBACK(elem) |
---|
3268 | #define INIT_NWCHILD_CALLBACK(elem) callback_trace TRACESTRUCT(elem,newchild); ADD_NWCHILD_CALLBACK(elem) |
---|
3269 | |
---|
3270 | #define INIT_CHANGED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,changed); ADD_CHANGED_HIERARCHY_CALLBACK(elem) |
---|
3271 | #define INIT_DELETED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,deleted); ADD_DELETED_HIERARCHY_CALLBACK(elem) |
---|
3272 | #define INIT_NWCHILD_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,newchild); ADD_NWCHILD_HIERARCHY_CALLBACK(elem) |
---|
3273 | |
---|
3274 | #define ENSURE_ENTRY_CALLBACKS(entry) ENSURE_CHANGED_CALLBACK(entry); ENSURE_DELETED_CALLBACK(entry) |
---|
3275 | #define ENSURE_CONTAINER_CALLBACKS(cont) ENSURE_CHANGED_CALLBACK(cont); ENSURE_NWCHILD_CALLBACK(cont); ENSURE_DELETED_CALLBACK(cont) |
---|
3276 | |
---|
3277 | #define REMOVE_ENTRY_CALLBACKS(entry) REMOVE_CHANGED_CALLBACK(entry); REMOVE_DELETED_CALLBACK(entry) |
---|
3278 | #define REMOVE_CONTAINER_CALLBACKS(cont) REMOVE_CHANGED_CALLBACK(cont); REMOVE_NWCHILD_CALLBACK(cont); REMOVE_DELETED_CALLBACK(cont) |
---|
3279 | |
---|
3280 | #define INIT_ENTRY_CALLBACKS(entry) INIT_CHANGED_CALLBACK(entry); INIT_DELETED_CALLBACK(entry) |
---|
3281 | #define INIT_CONTAINER_CALLBACKS(cont) INIT_CHANGED_CALLBACK(cont); INIT_NWCHILD_CALLBACK(cont); INIT_DELETED_CALLBACK(cont) |
---|
3282 | |
---|
3283 | #define TRIGGER_CHANGE(gbd) do { \ |
---|
3284 | GB_initial_transaction ta(gb_main); \ |
---|
3285 | if (ta.ok()) GB_touch(gbd); \ |
---|
3286 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); \ |
---|
3287 | } while(0) |
---|
3288 | |
---|
3289 | #define TRIGGER_2_CHANGES(gbd1, gbd2) do { \ |
---|
3290 | GB_initial_transaction ta(gb_main); \ |
---|
3291 | if (ta.ok()) { \ |
---|
3292 | GB_touch(gbd1); \ |
---|
3293 | GB_touch(gbd2); \ |
---|
3294 | } \ |
---|
3295 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); \ |
---|
3296 | } while(0) |
---|
3297 | |
---|
3298 | #define TRIGGER_DELETE(gbd) do { \ |
---|
3299 | GB_initial_transaction ta(gb_main); \ |
---|
3300 | GB_ERROR error = NULL; \ |
---|
3301 | if (ta.ok()) error = GB_delete(gbd); \ |
---|
3302 | TEST_EXPECT_NO_ERROR(ta.close(error)); \ |
---|
3303 | } while(0) |
---|
3304 | |
---|
3305 | #define TEST_EXPECT_NO_CALLBACK_TRIGGERED() \ |
---|
3306 | TEST_EXPECT(trace_top_deleted.was_not_called()); \ |
---|
3307 | TEST_EXPECT(trace_son_deleted.was_not_called()); \ |
---|
3308 | TEST_EXPECT(trace_grandson_deleted.was_not_called()); \ |
---|
3309 | TEST_EXPECT(trace_ograndson_deleted.was_not_called()); \ |
---|
3310 | TEST_EXPECT(trace_cont_top_deleted.was_not_called()); \ |
---|
3311 | TEST_EXPECT(trace_cont_son_deleted.was_not_called()); \ |
---|
3312 | TEST_EXPECT(trace_top_changed.was_not_called()); \ |
---|
3313 | TEST_EXPECT(trace_son_changed.was_not_called()); \ |
---|
3314 | TEST_EXPECT(trace_grandson_changed.was_not_called()); \ |
---|
3315 | TEST_EXPECT(trace_ograndson_changed.was_not_called()); \ |
---|
3316 | TEST_EXPECT(trace_cont_top_changed.was_not_called()); \ |
---|
3317 | TEST_EXPECT(trace_cont_son_changed.was_not_called()); \ |
---|
3318 | TEST_EXPECT(trace_cont_top_newchild.was_not_called()); \ |
---|
3319 | TEST_EXPECT(trace_cont_son_newchild.was_not_called()); \ |
---|
3320 | |
---|
3321 | |
---|
3322 | #define TEST_EXPECT_CB_TRIGGERED(TRACE,GBD,TYPE) TEST_EXPECT(TRACE.was_called_by(GBD, TYPE)) |
---|
3323 | #define TEST_EXPECT_CB_TRIGGERED_AT(TRACE,GBD,TYPE,TIME) TEST_EXPECT_EQUAL(TRACE.call_time(GBD, TYPE), TIME) |
---|
3324 | |
---|
3325 | #define TEST_EXPECT_CHANGE_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_CHANGED) |
---|
3326 | #define TEST_EXPECT_DELETE_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_DELETE) |
---|
3327 | #define TEST_EXPECT_NCHILD_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_SON_CREATED) |
---|
3328 | |
---|
3329 | #define TEST_EXPECT_CHANGE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_CHANGED, TIME) |
---|
3330 | #define TEST_EXPECT_DELETE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_DELETE, TIME) |
---|
3331 | |
---|
3332 | void TEST_db_callbacks() { |
---|
3333 | GB_shell shell; |
---|
3334 | GBDATA *gb_main = GB_open("new.arb", "c"); |
---|
3335 | |
---|
3336 | // create some data |
---|
3337 | GB_begin_transaction(gb_main); |
---|
3338 | |
---|
3339 | GBDATA *cont_top = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top); |
---|
3340 | GBDATA *cont_son = GB_create_container(cont_top, "cont_son"); TEST_REJECT_NULL(cont_son); |
---|
3341 | |
---|
3342 | GBDATA *top = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top); |
---|
3343 | GBDATA *son = GB_create(cont_top, "son", GB_INT); TEST_REJECT_NULL(son); |
---|
3344 | GBDATA *grandson = GB_create(cont_son, "grandson", GB_STRING); TEST_REJECT_NULL(grandson); |
---|
3345 | GBDATA *ograndson = GB_create(cont_son, "ograndson", GB_STRING); TEST_REJECT_NULL(ograndson); |
---|
3346 | |
---|
3347 | GB_commit_transaction(gb_main); |
---|
3348 | |
---|
3349 | // install callbacks |
---|
3350 | GB_begin_transaction(gb_main); |
---|
3351 | INIT_CONTAINER_CALLBACKS(cont_top); |
---|
3352 | INIT_CONTAINER_CALLBACKS(cont_son); |
---|
3353 | INIT_ENTRY_CALLBACKS(top); |
---|
3354 | INIT_ENTRY_CALLBACKS(son); |
---|
3355 | INIT_ENTRY_CALLBACKS(grandson); |
---|
3356 | INIT_ENTRY_CALLBACKS(ograndson); |
---|
3357 | GB_commit_transaction(gb_main); |
---|
3358 | |
---|
3359 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3360 | |
---|
3361 | // trigger change callbacks via change |
---|
3362 | GB_begin_transaction(gb_main); |
---|
3363 | GB_write_string(top, "hello world"); |
---|
3364 | GB_commit_transaction(gb_main); |
---|
3365 | TEST_EXPECT_CHANGE_TRIGGERED(trace_top_changed, top); |
---|
3366 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3367 | |
---|
3368 | GB_begin_transaction(gb_main); |
---|
3369 | GB_write_string(top, "hello world"); // no change |
---|
3370 | GB_commit_transaction(gb_main); |
---|
3371 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3372 | |
---|
3373 | #if 0 |
---|
3374 | // code is wrong (cannot set terminal entry to "marked") |
---|
3375 | GB_begin_transaction(gb_main); |
---|
3376 | GB_write_flag(son, 1); // only change "mark" |
---|
3377 | GB_commit_transaction(gb_main); |
---|
3378 | TEST_EXPECT_CHANGE_TRIGGERED(trace_son_changed, son); |
---|
3379 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3380 | TEST_EXPECT_TRIGGER__UNWANTED(trace_cont_top_newchild); // @@@ modifying son should not trigger newchild callback |
---|
3381 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3382 | #else |
---|
3383 | // @@@ add test code similar to wrong section above |
---|
3384 | #endif |
---|
3385 | |
---|
3386 | GB_begin_transaction(gb_main); |
---|
3387 | GB_touch(grandson); |
---|
3388 | GB_commit_transaction(gb_main); |
---|
3389 | TEST_EXPECT_CHANGE_TRIGGERED(trace_grandson_changed, grandson); |
---|
3390 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3391 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3392 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3393 | |
---|
3394 | // trigger change- and soncreate-callbacks via create |
---|
3395 | |
---|
3396 | GB_begin_transaction(gb_main); |
---|
3397 | GBDATA *son2 = GB_create(cont_top, "son2", GB_INT); TEST_REJECT_NULL(son2); |
---|
3398 | GB_commit_transaction(gb_main); |
---|
3399 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3400 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
---|
3401 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3402 | |
---|
3403 | GB_begin_transaction(gb_main); |
---|
3404 | GBDATA *grandson2 = GB_create(cont_son, "grandson2", GB_STRING); TEST_REJECT_NULL(grandson2); |
---|
3405 | GB_commit_transaction(gb_main); |
---|
3406 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3407 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_son_newchild, cont_son); |
---|
3408 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3409 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3410 | |
---|
3411 | // trigger callbacks via delete |
---|
3412 | |
---|
3413 | TRIGGER_DELETE(son2); |
---|
3414 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3415 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3416 | |
---|
3417 | TRIGGER_DELETE(grandson2); |
---|
3418 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3419 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3420 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3421 | |
---|
3422 | TEST_EXPECT_NO_ERROR(GB_request_undo_type(gb_main, GB_UNDO_UNDO)); |
---|
3423 | |
---|
3424 | TRIGGER_DELETE(top); |
---|
3425 | TEST_EXPECT_DELETE_TRIGGERED(trace_top_deleted, top); |
---|
3426 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3427 | |
---|
3428 | TRIGGER_DELETE(grandson); |
---|
3429 | TEST_EXPECT_DELETE_TRIGGERED(trace_grandson_deleted, grandson); |
---|
3430 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3431 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3432 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3433 | |
---|
3434 | TRIGGER_DELETE(cont_son); |
---|
3435 | TEST_EXPECT_DELETE_TRIGGERED(trace_ograndson_deleted, ograndson); |
---|
3436 | TEST_EXPECT_DELETE_TRIGGERED(trace_cont_son_deleted, cont_son); |
---|
3437 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3438 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3439 | |
---|
3440 | // trigger callbacks by undoing last 3 delete transactions |
---|
3441 | |
---|
3442 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of cont_son |
---|
3443 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3444 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
---|
3445 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3446 | |
---|
3447 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of grandson |
---|
3448 | // cont_son callbacks are not triggered (they are not restored by undo) |
---|
3449 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3450 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3451 | |
---|
3452 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of top |
---|
3453 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3454 | |
---|
3455 | // reinstall callbacks that were removed by deletes |
---|
3456 | |
---|
3457 | GB_begin_transaction(gb_main); |
---|
3458 | ENSURE_CONTAINER_CALLBACKS(cont_top); |
---|
3459 | ENSURE_CONTAINER_CALLBACKS(cont_son); |
---|
3460 | ENSURE_ENTRY_CALLBACKS(top); |
---|
3461 | ENSURE_ENTRY_CALLBACKS(son); |
---|
3462 | ENSURE_ENTRY_CALLBACKS(grandson); |
---|
3463 | GB_commit_transaction(gb_main); |
---|
3464 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3465 | |
---|
3466 | // trigger callbacks which will be removed |
---|
3467 | |
---|
3468 | TRIGGER_CHANGE(son); |
---|
3469 | TEST_EXPECT_CHANGE_TRIGGERED(trace_son_changed, son); |
---|
3470 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3471 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3472 | |
---|
3473 | GB_begin_transaction(gb_main); |
---|
3474 | GBDATA *son3 = GB_create(cont_top, "son3", GB_INT); TEST_REJECT_NULL(son3); |
---|
3475 | GB_commit_transaction(gb_main); |
---|
3476 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3477 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
---|
3478 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3479 | |
---|
3480 | // test remove callback |
---|
3481 | |
---|
3482 | GB_begin_transaction(gb_main); |
---|
3483 | REMOVE_ENTRY_CALLBACKS(son); |
---|
3484 | REMOVE_CONTAINER_CALLBACKS(cont_top); |
---|
3485 | GB_commit_transaction(gb_main); |
---|
3486 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3487 | |
---|
3488 | // "trigger" removed callbacks |
---|
3489 | |
---|
3490 | TRIGGER_CHANGE(son); |
---|
3491 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3492 | |
---|
3493 | GB_begin_transaction(gb_main); |
---|
3494 | GBDATA *son4 = GB_create(cont_top, "son4", GB_INT); TEST_REJECT_NULL(son4); |
---|
3495 | GB_commit_transaction(gb_main); |
---|
3496 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3497 | |
---|
3498 | GB_close(gb_main); |
---|
3499 | } |
---|
3500 | |
---|
3501 | void TEST_POSTCOND_arbdb() { |
---|
3502 | GB_ERROR error = NULL; |
---|
3503 | if (GB_have_error()) { |
---|
3504 | error = GB_await_error(); // clears the error (to make further tests succeed) |
---|
3505 | } |
---|
3506 | |
---|
3507 | bool unclosed_GB_shell = closed_open_shell_for_unit_tests(); |
---|
3508 | |
---|
3509 | TEST_REJECT(error); // your test finished with an exported error |
---|
3510 | TEST_REJECT(unclosed_GB_shell); // your test finished w/o destroying GB_shell |
---|
3511 | } |
---|
3512 | |
---|
3513 | #define TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED() \ |
---|
3514 | TEST_EXPECT(traceHier_anyGrandson_changed.was_not_called()); \ |
---|
3515 | TEST_EXPECT(traceHier_anyGrandson_deleted.was_not_called()); \ |
---|
3516 | TEST_EXPECT(traceHier_anySonContainer_newchild.was_not_called()) |
---|
3517 | |
---|
3518 | #undef TEST_EXPECT_NO_CALLBACK_TRIGGERED |
---|
3519 | |
---|
3520 | #define TEST_EXPECT_NO_CALLBACK_TRIGGERED() \ |
---|
3521 | TEST_EXPECT(trace_anotherGrandson_changed.was_not_called()); \ |
---|
3522 | TEST_EXPECT(trace_elimGrandson2_deleted.was_not_called()) |
---|
3523 | |
---|
3524 | void TEST_hierarchy_callbacks() { |
---|
3525 | GB_shell shell; |
---|
3526 | GBDATA *gb_main = GB_open("new.arb", "c"); |
---|
3527 | |
---|
3528 | // create some data |
---|
3529 | GB_begin_transaction(gb_main); |
---|
3530 | |
---|
3531 | GBDATA *cont_top1 = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top1); |
---|
3532 | GBDATA *cont_top2 = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top2); |
---|
3533 | |
---|
3534 | GBDATA *cont_son11 = GB_create_container(cont_top1, "cont_son"); TEST_REJECT_NULL(cont_son11); |
---|
3535 | GBDATA *cont_son21 = GB_create_container(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son21); |
---|
3536 | GBDATA *cont_son22 = GB_create_container(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son22); |
---|
3537 | |
---|
3538 | GBDATA *top1 = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top1); |
---|
3539 | GBDATA *top2 = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top2); |
---|
3540 | |
---|
3541 | GBDATA *son11 = GB_create(cont_top1, "son", GB_INT); TEST_REJECT_NULL(son11); |
---|
3542 | GBDATA *son12 = GB_create(cont_top1, "son", GB_INT); TEST_REJECT_NULL(son12); |
---|
3543 | GBDATA *son21 = GB_create(cont_top2, "son", GB_INT); TEST_REJECT_NULL(son21); |
---|
3544 | |
---|
3545 | GBDATA *grandson111 = GB_create(cont_son11, "grandson", GB_STRING); TEST_REJECT_NULL(grandson111); |
---|
3546 | GBDATA *grandson112 = GB_create(cont_son11, "grandson", GB_STRING); TEST_REJECT_NULL(grandson112); |
---|
3547 | GBDATA *grandson211 = GB_create(cont_son21, "grandson", GB_STRING); TEST_REJECT_NULL(grandson211); |
---|
3548 | GBDATA *grandson221 = GB_create(cont_son22, "grandson", GB_STRING); TEST_REJECT_NULL(grandson221); |
---|
3549 | GBDATA *grandson222 = GB_create(cont_son22, "grandson", GB_STRING); TEST_REJECT_NULL(grandson222); |
---|
3550 | |
---|
3551 | // create some entries at uncommon locations (compared to entries created above) |
---|
3552 | GBDATA *ctop_top = GB_create (cont_top2, "top", GB_STRING); TEST_REJECT_NULL(ctop_top); |
---|
3553 | GBDATA *top_son = GB_create (gb_main, "son", GB_INT); TEST_REJECT_NULL(top_son); |
---|
3554 | GBDATA *cson = GB_create_container(gb_main, "cont_son"); TEST_REJECT_NULL(cson); |
---|
3555 | GBDATA *cson_gs = GB_create (cson, "grandson", GB_STRING); TEST_REJECT_NULL(cson_gs); |
---|
3556 | |
---|
3557 | GB_commit_transaction(gb_main); |
---|
3558 | |
---|
3559 | // test gb_hierarchy_location |
---|
3560 | { |
---|
3561 | gb_hierarchy_location loc_top(top1); |
---|
3562 | gb_hierarchy_location loc_son(son11); |
---|
3563 | gb_hierarchy_location loc_grandson(grandson222); |
---|
3564 | |
---|
3565 | TEST_EXPECT(loc_top.matches(top1)); |
---|
3566 | TEST_EXPECT(loc_top.matches(top2)); |
---|
3567 | TEST_EXPECT(!loc_top.matches(cont_top1)); |
---|
3568 | TEST_EXPECT(!loc_top.matches(son12)); |
---|
3569 | TEST_EXPECT(!loc_top.matches(cont_son22)); |
---|
3570 | TEST_EXPECT(!loc_top.matches(ctop_top)); |
---|
3571 | |
---|
3572 | TEST_EXPECT(loc_son.matches(son11)); |
---|
3573 | TEST_EXPECT(loc_son.matches(son21)); |
---|
3574 | TEST_EXPECT(!loc_son.matches(top1)); |
---|
3575 | TEST_EXPECT(!loc_son.matches(grandson111)); |
---|
3576 | TEST_EXPECT(!loc_son.matches(cont_son22)); |
---|
3577 | TEST_EXPECT(!loc_son.matches(top_son)); |
---|
3578 | |
---|
3579 | TEST_EXPECT(loc_grandson.matches(grandson222)); |
---|
3580 | TEST_EXPECT(loc_grandson.matches(grandson111)); |
---|
3581 | TEST_EXPECT(!loc_grandson.matches(son11)); |
---|
3582 | TEST_EXPECT(!loc_grandson.matches(top1)); |
---|
3583 | TEST_EXPECT(!loc_grandson.matches(cont_son22)); |
---|
3584 | TEST_EXPECT(!loc_grandson.matches(cson_gs)); |
---|
3585 | |
---|
3586 | gb_hierarchy_location loc_ctop_top(ctop_top); |
---|
3587 | TEST_EXPECT(loc_ctop_top.matches(ctop_top)); |
---|
3588 | TEST_EXPECT(!loc_ctop_top.matches(top1)); |
---|
3589 | |
---|
3590 | gb_hierarchy_location loc_top_son(top_son); |
---|
3591 | TEST_EXPECT(loc_top_son.matches(top_son)); |
---|
3592 | TEST_EXPECT(!loc_top_son.matches(son11)); |
---|
3593 | |
---|
3594 | gb_hierarchy_location loc_gs(cson_gs); |
---|
3595 | TEST_EXPECT(loc_gs.matches(cson_gs)); |
---|
3596 | TEST_EXPECT(!loc_gs.matches(grandson211)); |
---|
3597 | |
---|
3598 | gb_hierarchy_location loc_root(gb_main); |
---|
3599 | TEST_EXPECT(loc_root.matches(gb_main)); |
---|
3600 | TEST_EXPECT(!loc_root.matches(cont_top1)); |
---|
3601 | TEST_EXPECT(!loc_root.matches(cont_son11)); |
---|
3602 | TEST_EXPECT(!loc_root.matches(top1)); |
---|
3603 | TEST_EXPECT(!loc_root.matches(son11)); |
---|
3604 | TEST_EXPECT(!loc_root.matches(grandson211)); |
---|
3605 | } |
---|
3606 | |
---|
3607 | |
---|
3608 | // instanciate callback_trace data and install hierarchy callbacks |
---|
3609 | GBDATA *anySon = son11; |
---|
3610 | |
---|
3611 | GBDATA *anySonContainer = cont_son11; |
---|
3612 | GBDATA *anotherSonContainer = cont_son22; |
---|
3613 | |
---|
3614 | GBDATA *anyGrandson = grandson221; |
---|
3615 | GBDATA *anotherGrandson = grandson112; |
---|
3616 | GBDATA *elimGrandson = grandson222; |
---|
3617 | GBDATA *elimGrandson2 = grandson111; |
---|
3618 | GBDATA *newGrandson = NULL; |
---|
3619 | |
---|
3620 | INIT_CHANGED_HIERARCHY_CALLBACK(anyGrandson); |
---|
3621 | INIT_DELETED_HIERARCHY_CALLBACK(anyGrandson); |
---|
3622 | INIT_NWCHILD_HIERARCHY_CALLBACK(anySonContainer); |
---|
3623 | |
---|
3624 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3625 | |
---|
3626 | // trigger change-callback using same DB entry |
---|
3627 | TRIGGER_CHANGE(anyGrandson); |
---|
3628 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
---|
3629 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3630 | |
---|
3631 | // trigger change-callback using another DB entry (same hierarchy) |
---|
3632 | TRIGGER_CHANGE(anotherGrandson); |
---|
3633 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
---|
3634 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3635 | |
---|
3636 | // check nothing is triggered by an element at different hierarchy |
---|
3637 | TRIGGER_CHANGE(anySon); |
---|
3638 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3639 | |
---|
3640 | // trigger change-callback using both DB entries (in two TAs) |
---|
3641 | TRIGGER_CHANGE(anyGrandson); |
---|
3642 | TRIGGER_CHANGE(anotherGrandson); |
---|
3643 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
---|
3644 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
---|
3645 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3646 | |
---|
3647 | // trigger change-callback using both DB entries (in one TA) |
---|
3648 | TRIGGER_2_CHANGES(anyGrandson, anotherGrandson); |
---|
3649 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
---|
3650 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
---|
3651 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3652 | |
---|
3653 | // trigger son-created-callback |
---|
3654 | { |
---|
3655 | GB_initial_transaction ta(gb_main); |
---|
3656 | if (ta.ok()) { |
---|
3657 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
---|
3658 | } |
---|
3659 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
---|
3660 | } |
---|
3661 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anySonContainer); |
---|
3662 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3663 | |
---|
3664 | // trigger 2 son-created-callbacks (for 2 containers) and one change-callback (for a newly created son) |
---|
3665 | { |
---|
3666 | GB_initial_transaction ta(gb_main); |
---|
3667 | if (ta.ok()) { |
---|
3668 | newGrandson = GB_create(anotherSonContainer, "grandson", GB_STRING); TEST_REJECT_NULL(newGrandson); |
---|
3669 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
---|
3670 | } |
---|
3671 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
---|
3672 | } |
---|
3673 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, newGrandson); |
---|
3674 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anotherSonContainer); |
---|
3675 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anySonContainer); |
---|
3676 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3677 | |
---|
3678 | // trigger delete-callback |
---|
3679 | { |
---|
3680 | GB_initial_transaction ta(gb_main); |
---|
3681 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson)); |
---|
3682 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
---|
3683 | } |
---|
3684 | TEST_EXPECT_DELETE_TRIGGERED(traceHier_anyGrandson_deleted, elimGrandson); |
---|
3685 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3686 | |
---|
3687 | // bind normal (non-hierarchical) callbacks to entries which trigger hierarchical callbacks and .. |
---|
3688 | calledWith::timer = 0; |
---|
3689 | GB_begin_transaction(gb_main); |
---|
3690 | INIT_CHANGED_CALLBACK(anotherGrandson); |
---|
3691 | INIT_DELETED_CALLBACK(elimGrandson2); |
---|
3692 | GB_commit_transaction(gb_main); |
---|
3693 | |
---|
3694 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3695 | |
---|
3696 | { |
---|
3697 | GB_initial_transaction ta(gb_main); |
---|
3698 | if (ta.ok()) { |
---|
3699 | GB_touch(anotherGrandson); |
---|
3700 | GB_touch(elimGrandson2); |
---|
3701 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson2)); |
---|
3702 | } |
---|
3703 | } |
---|
3704 | |
---|
3705 | // .. test call-order (delete before change, hierarchical before normal): |
---|
3706 | TEST_EXPECT_DELETE_TRIGGERED_AT(traceHier_anyGrandson_deleted, elimGrandson2, 1); |
---|
3707 | TEST_EXPECT_DELETE_TRIGGERED_AT(trace_elimGrandson2_deleted, elimGrandson2, 2); |
---|
3708 | TEST_EXPECT_CHANGE_TRIGGERED_AT(traceHier_anyGrandson_changed, anotherGrandson, 3); |
---|
3709 | TEST_EXPECT_CHANGE_TRIGGERED_AT(trace_anotherGrandson_changed, anotherGrandson, 4); |
---|
3710 | |
---|
3711 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3712 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3713 | |
---|
3714 | // cleanup |
---|
3715 | GB_close(gb_main); |
---|
3716 | } |
---|
3717 | |
---|
3718 | #endif // UNIT_TESTS |
---|
3719 | |
---|
3720 | |
---|