-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.h
executable file
·247 lines (221 loc) · 8.5 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef __UTILS_H__
#define __UTILS_H__
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <sys/time.h>
/* Taken from Python 3.8 source code which is GPL compatible */
#define Py_RETURN_RICHCOMPARE_internal(val1, val2, op) \
do { \
switch (op) { \
case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
default: \
Py_UNREACHABLE(); \
} \
} while (0)
#define SAFE_OPTIONAL_COMPARE(val1, val2) \
({ \
bool result = false; \
if((val1) == NULL && (val2) == NULL) \
result = true; \
else if((val1) != NULL && (val2) != NULL) { \
if(*(val1) == *(val2)) \
result = true; \
} \
result; \
})
#define TIME_MARK_START(start_marker) \
struct timeval tv_mark_##start_marker; \
gettimeofday(&tv_mark_##start_marker, 0)
#define TIME_CHECK_ON(start_marker,end_marker) do { \
struct timeval tv_mark_##start_marker##_##end_marker; \
gettimeofday(&tv_mark_##start_marker##_##end_marker, 0); \
printf("@Elapsed ("#start_marker" -> "#end_marker"): (%f)[s]\n", \
(double) (tv_mark_##start_marker##_##end_marker.tv_usec - tv_mark_##start_marker.tv_usec) / 1000000 + \
(double) (tv_mark_##start_marker##_##end_marker.tv_sec - tv_mark_##start_marker.tv_sec) ); \
} while(0)
#define TIME_CHECK_FMT(start_marker,end_marker,fmt) do { \
struct timeval tv_mark_##start_marker##_##end_marker; \
gettimeofday(&tv_mark_##start_marker##_##end_marker, 0); \
printf(fmt, \
(double) (tv_mark_##start_marker##_##end_marker.tv_usec - tv_mark_##start_marker.tv_usec) / 1000000 + \
(double) (tv_mark_##start_marker##_##end_marker.tv_sec - tv_mark_##start_marker.tv_sec) ); \
} while(0)
#define ERRMSG_BUFFER_SIZE 8192
#define ASSERT_WITH_NFSDB_ERROR(__expr,__msg,...) do { \
if (!(__expr)) { \
PyErr_SetString(libetrace_nfsdbError, __msg); \
return 0; \
} \
} while(0)
#define ASSERT_BREAK_WITH_NFSDB_ERROR(__expr,__msg,...) { \
if (!(__expr)) { \
PyErr_SetString(libetrace_nfsdbError, __msg); \
break; \
} \
}
#define ASSERT_CLEANUP_WITH_NFSDB_ERROR(__expr,__msg,...) do { \
if (!(__expr)) { \
PyErr_SetString(libetrace_nfsdbError, __msg); \
goto cleanup_and_err; \
} \
} while(0)
#define ASSERT_WITH_NFSDB_FORMAT_ERROR(__expr,__msg,...) do { \
if (!(__expr)) { \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,__msg,__VA_ARGS__); \
PyErr_SetString(libetrace_nfsdbError, errmsg); \
return 0; \
} \
} while(0)
#define ASSERT_BREAK_WITH_NFSDB_FORMAT_ERROR(__expr,__msg,...) { \
if (!(__expr)) { \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,__msg,__VA_ARGS__); \
PyErr_SetString(libetrace_nfsdbError, errmsg); \
break; \
} \
}
#define ASSERT_CLEANUP_WITH_NFSDB_FORMAT_ERROR(__expr,__msg,...) do { \
if (!(__expr)) { \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,__msg,__VA_ARGS__); \
PyErr_SetString(libetrace_nfsdbError, errmsg); \
goto cleanup_and_err; \
} \
} while(0)
#define ASSERT_RETURN_WITH_NFSDB_FORMAT_ERROR(__expr,__msg,__retcode,...) do { \
if (!(__expr)) { \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,__msg,__VA_ARGS__); \
PyErr_SetString(libetrace_nfsdbError, errmsg); \
return __retcode; \
} \
} while(0)
#define ASSERT_WITH_FTDB_ERROR(__expr,__msg,...) do { \
if (!(__expr)) { \
PyErr_SetString(libftdb_ftdbError, __msg); \
return 0; \
} \
} while(0)
#define ASSERT_WITH_FTDB_FORMAT_ERROR(__expr,__msg,...) do { \
if (!(__expr)) { \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,__msg,__VA_ARGS__); \
PyErr_SetString(libftdb_ftdbError, errmsg); \
return 0; \
} \
} while(0)
#define ASSERT_RETURN_WITH_FTDB_FORMAT_ERROR(__expr,__msg,__retcode,...) do { \
if (!(__expr)) { \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,__msg,__VA_ARGS__); \
PyErr_SetString(libftdb_ftdbError, errmsg); \
return __retcode; \
} \
} while(0)
#define CHECK_ASSIGN_FILE_FILTER_FUNC_WITH_FTDB_FORMAT_ERROR(__fltkey,...) do { \
if (PyDict_Contains(self->libetrace_nfsdb_entry_openfile_filterMap,__fltkey)) { \
PyObject* py_fp = PyDict_GetItem(self->libetrace_nfsdb_entry_openfile_filterMap,__fltkey); \
nfsdb_entry_openfile_filter_func __func = (nfsdb_entry_openfile_filter_func)PyLong_AsUnsignedLong(py_fp); \
__VA_ARGS__ \
} \
else { \
const char* __fltkey_s__ = PyString_get_c_str(__fltkey); \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,"Invalid built-in file filter specification: %s",__fltkey_s__); \
PYASSTR_DECREF(__fltkey_s__); \
PyErr_SetString(libetrace_nfsdbError, errmsg); \
goto cleanup_and_err; \
} \
} while(0)
#define CHECK_ASSIGN_COMMAND_FILTER_FUNC_WITH_FTDB_FORMAT_ERROR(__fltkey,...) do { \
if (PyDict_Contains(self->libetrace_nfsdb_entry_command_filterMap,__fltkey)) { \
PyObject* py_fp = PyDict_GetItem(self->libetrace_nfsdb_entry_command_filterMap,__fltkey); \
nfsdb_entry_command_filter_func __func = (nfsdb_entry_command_filter_func)PyLong_AsUnsignedLong(py_fp); \
__VA_ARGS__ \
} \
else { \
const char* __fltkey_s__ = PyString_get_c_str(__fltkey); \
snprintf(errmsg,ERRMSG_BUFFER_SIZE,"Invalid built-in command filter specification: %s",__fltkey_s__); \
PYASSTR_DECREF(__fltkey_s__); \
PyErr_SetString(libetrace_nfsdbError, errmsg); \
goto cleanup_and_err; \
} \
} while(0)
#define KWARGS_HAVE(__kwargs__,__kwarg_s__) ({ \
int __have__ = 0; \
if (__kwargs__) { \
PyObject* __key__ = PyUnicode_FromString(__kwarg_s__); \
__have__ = PyDict_Contains(__kwargs__,__key__); \
Py_DecRef(__key__); \
} \
__have__; \
})
#define KWARGS_GET(__kwargs__,__kwarg_s__) ({ \
PyObject* __key__ = PyUnicode_FromString(__kwarg_s__); \
PyObject* __item__ = PyDict_GetItem(__kwargs__,__key__); \
Py_DecRef(__key__); \
__item__; \
})
#define DBG(n,...) do { if (n) printf(__VA_ARGS__); } while(0)
#ifndef DBGC
#define DBGC(...)
#endif
#define lazy_alloc_output() do { \
if (!output) { \
output = __output = malloc(2*len+1); \
memcpy(output,s,i); \
output+=i; \
} \
} while(0)
static inline char* strappend(char* s, const char* news) {
size_t ssize = strlen(s);
size_t newsize = strlen(news);
s = (char*)realloc(s,ssize+newsize+1);
assert(s);
memcpy(s+ssize,news,newsize+1);
return s;
}
static inline char* strnappend(char* s, const char* news, size_t newsize) {
size_t ssize = strlen(s);
s = (char*)realloc(s,ssize+newsize+1);
assert(s);
memcpy(s+ssize,news,newsize);
s[ssize+newsize] = 0;
return s;
}
#if defined __cplusplus
extern "C" {
#endif
const char* json_escape(const char* s);
char *normpath(const char *in);
const char* path_join(const char* start, const char* end);
unsigned char* base64_decode (const char *text, size_t* out_len);
char * base64_encode (const unsigned char* data, size_t len);
enum format_type {
FORMAT_TYPE_CHAR,
FORMAT_TYPE_STR,
FORMAT_TYPE_PTR,
FORMAT_TYPE_LONG_LONG,
FORMAT_TYPE_ULONG,
FORMAT_TYPE_LONG,
FORMAT_TYPE_SIZE_T,
FORMAT_TYPE_PTRDIFF,
FORMAT_TYPE_UBYTE,
FORMAT_TYPE_BYTE,
FORMAT_TYPE_USHORT,
FORMAT_TYPE_SHORT,
FORMAT_TYPE_UINT,
FORMAT_TYPE_INT,
FORMAT_TYPE_WIDTH,
FORMAT_TYPE_PRECISION,
/* Internal */
FORMAT_TYPE_NONE,
FORMAT_TYPE_PERCENT_CHAR,
FORMAT_TYPE_INVALID,
};
int vsnprintf_parse_format(enum format_type** par_type, size_t count, const char *fmt);
#if defined __cplusplus
}
#endif
#endif /* __UTILS_H__ */