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