ARB
ad_config.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : ad_config.cxx //
4 // Purpose : editor configurations (aka species selections) //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in May 2005 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // =============================================================== //
11 
12 #include "gb_local.h"
13 
14 #include <ad_config.h>
15 #include <arbdbt.h>
16 
17 #include <arb_defs.h>
18 #include <arb_strarray.h>
19 
21  /* returns existing configurations in 'configNames'
22  * Note: automatically generates names for configs w/o legal name.
23  */
24 
25  GB_transaction ta(gb_main);
26  GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_CREATE_CONTAINER);
27 
28  if (gb_config_data) {
29  int unnamed_count = 0;
30 
31  configNames.reserve(GB_number_of_subentries(gb_config_data));
32 
33  for (GBDATA *gb_config = GB_entry(gb_config_data, CONFIG_ITEM);
34  gb_config;
35  gb_config = GB_nextEntry(gb_config))
36  {
37  const char *name = GBT_read_char_pntr(gb_config, "name");
38 
39  if (!name || name[0] == 0) { // no name or empty name
40  char *new_name = GBS_global_string_copy("<unnamed%i>", ++unnamed_count);
41  GB_ERROR error = GBT_write_string(gb_config, "name", new_name);
42 
43  if (error) {
44  GB_warningf("Failed to rename unnamed configuration to '%s'", new_name);
45  freenull(new_name);
46  name = NULp;
47  }
48  else {
49  name = GBT_read_char_pntr(gb_config, "name");
50  }
51  }
52 
53  if (name) configNames.put(name);
54  }
55  }
56 }
57 
59  GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_DB);
60  GBDATA *gb_config_name = GB_find_string(gb_config_data, "name", name, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
61  return gb_config_name ? GB_get_father(gb_config_name) : NULp;
62 }
63 
64 static GBDATA *findOrCreate_configuration(GBDATA *gb_main, const char *name) {
65  GBDATA *gb_config = GBT_find_configuration(gb_main, name);
66  if (!gb_config) {
67  GBDATA *gb_config_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_DB);
68 
69  gb_config = GB_create_container(gb_config_data, CONFIG_ITEM); // create new container
70  if (gb_config) {
71  GB_ERROR error = GBT_write_string(gb_config, "name", name);
72  if (error) GB_export_error(error);
73  }
74  }
75  return gb_config;
76 }
77 
79  GB_transaction ta(gb_main);
80  GBDATA *gb_config = GBT_find_configuration(gb_main, name);
81 
82  error = NULp;
83  if (!gb_config) {
84  error = GBS_global_string("No such configuration '%s'", name);
85  top_area = NULp;
86  middle_area = NULp;
87  comment = NULp;
88  }
89  else {
90  top_area = GBT_read_string(gb_config, "top_area");
91  middle_area = GBT_read_string(gb_config, "middle_area");
92 
93  if (!top_area || !middle_area) {
94  error = GBS_global_string("Configuration '%s' is corrupted (Reason: %s)", name, GB_await_error());
95  }
96 
97  comment = GBT_read_string(gb_config, "comment");
98  }
99 }
100 
101 GB_ERROR GBT_config::saveAsOver(GBDATA *gb_main, const char *name, const char *oldName, bool warnIfSavingDefault) const {
105  GB_ERROR error = NULp;
106 
107  GB_push_transaction(gb_main);
108 
109  GBDATA *gb_config = findOrCreate_configuration(gb_main, oldName);
110  if (!gb_config) {
111  error = GBS_global_string("Can't create configuration '%s' (Reason: %s)", oldName, GB_await_error());
112  }
113  else {
114  if (strcmp(name, oldName) != 0) error = GBT_write_string(gb_config, "name", name);
115 
116  error = GBT_write_string(gb_config, "top_area", top_area);
117  if (!error) error = GBT_write_string(gb_config, "middle_area", middle_area);
118 
119  if (!error) {
120  if (comment && comment[0]) { // non-empty
121  error = GBT_write_string(gb_config, "comment", comment);
122  }
123  else {
124  GBDATA *gb_comment = GB_entry(gb_config, "comment");
125  if (gb_comment) error = GB_delete(gb_comment); // delete field if comment empty
126  }
127  }
128 
129  if (error) error = GBS_global_string("%s (in configuration '%s')", error, name);
130  }
131 
132  if (warnIfSavingDefault && strcmp(name, DEFAULT_CONFIGURATION) == 0) {
133  GBT_message(gb_main, "Note: You saved the '" DEFAULT_CONFIGURATION "'.\nStarting ARB_EDIT4 will probably overwrite it!");
134  }
135 
136  return GB_end_transaction(gb_main, error);
137 }
138 
140  error = NULp;
141 
142  freenull(item.name);
143  item.type = CI_END_OF_CONFIG;
144 
145  if (config_string[parse_pos]) { // if not at 0-byte
146  char label = config_string[parse_pos+1];
147  item.type = CI_UNKNOWN;
148 
149  switch (label) {
150  case 'L': item.type = CI_SPECIES; break;
151  case 'S': item.type = CI_SAI; break;
152  case 'F': item.type = CI_FOLDED_GROUP; break;
153  case 'G': item.type = CI_GROUP; break;
154  case 'E': item.type = CI_CLOSE_GROUP; break;
155  default: item.type = CI_UNKNOWN; break;
156  }
157 
158  if (item.type == CI_CLOSE_GROUP) {
159  parse_pos += 2;
160  }
161  else {
162  const char *start_of_name = config_string+parse_pos+2;
163  const char *behind_name = strchr(start_of_name, '\1');
164 
165  if (!behind_name) behind_name = strchr(start_of_name, '\0'); // eos
166  gb_assert(behind_name);
167 
168  char *data = ARB_strpartdup(start_of_name, behind_name-1);
169  if (item.type == CI_UNKNOWN) {
170  error = GBS_global_string_copy("Unknown flag '%c' (followed by '%s')", label, data);
171  free(data);
172  }
173  else {
174  item.name = data;
175  parse_pos = behind_name-config_string;
176  }
177  }
178 
179  if (error) { // stop parser
180  const char *end_of_config = strchr(config_string+parse_pos, '\0');
181  parse_pos = end_of_config-config_string;
182  gb_assert(config_string[parse_pos] == 0);
183  }
184  }
185 
186  return item;
187 }
188 
191 
192  char prefix[] = "\1?";
193  if (type == CI_CLOSE_GROUP) {
194  prefix[1] = 'E';
195  out.cat(prefix);
196  }
197  else {
198  char label = 0;
199  switch (type) {
200  case CI_SPECIES: label = 'L'; break;
201  case CI_SAI: label = 'S'; break;
202  case CI_GROUP: label = 'G'; break;
203  case CI_FOLDED_GROUP: label = 'F'; break;
204 
205  default: gb_assert(0); break;
206  }
207  prefix[1] = label;
208  out.cat(prefix);
209  out.cat(name);
210  }
211 }
212 
213 // --------------------------------------------------------------------------------
214 
215 #ifdef UNIT_TESTS
216 #include <test_unit.h>
217 
218 void TEST_GBT_get_configuration_names() {
219  GB_shell shell;
220  GBDATA *gb_main = GB_open("nosuch.arb", "c");
221 
222  {
223  GB_transaction ta(gb_main);
224 
225  const char *configs[] = { "arb", "BASIC", "Check it", "dummy" };
226  for (size_t i = 0; i<ARRAY_ELEMS(configs); ++i) {
227  TEST_EXPECT_RESULT__NOERROREXPORTED(findOrCreate_configuration(gb_main, configs[i]));
228  }
229 
230  ConstStrArray cnames;
231  GBT_get_configuration_names(cnames, gb_main);
232 
233  TEST_EXPECT_EQUAL(cnames.size(), 4U);
234  TEST_EXPECT_STRARRAY_CONTAINS(cnames, '*', "arb*BASIC*Check it*dummy");
235  }
236 
237  GB_close(gb_main);
238 }
239 
240 #endif // UNIT_TESTS
241 
242 
static GBDATA * findOrCreate_configuration(GBDATA *gb_main, const char *name)
Definition: ad_config.cxx:64
const char * GB_ERROR
Definition: arb_core.h:25
GBDATA * GB_open(const char *path, const char *opent)
Definition: ad_load.cxx:1363
void put(const char *elem)
Definition: arb_strarray.h:188
size_t size() const
Definition: arb_strarray.h:85
GBDATA * GB_nextEntry(GBDATA *entry)
Definition: adquery.cxx:339
GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error)
Definition: arbdb.cxx:2561
#define TEST_EXPECT_STRARRAY_CONTAINS(strings, separator, expected)
Definition: test_unit.h:1338
#define CONFIG_ITEM
Definition: ad_config.h:28
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
void cat(const char *from)
Definition: arb_strbuf.h:199
char * ARB_strpartdup(const char *start, const char *end)
Definition: arb_string.h:51
#define ARRAY_ELEMS(array)
Definition: arb_defs.h:19
GBDATA * GB_get_father(GBDATA *gbd)
Definition: arbdb.cxx:1722
GB_ERROR GB_push_transaction(GBDATA *gbd)
Definition: arbdb.cxx:2494
#define DEFAULT_CONFIGURATION
Definition: ad_config.h:30
void append_to_config_string(GBS_strstruct &out) const
Definition: ad_config.cxx:189
GB_ERROR GB_delete(GBDATA *&source)
Definition: arbdb.cxx:1916
GB_ERROR GB_export_error(const char *error)
Definition: arb_msg.cxx:257
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
GBDATA * GB_create_container(GBDATA *father, const char *key)
Definition: arbdb.cxx:1829
char * GBT_read_string(GBDATA *gb_container, const char *fieldpath)
Definition: adtools.cxx:267
Definition: arbdb.h:78
void GB_warningf(const char *templat,...)
Definition: arb_msg.cxx:536
long GB_number_of_subentries(GBDATA *gbd)
Definition: arbdb.cxx:2892
GBT_CONFIG_ITEM_TYPE type
Definition: ad_config.h:83
#define CONFIG_DATA_PATH
Definition: ad_config.h:27
static void error(const char *msg)
Definition: mkptypes.cxx:96
GB_ERROR saveAsOver(GBDATA *gb_main, const char *name, const char *oldName, bool warnIfSavingDefault) const
Definition: ad_config.cxx:101
GBDATA * GBT_find_configuration(GBDATA *gb_main, const char *name)
Definition: ad_config.cxx:58
#define gb_assert(cond)
Definition: arbdbt.h:11
GB_ERROR GBT_write_string(GBDATA *gb_container, const char *fieldpath, const char *content)
Definition: adtools.cxx:451
const GBT_config_item & nextItem(GB_ERROR &error)
Definition: ad_config.cxx:139
void GBT_message(GBDATA *gb_main, const char *msg)
Definition: adtools.cxx:238
GBDATA * GB_find_string(GBDATA *gbd, const char *key, const char *str, GB_CASE case_sens, GB_SEARCH_TYPE gbs)
Definition: adquery.cxx:302
void GBT_get_configuration_names(ConstStrArray &configNames, GBDATA *gb_main)
Definition: ad_config.cxx:20
#define NULp
Definition: cxxforward.h:116
void reserve(size_t forElems)
Definition: arb_strarray.h:82
GB_transaction ta(gb_var)
GBDATA * gb_main
Definition: adname.cxx:32
GBDATA * GB_search(GBDATA *gbd, const char *fieldpath, GB_TYPES create)
Definition: adquery.cxx:531
const char * GBT_read_char_pntr(GBDATA *gb_container, const char *fieldpath)
Definition: adtools.cxx:307
#define TEST_EXPECT_EQUAL(expr, want)
Definition: test_unit.h:1294
GBT_config()
Definition: ad_config.h:41
GBDATA * GB_entry(GBDATA *father, const char *key)
Definition: adquery.cxx:334
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:194
void GB_close(GBDATA *gbd)
Definition: arbdb.cxx:655
static Score ** U
Definition: align.cxx:67
const char * label