1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : dupstr.h // |
---|
4 | // Purpose : C-string (heap-copies) handling // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in January 2010 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // =============================================================== // |
---|
11 | |
---|
12 | #ifndef DUPSTR_H |
---|
13 | #define DUPSTR_H |
---|
14 | |
---|
15 | /* -------------------------------------------------------------------------------- |
---|
16 | * The following function handle char*'s, which either own a heap copy or are NULp. |
---|
17 | * |
---|
18 | * freeset: assigns a heap-copy to a variable (variable is automatically freed) |
---|
19 | * freenull: assigns NULp to a variable (variable is automatically freed) |
---|
20 | * freedup: similar to freeset, but strdup's the rhs-expression |
---|
21 | * reassign: similar to freeset, but rhs must be variable and will be set to NULp |
---|
22 | * nulldup: like strdup, but pass-through NULp |
---|
23 | * |
---|
24 | * Notes: |
---|
25 | * - freeset, freedup and reassign may safely use the changed variable in the rhs-expression! |
---|
26 | * - freeset and freenull work with any pointer-type allocated using malloc et al. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _GLIBCXX_CSTRING |
---|
30 | #include <cstring> |
---|
31 | #endif |
---|
32 | #ifndef _GLIBCXX_CSTDLIB |
---|
33 | #include <cstdlib> |
---|
34 | #endif |
---|
35 | #ifndef CXXFORWARD_H |
---|
36 | #include "cxxforward.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #ifdef __cplusplus |
---|
40 | |
---|
41 | template<typename T> |
---|
42 | inline void freenull(T *& var) { |
---|
43 | free(var); |
---|
44 | var = NULp; |
---|
45 | } |
---|
46 | |
---|
47 | template<typename T> |
---|
48 | inline void freeset(T *& var, T *heapcopy) { |
---|
49 | free(var); |
---|
50 | var = heapcopy; |
---|
51 | } |
---|
52 | |
---|
53 | inline char *nulldup(const char *maybeStr) { |
---|
54 | return maybeStr ? strdup(maybeStr) : NULp; // @@@ how can ARB_strdup be used in nulldup/freedup? using it causes link problems |
---|
55 | } |
---|
56 | inline void freedup(char *& strvar, const char *maybeStr) { |
---|
57 | freeset(strvar, nulldup(maybeStr)); |
---|
58 | } |
---|
59 | inline void reassign(char *& dstvar, char *& srcvar) { |
---|
60 | freeset(dstvar, srcvar); |
---|
61 | srcvar = NULp; |
---|
62 | } |
---|
63 | |
---|
64 | inline const char *null2empty(const char *str) { |
---|
65 | /*! if 'str' is NULp -> use empty string. otherwise use str. |
---|
66 | */ |
---|
67 | return str ? str : ""; |
---|
68 | } |
---|
69 | |
---|
70 | #endif // __cplusplus |
---|
71 | |
---|
72 | // helper to use char as array index: |
---|
73 | CONSTEXPR_INLINE unsigned char safeCharIndex(char c) { return static_cast<unsigned char>(c); } |
---|
74 | |
---|
75 | struct charpLess { // sort type for set<char*> / map<char*, ...> |
---|
76 | bool operator()(const char *n1, const char *n2) const { |
---|
77 | return strcmp(n1, n2)<0; |
---|
78 | } |
---|
79 | }; |
---|
80 | |
---|
81 | #else |
---|
82 | #error dupstr.h included twice |
---|
83 | #endif // DUPSTR_H |
---|