Tchatator413
TripEnArvor instant messaging protocol - JSON-based
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1
5
6#ifndef UTIL_H
7#define UTIL_H
8
9#include <stdarg.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
15#define QUOTE(name) #name
17#define STR(macro) QUOTE(macro)
18
20#define CAT(x, y) CAT_(x, y)
22#define CAT_(x, y) x##y
23
25#define buffer_size(format, ...) (snprintf(NULL, 0, (format), __VA_ARGS__) + 1) // safe byte for \0
26
34#define streq(x, y) (strcmp((x), (y)) == 0)
35
44#define strneq(x, y, n) (strncmp((x), (y), (n)) == 0)
45
54#define MAX(a, b) ((a) > (b) ? (a) : (b))
55
64#define MIN(a, b) ((a) < (b) ? (a) : (b))
65
74#define COALESCE(a, b) ((a == NULL) ? (b) : (a))
75
79#define errno_exit(of) \
80 do { \
81 perror(of); \
82 exit(EXIT_FAILURE); \
83 } while (0)
84
91#define array_len(array) (sizeof(array) / sizeof((array)[0]))
92
93#ifdef __clang__
94#define ATTR_FLAG_ENUM [[clang::flag_enum]]
95#else
96#define ATTR_FLAG_ENUM
97#endif // __clang__
98
99#ifdef __GNUC__
100#define ATTR_FORMAT(archetype, string_index, first_to_check) __attribute__((format(archetype, string_index, first_to_check)))
101#else
102#define ATTR_FORMAT(archetype, string_index, first_to_check)
103#endif // __GNUC__
104
114char *strfmt(const char *fmt, ...) ATTR_FORMAT(printf, 1, 2);
115
125char *vstrfmt(const char *fmt, va_list ap);
126
133char *fslurp(FILE *fp);
134
135#if __STDC_VERSION__ < 202000L
136#define unreachable() abort()
137#else
138#include <stddef.h>
139#endif
140
141#endif // UTIL_H
char * fslurp(FILE *fp)
Slurps a stream into a dynamically allocated string.
char char * vstrfmt(const char *fmt, va_list ap)
Formats a string using a printf-style format string and a va_list of arguments.
char * strfmt(const char *fmt,...) ATTR_FORMAT(printf
Formats a string using a printf-style format string and arguments.