forked from aremmell/libsir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsirhelpers.h
151 lines (126 loc) · 4.38 KB
/
sirhelpers.h
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file sirhelpers.h
* @brief Internal macros and inline functions.
*
* This file and accompanying source code originated from <https://github.com/aremmell/libsir>.
* If you obtained it elsewhere, all bets are off.
*
* @author Ryan M. Lederman <[email protected]>
* @copyright
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ryan M. Lederman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _SIR_HELPERS_H_INCLUDED
#define _SIR_HELPERS_H_INCLUDED
#include "sirtypes.h"
/**
* @addtogroup intern
* @{
*/
/** Computes the size of an array. */
#define _sir_countof(arr) (sizeof(arr) / sizeof(arr[0]))
/**
* Creates an error code that (hopefully) doesn't conflict
* with any of those defined by the platform.
*/
#define _sir_mkerror(code) \
(((uint32_t)(code & 0x7fff) << 16) | 0x80000000)
/** Validates an internal error. */
static inline
bool _sir_validerror(sirerror_t err) {
sirerror_t masked = err & 0x8fffffff;
return masked >= 0x80000000 && masked <= 0x8fff0000;
}
/** Extracts just the code from an internal error. */
static inline
uint16_t _sir_geterrcode(sirerror_t err) {
return (err >> 16) & 0x7fff;
}
/** Evil macro used for _sir_lv wrappers. */
#define _SIR_L_START(format) \
bool r = false; \
va_list args; \
va_start(args, format);
/** Evil macro used for _sir_lv wrappers. */
#define _SIR_L_END(args) va_end(args);
/** Validates a pointer. */
#define _sir_validptr(p) (NULL != p)
/** Checks a bitfield for a specific set of bits. */
static inline
bool _sir_bittest(uint32_t flags, uint32_t test) {
return (flags & test) == test;
}
/** Wraps \a free. */
static inline
void __sir_safefree(void** p) {
if (!p || (p && !*p))
return;
free(*p);
*p = NULL;
}
/** Wraps \a free. */
static inline
void _sir_safefree(void* p) {
__sir_safefree(&p);
}
/** Validates a log file identifier. */
bool _sir_validfid(int id);
/** Validates a set of ::sir_level flags. */
bool _sir_validlevels(sir_levels levels);
/** Validates a single ::sir_level. */
bool _sir_validlevel(sir_level level);
/** Applies default ::sir_level flags if applicable. */
static inline
void _sir_defaultlevels(sir_levels* levels, sir_levels def) {
if (levels && SIRL_DEFAULT == *levels) *levels = def;
}
/** Applies default ::sir_options flags if applicable. */
static inline
void _sir_defaultopts(sir_options* opts, sir_options def) {
if (opts && SIRO_DEFAULT == *opts) *opts = def;
}
/** Validates a set of ::sir_option flags. */
bool _sir_validopts(sir_options opts);
/** Validates a string pointer and optionally fails if it's invalid. */
bool __sir_validstr(const sirchar_t* str, bool fail);
/** Validates a string pointer and fails if it's invalid. */
static inline
bool _sir_validstr(const sirchar_t* str) {
return __sir_validstr(str, true);
}
/** Validates a string pointer but ignores if it's invalid. */
static inline
bool _sir_validstrnofail(const sirchar_t* str) {
return __sir_validstr(str, false);
}
static inline
bool _sir_validupdatedata(sir_update_data* data) {
return NULL != data && ((NULL == data->levels || _sir_validlevels(*data->levels)) &&
(NULL == data->opts || _sir_validopts(*data->opts)));
}
/** Places a null terminator at the first index in a string buffer. */
static inline
void _sir_resetstr(sirchar_t* str) {
str[0] = (sirchar_t)'\0';
}
/** @} */
#endif /* !_SIR_HELPERS_H_INCLUDED */