ARB
adperl.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : adperl.cxx //
4 // Purpose : helper functions used by perl interface //
5 // (see ../PERL2ARB) //
6 // //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // =============================================================== //
11 
12 #include "gb_local.h"
13 #include "adperl.h"
14 
15 // used by perl interface, see ../PERL2ARB/ARB_ext.c@GBP_croak_function
16 void (*GBP_croak_function)(const char *message) = NULp;
17 
18 static void die(const char *with_last_words) {
19  // raise exception in caller (assuming caller is a perl script)
20 
21  if (GBP_croak_function) {
22  GBP_croak_function(with_last_words);
23  }
24  else {
25  fputs("Warning: GBP_croak_function undefined. terminating..\n", stderr);
26  GBK_terminate(with_last_words);
27  }
28 }
29 
32 }
33 
34 // -------------------------------------------
35 // "generic" enum<->string-conversion
36 
37 union known_enum {
38  int as_int;
39 
45 };
46 
47 #define ILLEGAL_VALUE (-666)
48 
49 
50 typedef const char *(*enum2string)(known_enum enumValue);
51 
53  known_enum enumValue, lookupLimit;
54 
55  enumValue.as_int = greaterThan.as_int+1;
56  lookupLimit.as_int = enumValue.as_int+256;
57 
58  while (enumValue.as_int <= lookupLimit.as_int) {
59  const char *valueExists = lookup(enumValue);
60  if (valueExists) return enumValue;
61  enumValue.as_int++;
62  }
63 
64  enumValue.as_int = ILLEGAL_VALUE;
65  return enumValue;
66 }
67 
68 static known_enum first_known_enum_value(known_enum greaterEqualThan, enum2string lookup) {
69  return (lookup(greaterEqualThan))
70  ? greaterEqualThan
71  : next_known_enum_value(greaterEqualThan, lookup);
72 }
73 
74 static known_enum string2enum(const char *string, enum2string lookup, known_enum start) {
75 
76  for (start = first_known_enum_value(start, lookup);
77  start.as_int != ILLEGAL_VALUE;
78  start = next_known_enum_value(start, lookup))
79  {
80  const char *asString = lookup(start);
81  gb_assert(asString);
82  if (strcasecmp(asString, string) == 0) break; // found
83  }
84  return start;
85 }
86 
88  char *allowed = NULp;
89 
90  for (start = first_known_enum_value(start, lookup);
91  start.as_int != ILLEGAL_VALUE;
92  start = next_known_enum_value(start, lookup))
93  {
94  const char *asString = lookup(start);
95  gb_assert(asString);
96 
97  if (allowed) freeset(allowed, GBS_global_string_copy("%s, '%s'", allowed, asString));
98  else allowed = GBS_global_string_copy("'%s'", asString);
99  }
100 
101  if (!allowed) allowed = ARB_strdup("none (this is a bug)");
102 
103  return allowed;
104 }
105 
106 static known_enum string2enum_or_die(const char *enum_name, const char *string, enum2string lookup, known_enum start) {
107  known_enum found = string2enum(string, lookup, start);
108 
109  if (found.as_int == ILLEGAL_VALUE) {
110  char *allowed_values = buildAllowedValuesString(start, lookup);
111  char *usage = GBS_global_string_copy("Error: value '%s' is not a legal %s\n"
112  "Known %ss are: %s",
113  string, enum_name, enum_name, allowed_values);
114  free(allowed_values);
115  die(usage);
116  }
117 
118  return found;
119 }
120 
121 /* --------------------------------------------------------------------------------
122  * conversion declarations for different used enums
123  *
124  * To add a new enum type
125  * - write a function to convert your enum-values into a string (example: GBP_gb_search_types_to_string)
126  * - write a reverse-wrapper (example: GBP_string_to_gb_search_types)
127  *
128  * [Code-Tag: enum_conversion_functions]
129  * see also ../PERLTOOLS/arb_proto_2_xsub.cxx@enum_type_replacement
130  */
131 
132 // ------------------------
133 // GB_SEARCH_TYPE
134 
136  switch (search_type) {
137  case SEARCH_BROTHER: return "brother";
138  case SEARCH_CHILD: return "child";
139  case SEARCH_GRANDCHILD: return "grandchild";
140  case SEARCH_NEXT_BROTHER: return "next_brother";
141  case SEARCH_CHILD_OF_NEXT: return "child_of_next";
142  }
143 
144  return NULp;
145 }
146 
148  known_enum start; start.as_int = 0;
149  known_enum found = string2enum_or_die("search-type", search_mode, CASTSIG(enum2string, GBP_GB_SEARCH_TYPE_2_charPtr), start);
150  return found.search_type;
151 }
152 
153 // ------------------
154 // GB_TYPES
155 
157  switch (type) {
158  case GB_NONE: return "NONE";
159  case GB_BIT: return "BIT";
160  case GB_BYTE: return "BYTE";
161  case GB_INT: return "INT";
162  case GB_FLOAT: return "FLOAT";
163  case GB_BITS: return "BITS";
164  case GB_BYTES: return "BYTES";
165  case GB_INTS: return "INTS";
166  case GB_FLOATS: return "FLOATS";
167  case GB_STRING: return "STRING";
168  case GB_DB: return "CONTAINER";
169 
170  default: break;
171  }
172  return NULp;
173 }
174 
175 GB_TYPES GBP_charPtr_2_GB_TYPES(const char *type_name) {
176  known_enum start; start.as_int = 0;
177  known_enum found = string2enum_or_die("db-type", type_name, CASTSIG(enum2string, GBP_GB_TYPES_2_charPtr), start);
178  return found.db_type;
179 }
180 
181 
182 // ----------------------
183 // GB_UNDO_TYPE
184 
185 const char *GBP_GB_UNDO_TYPE_2_charPtr(GB_UNDO_TYPE undo_type) {
186  switch (undo_type) {
187  case GB_UNDO_UNDO: return "undo";
188  case GB_UNDO_REDO: return "redo";
189 
190  case GB_UNDO_NONE:
191  case GB_UNDO_KILL:
192  case GB_UNDO_UNDO_REDO:
193  break;
194  }
195  return NULp;
196 }
197 
198 GB_UNDO_TYPE GBP_charPtr_2_GB_UNDO_TYPE(const char *undo_type) {
199  known_enum start; start.as_int = 0;
200  known_enum found = string2enum_or_die("undo-type", undo_type, CASTSIG(enum2string, GBP_GB_UNDO_TYPE_2_charPtr), start);
201  return found.undo_type;
202 }
203 
204 
205 // -----------------
206 // GB_CASE
207 
208 const char *GBP_GB_CASE_2_charPtr(GB_CASE sensitivity) {
209  switch (sensitivity) {
210  case GB_IGNORE_CASE: return "ignore_case";
211  case GB_MIND_CASE: return "mind_case";
212  case GB_CASE_UNDEFINED: return "case_undef";
213  }
214  return NULp;
215 }
216 
217 GB_CASE GBP_charPtr_2_GB_CASE(const char *sensitivity) {
218  known_enum start; start.as_int = 0;
219  known_enum found = string2enum_or_die("sensitivity", sensitivity, CASTSIG(enum2string, GBP_GB_CASE_2_charPtr), start);
220  return found.case_sensitivity;
221 }
222 
223 // ---------------------------
224 // GB_alignment_type
225 
227  switch (ali_type) {
228  case GB_AT_RNA: return "RNA";
229  case GB_AT_DNA: return "DNA";
230  case GB_AT_AA: return "AMINO";
231 
232  case GB_AT_UNKNOWN: break;
233  }
234  return NULp;
235 }
236 
238  known_enum start; start.as_int = 0;
239  known_enum found = string2enum_or_die("alignment-type", ali_type, CASTSIG(enum2string, GBP_GB_alignment_type_2_charPtr), start);
240  return found.ali_type;
241 }
242 
243 // -----------------------------------------
244 // wrap functions moved to CORE lib
245 //
246 // As long as CORE lib is not xsub'd, we use wrappers for some
247 // functions used in perl
248 
250  return GB_await_error();
251 }
252 
253 // --------------------
254 // perl panic
255 
257  // called from die() in ../PERL2ARB/ARB.pm@prepare_to_die
259 }
const char * GB_ERROR
Definition: arb_core.h:25
GB_TYPES type
static char * buildAllowedValuesString(known_enum start, enum2string lookup)
Definition: adperl.cxx:87
Definition: arbdb.h:65
int as_int
Definition: adperl.cxx:38
Definition: arbdb.h:69
GB_CASE case_sensitivity
Definition: adperl.cxx:41
const char * GBP_GB_SEARCH_TYPE_2_charPtr(GB_SEARCH_TYPE search_type)
Definition: adperl.cxx:135
GB_alignment_type ali_type
Definition: adperl.cxx:44
GB_TYPES db_type
Definition: adperl.cxx:42
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
static known_enum string2enum(const char *string, enum2string lookup, known_enum start)
Definition: adperl.cxx:74
const char * GBP_GB_TYPES_2_charPtr(GB_TYPES type)
Definition: adperl.cxx:156
#define CASTSIG(sig, cb)
Definition: arbtools.h:120
const char * GBP_GB_CASE_2_charPtr(GB_CASE sensitivity)
Definition: adperl.cxx:208
static HelixNrInfo * start
GB_UNDO_TYPE
Definition: arbdb.h:107
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
GB_alignment_type GBP_charPtr_2_GB_alignment_type(const char *ali_type)
Definition: adperl.cxx:237
Definition: arbdb.h:67
Definition: arbdb.h:78
void GBK_terminate(const char *error) __ATTR__NORETURN
Definition: arb_msg.cxx:463
void GBP_prepare_to_die()
Definition: adperl.cxx:256
Definition: arbdb.h:64
void message(char *errortext)
GB_ERROR GBC_await_error()
Definition: adperl.cxx:249
GB_SEARCH_TYPE search_type
Definition: adperl.cxx:40
GB_UNDO_TYPE undo_type
Definition: adperl.cxx:43
GB_SEARCH_TYPE
Definition: arbdb.h:99
GB_CASE
Definition: arb_core.h:30
GB_alignment_type
Definition: arbdb_base.h:61
~GB_shell4perl()
Definition: adperl.cxx:30
void(* GBP_croak_function)(const char *message)
Definition: adperl.cxx:16
Definition: arbdb.h:72
fputs(TRACE_PREFIX, stderr)
Definition: arbdb.h:63
#define gb_assert(cond)
Definition: arbdbt.h:11
static void die(const char *with_last_words)
Definition: adperl.cxx:18
static known_enum next_known_enum_value(known_enum greaterThan, enum2string lookup)
Definition: adperl.cxx:52
const char *(* enum2string)(known_enum enumValue)
Definition: adperl.cxx:50
#define NULp
Definition: cxxforward.h:97
const char * GBP_GB_alignment_type_2_charPtr(GB_alignment_type ali_type)
Definition: adperl.cxx:226
GB_UNDO_TYPE GBP_charPtr_2_GB_UNDO_TYPE(const char *undo_type)
Definition: adperl.cxx:198
GB_TYPES
Definition: arbdb.h:62
static known_enum first_known_enum_value(known_enum greaterEqualThan, enum2string lookup)
Definition: adperl.cxx:68
#define ILLEGAL_VALUE
Definition: adperl.cxx:47
Definition: arbdb.h:71
void usage()
Definition: readseq.c:328
GB_TYPES GBP_charPtr_2_GB_TYPES(const char *type_name)
Definition: adperl.cxx:175
void gb_abort_and_close_all_DBs()
Definition: arbdb.cxx:680
const char * GBP_GB_UNDO_TYPE_2_charPtr(GB_UNDO_TYPE undo_type)
Definition: adperl.cxx:185
GB_CASE GBP_charPtr_2_GB_CASE(const char *sensitivity)
Definition: adperl.cxx:217
GB_SEARCH_TYPE GBP_charPtr_2_GB_SEARCH_TYPE(const char *search_mode)
Definition: adperl.cxx:147
static known_enum string2enum_or_die(const char *enum_name, const char *string, enum2string lookup, known_enum start)
Definition: adperl.cxx:106
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:195
Definition: arbdb.h:66