| 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 | |
|---|
| 37 | class Convaln_exception { |
|---|
| 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 | } |
|---|
| 51 | Convaln_exception(const Convaln_exception& other) |
|---|
| 52 | : code(other.code), |
|---|
| 53 | msg(ARB_strdup(other.msg)) |
|---|
| 54 | {} |
|---|
| 55 | DECLARE_ASSIGNMENT_OPERATOR(Convaln_exception); |
|---|
| 56 | ~Convaln_exception() { |
|---|
| 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 | |
|---|
| 82 | Warnings() { |
|---|
| 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 | |
|---|
| 96 | // Note: str_equal not accepted as constexpr by OSX (because strcmp() isnt) |
|---|
| 97 | CONSTEXPR_INLINE_NC bool str_equal(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; } |
|---|
| 98 | CONSTEXPR_INLINE bool str_iequal(const char *s1, const char *s2) { return strcasecmp(s1, s2) == 0; } |
|---|
| 99 | |
|---|
| 100 | CONSTEXPR_INLINE int str0len(const char *str) { |
|---|
| 101 | return str ? strlen(str) : 0; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | inline char *strndup(const char *str, int len) { |
|---|
| 105 | char *result = ARB_alloc<char>(len+1); |
|---|
| 106 | memcpy(result, str, len); |
|---|
| 107 | result[len] = 0; |
|---|
| 108 | return result; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | CONSTEXPR_INLINE int count_spaces(const char *str) { return strspn(str, " "); } |
|---|
| 112 | |
|---|
| 113 | inline bool occurs_in(char ch, const char *in) { ca_assert(ch != 0); return strchr(in, ch); } |
|---|
| 114 | |
|---|
| 115 | CONSTEXPR_INLINE bool is_end_mark(char ch) { return ch == '.' || ch == ';'; } |
|---|
| 116 | |
|---|
| 117 | CONSTEXPR_INLINE bool is_sequence_terminator(const char *str) { return str[0] == '/' && str[1] == '/'; } |
|---|
| 118 | |
|---|
| 119 | #define WORD_SEP ",.; ?:!)]}" |
|---|
| 120 | |
|---|
| 121 | inline bool is_gapchar(char ch) { return GAP::is_import_gap(ch); } |
|---|
| 122 | inline bool is_word_char(char ch) { return !occurs_in(ch, WORD_SEP); } |
|---|
| 123 | |
|---|
| 124 | CONSTEXPR_INLINE bool has_no_content(const char *field) { |
|---|
| 125 | return !field || |
|---|
| 126 | !field[0] || |
|---|
| 127 | (field[0] == '\n' && !field[1]); |
|---|
| 128 | } |
|---|
| 129 | CONSTEXPR_INLINE bool has_content(const char *field) { return !has_no_content(field); } |
|---|
| 130 | |
|---|
| 131 | inline char *no_content() { |
|---|
| 132 | char *nothing = ARB_alloc<char>(2); |
|---|
| 133 | nothing[0] = '\n'; |
|---|
| 134 | nothing[1] = 0; |
|---|
| 135 | return nothing; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | inline bool copy_content(char*& entry, const char *content) { |
|---|
| 139 | bool copy = has_content(content); |
|---|
| 140 | if (copy) freedup(entry, content); |
|---|
| 141 | return copy; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | // -------------------- |
|---|
| 145 | |
|---|
| 146 | #define lookup_keyword(keyword,table) ___lookup_keyword(keyword, table, ARRAY_ELEMS(table)) |
|---|
| 147 | |
|---|
| 148 | #else |
|---|
| 149 | #error global.h included twice |
|---|
| 150 | #endif // GLOBAL_H |
|---|
| 151 | |
|---|