source: trunk/TEMPLATES/dupstr.h

Last change on this file was 18159, checked in by westram, 5 years ago
  • full update from child 'fix' into 'trunk'
    • fix item name accessors (GBT_get_name + GBT_get_name_or_description)
    • add null2empty
  • adds: log:branches/fix@18140:18158
File size: 2.7 KB
Line 
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
41template<typename T>
42inline void freenull(T *& var) {
43    free(var);
44    var = NULp;
45}
46
47template<typename T>
48inline void freeset(T *& var, T *heapcopy) {
49    free(var);
50    var = heapcopy;
51}
52
53inline 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}
56inline void freedup(char *& strvar, const char *maybeStr) {
57    freeset(strvar, nulldup(maybeStr));
58}
59inline void reassign(char *& dstvar, char *& srcvar) {
60    freeset(dstvar, srcvar);
61    srcvar = NULp;
62}
63
64inline 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:
73CONSTEXPR_INLINE unsigned char safeCharIndex(char c) { return static_cast<unsigned char>(c); }
74
75struct 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
Note: See TracBrowser for help on using the repository browser.