polonium/include/common.h

98 lines
3.1 KiB
C

#ifndef _COMMON_H
#define _COMMON_H
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
/* enums */
enum configurations_bitshift {
C_BITSHIFT_CONFIRM = 0,
C_BITSHIFT_CONTENTS,
C_BITSHIFT_LINE_ENDINGS,
C_BITSHIFT_PRINTABLE,
C_BITSHIFT_CUSTOM_SEED
};
enum configurations {
C_CONFIRM = 1 << C_BITSHIFT_CONFIRM,
C_CONTENTS = 1 << C_BITSHIFT_CONTENTS,
C_LINE_ENDINGS = 1 << C_BITSHIFT_LINE_ENDINGS,
C_PRINTABLE = 1 << C_BITSHIFT_PRINTABLE,
C_CUSTOM_SEED = 1 << C_BITSHIFT_CUSTOM_SEED
};
/* macros: procedures */
#define FPRINTF_MACRO(stream, ...) do { \
fflush(stdout); \
fprintf(stream, __VA_ARGS__); \
} while (0)
#define PERROR_MACRO(s) do { \
int errnum = errno; \
char* err_msg = strerror(errnum); /* XXX: Thread race possible */ \
fflush(stdout); \
fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, (s), err_msg); \
} while (0)
#define FATAL_ERROR_PRINT(...) do { \
FPRINTF_MACRO(stderr, __VA_ARGS__); \
putc('\n', stderr); \
} while (0)
#define FATAL_ERROR(...) do { \
FATAL_ERROR_PRINT(__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
#define FATAL_ERROR_RET(...) do { \
FATAL_ERROR_PRINT(__VA_ARGS__); \
return EXIT_FAILURE; \
} while (0)
/* macros: definitions */
#ifndef SIZE_T_C
# if defined(UINT64_MAX) && SIZE_MAX == UINT64_MAX
# define SIZE_T_C(x) UINT64_C(x)
# elif defined(UINT32_MAX) && SIZE_MAX == UINT32_MAX
# define SIZE_T_C(x) UINT32_C(x)
# else
# define SIZE_T_C(x) ((size_t) x)
# endif
#endif
#ifndef SIZE_MAX
# define SIZE_MAX (~SIZE_T_C(0))
#endif
#define STR_YN_YES "yes"
#define STR_YN_NO "no"
#ifdef HAVE_NO_FSEEKO_FTELLO
# define FSEEK_MACRO fseek
# define FTELL_MACRO ftell
typedef long file_offset_t;
# define FILE_OFFSET_C(x) ((x)##L)
#else
# define FSEEK_MACRO fseeko
# define FTELL_MACRO ftello
typedef off_t file_offset_t;
# define FILE_OFFSET_C(x) ((off_t) x)
#endif
/* macros: lambdas */
#define STRINGIZE(x) #x
#define INT2STR(x) STRINGIZE(x)
#define YES_NO(x) ((x) ? STR_YN_YES : STR_YN_NO)
#define READ_CONFIG(x) (config & (x))
#define SET_CONFIG(x) config |= (x)
#define CLEAR_CONFIG(x) config &= ~(x)
#define LOOP_ACTION_CONFIG(f, x) \
f(x); \
continue
#endif /* _COMMON_H */