
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#ifndef __UTILS_H__
#define __UTILS_H__

/**
 * Test \a expr for C error and kill app on error
 *
 * \param expr Expression to test. If expr != 0 => error
 * \param messgae Error message
 */
#define C_ERROR(expr, message)           \
  do {                                   \
    assert(message != NULL);             \
    if(expr) {                           \
      printf("Error %s in file %s:%d\n", \
        message, __FILE__, __LINE__);    \
      exit(1);                           \
    }                                    \
  } while(0)

#define OPEN(fp, name, mode)                                   \
  do {                                                         \
    assert(name != NULL);                                      \
    assert(mode != NULL);                                      \
		fp = fopen(name, mode);                                    \
		if(fp == NULL) {                                           \
      int error = errno;                                       \
			printf("Can't open file %s, errno=%d. Error on %s:%d\n", \
				name, errno, __FILE__, __LINE__);                      \
      printf("\tDescription: %s\n", strerror(error));          \
			exit(1);                                                 \
		}                                                          \
	} while(0)

#endif
