-
Notifications
You must be signed in to change notification settings - Fork 4
/
cexc.h
239 lines (213 loc) · 8.95 KB
/
cexc.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
/* INFO ************************************************************************
** **
** cutils **
** ====== **
** **
** Modern and Lightweight C Utilities **
** Version: 0.8.90.484 (20140819) **
** **
** File: cexc.h **
** **
** For more information about the project, visit <http://www.cutils.org>. **
** Copyright (C) 2014 Peter Varo **
** **
** This program is free software: you can redistribute it and/or modify it **
** under the terms of the GNU General Public License as published by the **
** Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, but **
** WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. **
** See the GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program, most likely a file in the root directory, **
** called 'LICENSE'. If not, see <http://www.gnu.org/licenses>. **
** **
************************************************************************ INFO */
#ifndef _C_EXCEPTIONS_H_5362957038900146_
#define _C_EXCEPTIONS_H_5362957038900146_
#include <stdio.h> /* size_t, stdout, stderr, fprintf() */
#include <stdlib.h> /* malloc(), realloc(), free() */
#include <string.h> /* strncpy() */
#include <stdbool.h> /* bool, true, false */
#undef cutils_cexc_start_exception_handling
#undef cutils_cexc_stop_exception_handling
#undef cutils_cexc_set_exception_handling_pointer
#undef cutils_cexc_set_exception_handling_custom_raise
#undef cutils_cexc_set_exception_handling_custom_catch
#undef cutils_cexc_raise
#undef cutils_cexc_catch
#undef cutils_cexc_craise
#undef cutils_cexc_ccatch
#ifdef CEXC_OFF
#define cutils_cexc_start_exception_handling(out)
#define cutils_cexc_stop_exception_handling()
#define cutils_cexc_raise(msg, len)
#define cutils_cexc_catch()
#define cutils_cexc_set_exception_handling_pointer(ptr)
#define cutils_cexc_set_exception_handling_custom_raise(func)
#define cutils_cexc_set_exception_handling_custom_catch(func)
#define cutils_cexc_craise(msg, len)
#define cutils_cexc_ccatch()
#else
#undef __CEXC_PREFIX
#undef __CEXC_NO_ERROR
#undef __CEXC_INTERNAL
#define __CEXC_PREFIX "EXCEPTION: "
#define __CEXC_NO_ERROR "NO ERROR"
#define __CEXC_INTERNAL "INTERNAL"
/*----------------------------------------------------------------------------*/
typedef struct
{
// Essential values
char *buffer;
size_t chars;
bool error;
FILE *output;
// Custom values
void *custom;
void (*raisecb)(void*);
void (*catchcb)(void*);
} __cexc_ExceptionHandler;
#ifdef CEXC_SET
/*----------------------------------------------------------------------------*/
static inline void __cexc_callback(void *ptr){return;}
static char __CUTILS_CEXC_EXCEPTION_HANDLER_BUFFER[] = __CEXC_NO_ERROR;
/*----------------------------------------------------------------------------*/
__cexc_ExceptionHandler __CUTILS_CEXC_EXCEPTION_HANDLER = {
.chars = sizeof __CEXC_NO_ERROR,
.error = false,
.buffer = __CUTILS_CEXC_EXCEPTION_HANDLER_BUFFER,
.custom = (void *)NULL,
.raisecb = __cexc_callback,
.catchcb = __cexc_callback
};
#else
extern __cexc_ExceptionHandler __CUTILS_CEXC_EXCEPTION_HANDLER;
#endif /* CEXC_SET */
/*----------------------------------------------------------------------------*/
static inline void
cutils_cexc_start_exception_handling(FILE *output)
{
__CUTILS_CEXC_EXCEPTION_HANDLER.output = output;
/* Try to dynamically allocate space for buffer */
char *buffer = malloc(sizeof __CEXC_NO_ERROR);
if (!buffer)
{
__CUTILS_CEXC_EXCEPTION_HANDLER.error = true;
__CUTILS_CEXC_EXCEPTION_HANDLER.buffer = __CEXC_INTERNAL;
return;
}
/* Reset error message and set newly allocated space */
strncpy(buffer, __CEXC_NO_ERROR, sizeof __CEXC_NO_ERROR);
__CUTILS_CEXC_EXCEPTION_HANDLER.buffer = buffer;
}
/*----------------------------------------------------------------------------*/
static inline void
cutils_cexc_stop_exception_handling(void)
{
/* If the initialisation was successful */
if (strncmp(__CUTILS_CEXC_EXCEPTION_HANDLER.buffer,
__CEXC_INTERNAL,
sizeof __CEXC_INTERNAL))
free(__CUTILS_CEXC_EXCEPTION_HANDLER.buffer);
}
/*----------------------------------------------------------------------------*/
static inline void
cutils_cesc_set_exception_handling_pointer(void *pointer)
{
__CUTILS_CEXC_EXCEPTION_HANDLER.custom = pointer;
}
/*----------------------------------------------------------------------------*/
static inline void
cutils_cexc_set_exception_handling_custom_catch(void (*callback)(void*))
{
__CUTILS_CEXC_EXCEPTION_HANDLER.catchcb = callback;
}
/*----------------------------------------------------------------------------*/
static inline void
cutils_cexc_set_exception_handling_custom_raise(void (*callback)(void*))
{
__CUTILS_CEXC_EXCEPTION_HANDLER.raisecb = callback;
}
/*----------------------------------------------------------------------------*/
/* TODO: OPTION 1: catch returns 1 if error, else 0, CON: if func removed by macro?
OPTION 2: use ccatch for that? (the callback does what the
`if (catch())` would have done) */
#ifdef CEXC_LOG
#define cutils_cexc_catch(...)
#else
static inline void
cutils_cexc_catch(void)
{
/* Write buffer data to output */
fprintf(__CUTILS_CEXC_EXCEPTION_HANDLER.output,
__CEXC_PREFIX "%s\n",
__CUTILS_CEXC_EXCEPTION_HANDLER.buffer);
/* Clear buffer */
strncpy(__CUTILS_CEXC_EXCEPTION_HANDLER.buffer,
__CEXC_NO_ERROR,
sizeof __CEXC_NO_ERROR);
__CUTILS_CEXC_EXCEPTION_HANDLER.error = false;
}
#endif /* CEXC_LOG */
/*----------------------------------------------------------------------------*/
static inline void
cutils_cexc_raise(const char *message,
size_t length)
{
#ifdef CEXC_LOG
fprintf(__CUTILS_CEXC_EXCEPTION_HANDLER.output,
__CEXC_PREFIX "%s\n",
message);
#else
/* Raise new error only, if no previous error raised */
if (__CUTILS_CEXC_EXCEPTION_HANDLER.error) return;
/* Switch error flag */
__CUTILS_CEXC_EXCEPTION_HANDLER.error = true;
/* Check if buffer has enough size */
if ((length + 1) > __CUTILS_CEXC_EXCEPTION_HANDLER.chars)
{
/* If buffer is too small resize it */
char *buffer = realloc(__CUTILS_CEXC_EXCEPTION_HANDLER.buffer,
2 * length * sizeof(char));
if (!buffer)
{
strncpy(__CUTILS_CEXC_EXCEPTION_HANDLER.buffer,
__CEXC_INTERNAL, sizeof __CEXC_INTERNAL);
return;
}
/* Update size information and point at new memory block */
__CUTILS_CEXC_EXCEPTION_HANDLER.chars = length;
__CUTILS_CEXC_EXCEPTION_HANDLER.buffer = buffer;
}
/* Set exception buffer */
strncpy(__CUTILS_CEXC_EXCEPTION_HANDLER.buffer, message, length);
/* Make sure it is null-terminated */
if (length < __CUTILS_CEXC_EXCEPTION_HANDLER.chars)
__CUTILS_CEXC_EXCEPTION_HANDLER.buffer[length] = '\0';
#endif /* CEXC_LOG */
}
/*----------------------------------------------------------------------------*/
#ifdef CEXC_LOG
#define cutils_cexc_ccatch(...)
#else
static inline void
cutils_cexc_ccatch(void)
{
cutils_cexc_catch();
__CUTILS_CEXC_EXCEPTION_HANDLER.catchcb(__CUTILS_CEXC_EXCEPTION_HANDLER.custom);
}
#endif /* CEXC_LOG */
/*----------------------------------------------------------------------------*/
static inline void
cutils_cexc_craise(const char *message,
size_t length)
{
cutils_cexc_raise(message, length);
__CUTILS_CEXC_EXCEPTION_HANDLER.raisecb(__CUTILS_CEXC_EXCEPTION_HANDLER.custom);
}
#endif /* CEXC_OFF */
#endif /* _C_EXCEPTIONS_H_5362957038900146_ */