1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | // #include <malloc.h> |
---|
5 | #include <arbdb.h> |
---|
6 | #include <arbdbt.h> |
---|
7 | #include <aw_root.hxx> |
---|
8 | #include <aw_device.hxx> |
---|
9 | #include <aw_window.hxx> |
---|
10 | #include <aw_awars.hxx> |
---|
11 | #include <awt.hxx> |
---|
12 | #include <awt_item_sel_list.hxx> |
---|
13 | |
---|
14 | #include "nt_sort.hxx" |
---|
15 | |
---|
16 | // test |
---|
17 | |
---|
18 | struct customsort_struct { |
---|
19 | const char *key1; |
---|
20 | const char *key2; |
---|
21 | const char *key3; |
---|
22 | }; |
---|
23 | |
---|
24 | static int cmpByKey(GBDATA *gbd1, GBDATA *gbd2, const char *field) { |
---|
25 | GBDATA *gb_field1 = GB_entry(gbd1, field); |
---|
26 | GBDATA *gb_field2 = GB_entry(gbd2, field); |
---|
27 | |
---|
28 | int cmp; |
---|
29 | if (gb_field1) { |
---|
30 | if (gb_field2) { |
---|
31 | switch (GB_read_type(gb_field1)) { |
---|
32 | case GB_STRING: { |
---|
33 | const char *s1 = GB_read_char_pntr(gb_field1); |
---|
34 | const char *s2 = GB_read_char_pntr(gb_field2); |
---|
35 | |
---|
36 | cmp = strcmp(s1, s2); |
---|
37 | break; |
---|
38 | } |
---|
39 | case GB_FLOAT: { |
---|
40 | double d1 = GB_read_float(gb_field1); |
---|
41 | double d2 = GB_read_float(gb_field2); |
---|
42 | |
---|
43 | cmp = d1<d2 ? -1 : (d1>d2 ? 1 : 0); |
---|
44 | break; |
---|
45 | } |
---|
46 | case GB_INT: { |
---|
47 | int i1 = GB_read_int(gb_field1); |
---|
48 | int i2 = GB_read_int(gb_field2); |
---|
49 | |
---|
50 | cmp = i1-i2; |
---|
51 | break; |
---|
52 | } |
---|
53 | default: |
---|
54 | cmp = 0; // other field type -> no idea how to compare |
---|
55 | break; |
---|
56 | } |
---|
57 | } |
---|
58 | else cmp = -1; // existing < missing! |
---|
59 | } |
---|
60 | else cmp = gb_field2 ? 1 : 0; |
---|
61 | |
---|
62 | return cmp; |
---|
63 | } |
---|
64 | |
---|
65 | static int resort_data_by_customsub(const void *v1, const void *v2, void *cd_sortBy) { |
---|
66 | GBDATA *gbd1 = (GBDATA*)v1; |
---|
67 | GBDATA *gbd2 = (GBDATA*)v2; |
---|
68 | customsort_struct *sortBy = (customsort_struct*)cd_sortBy; |
---|
69 | |
---|
70 | int cmp = cmpByKey(gbd1, gbd2, sortBy->key1); |
---|
71 | if (!cmp) { |
---|
72 | cmp = cmpByKey(gbd1, gbd2, sortBy->key2); |
---|
73 | if (!cmp) { |
---|
74 | cmp = cmpByKey(gbd1, gbd2, sortBy->key3); |
---|
75 | } |
---|
76 | } |
---|
77 | return cmp; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | static GBDATA **gb_resort_data_list; |
---|
82 | static long gb_resort_data_count; |
---|
83 | |
---|
84 | static void NT_resort_data_base_by_tree(GBT_TREE *tree, GBDATA *gb_species_data) { |
---|
85 | if (tree) { |
---|
86 | if (tree->is_leaf) { |
---|
87 | if (tree->gb_node) { |
---|
88 | gb_resort_data_list[gb_resort_data_count++] = tree->gb_node; |
---|
89 | } |
---|
90 | } |
---|
91 | else { |
---|
92 | NT_resort_data_base_by_tree(tree->leftson, gb_species_data); |
---|
93 | NT_resort_data_base_by_tree(tree->rightson, gb_species_data); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | GB_ERROR NT_resort_data_base(GBT_TREE *tree, const char *key1, const char *key2, const char *key3) { |
---|
100 | customsort_struct sortBy; |
---|
101 | |
---|
102 | sortBy.key1 = key1; |
---|
103 | sortBy.key2 = key2; |
---|
104 | sortBy.key3 = key3; |
---|
105 | |
---|
106 | GB_ERROR error = GB_begin_transaction(GLOBAL_gb_main); |
---|
107 | if (!error) { |
---|
108 | GBDATA *gb_sd = GB_search(GLOBAL_gb_main,"species_data",GB_CREATE_CONTAINER); |
---|
109 | if (!gb_sd) error = GB_await_error(); |
---|
110 | else { |
---|
111 | if (tree) { |
---|
112 | gb_resort_data_count = 0; |
---|
113 | gb_resort_data_list = (GBDATA **)calloc( sizeof(GBDATA *), GB_nsons(gb_sd) + 256); |
---|
114 | NT_resort_data_base_by_tree(tree,gb_sd); |
---|
115 | } |
---|
116 | else { |
---|
117 | gb_resort_data_list = GBT_gen_species_array(GLOBAL_gb_main,&gb_resort_data_count); |
---|
118 | GB_sort((void **)gb_resort_data_list, 0, gb_resort_data_count, resort_data_by_customsub, &sortBy); |
---|
119 | |
---|
120 | } |
---|
121 | error = GB_resort_data_base(GLOBAL_gb_main,gb_resort_data_list,gb_resort_data_count); |
---|
122 | free(gb_resort_data_list); |
---|
123 | } |
---|
124 | } |
---|
125 | return GB_end_transaction(GLOBAL_gb_main, error); |
---|
126 | } |
---|
127 | |
---|
128 | void NT_resort_data_by_phylogeny(AW_window *dummy, GBT_TREE **ptree){ |
---|
129 | aw_openstatus("resorting data"); |
---|
130 | AWUSE(dummy); |
---|
131 | GB_ERROR error = 0; |
---|
132 | GBT_TREE *tree = *ptree; |
---|
133 | if (!tree) error = "Please select/build a tree first"; |
---|
134 | if (!error) error = NT_resort_data_base(tree,0,0,0); |
---|
135 | aw_closestatus(); |
---|
136 | if (error) aw_message(error); |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | void NT_resort_data_by_user_criteria(AW_window *aw){ |
---|
141 | aw_openstatus("resorting data"); |
---|
142 | GB_ERROR error = 0; |
---|
143 | char *s1 = aw->get_root()->awar("ad_tree/sort_1")->read_string(); |
---|
144 | char *s2 = aw->get_root()->awar("ad_tree/sort_2")->read_string(); |
---|
145 | char *s3 = aw->get_root()->awar("ad_tree/sort_3")->read_string(); |
---|
146 | if (!error) error = NT_resort_data_base(0,s1,s2,s3); |
---|
147 | aw_closestatus(); |
---|
148 | if (error) aw_message(error); |
---|
149 | } |
---|
150 | |
---|
151 | void NT_build_resort_awars(AW_root *awr, AW_default aw_def){ |
---|
152 | awr->awar_string( "ad_tree/sort_1", "name", aw_def); |
---|
153 | awr->awar_string( "ad_tree/sort_2", "name", aw_def); |
---|
154 | awr->awar_string( "ad_tree/sort_3", "name", aw_def); |
---|
155 | } |
---|
156 | |
---|
157 | AW_window *NT_build_resort_window(AW_root *awr) { |
---|
158 | AW_window_simple *aws = new AW_window_simple; |
---|
159 | aws->init( awr, "SORT_DATABASE", "SORT DATABASE"); |
---|
160 | aws->load_xfig("nt_sort.fig"); |
---|
161 | |
---|
162 | aws->callback( (AW_CB0)AW_POPDOWN); |
---|
163 | aws->at("close"); |
---|
164 | aws->create_button("CLOSE","CLOSE","C"); |
---|
165 | |
---|
166 | aws->callback( AW_POPUP_HELP,(AW_CL)"sp_sort_fld.hlp"); |
---|
167 | aws->at("help"); |
---|
168 | aws->create_button("HELP","HELP","H"); |
---|
169 | |
---|
170 | aws->at("go"); |
---|
171 | aws->callback((AW_CB0)NT_resort_data_by_user_criteria); |
---|
172 | aws->create_button("GO","GO","G"); |
---|
173 | |
---|
174 | awt_create_selection_list_on_scandb(GLOBAL_gb_main, |
---|
175 | (AW_window*)aws,"ad_tree/sort_1", |
---|
176 | NT_RESORT_FILTER, |
---|
177 | "key1",0, &AWT_species_selector, 20, 10); |
---|
178 | |
---|
179 | awt_create_selection_list_on_scandb(GLOBAL_gb_main, |
---|
180 | (AW_window*)aws,"ad_tree/sort_2", |
---|
181 | NT_RESORT_FILTER, |
---|
182 | "key2",0, &AWT_species_selector, 20, 10); |
---|
183 | |
---|
184 | awt_create_selection_list_on_scandb(GLOBAL_gb_main, |
---|
185 | (AW_window*)aws,"ad_tree/sort_3", |
---|
186 | NT_RESORT_FILTER, |
---|
187 | "key3",0, &AWT_species_selector, 20, 10); |
---|
188 | |
---|
189 | return (AW_window *)aws; |
---|
190 | |
---|
191 | } |
---|