source: tags/svn.1.5.4/NTREE/NT_join.cxx

Last change on this file was 8309, checked in by westram, 14 years ago
  • moved much code into static scope

(partly reverted by [8310])

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : NT_join.cxx                                       //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include <item_sel_list.h>
12#include <awt_sel_boxes.hxx>
13#include <aw_awar.hxx>
14#include <aw_root.hxx>
15#include <aw_msg.hxx>
16#include <arb_progress.h>
17#include <arbdbt.h>
18
19#define nt_assert(bed) arb_assert(bed)
20
21extern GBDATA *GLOBAL_gb_main;
22
23#define AWAR_SPECIES_JOIN_FIELD "/tmp/NT/species_join/field"
24#define AWAR_SPECIES_JOIN_SEP   "/tmp/NT/species_join/separator"
25#define AWAR_SPECIES_JOIN_SEP2  "/tmp/NT/species_join/separator_sequences"
26
27static GB_ERROR nt_species_join(GBDATA *dest, GBDATA *source, int deep, char *sep, char *sep2) {
28    GB_TYPES dtype = GB_read_type(dest);
29    GB_TYPES stype = GB_read_type(source);
30    if (dtype != stype) return 0;
31
32    GB_ERROR error = 0;
33
34    switch (dtype) {
35        case GB_DB: {
36            GBDATA     *gb_source_field;
37            const char *source_field;
38            GBDATA     *gb_dest_field;
39
40            for (gb_source_field = GB_child(source);
41                 !error && gb_source_field;
42                 gb_source_field = GB_nextChild(gb_source_field))
43            {
44                source_field = GB_read_key_pntr(gb_source_field);
45                if (!strcmp(source_field, "name")) continue;
46
47                gb_dest_field = GB_entry(dest, source_field);
48                if (gb_dest_field) { // if destination exists -> recurse
49                    error = nt_species_join(gb_dest_field, gb_source_field, 0, sep, sep2);
50                }
51                else {
52                    GB_TYPES type = GB_read_type(gb_source_field);
53
54                    if (type == GB_DB) {
55                        GBDATA *gb_dest_container     = GB_create_container(dest, source_field);
56                        if (!gb_dest_container) error = GB_await_error();
57                        else    error                 = nt_species_join(gb_dest_container, gb_source_field, 0, sep, sep2);
58                    }
59                    else {
60                        gb_dest_field             = GB_create(dest, source_field, GB_read_type(gb_source_field));
61                        if (!gb_dest_field) error = GB_await_error();
62                        else error                = GB_copy(gb_dest_field, gb_source_field);
63                    }
64                }
65            }
66            break;
67        }
68
69        case GB_STRING: {
70            char *sf = GB_read_string(source);
71            char *df = GB_read_string(dest);
72            if (!strcmp(sf, df)) {
73                free(sf);
74                free(df);
75                break;
76            }
77            char *s = sep;
78            const char *spacers = " ";
79            if (deep) {
80                s = sep2;
81                spacers = ".- ";
82            }
83            int i;
84            // remove trailing spacers;
85            for (i = strlen(df)-1; i>=0; i--) {
86                if (strchr(spacers, df[i])) df[i] = 0;
87            }
88            // remove leading spacers
89            int end = strlen(sf);
90            for (i=0; i<end; i++) {
91                if (!strchr(spacers, sf[i])) break;
92            }
93            char *str = new char [strlen(sf) + strlen(df) + strlen(s) + 1];
94            sprintf(str, "%s%s%s", df, s, sf+i);
95            error = GB_write_string(dest, str);
96            delete [] str;
97            free(sf); free(df);
98            break;
99        }
100        default: {
101            break;
102        }
103    }
104    return error;
105}
106
107static void species_rename_join(AW_window *aww) {
108    char     *field = aww->get_root()->awar(AWAR_SPECIES_JOIN_FIELD)->read_string();
109    char     *sep   = aww->get_root()->awar(AWAR_SPECIES_JOIN_SEP)->read_string();
110    char     *sep2  = aww->get_root()->awar(AWAR_SPECIES_JOIN_SEP2)->read_string();
111    GB_ERROR  error = GB_begin_transaction(GLOBAL_gb_main);
112
113    if (!error) {
114        GB_HASH *hash = GBS_create_hash(1000, GB_MIND_CASE);
115        long     maxs = GBT_count_marked_species(GLOBAL_gb_main);
116       
117        arb_progress progress("Joining species", maxs);
118
119        GBDATA *gb_next = 0;
120        for (GBDATA *gb_species = GBT_first_marked_species(GLOBAL_gb_main);
121             gb_species && !error;
122             gb_species = gb_next)
123        {
124            gb_next = GBT_next_marked_species(gb_species);
125
126            GBDATA *gb_field = GB_entry(gb_species, field);
127            if (gb_field) {
128                const char *field_value = GB_read_char_pntr(gb_field);
129                GBDATA     *gb_old      = (GBDATA *)GBS_read_hash(hash, field_value);
130
131                if (!gb_old) {
132                    GBS_write_hash(hash, field_value, (long)gb_species);
133                }
134                else {
135                    error = nt_species_join(gb_old, gb_species, 0, sep, sep2);
136                    if (!error) error = GB_delete(gb_species);
137                }
138            }
139            progress.inc_and_check_user_abort(error);
140        }
141
142        GBS_free_hash(hash);
143    }
144    GB_end_transaction_show_error(GLOBAL_gb_main, error, aw_message);
145
146    free(sep2);
147    free(sep);
148    free(field);
149}
150
151
152AW_window *create_species_join_window(AW_root *root)
153{
154    static AW_window_simple *aws = 0;
155    if (aws) return (AW_window *)aws;
156
157    root->awar_string(AWAR_SPECIES_JOIN_FIELD, "name", AW_ROOT_DEFAULT);
158    root->awar_string(AWAR_SPECIES_JOIN_SEP, "#", AW_ROOT_DEFAULT);
159    root->awar_string(AWAR_SPECIES_JOIN_SEP2, "#", AW_ROOT_DEFAULT);
160
161    aws = new AW_window_simple;
162    aws->init(root, "JOIN_SPECIES", "JOIN SPECIES");
163    aws->load_xfig("join_species.fig");
164
165    aws->callback((AW_CB0)AW_POPDOWN);
166    aws->at("close");
167    aws->create_button("CLOSE", "CLOSE", "C");
168
169    aws->at("help"); aws->callback(AW_POPUP_HELP, (AW_CL)"species_join.hlp");
170    aws->create_button("HELP", "HELP", "H");
171
172    aws->at("sym");
173    aws->create_input_field(AWAR_SPECIES_JOIN_SEP);
174
175    aws->at("symseq");
176    aws->create_input_field(AWAR_SPECIES_JOIN_SEP2);
177
178    aws->at("go");
179    aws->callback(species_rename_join);
180    aws->help_text("species_join.hlp");
181    aws->create_button("GO", "GO", "G");
182
183    create_selection_list_on_itemfields(GLOBAL_gb_main,
184                                            aws, AWAR_SPECIES_JOIN_FIELD,
185                                            FIELD_FILTER_NDS,
186                                            "field", 0, SPECIES_get_selector(), 20, 10);
187
188    return (AW_window *)aws;
189}
Note: See TracBrowser for help on using the repository browser.