source: tags/ms_r16q3/CONVERTALN/global.h

Last change on this file was 15176, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 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
30#define ca_assert(cond) arb_assert(cond)
31
32// --------------------
33
34class Convaln_exception
35// : virtual Noncopyable
36{
37    static Convaln_exception *thrown;
38
39    int           code;
40    mutable char *msg;
41
42public:
43    Convaln_exception(int error_code, const char *error_msg)
44        : code(error_code),
45          msg(ARB_strdup(error_msg))
46    {
47        ca_assert(!thrown); // 2 exceptions at the same time ? very exceptional! :)
48        thrown = this;
49    }
50    Convaln_exception(const Convaln_exception& other)
51        : code(other.code),
52          msg(ARB_strdup(other.msg))
53    {}
54    DECLARE_ASSIGNMENT_OPERATOR(Convaln_exception);
55    ~Convaln_exception() {
56        ca_assert(thrown);
57        thrown = NULL;
58        free(msg);
59    }
60
61    int get_code() const { return code; }
62    const char *get_msg() const { return msg; }
63    void replace_msg(const char *new_msg) const { freedup(msg, new_msg); }
64
65    void catched() {
66        ca_assert(thrown == this);
67        thrown = NULL;
68    }
69
70    static const Convaln_exception *exception_thrown() { return thrown; }
71};
72
73// --------------------
74
75class Warnings : virtual Noncopyable {
76    static bool show_warnings;
77    bool        old_state;
78public:
79    static bool shown() { return show_warnings; }
80
81    Warnings() {
82        old_state     = shown();
83        show_warnings = false;
84    }
85    ~Warnings() { show_warnings = old_state; }
86};
87
88
89// --------------------
90
91inline int min(int t1, int t2) { return t1<t2 ? t1 : t2; }
92inline int max(int t1, int t2) { return t1>t2 ? t1 : t2; }
93
94inline bool str_equal(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
95inline bool str_iequal(const char *s1, const char *s2) { return strcasecmp(s1, s2) == 0; }
96
97inline int str0len(const char *str) {
98    return str ? strlen(str) : 0;
99}
100
101inline char *strndup(const char *str, int len) {
102    char *result = ARB_alloc<char>(len+1);
103    memcpy(result, str, len);
104    result[len]  = 0;
105    return result;
106}
107
108inline int count_spaces(const char *str) { return strspn(str, " "); }
109
110inline bool occurs_in(char ch, const char *in) { ca_assert(ch != 0); return strchr(in, ch) != 0; }
111
112inline bool is_end_mark(char ch) { return ch == '.' || ch == ';'; }
113
114inline bool is_sequence_terminator(const char *str) { return str[0] == '/' && str[1] == '/'; }
115
116#define WORD_SEP ",.; ?:!)]}"
117
118inline bool is_gapchar(char ch) { return occurs_in(ch, "-.~"); }
119inline bool is_word_char(char ch) { return !occurs_in(ch, WORD_SEP); }
120
121inline bool has_no_content(const char *field) {
122    return !field ||
123        !field[0] ||
124        (field[0] == '\n' && !field[1]);
125}
126inline bool has_content(const char *field) { return !has_no_content(field); }
127
128inline char *no_content() {
129    char *nothing = ARB_alloc<char>(2);
130    nothing[0]    = '\n';
131    nothing[1]    = 0;
132    return nothing;
133}
134
135inline bool copy_content(char*& entry, const char *content) {
136    bool copy = has_content(content);
137    if (copy) freedup(entry, content);
138    return copy;
139}
140
141// --------------------
142
143#define lookup_keyword(keyword,table) ___lookup_keyword(keyword, table, ARRAY_ELEMS(table))
144
145#else
146#error global.h included twice
147#endif // GLOBAL_H
148
Note: See TracBrowser for help on using the repository browser.