gol

Implementation of Conway's Game of Life writen in C
git clone git://git.dimitrijedobrota.com/gol.git
Log | Files | Refs | README

utils.h (1325B)


      1 /**
      2  * @file utils.h
      3  * @author Dimitrije Dobrota
      4  * @date 19 June 2022
      5  * @brief Common macros for use in other modules
      6  */
      7 
      8 #ifndef UTILS_H
      9 #define UTILS_H
     10 
     11 #include <stdarg.h>
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <curses.h>
     15 
     16 /// maximum of the two numbers
     17 #define MAX(a, b) ((a > b) ? a : b)
     18 
     19 /// minimum of the two numbers
     20 #define MIN(a, b) ((a < b) ? a : b)
     21 
     22 /// clamp the number a between x and y
     23 #define CLAMP(a, x, y) ((a) = (MAX(x, MIN(a, y))))
     24 
     25 /// clamp the value of a between x and y without assignment
     26 #define ACLAMP(a, x, y) (MAX(x, MIN(a, y)))
     27 
     28 /// clamp with wrapping
     29 #define WCLAMP(a, x) ((a + x) % x)
     30 
     31 /// Check if compiled with Unicode support
     32 #ifdef NO_UNICODE
     33 #define UNICODE 0
     34 #else
     35 #define UNICODE 1
     36 #endif
     37 
     38 static void err(char *fmt, ...) {
     39   va_list ap;
     40   va_start(ap, fmt);
     41   vfprintf(stderr, fmt, ap);
     42   va_end(ap);
     43   exit(1);
     44 }
     45 
     46 /// Check for memory error, and abort
     47 #define MEM_CHECK(x)                                                           \
     48   if ((x) == NULL)                                                             \
     49     err("MEMORY ERROR");
     50 
     51 /// Check for file error, and abort
     52 #define FILE_CHECK(x)                                                          \
     53   if ((x) == NULL)                                                             \
     54     err("MEMORY ERROR");
     55 
     56 #endif