From 4bd84d27ec94e5d591a4a8d1bdd035796092207e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D1=80=D0=B0=D1=85=D0=B8=D1=9A=D0=B0=20=D0=A0?= =?UTF-8?q?=D0=B0=D0=B4=D0=B8=D1=9B?= Date: Sat, 6 Jul 2024 12:23:21 +0000 Subject: [PATCH] defs.h: Move macros to defs.h; use perror(3) --- defs.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/defs.h b/defs.h index 7ee3d23..703f6a8 100644 --- a/defs.h +++ b/defs.h @@ -12,9 +12,78 @@ #define ANSI_SGR_BOLD_ON "\033[1m" #define ANSI_SGR_BOLD_OFF "\033[0m" +#define LEN(x) (sizeof(x) / sizeof(x[0])) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define UNUSED(x) ((void)(x)) +#define ENSURE_SIZE(to, temp, tosize, testsize, fromsize, type) \ + do \ + { \ + if (!to || tosize < testsize) \ + { \ + tosize = fromsize; \ + temp = realloc(to, tosize * sizeof(type)); \ + if (!temp) \ + { \ + perror(PROGRAMNAME ": realloc"); \ + exit(1); \ + } \ + to = temp; \ + } \ + } while (0) + +#define CALLOC(ptr, ptrtype, nmemb) \ + do \ + { \ + ptr = calloc(nmemb, sizeof(ptrtype)); \ + if (!ptr) \ + { \ + perror(PROGRAMNAME ": calloc"); \ + exit(1); \ + } \ + } while (0) + +#define REALLOC(ptr, ptrtype, newsize) \ + do \ + { \ + ptrtype* newptr = realloc(ptr, newsize); \ + if (!newptr) \ + { \ + perror(PROGRAMNAME ": realloc"); \ + exit(1); \ + } \ + ptr = newptr; \ + } while (0) + +/* Copy *parg to *ptoken and increase ptoken */ +#define CHECKCOPY(token, ptoken, token_size, parg) \ + do \ + { \ + if (ptoken + 2 > token + token_size) \ + { \ + size_t old_size = token_size; \ + token_size += BUF_DELTA; \ + REALLOC(token, char, token_size); \ + ptoken = token + old_size - 1; \ + } \ + *ptoken++ = *parg++; \ + } while (0) + +/* Set *pformat to num and increase pformat */ +#define CHECKSET(format, pformat, format_size, num) \ + do \ + { \ + if (pformat + 2 > format + format_size) \ + { \ + size_t old_size = format_size; \ + format_size += BUF_DELTA; \ + REALLOC(format, int, format_size); \ + pformat = format + old_size - 1; \ + } \ + *pformat++ = num; \ + } while (0) + typedef enum { CMD_NONE, CMD_ALIGN, -- 2.45.2