#ifndef X_GENERAL_WRAPPER
#define X_GENERAL_WRAPPER

#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>

#define STATIC_STRLEN(x) (sizeof(x)/sizeof(char) - 1)
#define ARRAY_LEN(x,t)   (sizeof(x)/sizeof(t))

int     xatol (const char* str, long* result);
void*   xmalloc (size_t size);
void*   xrealloc (void* ptr, size_t size);
bool    xstrend (const char* str, const char* suffix);
char*   xstrcat (const char* str, ...);
char*   xstrdup (const char* str);
char*   xsprintf (const char* format, ...);
int     xfaddfd (int fd, int fdflags);
int     xfdelfd (int fd, int fdflags);
int     xfaddfl (int fd, int flflags);
int     xfdelfl (int fd, int flflags);
char*   xreadlink (const char* filename);
char*   xgetcwd (void);
char*   xgetexe (void);
char*   xgetres (const char* filename);
size_t  xwrite (int fd, const char* str, size_t size);

static inline int xmax (int a, int b) {
	return a > b ? a : b;
}
static inline int xmin (int a, int b) {
	return a < b ? a : b;
}

#define XBUFSIZ 512

typedef struct {
	char    buf[XBUFSIZ];
	off_t   buf_start;
	off_t   buf_len;
	char*   buf_line;
	ssize_t buf_line_len;
	int     buf_error : 1;
	int     buf_eof   : 1;
} XBuf;

void    xbufinit (XBuf* buf);
char*   xgetline (int fd, XBuf* buf, int delim);

#endif /* X_GENERAL_WRAPPER */