ARB
global.h
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : global.h //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #ifndef GLOBAL_H
12 #define GLOBAL_H
13 
14 #ifndef ARB_ASSERT_H
15 #include <arb_assert.h>
16 #endif
17 #ifndef ARB_DEFS_H
18 #include <arb_defs.h>
19 #endif
20 #ifndef ARBTOOLS_H
21 #include <arbtools.h>
22 #endif
23 #ifndef SMARTPTR_H
24 #include <smartptr.h>
25 #endif
26 #ifndef ARB_STRING_H
27 #include <arb_string.h>
28 #endif
29 #ifndef ARB_GLOBAL_DEFS_H
30 #include <arb_global_defs.h>
31 #endif
32 
33 #define ca_assert(cond) arb_assert(cond)
34 
35 // --------------------
36 
38  static Convaln_exception *thrown;
39 
40  int code;
41  mutable char *msg;
42 
43 public:
44  Convaln_exception(int error_code, const char *error_msg)
45  : code(error_code),
46  msg(ARB_strdup(error_msg))
47  {
48  ca_assert(!thrown); // 2 exceptions at the same time ? very exceptional! :)
49  thrown = this;
50  }
52  : code(other.code),
53  msg(ARB_strdup(other.msg))
54  {}
57  ca_assert(thrown);
58  thrown = NULp;
59  free(msg);
60  }
61 
62  int get_code() const { return code; }
63  const char *get_msg() const { return msg; }
64  void replace_msg(const char *new_msg) const { freedup(msg, new_msg); }
65 
66  void catched() {
67  ca_assert(thrown == this);
68  thrown = NULp;
69  }
70 
71  static const Convaln_exception *exception_thrown() { return thrown; }
72 };
73 
74 // --------------------
75 
76 class Warnings : virtual Noncopyable {
77  static bool show_warnings;
78  bool old_state;
79 public:
80  static bool shown() { return show_warnings; }
81 
83  old_state = shown();
84  show_warnings = false;
85  }
86  ~Warnings() { show_warnings = old_state; }
87 };
88 
89 
90 // --------------------
91 
92 CONSTEXPR_INLINE int min(int t1, int t2) { return t1<t2 ? t1 : t2; }
93 CONSTEXPR_INLINE int max(int t1, int t2) { return t1>t2 ? t1 : t2; }
94 
95 CONSTEXPR_INLINE bool str_equal(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
96 CONSTEXPR_INLINE bool str_iequal(const char *s1, const char *s2) { return strcasecmp(s1, s2) == 0; }
97 
98 CONSTEXPR_INLINE int str0len(const char *str) {
99  return str ? strlen(str) : 0;
100 }
101 
102 inline char *strndup(const char *str, int len) {
103  char *result = ARB_alloc<char>(len+1);
104  memcpy(result, str, len);
105  result[len] = 0;
106  return result;
107 }
108 
109 CONSTEXPR_INLINE int count_spaces(const char *str) { return strspn(str, " "); }
110 
111 inline bool occurs_in(char ch, const char *in) { ca_assert(ch != 0); return strchr(in, ch); }
112 
113 CONSTEXPR_INLINE bool is_end_mark(char ch) { return ch == '.' || ch == ';'; }
114 
115 CONSTEXPR_INLINE bool is_sequence_terminator(const char *str) { return str[0] == '/' && str[1] == '/'; }
116 
117 #define WORD_SEP ",.; ?:!)]}"
118 
119 inline bool is_gapchar(char ch) { return GAP::is_import_gap(ch); }
120 inline bool is_word_char(char ch) { return !occurs_in(ch, WORD_SEP); }
121 
122 CONSTEXPR_INLINE bool has_no_content(const char *field) {
123  return !field ||
124  !field[0] ||
125  (field[0] == '\n' && !field[1]);
126 }
127 CONSTEXPR_INLINE bool has_content(const char *field) { return !has_no_content(field); }
128 
129 inline char *no_content() {
130  char *nothing = ARB_alloc<char>(2);
131  nothing[0] = '\n';
132  nothing[1] = 0;
133  return nothing;
134 }
135 
136 inline bool copy_content(char*& entry, const char *content) {
137  bool copy = has_content(content);
138  if (copy) freedup(entry, content);
139  return copy;
140 }
141 
142 // --------------------
143 
144 #define lookup_keyword(keyword,table) ___lookup_keyword(keyword, table, ARRAY_ELEMS(table))
145 
146 #else
147 #error global.h included twice
148 #endif // GLOBAL_H
149 
CONSTEXPR_INLINE int str0len(const char *str)
Definition: global.h:98
string result
CONSTEXPR_INLINE bool str_iequal(const char *s1, const char *s2)
Definition: global.h:96
bool is_import_gap(const char c)
DECLARE_ASSIGNMENT_OPERATOR(Convaln_exception)
#define ca_assert(cond)
Definition: global.h:33
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
#define WORD_SEP
Definition: global.h:117
const char * get_msg() const
Definition: global.h:63
CONSTEXPR_INLINE bool has_content(const char *field)
Definition: global.h:127
~Convaln_exception()
Definition: global.h:56
bool copy_content(char *&entry, const char *content)
Definition: global.h:136
int get_code() const
Definition: global.h:62
Convaln_exception(int error_code, const char *error_msg)
Definition: global.h:44
bool is_word_char(char ch)
Definition: global.h:120
#define CONSTEXPR_INLINE
Definition: cxxforward.h:92
CONSTEXPR_INLINE bool is_end_mark(char ch)
Definition: global.h:113
void replace_msg(const char *new_msg) const
Definition: global.h:64
Convaln_exception(const Convaln_exception &other)
Definition: global.h:51
static void copy(double **i, double **j)
Definition: trnsprob.cxx:32
static const Convaln_exception * exception_thrown()
Definition: global.h:71
~Warnings()
Definition: global.h:86
CONSTEXPR_INLINE int max(int t1, int t2)
Definition: global.h:93
bool is_gapchar(char ch)
Definition: global.h:119
CONSTEXPR_INLINE bool has_no_content(const char *field)
Definition: global.h:122
CONSTEXPR_INLINE int min(int t1, int t2)
Definition: global.h:92
CONSTEXPR_INLINE bool str_equal(const char *s1, const char *s2)
Definition: global.h:95
static bool shown()
Definition: global.h:80
#define NULp
Definition: cxxforward.h:97
CONSTEXPR_INLINE int count_spaces(const char *str)
Definition: global.h:109
char * no_content()
Definition: global.h:129
char * strndup(const char *str, int len)
Definition: global.h:102
Warnings()
Definition: global.h:82
CONSTEXPR_INLINE bool is_sequence_terminator(const char *str)
Definition: global.h:115
void catched()
Definition: global.h:66
bool occurs_in(char ch, const char *in)
Definition: global.h:111