source: tags/arb_5.5/SECEDIT/SEC_toggle.cxx

Last change on this file was 5854, checked in by westram, 15 years ago
  • followup to [5853] (more to follow)
    • GB_get_error() → GB_await_error() where appropriate (EDIT4, SECEDIT)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1// ================================================================= //
2//                                                                   //
3//   File      : SEC_toggle.cxx                                      //
4//   Purpose   :                                                     //
5//                                                                   //
6//   Coded by Ralf Westram (coder@reallysoft.de) in September 2007   //
7//   Institute of Microbiology (Technical University Munich)         //
8//   http://www.arb-home.de/                                         //
9//                                                                   //
10// ================================================================= //
11
12#include "SEC_toggle.hxx"
13#include "SEC_graphic.hxx"
14#include "SEC_defs.hxx"
15
16#include <arbdbt.h>
17
18#include <cstdlib>
19#include <climits>
20
21
22using namespace std;
23
24GB_ERROR SEC_structure_toggler::store(GBDATA *gb_struct) {
25    char     *data    = 0;
26    char     *xstring = 0;
27    GB_ERROR  error   = gfx->read_data_from_db(&data, &xstring);
28
29    if (!error) error = GBT_write_string(gb_struct, "data", data);
30    if (!error) error = GBT_write_string(gb_struct, "ref", xstring);
31
32    free(xstring);
33    free(data);
34
35    return error;
36}
37
38GB_ERROR SEC_structure_toggler::restore(GBDATA *gb_struct) {
39    char     *data    = 0;
40    char     *xstring = 0;
41    GB_ERROR  error   = 0;
42
43    GBDATA *gb_data   = GB_search(gb_struct, "data", GB_FIND);
44    if (gb_data) data = GB_read_string(gb_data);
45    if (!data) error  = GB_await_error();
46
47    if (!error) {
48        GBDATA *gb_ref      = GB_search(gb_struct, "ref", GB_FIND);
49        if (gb_ref) xstring = GB_read_string(gb_ref);
50        if (!xstring) error = GB_await_error();
51    }
52
53    if (!error) {
54        sec_assert(data && xstring);
55        error = gfx->write_data_to_db(data, xstring);
56    }
57
58    free(xstring);
59    free(data);
60    return error;
61}
62
63int SEC_structure_toggler::current() {
64    return *GBT_read_int(gb_structures, "current");
65}
66
67GB_ERROR SEC_structure_toggler::set_current(int idx) {
68    GBDATA   *gb_num = GB_search(gb_structures, "current", GB_INT);
69    GB_ERROR  error;
70
71    if (!gb_num) error = GB_await_error();
72    else {
73        sec_assert(find(idx));                      // oops - nonexisting container
74        error = GB_write_int(gb_num, idx);
75    }
76    return error;
77}
78
79GBDATA *SEC_structure_toggler::find(int num) {
80    int     cnt      = 0;
81    GBDATA *gb_found = GB_entry(gb_structures, "struct");
82    while (gb_found && num>0) {
83        cnt++;
84        num--;
85        gb_found = GB_nextEntry(gb_found);
86    }
87    if (!gb_found) Count = cnt;  // seen all -> set count
88    return gb_found;
89}
90
91GBDATA *SEC_structure_toggler::create(const char *structure_name) {
92    sec_assert(!st_error);
93    if (st_error) return 0;
94
95    GBDATA *gb_new        = GB_create_container(gb_structures, "struct");
96    if (!gb_new) st_error = GB_await_error();
97    else {
98        st_error = setName(gb_new, structure_name);
99
100        if (!st_error) st_error = store(gb_new);
101        if (!st_error) st_error = set_current(Count);
102        if (!st_error) {
103            gb_current = gb_new;
104            sec_assert(find(current()) == gb_current);
105            Count++;
106        }
107    }
108
109    return gb_new;
110}
111
112// --------------------------------------------------------------------------------
113// public
114
115SEC_structure_toggler::SEC_structure_toggler(GBDATA *gb_main, const char *ali_name, SEC_graphic *Gfx)
116    : gfx(Gfx)
117    , st_error(0)
118    , Count(0)
119{
120    GB_transaction ta(gb_main);
121    gb_structures = GB_search(gb_main, GBS_global_string("secedit/structs/%s", ali_name), GB_CREATE_CONTAINER);
122    if (!gb_structures) {
123        st_error   = GB_await_error();
124        gb_current = 0;
125    }
126    else {
127        find(INT_MAX); // sets Count
128        if (Count == 0) { // init
129            gb_current = create(ali_name);
130            st_error   = set_current(0);
131        }
132        else {
133            int curr = current();
134            if (curr<Count) {
135                gb_current = find(current());
136            }
137            else { // wrong -> reset
138                st_error   = set_current(0);
139                gb_current = find(0);
140            }
141        }
142        sec_assert(gb_current);
143    }
144}
145
146GB_ERROR SEC_structure_toggler::next() {
147    GB_ERROR       error = 0;
148    GB_transaction ta(gb_structures);
149   
150    if (Count<2) {
151        error = "No other structure in DB";
152    }
153    else {
154        int nextNum = current()+1;
155        if (nextNum >= Count) nextNum = 0;
156
157        sec_assert(find(current()) == gb_current);
158
159        error             = store(gb_current);
160        if (!error) error = set_current(nextNum);
161        if (!error) {
162            gb_current = find(nextNum);
163            if (!gb_current) {
164                error = GBS_global_string("Failed to find structure #%i", nextNum);
165            }
166            else {
167                error = restore(gb_current);
168            }
169        }
170    }
171    return ta.close(error);
172}
173
174GB_ERROR SEC_structure_toggler::copyTo(const char *structure_name) {
175    GB_ERROR error = 0;
176    GB_transaction ta(gb_structures);
177
178    sec_assert(find(current()) == gb_current);
179   
180    error = store(gb_current);
181
182    if (!error) {
183        GBDATA *gb_new = create(structure_name);
184        if (!gb_new) {
185            sec_assert(st_error);
186            error = st_error;
187        }
188        else {
189            gb_current = gb_new;
190        }
191    }
192    sec_assert(error || (find(current()) == gb_current));
193    return ta.close(error);
194}
195
196GB_ERROR SEC_structure_toggler::remove() {
197    GB_ERROR       error = 0;
198    GB_transaction ta(gb_structures);
199
200    sec_assert(Count > 1);
201
202    GBDATA *gb_del = gb_current;
203    int     del    = current();
204       
205    error = next();
206
207    if (!error) {
208        int curr = current();
209        error    = GB_delete(gb_del);
210        if (!error) {
211            Count--;
212            if (curr >= del) error = set_current(curr-1);
213        }
214    }
215    return ta.close(error);
216}
217
218const char *SEC_structure_toggler::name() {
219    const char     *structure_name = 0;
220    GB_transaction  ta(gb_structures);
221
222    GBDATA *gb_name               = GB_search(gb_current, "name", GB_FIND);
223    if (gb_name) structure_name   = GB_read_char_pntr(gb_name);
224    if (!structure_name) st_error = GB_await_error();
225
226    st_error = ta.close(st_error);
227    return structure_name;
228}
229
230GB_ERROR SEC_structure_toggler::setName(GBDATA *gb_struct, const char *new_name) {
231    return GBT_write_string(gb_struct, "name", new_name);
232}
233
234GB_ERROR SEC_structure_toggler::setName(const char *new_name) {
235    GB_transaction ta(gb_structures);
236    GB_ERROR       error = setName(gb_current, new_name);
237    return ta.close(error);
238}
239
Note: See TracBrowser for help on using the repository browser.