File: ad_config.cxx

package info (click to toggle)
arb 6.0.2-1%2Bdeb8u1
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie
  • size: 65,916 kB
  • ctags: 53,258
  • sloc: ansic: 394,903; cpp: 250,252; makefile: 19,620; sh: 15,878; perl: 10,461; fortran: 6,019; ruby: 683; xml: 503; python: 53; awk: 32
file content (338 lines) | stat: -rw-r--r-- 11,786 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// =============================================================== //
//                                                                 //
//   File      : ad_config.cxx                                     //
//   Purpose   : editor configurations                             //
//                                                                 //
//   Coded by Ralf Westram (coder@reallysoft.de) in May 2005       //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //

#include "gb_local.h"

#include <ad_config.h>
#include <arbdbt.h>

#include <arb_strbuf.h>
#include <arb_defs.h>
#include <arb_strarray.h>

void GBT_get_configuration_names(ConstStrArray& configNames, GBDATA *gb_main) {
    /* returns existing configurations in 'configNames'
     * Note: automatically generates names for configs w/o legal name.
     */

    GB_transaction  ta(gb_main);
    GBDATA         *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_CREATE_CONTAINER);

    if (gb_config_data) {
        int unnamed_count = 0;

        configNames.reserve(GB_number_of_subentries(gb_config_data));
        
        for (GBDATA *gb_config = GB_entry(gb_config_data, CONFIG_ITEM);
             gb_config;
             gb_config = GB_nextEntry(gb_config))
        {
            const char *name = GBT_read_char_pntr(gb_config, "name");

            if (!name || name[0] == 0) { // no name or empty name
                char     *new_name = GBS_global_string_copy("<unnamed%i>", ++unnamed_count);
                GB_ERROR  error    = GBT_write_string(gb_config, "name", new_name);

                if (error) {
                    GB_warningf("Failed to rename unnamed configuration to '%s'", new_name);
                    freenull(new_name);
                    name = NULL;
                }
                else {
                    name = GBT_read_char_pntr(gb_config, "name");
                }
            }

            if (name) configNames.put(name);
        }
    }
}

GBDATA *GBT_find_configuration(GBDATA *gb_main, const char *name) {
    GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_DB);
    GBDATA *gb_config_name = GB_find_string(gb_config_data, "name", name, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
    return gb_config_name ? GB_get_father(gb_config_name) : 0;
}

GBDATA *GBT_create_configuration(GBDATA *gb_main, const char *name) {
    GBDATA *gb_config = GBT_find_configuration(gb_main, name);
    if (!gb_config) {
        GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_DB);

        gb_config = GB_create_container(gb_config_data, CONFIG_ITEM); // create new container
        if (gb_config) {
            GB_ERROR error = GBT_write_string(gb_config, "name", name);
            if (error) GB_export_error(error);
        }
    }
    return gb_config;
}

void GBT_free_configuration_data(GBT_config *data) {
    free(data->top_area);
    free(data->middle_area);
    free(data);
}

GBT_config *GBT_load_configuration_data(GBDATA *gb_main, const char *name, GB_ERROR *error) {
    GBT_config *config = 0;

    *error            = GB_push_transaction(gb_main);
    GBDATA *gb_config = GBT_find_configuration(gb_main, name);

    if (!gb_config) {
        *error = GBS_global_string("No such configuration '%s'", name);
    }
    else {
        config              = (GBT_config*)GB_calloc(1, sizeof(*config));
        config->top_area    = GBT_read_string(gb_config, "top_area");
        config->middle_area = GBT_read_string(gb_config, "middle_area");

        if (!config->top_area || !config->middle_area) {
            GBT_free_configuration_data(config);
            config = 0;
            *error = GBS_global_string("Configuration '%s' is corrupted (Reason: %s)",
                                       name, GB_await_error());
        }
    }

    *error = GB_end_transaction(gb_main, *error);
    return config;
}

GB_ERROR GBT_save_configuration_data(GBT_config *config, GBDATA *gb_main, const char *name) {
    GB_ERROR  error = 0;
    GBDATA   *gb_config;

    GB_push_transaction(gb_main);

    gb_config = GBT_create_configuration(gb_main, name);
    if (!gb_config) {
        error = GBS_global_string("Can't create configuration '%s' (Reason: %s)", name, GB_await_error());
    }
    else {
        error             = GBT_write_string(gb_config, "top_area", config->top_area);
        if (!error) error = GBT_write_string(gb_config, "middle_area", config->middle_area);

        if (error) error = GBS_global_string("%s (in configuration '%s')", error, name);
    }

    return GB_end_transaction(gb_main, error);
}

GBT_config_parser *GBT_start_config_parser(const char *config_string) {
    GBT_config_parser *parser = (GBT_config_parser*)GB_calloc(1, sizeof(*parser));

    parser->config_string = nulldup(config_string);
    parser->parse_pos     = 0;

    return parser;
}

GBT_config_item *GBT_create_config_item() {
    GBT_config_item *item = (GBT_config_item*)GB_calloc(1, sizeof(*item));
    item->type            = CI_UNKNOWN;
    item->name            = 0;
    return item;
}

void GBT_free_config_item(GBT_config_item *item) {
    free(item->name);
    free(item);
}

GB_ERROR GBT_parse_next_config_item(GBT_config_parser *parser, GBT_config_item *item) {
    // the passed 'item' gets filled with parsed data from the config string
    GB_ERROR error = 0;

    const char *str = parser->config_string;
    int         pos = parser->parse_pos;

    freenull(item->name);
    item->type = CI_END_OF_CONFIG;

    if (str[pos]) {             // if not at 0-byte
        char label = str[pos+1];
        item->type = CI_UNKNOWN;

        switch (label) {
            case 'L': item->type = CI_SPECIES; break;
            case 'S': item->type = CI_SAI; break;
            case 'F': item->type = CI_FOLDED_GROUP; break;
            case 'G': item->type = CI_GROUP; break;
            case 'E': item->type = CI_CLOSE_GROUP; break;
            default: item->type = CI_UNKNOWN; break;
        }

        if (item->type == CI_CLOSE_GROUP) {
            pos += 2;
        }
        else {
            const char *start_of_name = str+pos+2;
            const char *behind_name   = strchr(start_of_name, '\1');

            if (!behind_name) behind_name = strchr(start_of_name, '\0'); // eos
            gb_assert(behind_name);

            char *data = GB_strpartdup(start_of_name, behind_name-1);
            if (item->type == CI_UNKNOWN) {
                error = GBS_global_string_copy("Unknown flag '%c' (followed by '%s')", label, data);
                free(data);
            }
            else {
                item->name = data;
                pos        = behind_name-str;
            }
        }

        if (error) { // stop parser
            const char *end_of_config = strchr(str+pos, '\0');
            pos                 = end_of_config-str;
            gb_assert(str[pos] == 0);
        }

        parser->parse_pos = pos;
    }
    return error;
}

void GBT_append_to_config_string(const GBT_config_item *item, GBS_strstruct *strstruct) {
    // strstruct has to be created by GBS_stropen()

    gb_assert((item->type & (CI_UNKNOWN|CI_END_OF_CONFIG)) == 0);

    char prefix[] = "\1?";
    if (item->type == CI_CLOSE_GROUP) {
        prefix[1] = 'E';
        GBS_strcat(strstruct, prefix);
    }
    else {
        char label = 0;
        switch (item->type) {
            case CI_SPECIES:      label = 'L'; break;
            case CI_SAI:          label = 'S'; break;
            case CI_GROUP:        label = 'G'; break;
            case CI_FOLDED_GROUP: label = 'F'; break;

            default: gb_assert(0); break;
        }
        prefix[1] = label;
        GBS_strcat(strstruct, prefix);
        GBS_strcat(strstruct, item->name);
    }
}


void GBT_free_config_parser(GBT_config_parser *parser) {
    free(parser->config_string);
    free(parser);
}

#if defined(DEBUG) 
void GBT_test_config_parser(GBDATA *gb_main) {
    ConstStrArray config_names;
    GBT_get_configuration_names(config_names, gb_main);
    if (!config_names.empty()) {
        int count;
        for (count = 0; config_names[count]; ++count) {
            const char *config_name = config_names[count];
            GBT_config *config;
            GB_ERROR    error       = 0;

            printf("Testing configuration '%s':\n", config_name);
            config = GBT_load_configuration_data(gb_main, config_name, &error);
            if (!config) {
                gb_assert(error);
                printf("* Error loading config: %s\n", error);
            }
            else {
                int area;

                gb_assert(!error);
                printf("* Successfully loaded\n");

                for (area = 0; area<2 && !error; ++area) {
                    const char        *area_name       = area ? "middle_area" : "top_area";
                    const char        *area_config_def = area ? config->middle_area : config->top_area;
                    GBT_config_parser *parser          = GBT_start_config_parser(area_config_def);
                    GBT_config_item   *item            = GBT_create_config_item();
                    GBS_strstruct     *new_config      = GBS_stropen(1000);
                    char              *new_config_str;

                    gb_assert(parser);
                    printf("* Created parser for '%s'\n", area_name);

                    while (1) {
                        error = GBT_parse_next_config_item(parser, item);
                        if (error || item->type == CI_END_OF_CONFIG) break;

                        printf("  - %i %s\n", item->type, item->name ? item->name : "[close group]");
                        GBT_append_to_config_string(item, new_config);
                    }

                    GBT_free_config_item(item);
                    new_config_str = GBS_strclose(new_config);

                    if (error) printf("* Parser error: %s\n", error);
                    else {
                        if (strcmp(area_config_def, new_config_str) == 0) {
                            printf("* Re-Created config is identical to original\n");
                        }
                        else {
                            printf("* Re-Created config is NOT identical to original:\n"
                                   "  - original : '%s'\n"
                                   "  - recreated: '%s'\n",
                                   area_config_def,
                                   new_config_str);
                        }
                    }

                    GBT_free_config_parser(parser);
                    free(new_config_str);
                }
            }
            GBT_free_configuration_data(config);
        }
    }
}
#endif // DEBUG

// --------------------------------------------------------------------------------

#ifdef UNIT_TESTS
#include <test_unit.h>

void TEST_GBT_get_configuration_names() {
    GB_shell  shell;
    GBDATA   *gb_main = GB_open("nosuch.arb", "c");

    {
        GB_transaction ta(gb_main);

        const char *configs[] = { "arb", "BASIC", "Check it", "dummy" };
        for (size_t i = 0; i<ARRAY_ELEMS(configs); ++i) {
            TEST_EXPECT_RESULT__NOERROREXPORTED(GBT_create_configuration(gb_main, configs[i]));
        }

        ConstStrArray cnames;
        GBT_get_configuration_names(cnames, gb_main);

        TEST_EXPECT_EQUAL(cnames.size(), 4U);

        char *joined = GBT_join_names(cnames, '*');
        TEST_EXPECT_EQUAL(joined, "arb*BASIC*Check it*dummy");
        free(joined);
    }

    GB_close(gb_main);
}

#endif // UNIT_TESTS