summaryrefslogtreecommitdiffstats
path: root/common/eurephia_nullsafe.h
blob: 167da387bdd06ac8d55b78406793fd8f5acba554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* eurephia_nullsafe.h
 *
 *  standard C string functions, which is made NULL safe by checking
 *  if input value is NULL before performing the action.
 *
 *  GPLv2 only - Copyright (C) 2008 - 2012
 *               David Sommerseth <dazo@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; version 2
 *  of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

/**
 * @file   eurephia_nullsafe.h
 * @author David Sommerseth <dazo@users.sourceforge.net>
 * @date   2008-08-06
 *
 * @brief standard C string functions, which is made NULL safe by checking
 *        if input value is NULL before performing the action.
 *
 */

#include <eurephia_context.h>

#ifndef   	EUREPHIA_NULLSAFE_H_
#define    	EUREPHIA_NULLSAFE_H_

/**
 * atoi() wrapper.  Converts any string into a integer
 *
 * @param str Input string
 *
 * @return Returns integer
 */
#define atoi_nullsafe(str) (str != NULL ? atoi(str) : 0)


/**
 * strdup() wrapper.  Duplicates the input string.
 *
 * @param str Input string to be duplicated
 *
 * @return Returns a pointer to the duplicate (char *) on success, NULL otherwise.
 * If input was NULL, NULL is returned.
 */
#define strdup_nullsafe(str) (str != NULL ? strdup(str) : NULL)


/**
 * Wrapper macro, which appends a string to a destination string without exceeding the size
 * of the destination buffer.
 *
 * @param dest Pointer to the destination buffer
 * @param src  Pointer to the value being concatenated to the destination string.
 * @param size Size of the destination buffer
 */
#define append_str(dest, src, size) strncat(dest, src, (size - strlen_nullsafe(dest)))


/**
 * strlen() wrapper.  Returns the length of a string
 *
 * @param str Input string
 *
 * @return Returns int with length of string.  If input is NULL, it returns 0.
 */
#define strlen_nullsafe(str) (str != NULL ? strlen(str) : 0)



void *_malloc_nullsafe(eurephiaCTX *, size_t, const char *, int);

/**
 * Front-end macro for the _malloc_nullsafe() function.
 *
 * @param ctx  eurephiaCTX (used for debugg logging)
 * @param sz Size of the memory region wanted
 *
 * @return Returns a pointer to the memory region on success, otherwise NULL.
 *
 */
#define malloc_nullsafe(ctx, sz) _malloc_nullsafe(ctx, sz, __FILE__, __LINE__)


void inline _free_nullsafe(eurephiaCTX *ctx, void *ptr, const char *file, int line);

/**
 * Front-end macro for the __ree_nullsafe() function.
 *
 * @param ctx  eurephiaCTX (used for debugg logging)
 * @param ptr Pointer to the memory region being freed.
 *
 */
#define free_nullsafe(ctx, ptr) { _free_nullsafe(ctx, ptr, __FILE__, __LINE__); ptr = NULL; }


/**
 * Function which will return a default string value if no input data was provided.
 *
 * @param str     Input string
 * @param defstr  Default string
 *
 * @return Returns the pointer to the input string if the string length > 0.  Otherwise it
 * will return a pointer to the default string.
 */
#define defaultValue(str, defstr) (strlen_nullsafe(str) == 0 ? defstr : str)


/**
 * Function which will return a default integer value if no input data was provided.
 *
 * @param ival   input integer value
 * @param defval default integer value
 *
 * @return Returns the ival value if it is > 0, otherwise defval value is returned.
 */
#define defaultIntValue(ival, defval) (ival == 0 ? defval : ival)
#endif 	    /* !EUREPHIA_NULLSAFE_H_ */