This source file includes following definitions.
- __sir_validptr
- __sir_validptrptr
- _sir_bittest
- _sir_setbitshigh
- _sir_setbitslow
- _sir_validfileid
- _sir_validpluginid
- _sir_defaultlevels
- _sir_defaultopts
- _sir_mkansifgcolor
- _sir_mkansibgcolor
- _sir_getansifgcmd
- _sir_getansibgcmd
- _sir_makergb
- __sir_validstr
- _sir_resetstr
- _sir_strsame
- _sir_localtime
- _sir_formattime
- FNV32_1a
- SANITIZE_SUPPRESS
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 #ifndef _SIR_HELPERS_H_INCLUDED
34 # define _SIR_HELPERS_H_INCLUDED
35
36 # include "sir/types.h"
37 # include "sir/errors.h"
38
39 # if defined(__cplusplus)
40 extern "C" {
41 # endif
42
43
44 # define _sir_countof(arr) (sizeof(arr) / sizeof(arr[0]))
45
46
47 # define _SIR_L_START(format) \
48 bool ret = false; \
49 va_list args; \
50 do { \
51 if (!_sir_validptr(format)) \
52 return false; \
53 va_start(args, format); \
54 } while (false)
55
56
57 # define _SIR_L_END() \
58 va_end(args)
59
60
61 # define _SIR_LOCK_SECTION(type, name, mid, ret) \
62 type* name = _sir_locksection(mid); \
63 do { \
64 if (!name) { \
65 (void)_sir_seterror(_SIR_E_INTERNAL); \
66 return ret; \
67 } \
68 } while (false)
69
70
71 # define _SIR_UNLOCK_SECTION(mid) \
72 _sir_unlocksection(mid)
73
74
75 # define SIR_UNUSED(param) (void)param
76
77
78 # define SIR_ASSERT_UNUSED(assertion, var) SIR_ASSERT(assertion); SIR_UNUSED(var)
79
80
81 # define _SIR_PRNSTR(str) (str ? str : "<null>")
82
83
84 # define _SIR_DECLARE_BIN_SEARCH(low, high) \
85 size_t _low = low, _high = high; \
86 size_t _mid = (_low + _high) / 2
87
88 # define _SIR_BEGIN_BIN_SEARCH() do {
89
90 # define _SIR_ITERATE_BIN_SEARCH(comparison) \
91 if (_low == _high) \
92 break; \
93 if (0 > comparison && (_mid - 1) >= _low) { \
94 _high = _mid - 1; \
95 } else if ((_mid + 1) <= _high) { \
96 _low = _mid + 1; \
97 } else { \
98 break; \
99 } \
100 _mid = (_low + _high) / 2
101
102 # define _SIR_END_BIN_SEARCH() \
103 } while (true)
104
105
106 static inline
107 bool __sir_validptr(const void* restrict p, bool fail, const char* func,
108 const char* file, uint32_t line) {
109 bool valid = NULL != p;
110 if (fail && !valid) {
111 (void)__sir_seterror(_SIR_E_NULLPTR, func, file, line);
112 SIR_ASSERT(!valid && fail);
113 }
114 return valid;
115 }
116
117
118 static inline
119 bool __sir_validptrptr(const void* restrict* pp, bool fail, const char* func,
120 const char* file, uint32_t line) {
121 bool valid = NULL != pp;
122 if (fail && !valid) {
123 (void)__sir_seterror(_SIR_E_NULLPTR, func, file, line);
124 SIR_ASSERT(!valid && fail);
125 }
126 return valid;
127 }
128
129
130 # define _sir_validptrnofail(p) \
131 __sir_validptr(p, false, __func__, __file__, __LINE__)
132
133
134 # define _sir_validptr(p) \
135 __sir_validptr((const void* restrict)p, true, __func__, __file__, __LINE__)
136
137
138 # define _sir_validfnptr(fnp) \
139 __sir_validptrptr((const void* restrict*)&fnp, true, __func__, __file__, __LINE__)
140
141
142 # define _sir_validptrptr(pp) \
143 __sir_validptrptr((const void* restrict*)pp, true, __func__, __file__, __LINE__)
144
145
146 static inline
147 bool _sir_bittest(uint32_t flags, uint32_t test) {
148 return (flags & test) == test;
149 }
150
151
152 static inline
153 void _sir_setbitshigh(uint32_t* flags, uint32_t set) {
154 if (NULL != flags)
155 *flags |= set;
156 }
157
158
159 static inline
160 void _sir_setbitslow(uint32_t* flags, uint32_t set) {
161 if (NULL != flags)
162 *flags &= ~set;
163 }
164
165
166
167 # define _sir_eqland(b, expr) ((b) = (expr) && (b))
168
169
170 void __sir_safefree(void** pp);
171
172
173 # define _sir_safefree(pp) __sir_safefree((void**)pp)
174
175
176 void _sir_safeclose(int* restrict fd);
177
178
179 void _sir_safefclose(FILE* restrict* restrict f);
180
181
182 bool _sir_validfd(int fd);
183
184
185 static inline
186 bool _sir_validfileid(sirfileid id) {
187 return 0U != id;
188 }
189
190
191 static inline
192 bool _sir_validpluginid(sirpluginid id) {
193 return 0U != id;
194 }
195
196
197 bool __sir_validupdatedata(const sir_update_config_data* data, const char* func,
198 const char* file, uint32_t line);
199
200
201 # define _sir_validupdatedata(data) \
202 __sir_validupdatedata(data, __func__, __file__, __LINE__)
203
204
205 bool __sir_validlevels(sir_levels levels, const char* func, const char* file,
206 uint32_t line);
207
208
209 # define _sir_validlevels(levels) \
210 __sir_validlevels(levels, __func__, __file__, __LINE__)
211
212
213 bool __sir_validlevel(sir_level level, const char* func, const char* file,
214 uint32_t line);
215
216
217 # define _sir_validlevel(level) \
218 __sir_validlevel(level, __func__, __file__, __LINE__)
219
220
221 static inline
222 void _sir_defaultlevels(sir_levels* levels, sir_levels def) {
223 if (levels && SIRL_DEFAULT == *levels)
224 *levels = def;
225 }
226
227
228 static inline
229 void _sir_defaultopts(sir_options* opts, sir_options def) {
230 if (opts && SIRO_DEFAULT == *opts)
231 *opts = def;
232 }
233
234
235 bool __sir_validopts(sir_options opts, const char* func, const char* file,
236 uint32_t line);
237
238
239 # define _sir_validopts(opts) \
240 __sir_validopts(opts, __func__, __file__, __LINE__)
241
242
243 bool __sir_validtextattr(sir_textattr attr, const char* func, const char* file,
244 uint32_t line);
245
246
247 # define _sir_validtextattr(attr) \
248 __sir_validtextattr(attr, __func__, __file__, __LINE__)
249
250
251 bool __sir_validtextcolor(sir_colormode mode, sir_textcolor color, const char* func,
252 const char* file, uint32_t line);
253
254
255 # define _sir_validtextcolor(mode, color) \
256 __sir_validtextcolor(mode, color, __func__, __file__, __LINE__)
257
258
259 bool __sir_validcolormode(sir_colormode mode, const char* func, const char* file,
260 uint32_t line);
261
262
263 # define _sir_validcolormode(mode) \
264 __sir_validcolormode(mode, __func__, __file__, __LINE__)
265
266
267 static inline
268 sir_textcolor _sir_mkansifgcolor(sir_textcolor fg) {
269 return SIRTC_DEFAULT == fg ? 39 : fg < 8 ? fg + 30 : fg + 82;
270 }
271
272
273 static inline
274 sir_textcolor _sir_mkansibgcolor(sir_textcolor bg) {
275 return SIRTC_DEFAULT == bg ? 49 : bg < 8 ? bg + 40 : bg + 92;
276 }
277
278
279 static inline
280 sir_textcolor _sir_getansifgcmd(sir_textcolor fg) {
281 return SIRTC_DEFAULT == fg ? 39 : 38;
282 }
283
284
285 static inline
286 sir_textcolor _sir_getansibgcmd(sir_textcolor bg) {
287 return SIRTC_DEFAULT == bg ? 49 : 48;
288 }
289
290
291 # define _sir_getredfromcolor(color) (uint8_t)(((color) >> 16) & 0x000000ffU)
292
293
294 # define _sir_setredincolor(color, red) (color |= (((red) << 16) & 0x00ff0000U))
295
296
297 # define _sir_getgreenfromcolor(color) (uint8_t)(((color) >> 8) & 0x000000ffU)
298
299
300 # define _sir_setgreenincolor(color, green) ((color) |= (((green) << 8) & 0x0000ff00U))
301
302
303 # define _sir_getbluefromcolor(color) (uint8_t)((color) & 0x000000ffU)
304
305
306 # define _sir_setblueincolor(color, blue) ((color) |= ((blue) & 0x000000ffU))
307
308
309 static inline
310 sir_textcolor _sir_makergb(sir_textcolor r, sir_textcolor g, sir_textcolor b) {
311 sir_textcolor retval = 0;
312 _sir_setredincolor(retval, r);
313 _sir_setgreenincolor(retval, g);
314 _sir_setblueincolor(retval, b);
315 return retval;
316 }
317
318
319 static inline
320 bool __sir_validstr(const char* restrict str, bool fail, const char* func,
321 const char* file, uint32_t line) {
322 bool valid = str && *str != '\0';
323 if (fail && !valid) {
324 SIR_ASSERT(!valid && fail);
325 (void)__sir_seterror(_SIR_E_STRING, func, file, line);
326 }
327 return valid;
328 }
329
330
331 # define _sir_validstr(str) \
332 __sir_validstr(str, true, __func__, __file__, __LINE__)
333
334
335 # define _sir_validstrnofail(str) \
336 __sir_validstr(str, false, __func__, __file__, __LINE__)
337
338
339 static inline
340 void _sir_resetstr(char* str) {
341 if (NULL != str)
342 *str = '\0';
343 }
344
345
346
347
348
349 static inline
350 bool _sir_strsame(const char* lhs, const char* rhs, size_t count) {
351 return 0 == strncmp(lhs, rhs, count);
352 }
353
354
355
356
357
358 int _sir_strncpy(char* restrict dest, size_t destsz,
359 const char* restrict src, size_t count);
360
361
362
363
364
365 int _sir_strncat(char* restrict dest, size_t destsz,
366 const char* restrict src, size_t count);
367
368
369
370
371
372 int _sir_fopen(FILE* restrict* restrict streamptr, const char* restrict filename,
373 const char* restrict mode);
374
375
376
377
378
379 static inline
380 struct tm* _sir_localtime(const time_t* timer, struct tm* buf) {
381 if (!timer || !buf)
382 return NULL;
383 # if defined(__HAVE_STDC_SECURE_OR_EXT1__) && !defined(__EMBARCADEROC__)
384 # if !defined(__WIN__)
385 struct tm* ret = localtime_s(timer, buf);
386 if (!ret) {
387 (void)_sir_handleerr(errno);
388 return NULL;
389 }
390 # else
391 errno_t ret = localtime_s(buf, timer);
392 if (0 != ret) {
393 (void)_sir_handleerr(ret);
394 return NULL;
395 }
396 # endif
397 return buf;
398 # else
399 # if !defined(__WIN__) || \
400 (defined(__EMBARCADEROC__) && (__clang_major__ < 15))
401 struct tm* ret = localtime_r(timer, buf);
402 # else
403 struct tm* ret = localtime(timer);
404 # endif
405 if (!ret)
406 (void)_sir_handleerr(errno);
407 return ret;
408 # endif
409 }
410
411
412 # if defined(__GNUC__)
413 __attribute__ ((format (strftime, 3, 0)))
414 # endif
415 static inline
416 bool _sir_formattime(time_t now, char* buffer, const char* format) {
417 if (!buffer || !format)
418 return false;
419 # if defined(__GNUC__) && !defined(__clang__) && \
420 !(defined(__OPEN64__) || defined(__OPENCC__) || defined(__PCC__))
421 # pragma GCC diagnostic push
422 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
423 # endif
424 struct tm timebuf;
425 const struct tm* ptb = _sir_localtime(&now, &timebuf);
426 return NULL != ptb && 0 != strftime(buffer, SIR_MAXTIME, format, ptb);
427 # if defined(__GNUC__) && !defined(__clang__) && \
428 !(defined(__OPEN64__) || defined(__OPENCC__) || defined(__PCC__))
429 # pragma GCC diagnostic pop
430 # endif
431 }
432
433
434
435
436
437 bool _sir_getchar(char* input);
438
439
440
441
442 # define _sir_snprintf_trunc(dst, size, ...) \
443 do { \
444 volatile size_t _n = size; \
445 if (!snprintf(dst, _n, __VA_ARGS__)) { (void)_n; }; \
446 } while (false)
447
448
449
450
451 static inline
452 uint32_t FNV32_1a(const uint8_t* data, size_t len) {
453 uint32_t hash = 2166136261U;
454 for (size_t n = 0; n < len; n++) {
455 hash ^= (uint32_t)data[n];
456 hash = (uint32_t)(hash * 16777619ULL);
457 }
458 return hash;
459 }
460
461
462
463
464
465 # if defined(__clang__)
466 SANITIZE_SUPPRESS("unsigned-integer-overflow")
467 # endif
468 static inline
469 uint64_t FNV64_1a(const char* str) {
470 uint64_t hash = 14695981039346656037ULL;
471 for (const char* c = str; *c; c++) {
472 hash ^= (uint64_t)(unsigned char)(*c);
473 hash *= 1099511628211ULL;
474 }
475 return hash;
476 }
477
478
479
480
481
482 void* _sir_explicit_memset(void *ptr, int c, size_t len);
483
484
485
486
487 char* _sir_strremove(char *str, const char *sub);
488
489
490
491
492
493
494 char* _sir_strsqueeze(char *str);
495
496
497
498
499 char* _sir_strredact(char *str, const char *sub, const char c);
500
501
502
503
504 char* _sir_strreplace(char *str, const char c, const char n);
505
506
507
508
509
510 size_t _sir_strcreplace(char *str, const char c, const char n, int32_t max);
511
512 # if defined(__cplusplus)
513 }
514 # endif
515
516 #endif