source: tags/arb_5.2/TOOLS/arb_test.cxx

Last change on this file was 5824, checked in by westram, 15 years ago
  • renamed GB_expect_error into GB_await_error
    • GB_expect_error was too similar to GB_export_error
    • GB_await_error now clears the error buffer!
  • declared GB_get_error() deprecated
    • Problem is: after calling GB_get_error it is undefined, whether the error message belongs to the caller or still to the error buffer
    • GB_have_error() and GB_await_error() - maybe combined - are the recommended replacement
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : arb_test.cxx                                      //
4//   Purpose   : small test programm (not part of ARB)             //
5//               If you need to test sth, you may completely       //
6//               overwrite and checkin this.                       //
7//                                                                 //
8//                                                                 //
9//   Coded by Ralf Westram (coder@reallysoft.de) in May 2008       //
10//   Institute of Microbiology (Technical University Munich)       //
11//   http://www.arb-home.de/                                       //
12//                                                                 //
13// =============================================================== //
14
15#include <cstdio>
16#include <cstring>
17#include <cstdlib>
18
19#include <arbdb.h>
20
21static GB_ERROR create_n_write(GBDATA *gb_father, const char *field, const char *content) {
22    GB_ERROR  error = 0;
23    GBDATA   *gbd   = GB_create(gb_father, field, GB_STRING);
24   
25    if (!gbd) {
26        error = GB_await_error();
27    }
28    else {
29        error = GB_write_string(gbd, content);
30    }
31
32    return error;
33}
34
35static GB_ERROR find_n_print(GBDATA *gb_father, const char *field) {
36    GB_ERROR  error = 0;
37    GBDATA   *gbd   = GB_entry(gb_father, field);
38
39    if (!gbd) {
40        printf("No such field '%s'\n", field);
41    }
42    else {
43        char *content = GB_read_string(gbd);
44        if (!content) {
45            error = GB_await_error();
46        }
47        else {
48            printf("field '%s' has content '%s'\n", field, content);
49            free(content);
50        }
51    }
52
53    return error;
54}
55
56static GB_ERROR dump_fields(GBDATA *gb_main) {
57    GB_transaction ta(gb_main);
58    GB_ERROR       error;
59   
60    error             = find_n_print(gb_main, "case");
61    if (!error) error = find_n_print(gb_main, "CASE");
62    if (!error) error = find_n_print(gb_main, "Case");
63
64    return error;
65}
66
67static GB_ERROR dump_key_data(GBDATA *gb_main) {
68    GB_transaction ta(gb_main);
69    GB_ERROR       error = 0;
70
71    GBDATA *gb_system = GB_entry(gb_main, "__SYSTEM__");
72    if (!gb_system) {
73        error = "can't find '__SYSTEM__'";
74    }
75    else {
76        GBDATA *gb_key_data = GB_entry(gb_system, "@key_data");
77        if (!gb_key_data) {
78            error = "can't find '@key_data'";
79        }
80        else {
81            int count = 0;
82
83            for (GBDATA *gb_key = GB_entry(gb_key_data, "@key"); gb_key; gb_key = GB_nextEntry(gb_key)) {
84                GBDATA *gb_name = GB_entry(gb_key, "@name");
85                if (!gb_name) {
86                    error = "@key w/o @name entry";
87                }
88                else {
89                    char *name = GB_read_string(gb_name);
90
91                    if (!name) {
92                        error = GB_await_error();
93                    }
94                    else {
95                        GBDATA *gb_cm = GB_entry(gb_key, "compression_mask");
96                       
97                        if (!gb_cm)  {
98                            error = "@key w/o compression_mask";
99                        }
100                        else {
101                            int cm = GB_read_int(gb_cm);
102
103                            printf("key %i name='%s' compression_mask=%i\n", count, name, cm);
104                        }
105
106                        free(name);
107                    }
108                }
109                count++;
110            }
111            printf("%i keys seen\n", count);
112        }
113    }
114
115    return error;
116}
117
118static GB_ERROR create_db(const char *filename) {
119    GB_ERROR  error   = 0;
120    GBDATA   *gb_main = GB_open(filename, "wc");
121
122    if (!gb_main) error = GB_await_error();
123
124    // write two fields with same case
125    if (!error) {
126        GB_transaction ta(gb_main);
127
128        error             = create_n_write(gb_main, "case", "lower case");
129        if (!error) error = create_n_write(gb_main, "CASE", "upper case");
130
131        error = ta.close(error);
132    }
133
134    // search for fields
135    if (!error) error = dump_fields(gb_main);
136    if (!error) error = dump_key_data(gb_main);
137    if (!error) error = GB_save(gb_main, filename, "b");
138   
139    GB_close(gb_main);
140
141    return error;
142}
143
144static GB_ERROR test_db(const char *filename) {
145    GB_ERROR  error   = 0;
146    GBDATA   *gb_main = GB_open(filename, "r");
147
148    if (!gb_main) error = GB_await_error();
149    if (!error) error   = dump_fields(gb_main);
150    if (!error) error   = dump_key_data(gb_main);
151   
152    GB_close(gb_main);
153
154    return error;
155}
156
157int main(int argc, char **argv)
158{
159    if (argc != 2) {
160        fprintf(stderr, "arb_test -- database test program\n"
161                "Syntax: arb_test database\n");
162        return EXIT_FAILURE;
163    }
164
165    const char *filename = argv[1];
166    GB_ERROR    error    = 0;
167
168    printf("Creating DB '%s'..\n", filename);
169    error = create_db(filename);
170   
171    if (!error) {
172        printf("Testing DB '%s'..\n", filename);
173        error = test_db(filename);
174    }
175
176    if (error) fprintf(stderr, "Error in arb_test: %s\n", error);
177    return error ? EXIT_FAILURE : EXIT_SUCCESS;
178}
179
180
Note: See TracBrowser for help on using the repository browser.