1 | #include <stdio.h> |
---|
2 | #include <string.h> |
---|
3 | #include <ctype.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 <awt.hxx> |
---|
10 | #include "merge.hxx" |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | #define AWAR_SOURCE_FIELD "/tmp/merge1/chk/source" |
---|
15 | #define AWAR_DEST_FIELD "/tmp/merge1/chk/dest" |
---|
16 | #define AWAR_TOUPPER "/tmp/merge1/chk/ToUpper" |
---|
17 | #define AWAR_EXCLUDE "/tmp/merge1/chk/exclude" |
---|
18 | #define AWAR_CORRECT "/tmp/merge1/chk/correct" |
---|
19 | #define AWAR_ETAG "/tmp/merge1/chk/tag" |
---|
20 | |
---|
21 | |
---|
22 | int gbs_cmp_strings(char *str1,char *str2, int *tab){ /* returns 0 if strings are equal */ |
---|
23 | register char *s1,*s2; |
---|
24 | register int c1,c2; |
---|
25 | s1 = str1; |
---|
26 | s2 = str2; |
---|
27 | int count = 10; |
---|
28 | do { |
---|
29 | do { c1= *(s1++); } while (tab[c1] < 0); |
---|
30 | do { c2= *(s2++); } while (tab[c2] < 0); |
---|
31 | if (tab[c1] != tab[c2]) { /* difference found */ |
---|
32 | return 1; |
---|
33 | } |
---|
34 | count --; |
---|
35 | } while (count && c1 && c2); |
---|
36 | return 0; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | char *GBS_diff_strings(char *str1,char * &str2, char *exclude , long ToUpper, long correct, |
---|
41 | char **res1, char **res2, long *corrrected){ |
---|
42 | |
---|
43 | char buffer1[256]; |
---|
44 | char buffer2[256]; |
---|
45 | char *dest1 = buffer1; |
---|
46 | char *dest2 = buffer2; |
---|
47 | register char *s1,*s2; |
---|
48 | register int c1,c2; |
---|
49 | int count = 3; |
---|
50 | int tab[256]; |
---|
51 | int i; |
---|
52 | |
---|
53 | s1 = str1; |
---|
54 | s2 = str2; |
---|
55 | *dest1 = 0; |
---|
56 | *dest2 = 0; |
---|
57 | tab[0] = 0; |
---|
58 | char gapchar = '#'; |
---|
59 | if (strlen(exclude)) gapchar = exclude[0]; |
---|
60 | else exclude = 0; |
---|
61 | |
---|
62 | for (i=1;i<256;i++) { |
---|
63 | tab[i] = i; |
---|
64 | if (exclude && strchr(exclude,i)) { |
---|
65 | tab[i] = -1; |
---|
66 | continue; |
---|
67 | } |
---|
68 | if (ToUpper && i>= 'a' && i<= 'z' ){ |
---|
69 | tab[i] = i-'a'+'A'; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | do { |
---|
74 | do { c1= *(s1++); } while (tab[c1] < 0); |
---|
75 | do { c2= *(s2++); } while (tab[c2] < 0); |
---|
76 | if (tab[c1] != tab[c2]) { /* difference found */ |
---|
77 | if (correct) { |
---|
78 | /* check subsitution */ |
---|
79 | { |
---|
80 | int c = s2[-1]; |
---|
81 | s2[-1] = s1[-1]; |
---|
82 | if (toupper(c1) == toupper(c2) || |
---|
83 | !gbs_cmp_strings(s1,s2,&tab[0]) ){ |
---|
84 | *corrrected = 1; |
---|
85 | continue; |
---|
86 | } |
---|
87 | s2[-1] = c; |
---|
88 | } |
---|
89 | |
---|
90 | /* check insertion in s2 */ |
---|
91 | if (!gbs_cmp_strings(s1-1,s2,&tab[0]) ){ |
---|
92 | s2[-1] = gapchar; |
---|
93 | do { c2= *(s2++); } while (tab[c2] < 0); /* eat s2 */ |
---|
94 | *corrrected = 1; |
---|
95 | continue; |
---|
96 | } |
---|
97 | /* check deletion in s2 */ |
---|
98 | if ( !gbs_cmp_strings(s1,s2-1,&tab[0]) ){ |
---|
99 | int toins = c1; |
---|
100 | char *toinspos = s2-1; |
---|
101 | if (toinspos > str2) toinspos--; |
---|
102 | if (tab[toinspos[0]]> 0) { /* real insertion */ |
---|
103 | void *str = GBS_stropen(strlen(str2+10)); |
---|
104 | int pos = s2-str2-1; |
---|
105 | GBS_strncat(str,str2,pos); |
---|
106 | GBS_chrcat(str,toins); |
---|
107 | GBS_strcat(str,str2+pos); |
---|
108 | delete str2; |
---|
109 | str2 = GBS_strclose(str,0); |
---|
110 | s2 = str2+pos+1; |
---|
111 | *corrrected = 1; |
---|
112 | continue; |
---|
113 | } |
---|
114 | int side=1; /* 0 = left 1= right */ |
---|
115 | if ( tab[s1[0]]<0 ) side = 0; |
---|
116 | if ( ! side ) { |
---|
117 | while ( toinspos > str2 && |
---|
118 | tab[toinspos[-1]] < 0 ) toinspos--; |
---|
119 | } |
---|
120 | toinspos[0] = toins; |
---|
121 | *corrrected = 1; |
---|
122 | do { c1= *(s1++); } while (tab[c1] < 0); /* eat s1 */ |
---|
123 | continue; |
---|
124 | } |
---|
125 | } |
---|
126 | if (count >=0){ |
---|
127 | sprintf(dest1,"%i ",s1-str1-1); |
---|
128 | sprintf(dest2,"%i ",s2-str2-1); |
---|
129 | dest1 += strlen(dest1); |
---|
130 | dest2 += strlen(dest2); |
---|
131 | } |
---|
132 | count --; |
---|
133 | } |
---|
134 | } while (c1 && c2); |
---|
135 | |
---|
136 | if (c1 || c2) { |
---|
137 | sprintf(dest1,"... %i ",s1-str1-1); |
---|
138 | sprintf(dest2,"... %i ",s2-str2-1); |
---|
139 | dest1 += strlen(dest1); |
---|
140 | dest2 += strlen(dest2); |
---|
141 | } |
---|
142 | if (count<0){ |
---|
143 | sprintf(dest1,"and %i more",1-count); |
---|
144 | sprintf(dest2,"and %i more",1-count); |
---|
145 | dest1 += strlen(dest1); |
---|
146 | dest2 += strlen(dest2); |
---|
147 | } |
---|
148 | if (strlen(buffer1) ) { |
---|
149 | *res1 = strdup(buffer1); |
---|
150 | *res2 = strdup(buffer2); |
---|
151 | }else{ |
---|
152 | *res1 = 0; |
---|
153 | *res2 = 0; |
---|
154 | } |
---|
155 | return 0; |
---|
156 | } |
---|
157 | |
---|
158 | #undef IS_QUERIED |
---|
159 | #define IS_QUERIED(gb_species) (1 & GB_read_usr_private(gb_species)) |
---|
160 | |
---|
161 | void mg_check_field_cb(AW_window *aww){ |
---|
162 | AW_root *root = aww->get_root(); |
---|
163 | GB_ERROR error = 0; |
---|
164 | char *source = root->awar(AWAR_SOURCE_FIELD)->read_string(); |
---|
165 | char *dest = root->awar(AWAR_DEST_FIELD)->read_string(); |
---|
166 | char *exclude = root->awar(AWAR_EXCLUDE)->read_string(); |
---|
167 | long ToUpper = root->awar(AWAR_TOUPPER)->read_int(); |
---|
168 | long correct = root->awar(AWAR_CORRECT)->read_int(); |
---|
169 | char *tag = root->awar(AWAR_ETAG)->read_string(); |
---|
170 | |
---|
171 | if (!strlen(source)) { |
---|
172 | delete source; |
---|
173 | delete dest; |
---|
174 | delete exclude; |
---|
175 | delete tag; |
---|
176 | aw_message("Please select a source field"); |
---|
177 | return; |
---|
178 | } |
---|
179 | if (!strlen(dest)) { |
---|
180 | delete source; |
---|
181 | delete tag; |
---|
182 | delete dest; |
---|
183 | delete exclude; |
---|
184 | aw_message("Please select a dest field"); |
---|
185 | return; |
---|
186 | } |
---|
187 | aw_openstatus("Checking fields"); |
---|
188 | GB_begin_transaction(gb_merge); |
---|
189 | GB_begin_transaction(gb_dest); |
---|
190 | |
---|
191 | GBDATA *gb_species_data1 = GB_search(gb_merge,"species_data",GB_CREATE_CONTAINER); |
---|
192 | GBDATA *gb_species_data2 = GB_search(gb_dest,"species_data",GB_CREATE_CONTAINER); |
---|
193 | |
---|
194 | GBDATA *gb_species1; |
---|
195 | GBDATA *gb_species2; |
---|
196 | GBDATA *gb_name1; |
---|
197 | GBDATA *gb_field1; |
---|
198 | GBDATA *gb_field2; |
---|
199 | GBDATA *gbd; |
---|
200 | int sum_species = 0; |
---|
201 | int species_count = 0; |
---|
202 | // First step: count selected species |
---|
203 | for (gb_species1 = GBT_first_species_rel_species_data(gb_species_data1); |
---|
204 | gb_species1; |
---|
205 | gb_species1 = GBT_next_species(gb_species1)){ |
---|
206 | gbd = GB_search(gb_species1,dest,GB_FIND); |
---|
207 | if (gbd) GB_delete(gbd); |
---|
208 | if (!IS_QUERIED(gb_species1)) continue; |
---|
209 | sum_species ++; |
---|
210 | } |
---|
211 | // Delete all 'dest' fields in gb_database 2 |
---|
212 | for ( gb_species2 = GBT_first_species_rel_species_data(gb_species_data2); |
---|
213 | gb_species2; |
---|
214 | gb_species2 = GBT_next_species(gb_species2)){ |
---|
215 | gbd = GB_search(gb_species2,dest,GB_FIND); |
---|
216 | if (gbd) GB_delete(gbd); |
---|
217 | } |
---|
218 | |
---|
219 | for ( gb_species1 = GBT_first_species_rel_species_data(gb_species_data1); |
---|
220 | gb_species1; |
---|
221 | gb_species1 = GBT_next_species(gb_species1)){ |
---|
222 | gbd = GB_search(gb_species1,dest,GB_FIND); |
---|
223 | if (gbd) GB_delete(gbd); |
---|
224 | |
---|
225 | gb_name1 = GB_find(gb_species1,"name",0,down_level); |
---|
226 | if (!gb_name1) continue; // no name what happend ??? |
---|
227 | |
---|
228 | if (!IS_QUERIED(gb_species1)) continue; |
---|
229 | species_count++; |
---|
230 | if ( (species_count & 0xf) == 0 ){ |
---|
231 | aw_status(species_count/ (double)sum_species); |
---|
232 | } |
---|
233 | gb_species2 = GB_find(gb_species_data2,"name", |
---|
234 | GB_read_char_pntr(gb_name1),down_2_level); |
---|
235 | |
---|
236 | if (!gb_species2) { |
---|
237 | sprintf(AW_ERROR_BUFFER,"WARNING: Species %s not found in DB II", |
---|
238 | GB_read_char_pntr(gb_name1)); |
---|
239 | aw_message(); |
---|
240 | continue; |
---|
241 | }else{ |
---|
242 | gb_species2 = GB_get_father(gb_species2); |
---|
243 | } |
---|
244 | char *s1 = 0; |
---|
245 | char *s2 = 0; |
---|
246 | gb_field1 = GB_search(gb_species1,source,GB_FIND); |
---|
247 | gb_field2 = GB_search(gb_species2,source,GB_FIND); |
---|
248 | |
---|
249 | if ( (!gb_field1) && (!gb_field2) ) continue; |
---|
250 | if (gb_field1 ) s1 = GB_read_as_tagged_string(gb_field1,tag); |
---|
251 | if (gb_field2 ) s2 = GB_read_as_tagged_string(gb_field2,tag); |
---|
252 | if (!s1 && !s2) continue; |
---|
253 | |
---|
254 | { |
---|
255 | char *positions1; |
---|
256 | char *positions2; |
---|
257 | if (s1 && s2){ |
---|
258 | long corrected= 0; |
---|
259 | GBS_diff_strings(s1,s2,exclude,ToUpper,correct,&positions1,&positions2,&corrected); |
---|
260 | if (corrected) { |
---|
261 | error = GB_write_as_string(gb_field2,s2); |
---|
262 | GB_write_flag(gb_species2,1); |
---|
263 | } |
---|
264 | }else{ |
---|
265 | if (s1) positions1 = strdup("missing field in other db"); |
---|
266 | else positions1 = strdup("missing field in this db"); |
---|
267 | if (s2) positions2 = strdup("missing field in other db"); |
---|
268 | else positions2 = strdup("missing field in this db"); |
---|
269 | } |
---|
270 | if (positions1){ |
---|
271 | gbd = GB_search(gb_species2,dest,GB_STRING); |
---|
272 | GB_write_string(gbd,positions2); |
---|
273 | delete positions2; |
---|
274 | gbd = GB_search(gb_species1,dest,GB_STRING); |
---|
275 | GB_write_string(gbd,positions1); |
---|
276 | delete positions1; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | delete s1; |
---|
281 | delete s2; |
---|
282 | } |
---|
283 | aw_closestatus(); |
---|
284 | if (error){ |
---|
285 | GB_abort_transaction(gb_merge); |
---|
286 | GB_abort_transaction(gb_dest); |
---|
287 | aw_message(error); |
---|
288 | error = 0; |
---|
289 | }else{ |
---|
290 | GB_commit_transaction(gb_merge); |
---|
291 | GB_commit_transaction(gb_dest); |
---|
292 | } |
---|
293 | delete tag; |
---|
294 | delete source; |
---|
295 | delete dest; |
---|
296 | delete exclude; |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | AW_window *create_mg_check_fields(AW_root *aw_root){ |
---|
301 | AW_window_simple *aws = 0; |
---|
302 | |
---|
303 | aw_root->awar_string(AWAR_SOURCE_FIELD); |
---|
304 | aw_root->awar_string(AWAR_DEST_FIELD,"tmp",AW_ROOT_DEFAULT); |
---|
305 | aw_root->awar_string(AWAR_EXCLUDE,".-",AW_ROOT_DEFAULT); |
---|
306 | aw_root->awar_string(AWAR_ETAG,""); |
---|
307 | aw_root->awar_int(AWAR_TOUPPER); |
---|
308 | aw_root->awar_int(AWAR_CORRECT); |
---|
309 | |
---|
310 | aws = new AW_window_simple; |
---|
311 | aws->init( aw_root, "MERGE_COMPARE_FIELDS","COMPARE DATABASE FIELDS", 600, 0 ); |
---|
312 | aws->load_xfig("merge/seqcheck.fig"); |
---|
313 | |
---|
314 | aws->callback( (AW_CB0)AW_POPDOWN); |
---|
315 | aws->create_button("CLOSE","CLOSE","C"); |
---|
316 | |
---|
317 | aws->at("help"); |
---|
318 | aws->callback(AW_POPUP_HELP,(AW_CL)"checkfield.hlp"); |
---|
319 | aws->create_button("HELP","HELP","H"); |
---|
320 | |
---|
321 | |
---|
322 | aws->at("exclude"); |
---|
323 | aws->create_input_field(AWAR_EXCLUDE); |
---|
324 | |
---|
325 | aws->at("toupper"); |
---|
326 | aws->create_toggle(AWAR_TOUPPER); |
---|
327 | |
---|
328 | aws->at("correct"); |
---|
329 | aws->create_toggle(AWAR_CORRECT); |
---|
330 | |
---|
331 | aws->at("tag"); |
---|
332 | aws->create_input_field(AWAR_ETAG,6); |
---|
333 | |
---|
334 | awt_create_selection_list_on_scandb(gb_dest,aws,AWAR_SOURCE_FIELD, |
---|
335 | AWT_STRING_FILTER, "source",0); |
---|
336 | |
---|
337 | awt_create_selection_list_on_scandb(gb_dest,aws,AWAR_DEST_FIELD, |
---|
338 | (1<<GB_STRING)|(1<<GB_INT), "dest",0); |
---|
339 | |
---|
340 | |
---|
341 | aws->at("go"); |
---|
342 | aws->highlight(); |
---|
343 | aws->callback(mg_check_field_cb); |
---|
344 | aws->create_button("GO","GO"); |
---|
345 | |
---|
346 | return (AW_window *)aws; |
---|
347 | } |
---|