-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception_handler.c
169 lines (149 loc) · 6.31 KB
/
exception_handler.c
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
#include "../include/exception_handler.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#--ADD_TO_INCLUDE
ExceptionResponse *ExceptionHandler_create(bool throws, char *error_message, bool suppress) {
ExceptionResponse *exceptionResponse = (ExceptionResponse *) calloc(1, sizeof(ExceptionResponse));
exceptionResponse->throws = throws;
exceptionResponse->error_message = error_message;
exceptionResponse->suppress = suppress;
return exceptionResponse;
}
char *ExceptionHandler_build_error_message(const char *function_name, const char* field_name, const char *error_message_core) {
const char *init_msg = "\nERROR: on function '";
const char *middle_msg = "'.\nERROR MESSAGE: ";
const int init_msg_len = strlen(init_msg);
const int function_name_len = strlen(function_name);
const int middle_msg_len = strlen(middle_msg);
const int field_name_len = strlen(field_name);
const int error_msg_core_len = strlen(error_message_core);
char *error_message = (char *) malloc(init_msg_len+function_name_len+middle_msg_len+field_name_len+error_msg_core_len);
memcpy(error_message, init_msg, init_msg_len);
memcpy(error_message+init_msg_len, function_name, function_name_len);
memcpy(error_message+init_msg_len+function_name_len, middle_msg, middle_msg_len);
memcpy(error_message+init_msg_len+function_name_len+middle_msg_len, field_name, field_name_len);
memcpy(error_message+init_msg_len+function_name_len+middle_msg_len+field_name_len, error_message_core, error_msg_core_len);
return error_message;
}
ExceptionResponse *f1(char *function, char *field, long value, bool includesZero, bool suppress) {
bool throws = false;
char *error_message = "";
if ((value == 0 && !includesZero) || value < 0) {
throws = true;
error_message = ExceptionHandler_build_error_message(function, field, " must be positive.\n");
}
return ExceptionHandler_create(throws, error_message, suppress);
}
ExceptionResponse *f2(char *function, char *field, void *value, bool suppress) {
bool throws = false;
char *error_message = "";
if (value == NULL) {
throws = true;
error_message = ExceptionHandler_build_error_message(function, field, " is NULL.\n");
}
return ExceptionHandler_create(throws, error_message, suppress);
}
ExceptionResponse *f3(char *function, char *field, void *value, bool (*is_empty_function)(void *data), bool suppress) {
bool throws = false;
char *error_message = "";
if (is_empty_function(value)) {
throws = true;
error_message = ExceptionHandler_build_error_message(function, field, " is empty.\n");
}
return ExceptionHandler_create(throws, error_message, suppress);
}
ExceptionResponse *f4(char *function, char *field, void *value, bool (*is_full_function)(void *data), bool suppress) {
bool throws = false;
char *error_message = "";
if (is_full_function(value)) {
throws = true;
error_message = ExceptionHandler_build_error_message(function, field, " is full.\n");
}
return ExceptionHandler_create(throws, error_message, suppress);
}
ExceptionResponse *f5(char *function, char *field, void *value, bool (*is_sorted_function)(void *data), bool suppress) {
bool throws = false;
char *error_message = "";
if (!is_sorted_function(value)) {
throws = true;
error_message = ExceptionHandler_build_error_message(function, field, " is not sorted.\n");
}
return ExceptionHandler_create(throws, error_message, suppress);
}
ExceptionResponse *f6(char *function, char *field, long value, long max_index, bool suppress) {
bool throws = false;
char *error_message = "";
if (value < 0 || value > max_index) {
throws = true;
error_message = ExceptionHandler_build_error_message(function, field, " is Out Of Bounds.\n");
}
return ExceptionHandler_create(throws, error_message, suppress);
}
function_declaration(ExceptionResponse *, ExceptionHandler_is_non_positive) {
char *function = args.function;
char *field = args.field;
long value = args.value;
bool set_var(includesZero, false)
bool set_var(suppress, false)
return f1(function, field, value, includesZero, suppress);
}
function_declaration(ExceptionResponse *, ExceptionHandler_is_null) {
char *function = args.function;
char *field = args.field;
void *value = args.value;
bool set_var(suppress, false)
return f2(function, field, value, suppress);
}
function_declaration(ExceptionResponse *, ExceptionHandler_is_empty) {
char *function = args.function;
char *field = args.field;
void *value = args.value;
bool (* function_to_apply)(void *) = args.function_to_apply;
bool set_var(suppress, false)
return f3(function, field, value, function_to_apply, suppress);
}
function_declaration(ExceptionResponse *, ExceptionHandler_is_full) {
char *function = args.function;
char *field = args.field;
void *value = args.value;
bool (* function_to_apply)(void *) = args.function_to_apply;
bool set_var(suppress, false)
return f4(function, field, value, function_to_apply, suppress);
}
function_declaration(ExceptionResponse *, ExceptionHandler_is_not_sorted) {
char *function = args.function;
char *field = args.field;
void *value = args.value;
bool (* function_to_apply)(void *) = args.function_to_apply;
bool set_var(suppress, false)
return f5(function, field, value, function_to_apply, suppress);
}
function_declaration(ExceptionResponse *, ExceptionHandler_is_out_of_bounds) {
char *function = args.function;
char *field = args.field;
long value = args.value;
long max_index = args.max_index;
bool set_var(suppress, false)
return f6(function, field, value, max_index, suppress);
}
bool anyThrows(int argc, ExceptionResponse *er, ...) {
if (er->throws) {
if (!er->suppress) fprintf(stderr, "%s", er->error_message);
return true;
} else {
if (argc == 1) return false;
va_list args;
va_start(args, er);
ExceptionResponse *e;
for (int i = 1; i < argc; i++) {
e = va_arg(args, ExceptionResponse *);
if (e->throws) {
if (!e->suppress) fprintf(stderr, "%s", e->error_message);
return true;
}
}
va_end(args);
}
return false;
}