| 1 | // ================================================================ // |
|---|
| 2 | // // |
|---|
| 3 | // File : arbdb.h // |
|---|
| 4 | // Purpose : external ARB DB interface // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ================================================================ // |
|---|
| 10 | |
|---|
| 11 | #ifndef ARBDB_H |
|---|
| 12 | #define ARBDB_H |
|---|
| 13 | |
|---|
| 14 | #ifndef ARBTOOLS_H |
|---|
| 15 | #include <arbtools.h> |
|---|
| 16 | #endif |
|---|
| 17 | #ifndef ARBDB_BASE_H |
|---|
| 18 | #include <arbdb_base.h> |
|---|
| 19 | #endif |
|---|
| 20 | #ifndef ARB_ERROR_H |
|---|
| 21 | #include <arb_error.h> |
|---|
| 22 | #endif |
|---|
| 23 | #ifndef _STDINT_H |
|---|
| 24 | #include <stdint.h> |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #define GB_SYSTEM_FOLDER "__SYSTEM__" |
|---|
| 28 | #define GB_SYSTEM_KEY_DATA "@key_data" |
|---|
| 29 | |
|---|
| 30 | #define GB_DEFAULT_ALIGNMENT "presets/use" // has to match ../WINDOW/aw_awar_defs.hxx@AWAR_DEFAULT_ALIGNMENT |
|---|
| 31 | |
|---|
| 32 | // --------------------- |
|---|
| 33 | // simple types |
|---|
| 34 | |
|---|
| 35 | typedef int GB_COMPRESSION_MASK; |
|---|
| 36 | |
|---|
| 37 | // ------------------------------ |
|---|
| 38 | // forward declare types |
|---|
| 39 | |
|---|
| 40 | struct GB_NUMHASH; |
|---|
| 41 | |
|---|
| 42 | struct GBS_regex; |
|---|
| 43 | struct GBS_string_matcher; |
|---|
| 44 | struct GBS_strstruct; |
|---|
| 45 | struct GEN_position; |
|---|
| 46 | struct DictData; |
|---|
| 47 | struct CharPtrArray; |
|---|
| 48 | struct StrArray; |
|---|
| 49 | struct ConstStrArray; |
|---|
| 50 | |
|---|
| 51 | // ------------------- |
|---|
| 52 | // constants |
|---|
| 53 | |
|---|
| 54 | #define GB_USERFLAG_ANY 127 // maximum for gb_flag_types2::usr_ref |
|---|
| 55 | #define GB_USERFLAG_QUERY 1 // bit used for search&query (used on species, genes, experiments) |
|---|
| 56 | #define GB_USERFLAG_WASMARKED 2 // bit used to temp. store marks |
|---|
| 57 | #define GB_USERFLAG_GHOSTNODE 1 // bit used by gbt_write_tree (only used on tree-nodes; so it does not clash with GB_USERFLAG_QUERY) |
|---|
| 58 | |
|---|
| 59 | // -------------- |
|---|
| 60 | // enums |
|---|
| 61 | |
|---|
| 62 | enum GB_TYPES { // supported DB entry types |
|---|
| 63 | GB_NONE = 0, |
|---|
| 64 | GB_BIT = 1, |
|---|
| 65 | GB_BYTE = 2, |
|---|
| 66 | GB_INT = 3, |
|---|
| 67 | GB_FLOAT = 4, |
|---|
| 68 | GB_POINTER = 5, // not savable! only allowed in temporary entries |
|---|
| 69 | GB_BITS = 6, |
|---|
| 70 | // 7 is unused |
|---|
| 71 | GB_BYTES = 8, |
|---|
| 72 | GB_INTS = 9, |
|---|
| 73 | GB_FLOATS = 10, |
|---|
| 74 | GB_OBSOLETE = 11, // former GB_LINK (stay backward compatible until arb 6.2 or later) |
|---|
| 75 | GB_STRING = 12, |
|---|
| 76 | GB_STRING_SHRT = 13, // used automatically during save |
|---|
| 77 | // 14 is unused |
|---|
| 78 | GB_DB = 15, |
|---|
| 79 | |
|---|
| 80 | // keep GB_TYPES consistent with AW_VARIABLE_TYPE |
|---|
| 81 | // see ../WINDOW/aw_base.hxx@sync_GB_TYPES_AW_VARIABLE_TYPE |
|---|
| 82 | |
|---|
| 83 | GB_TYPE_MAX = 16, |
|---|
| 84 | |
|---|
| 85 | GB_CREATE_CONTAINER = GB_DB, |
|---|
| 86 | GB_FIND = GB_NONE, |
|---|
| 87 | |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | inline bool GB_TYPE_readable_as_string(GB_TYPES type) { |
|---|
| 91 | /*! returns true if 'type' is readable using GB_read_as_string(). |
|---|
| 92 | * For other types GB_read_as_string() returns NULp. |
|---|
| 93 | * Implies that it is writeable using GB_write_autoconv_string(). |
|---|
| 94 | * @see GB_readable_as_string() |
|---|
| 95 | */ |
|---|
| 96 | return type == GB_STRING || type == GB_BYTE || type == GB_INT || type == GB_FLOAT || type == GB_BITS; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | enum GB_SEARCH_TYPE { |
|---|
| 100 | SEARCH_BROTHER = 1, // [was: this_level] |
|---|
| 101 | SEARCH_CHILD = 2, // [was: down_level] |
|---|
| 102 | SEARCH_GRANDCHILD = 4, // [was: down_2_level] |
|---|
| 103 | SEARCH_NEXT_BROTHER = SEARCH_BROTHER+8, // [was: this_level|search_next] |
|---|
| 104 | SEARCH_CHILD_OF_NEXT = SEARCH_CHILD+8, // [was: down_level|search_next] |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | enum GB_UNDO_TYPE { |
|---|
| 108 | GB_UNDO_NONE, // no undo |
|---|
| 109 | GB_UNDO_KILL, // no undo and delete all old undos |
|---|
| 110 | GB_UNDO_UNDO, // normal undo -> deleted all redoes |
|---|
| 111 | GB_UNDO_REDO, // moves to UNDO_REDO |
|---|
| 112 | GB_UNDO_UNDO_REDO // internal makes undo redoable |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | // ----------------------- |
|---|
| 116 | // callback types |
|---|
| 117 | |
|---|
| 118 | typedef long (*gb_hash_loop_type)(const char *key, long val, void *client_data); |
|---|
| 119 | typedef void (*gb_hash_const_loop_type)(const char *key, long val, void *client_data); |
|---|
| 120 | typedef int (*gbs_hash_compare_function) (const char *key0, long val0, const char *key1, long val1); |
|---|
| 121 | |
|---|
| 122 | typedef const char* (*gb_export_sequence_cb)(GBDATA *gb_species, size_t *seq_len, GB_ERROR *error); |
|---|
| 123 | |
|---|
| 124 | typedef GBDATA* (*GB_Link_Follower)(GBDATA *GB_root, GBDATA *GB_elem, const char *link); |
|---|
| 125 | |
|---|
| 126 | typedef const char *(*gb_getenv_hook)(const char *varname); |
|---|
| 127 | |
|---|
| 128 | // ----------------------- |
|---|
| 129 | // GB_transaction |
|---|
| 130 | |
|---|
| 131 | class GB_transaction : virtual Noncopyable { |
|---|
| 132 | GBDATA *ta_main; |
|---|
| 133 | bool ta_open; // is transaction open ? |
|---|
| 134 | GB_ERROR ta_err; |
|---|
| 135 | |
|---|
| 136 | #if defined(ASSERTION_USED) |
|---|
| 137 | mutable int checked_for_error; // true when ok() or close() was used while ta_err |
|---|
| 138 | void CFE(int flag) const { if (ta_err) checked_for_error = checked_for_error|flag; } |
|---|
| 139 | #else |
|---|
| 140 | void CFE(int) const {} |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | void init(GBDATA *gb_main, bool initial); |
|---|
| 144 | protected: |
|---|
| 145 | GB_transaction(GBDATA *gb_main, bool) { init(gb_main, true); } |
|---|
| 146 | public: |
|---|
| 147 | GB_transaction(GBDATA *gb_main) { init(gb_main, false); } |
|---|
| 148 | ~GB_transaction(); |
|---|
| 149 | |
|---|
| 150 | bool ok() const { CFE(1); return ta_open && !ta_err; } // ready to work on DB? |
|---|
| 151 | GB_ERROR close(GB_ERROR error); // abort transaction if error (e.g.: 'return ta.close(error);') |
|---|
| 152 | ARB_ERROR close(ARB_ERROR& error); // abort transaction if error (e.g.: 'return ta.close(error);') |
|---|
| 153 | }; |
|---|
| 154 | |
|---|
| 155 | struct GB_initial_transaction : public GB_transaction { |
|---|
| 156 | GB_initial_transaction(GBDATA *gb_main) : GB_transaction(gb_main, true) {} |
|---|
| 157 | }; |
|---|
| 158 | |
|---|
| 159 | class GB_shell { |
|---|
| 160 | // initialize and cleanup module ARBDB |
|---|
| 161 | // No database usage is possible when no GB_shell exists! |
|---|
| 162 | public: |
|---|
| 163 | GB_shell(); |
|---|
| 164 | ~GB_shell(); |
|---|
| 165 | |
|---|
| 166 | static void ensure_inside(); |
|---|
| 167 | static bool in_shell(); |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | // -------------------------- |
|---|
| 171 | // GB_securityLevel |
|---|
| 172 | |
|---|
| 173 | class GB_securityLevel : virtual Noncopyable { |
|---|
| 174 | // changes global security level until destruction of this. |
|---|
| 175 | // intended to be used as auto-variable. |
|---|
| 176 | |
|---|
| 177 | GBDATA *gbdata; |
|---|
| 178 | int previous_level; |
|---|
| 179 | |
|---|
| 180 | int whats_my_security() const; |
|---|
| 181 | void change_my_security(int level); |
|---|
| 182 | |
|---|
| 183 | public: |
|---|
| 184 | GBDATA *get_gbd() const { return gbdata; } // internal use only |
|---|
| 185 | |
|---|
| 186 | int previous() const { return previous_level; } |
|---|
| 187 | int current() const { return whats_my_security(); } |
|---|
| 188 | |
|---|
| 189 | GB_securityLevel(GBDATA *gbd, int level) : |
|---|
| 190 | gbdata(gbd), |
|---|
| 191 | previous_level(current()) |
|---|
| 192 | { |
|---|
| 193 | change_my_security(level); |
|---|
| 194 | } |
|---|
| 195 | ~GB_securityLevel() { |
|---|
| 196 | change_my_security(previous()); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | }; |
|---|
| 200 | struct GB_topSecurityLevel : public GB_securityLevel { // derived from Noncopyable |
|---|
| 201 | // raise security level to top (allow everything). |
|---|
| 202 | // Note: the highest user-level is 6 |
|---|
| 203 | GB_topSecurityLevel(GBDATA *gbd) : |
|---|
| 204 | GB_securityLevel(gbd, 7) |
|---|
| 205 | {} |
|---|
| 206 | }; |
|---|
| 207 | struct GB_previousSecurityLevel : public GB_securityLevel { // derived from Noncopyable |
|---|
| 208 | // temporarily lower security again (while another GB_securityLevel is active). |
|---|
| 209 | GB_previousSecurityLevel(const GB_securityLevel& active) : |
|---|
| 210 | GB_securityLevel(active.get_gbd(), active.previous()) |
|---|
| 211 | {} |
|---|
| 212 | }; |
|---|
| 213 | |
|---|
| 214 | // -------------------------------------------- |
|---|
| 215 | |
|---|
| 216 | #if defined(DEBUG) |
|---|
| 217 | struct GB_SizeInfo { |
|---|
| 218 | long containers; // no of containers |
|---|
| 219 | long terminals; // no of terminals |
|---|
| 220 | long structure; // structure size |
|---|
| 221 | long data; // data size |
|---|
| 222 | long mem; // data memory size (compressed) |
|---|
| 223 | |
|---|
| 224 | GB_SizeInfo() : containers(0), terminals(0), structure(0), data(0), mem(0) {} |
|---|
| 225 | |
|---|
| 226 | void collect(GBDATA *gbd); |
|---|
| 227 | }; |
|---|
| 228 | #endif |
|---|
| 229 | |
|---|
| 230 | // -------------------------------------------- |
|---|
| 231 | // include generated public prototypes |
|---|
| 232 | |
|---|
| 233 | #include <ad_prot.h> |
|---|
| 234 | |
|---|
| 235 | // to avoid arb-wide changes atm include some headers from CORE lib |
|---|
| 236 | #ifndef ARB_MEM_H |
|---|
| 237 | #include <arb_mem.h> |
|---|
| 238 | #endif |
|---|
| 239 | #ifndef ARB_MSG_H |
|---|
| 240 | #include <arb_msg.h> |
|---|
| 241 | #endif |
|---|
| 242 | #ifndef ARB_STRING_H |
|---|
| 243 | #include <arb_string.h> |
|---|
| 244 | #endif |
|---|
| 245 | |
|---|
| 246 | // ---------------------------------------------------- |
|---|
| 247 | // const wrappers for functions from ad_prot.h |
|---|
| 248 | |
|---|
| 249 | inline char *GBS_find_string(char *content, GB_CSTR key, int match_mode) { |
|---|
| 250 | return const_cast<char*>(GBS_find_string(const_cast<GB_CSTR>(content), key, match_mode)); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | // --------------------------------- |
|---|
| 254 | // error delivery functions |
|---|
| 255 | |
|---|
| 256 | inline void GB_end_transaction_show_error(GBDATA *gbd, ARB_ERROR& error, void (*error_handler)(GB_ERROR)) { |
|---|
| 257 | GB_end_transaction_show_error(gbd, error.deliver(), error_handler); |
|---|
| 258 | } |
|---|
| 259 | inline ARB_ERROR GB_end_transaction(GBDATA *gbd, ARB_ERROR& error) { |
|---|
| 260 | return GB_end_transaction(gbd, error.deliver()); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | #else |
|---|
| 265 | #error arbdb.h included twice |
|---|
| 266 | #endif // ARBDB_H |
|---|