source: branches/sina/ARBDB/ad_config.cxx

Last change on this file was 19731, checked in by westram, 3 months ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ad_config.cxx                                     //
4//   Purpose   : editor configurations (aka species selections)    //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in May 2005       //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include "gb_local.h"
13
14#include <ad_config.h>
15#include <arbdbt.h>
16
17#include <arb_defs.h>
18#include <arb_strarray.h>
19
20void GBT_get_configuration_names(ConstStrArray& configNames, GBDATA *gb_main) {
21    /* returns existing configurations in 'configNames'
22     * Note: automatically generates names for configs w/o legal name.
23     */
24
25    GB_transaction  ta(gb_main);
26    GBDATA         *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_CREATE_CONTAINER);
27
28    if (gb_config_data) {
29        int unnamed_count = 0;
30
31        configNames.reserve(GB_number_of_subentries(gb_config_data));
32
33        for (GBDATA *gb_config = GB_entry(gb_config_data, CONFIG_ITEM);
34             gb_config;
35             gb_config = GB_nextEntry(gb_config))
36        {
37            const char *name = GBT_read_char_pntr(gb_config, "name");
38
39            if (!name || name[0] == 0) { // no name or empty name
40                char     *new_name = GBS_global_string_copy("<unnamed%i>", ++unnamed_count);
41                GB_ERROR  error    = GBT_write_string(gb_config, "name", new_name);
42
43                if (error) {
44                    GB_warningf("Failed to rename unnamed configuration to '%s'", new_name);
45                    freenull(new_name);
46                    name = NULp;
47                }
48                else {
49                    name = GBT_read_char_pntr(gb_config, "name");
50                }
51            }
52
53            if (name) configNames.put(name);
54        }
55    }
56}
57
58GBDATA *GBT_find_configuration(GBDATA *gb_main, const char *name) {
59    GB_transaction  ta(gb_main);
60    GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_DB);
61    GBDATA *gb_config_name = GB_find_string(gb_config_data, "name", name, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
62    return gb_config_name ? GB_get_father(gb_config_name) : NULp;
63}
64
65static GBDATA *findOrCreate_configuration(GBDATA *gb_main, const char *name) {
66    GBDATA *gb_config = GBT_find_configuration(gb_main, name);
67    if (!gb_config) {
68        GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_DB);
69
70        gb_config = GB_create_container(gb_config_data, CONFIG_ITEM); // create new container
71        if (gb_config) {
72            GB_ERROR error = GBT_write_string(gb_config, "name", name);
73            if (error) GB_export_error(error);
74        }
75    }
76    return gb_config;
77}
78
79GBT_config::GBT_config(GBDATA *gb_main, const char *name, GB_ERROR& error) {
80    GB_transaction  ta(gb_main);
81    GBDATA         *gb_config = GBT_find_configuration(gb_main, name);
82
83    error = NULp;
84    if (!gb_config) {
85        error       = GBS_global_string("No such configuration '%s'", name);
86        top_area    = NULp;
87        middle_area = NULp;
88        comment     = NULp;
89    }
90    else {
91        top_area    = GBT_read_string(gb_config, "top_area");
92        middle_area = GBT_read_string(gb_config, "middle_area");
93
94        if (!top_area || !middle_area) {
95            error = GBS_global_string("Configuration '%s' is corrupted (Reason: %s)", name, GB_await_error());
96        }
97
98        comment = GBT_read_string(gb_config, "comment");
99    }
100}
101
102GB_ERROR GBT_config::saveAsOver(GBDATA *gb_main, const char *name, const char *oldName, bool warnIfSavingDefault) const {
103    /*! save config as 'name' (overwriting config 'oldName')
104     * if 'warnIfSavingDefault' is true, saving DEFAULT_CONFIGURATION raises a warning
105     */
106    GB_ERROR error = NULp;
107
108    GB_push_transaction(gb_main);
109
110    GBDATA *gb_config = findOrCreate_configuration(gb_main, oldName);
111    if (!gb_config) {
112        error = GBS_global_string("Can't create configuration '%s' (Reason: %s)", oldName, GB_await_error());
113    }
114    else {
115        if (strcmp(name, oldName) != 0) error = GBT_write_string(gb_config, "name",    name);
116
117        error             = GBT_write_string(gb_config, "top_area",    top_area);
118        if (!error) error = GBT_write_string(gb_config, "middle_area", middle_area);
119
120        if (!error) {
121            if (comment && comment[0]) { // non-empty
122                error = GBT_write_string(gb_config, "comment", comment);
123            }
124            else {
125                GBDATA *gb_comment = GB_entry(gb_config, "comment");
126                if (gb_comment) error = GB_delete(gb_comment); // delete field if comment empty
127            }
128        }
129
130        if (error) error = GBS_global_string("%s (in configuration '%s')", error, name);
131    }
132
133    if (warnIfSavingDefault && strcmp(name, DEFAULT_CONFIGURATION) == 0) {
134        GBT_message(gb_main, "Note: You saved the '" DEFAULT_CONFIGURATION "'.\nStarting ARB_EDIT4 will probably overwrite it!");
135    }
136
137    return GB_end_transaction(gb_main, error);
138}
139
140const GBT_config_item& GBT_config_parser::nextItem(GB_ERROR& error) {
141    // see als similar code in ../PERL_SCRIPTS/DB/databaseReport.pl@PARSE_SELECTION
142
143    error = NULp;
144
145    freenull(item.name);
146    item.type = CI_END_OF_CONFIG;
147
148    if (config_string[parse_pos]) {             // if not at 0-byte
149        char label = config_string[parse_pos+1];
150        item.type = CI_UNKNOWN;
151
152        switch (label) {
153            case 'L': item.type = CI_SPECIES;      break;
154            case 'S': item.type = CI_SAI;          break;
155            case 'F': item.type = CI_FOLDED_GROUP; break;
156            case 'G': item.type = CI_GROUP;        break;
157            case 'E': item.type = CI_CLOSE_GROUP;  break;
158            default:  item.type = CI_UNKNOWN;      break;
159        }
160
161        if (item.type == CI_CLOSE_GROUP) {
162            parse_pos += 2;
163        }
164        else {
165            const char *start_of_name = config_string+parse_pos+2;
166            const char *behind_name   = strchr(start_of_name, '\1');
167
168            if (!behind_name) behind_name = strchr(start_of_name, '\0'); // eos
169            gb_assert(behind_name);
170
171            char *data = ARB_strpartdup(start_of_name, behind_name-1);
172            if (item.type == CI_UNKNOWN) {
173                error = GBS_global_string_copy("Unknown flag '%c' (followed by '%s')", label, data);
174                free(data);
175            }
176            else {
177                item.name = data;
178                parse_pos = behind_name-config_string;
179            }
180        }
181
182        if (error) { // stop parser
183            const char *end_of_config = strchr(config_string+parse_pos, '\0');
184            parse_pos                 = end_of_config-config_string;
185            gb_assert(config_string[parse_pos] == 0);
186        }
187    }
188
189    return item;
190}
191
192void GBT_config_item::append_to_config_string(GBS_strstruct& out) const {
193    gb_assert((type & (CI_UNKNOWN|CI_END_OF_CONFIG)) == 0);
194
195    char prefix[] = "\1?";
196    if (type == CI_CLOSE_GROUP) {
197        prefix[1] = 'E';
198        out.cat(prefix);
199    }
200    else {
201        char label = 0;
202        switch (type) {
203            case CI_SPECIES:      label = 'L'; break;
204            case CI_SAI:          label = 'S'; break;
205            case CI_GROUP:        label = 'G'; break;
206            case CI_FOLDED_GROUP: label = 'F'; break;
207
208            default: gb_assert(0); break;
209        }
210        prefix[1] = label;
211        out.cat(prefix);
212        out.cat(name);
213    }
214}
215
216// --------------------------------------------------------------------------------
217
218#ifdef UNIT_TESTS
219#include <test_unit.h>
220
221void TEST_GBT_get_configuration_names() {
222    GB_shell  shell;
223    GBDATA   *gb_main = GB_open("nosuch.arb", "c");
224
225    {
226        GB_transaction ta(gb_main);
227
228        const char *configs[] = { "arb", "BASIC", "Check it", "dummy" };
229        for (size_t i = 0; i<ARRAY_ELEMS(configs); ++i) {
230            TEST_EXPECT_RESULT__NOERROREXPORTED(findOrCreate_configuration(gb_main, configs[i]));
231        }
232
233        ConstStrArray cnames;
234        GBT_get_configuration_names(cnames, gb_main);
235
236        TEST_EXPECT_EQUAL(cnames.size(), 4U);
237        TEST_EXPECT_STRARRAY_CONTAINS(cnames, '*', "arb*BASIC*Check it*dummy");
238    }
239
240    GB_close(gb_main);
241}
242
243#endif // UNIT_TESTS
244
245
Note: See TracBrowser for help on using the repository browser.