aboutsummaryrefslogtreecommitdiff
path: root/snag/src/util.h
blob: 0c684a401f0e26f20509eb2838b941998bbe0ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef UTIL_H
#define UTIL_H

#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>

// utility for errors
#if !DEBUG
    void die(const char* format, ...) __attribute__((format(printf, 1, 2)));
    void* emalloc(size_t size, char* alloc_reason);
    #define assert(condition) ((void)0)
#else
    #define die(fmt, ...) _die(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
    void _die(const char* file, int line, const char* format, ...) __attribute__((format(printf, 3, 4)));

    #define emalloc(size, alloc_reason) _emalloc(__FILE__, __LINE__, size, alloc_reason)
    void* _emalloc(const char* file, int line, size_t size, char* alloc_reason);

    #define assert(condition) do { if (!(condition)) _failassert(__FILE__, __LINE__, #condition); } while(0)
    void _failassert(const char* file, int line, char* contents);

#endif

// functions for outputting and displaying text, log is used for nonroutine bad things
void printout(const char* format, ...) __attribute__((format(printf, 1, 2)));
void print(const char* format, ...) __attribute__((format(printf, 1, 2)));

// string functions
#define vastrcat(...) vastrcat_(0, __VA_ARGS__, NULL)
char* vastrcat_(int dummy, ...);

// posix path functions
#define join_path(...) join_path_(0, __VA_ARGS__, NULL)
char* join_path_(int dummy, ...);

#endif