source: branches/stable/CONVERTALN/global.h

Last change on this file was 17534, checked in by westram, 6 years ago
  • partial merge from 'fix' into 'trunk'
    • globally define what are "gaps"
    • kept behavioral changes to a minimum:
      • defaults for (user-defined) gap-definition in EDIT4 changed
      • EDIT sequence search also uses user-defined gaps
  • adds: log:branches/fix@17529:17533
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
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
37class Convaln_exception {
38    static Convaln_exception *thrown;
39
40    int           code;
41    mutable char *msg;
42
43public:
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
76class Warnings : virtual Noncopyable {
77    static bool show_warnings;
78    bool        old_state;
79public:
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
92CONSTEXPR_INLINE int min(int t1, int t2) { return t1<t2 ? t1 : t2; }
93CONSTEXPR_INLINE int max(int t1, int t2) { return t1>t2 ? t1 : t2; }
94
95CONSTEXPR_INLINE bool str_equal(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
96CONSTEXPR_INLINE bool str_iequal(const char *s1, const char *s2) { return strcasecmp(s1, s2) == 0; }
97
98CONSTEXPR_INLINE int str0len(const char *str) {
99    return str ? strlen(str) : 0;
100}
101
102inline 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
109CONSTEXPR_INLINE int count_spaces(const char *str) { return strspn(str, " "); }
110
111inline bool occurs_in(char ch, const char *in) { ca_assert(ch != 0); return strchr(in, ch); }
112
113CONSTEXPR_INLINE bool is_end_mark(char ch) { return ch == '.' || ch == ';'; }
114
115CONSTEXPR_INLINE bool is_sequence_terminator(const char *str) { return str[0] == '/' && str[1] == '/'; }
116
117#define WORD_SEP ",.; ?:!)]}"
118
119inline bool is_gapchar(char ch) { return GAP::is_import_gap(ch); }
120inline bool is_word_char(char ch) { return !occurs_in(ch, WORD_SEP); }
121
122CONSTEXPR_INLINE bool has_no_content(const char *field) {
123    return !field ||
124        !field[0] ||
125        (field[0] == '\n' && !field[1]);
126}
127CONSTEXPR_INLINE bool has_content(const char *field) { return !has_no_content(field); }
128
129inline char *no_content() {
130    char *nothing = ARB_alloc<char>(2);
131    nothing[0]    = '\n';
132    nothing[1]    = 0;
133    return nothing;
134}
135
136inline 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
Note: See TracBrowser for help on using the repository browser.