stopif.h 755 B

123456789101112131415161718
  1. // Borrowed from https://github.com/b-k/21st-Century-Examples
  2. #include <stdio.h>
  3. #include <stdlib.h> //abort
  4. /** Set this to \c 's' to stop the program on an error.
  5. Otherwise, functions return a value on failure.*/
  6. char error_mode;
  7. /** To where should I write errors? If this is \c NULL, write to \c stderr. */
  8. FILE *error_log;
  9. #define Stopif(assertion, error_action, ...) { \
  10. if (assertion){ \
  11. fprintf(error_log ? error_log : stderr, __VA_ARGS__); \
  12. fprintf(error_log ? error_log : stderr, "\n"); \
  13. if (error_mode=='s') abort(); \
  14. else {error_action;} \
  15. } }