source: branches/profile/CONVERTALN/global.h

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