-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
93 lines (85 loc) · 5.44 KB
/
util.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include <stdio.h>
#define MSG(fmt, ...) fprintf(stderr, "%s(%d): " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
// ERR() macro reports the error. In C we just print it out. In Python we set
// the Python error
#ifndef ERR
#define ERR(fmt, ...) MSG("Failure!!! " fmt, ##__VA_ARGS__)
#endif
#define try(expr, ...) do { \
if(ctx->verbose) \
MSG("ctx=%p: Evaluating '" #expr "'", ctx); \
if(!(expr)) \
{ \
ERR("'" #expr "' is false" __VA_ARGS__); \
goto done; \
} \
} while(0)
#define try_arv(expr) do { \
if(ctx->verbose) \
MSG("ctx=%p: Calling '" #expr "'", ctx); \
expr; \
if(error != NULL) \
{ \
ERR("'" #expr "' produced '%s'", \
error->message); \
g_clear_error(&error); \
goto done; \
} \
} while(0)
#define try_arv_extra_reporting(expr, extra_verbose_before, extra_verbose_after, extra_err) do { \
if(ctx->verbose) \
{ \
extra_verbose_before; \
MSG("ctx=%p: Calling '" #expr "'", ctx); \
} \
expr; \
if(ctx->verbose) \
{ \
extra_verbose_after; \
} \
if(error != NULL) \
{ \
ERR("'" #expr "' produced '%s'", \
error->message); \
extra_err; \
g_clear_error(&error); \
goto done; \
} \
} while(0)
// THIS MACRO MAY LEAVE error ALLOCATED. YOU NEED TO g_clear_error() yourself
#define try_arv_or(expr, condition) do { \
if(ctx->verbose) \
MSG("ctx=%p: Calling '" #expr "'", ctx); \
expr; \
if(error != NULL) \
{ \
if(!(condition)) \
{ \
ERR("'" #expr "' produced '%s'", \
error->message); \
g_clear_error(&error); \
goto done; \
} \
else if(ctx->verbose) \
MSG(" failed ('%s'), but extra condition '" #condition "' is true, so this failure is benign", \
error->message); \
} \
} while(0)
#define try_arv_and(expr, condition) do { \
if(ctx->verbose) \
MSG("ctx=%p: Calling '" #expr "'", ctx); \
expr; \
if(error != NULL) \
{ \
ERR("'" #expr "' produced '%s'", \
error->message); \
g_clear_error(&error); \
goto done; \
} \
if(!(condition)) \
{ \
ERR("'" #expr "' produced no error, but the extra condition '" #condition "' failed"); \
goto done; \
} \
} while(0)