1 | // ==================================================================== // |
---|
2 | // // |
---|
3 | // File : aw_question.cpp // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // // |
---|
7 | // Coded by Ralf Westram (coder@reallysoft.de) in January 2002 // |
---|
8 | // Copyright Department of Microbiology (Technical University Munich) // |
---|
9 | // // |
---|
10 | // Visit our web site at: http://www.arb-home.de/ // |
---|
11 | // // |
---|
12 | // // |
---|
13 | // ==================================================================== // |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <string.h> |
---|
17 | #include <arbdb.h> |
---|
18 | #include <aw_question.hxx> |
---|
19 | |
---|
20 | using namespace std; |
---|
21 | |
---|
22 | |
---|
23 | // start of implementation of class AW_repeated_question: |
---|
24 | |
---|
25 | void AW_repeated_question::add_help(const char *help_file) { |
---|
26 | freedup(helpfile, help_file); |
---|
27 | } |
---|
28 | |
---|
29 | int AW_repeated_question::get_answer(const char *question, const char *buttons, const char *to_all, bool add_abort) |
---|
30 | { |
---|
31 | if (!buttons_used) { |
---|
32 | buttons_used = strdup(buttons); |
---|
33 | } |
---|
34 | else { |
---|
35 | // do not use the same instance of AW_repeated_question with different buttons! |
---|
36 | assert_or_exit(strcmp(buttons_used, buttons) == 0); |
---|
37 | } |
---|
38 | |
---|
39 | if (answer == -1 || dont_ask_again == false) { |
---|
40 | |
---|
41 | char *all = GBS_global_string_copy(" (%s)", to_all); |
---|
42 | int all_len = strlen(all); |
---|
43 | size_t but_len = strlen(buttons); |
---|
44 | size_t new_buttons_len = but_len*3+1+(add_abort ? 6 : 0)+all_len*3; |
---|
45 | char *new_buttons = (char*)malloc(new_buttons_len); |
---|
46 | int button_count = 0; // number of buttons in 'buttons' |
---|
47 | |
---|
48 | { // transform "YES,NO" -> "YES,YES (to_all),^NO,NO (to_all)" or "YES (to_all),NO (to_all)" |
---|
49 | char *w = new_buttons; |
---|
50 | const char *r = buttons; |
---|
51 | |
---|
52 | while (1) { |
---|
53 | const char *komma = strchr(r, ','); |
---|
54 | if (!komma) komma = strchr(r, 0); |
---|
55 | int len = komma-r; |
---|
56 | |
---|
57 | if (!dont_ask_again) { |
---|
58 | if (w>new_buttons) *w++ = '^'; // not in front of first button |
---|
59 | memcpy(w, r, len); w += len; |
---|
60 | *w++ = ','; |
---|
61 | } |
---|
62 | memcpy(w, r, len); w += len; |
---|
63 | memcpy(w, all, all_len); w += all_len; |
---|
64 | *w++ = ','; |
---|
65 | |
---|
66 | button_count++; |
---|
67 | |
---|
68 | if (!komma[0]) break; |
---|
69 | r = komma+1; |
---|
70 | } |
---|
71 | if (add_abort) { |
---|
72 | const char *abort = "^ABORT"; |
---|
73 | strcpy(w, abort); w += strlen(abort); |
---|
74 | } |
---|
75 | else { |
---|
76 | --w; // delete komma at end |
---|
77 | } |
---|
78 | w[0] = 0; |
---|
79 | |
---|
80 | aw_assert(size_t(w-new_buttons) < new_buttons_len); // oops buffer overflow |
---|
81 | |
---|
82 | free(all); |
---|
83 | } |
---|
84 | |
---|
85 | int user_answer = aw_question(question, new_buttons, true, helpfile); |
---|
86 | |
---|
87 | if (dont_ask_again) { // ask question as normal when called first (dont_ask_again later) |
---|
88 | answer = user_answer; |
---|
89 | } |
---|
90 | else { |
---|
91 | answer = user_answer/2; |
---|
92 | dont_ask_again = (user_answer%2) || (user_answer == (button_count*2)); |
---|
93 | } |
---|
94 | |
---|
95 | free(new_buttons); |
---|
96 | |
---|
97 | aw_assert(answer<(button_count+(add_abort ? 1 : 0))); |
---|
98 | } |
---|
99 | |
---|
100 | aw_assert(answer != -1); |
---|
101 | |
---|
102 | return answer; |
---|
103 | } |
---|
104 | |
---|
105 | // -end- of implementation of class AW_repeated_question. |
---|
106 | |
---|