This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
138 lines (118 loc) · 4.07 KB
/
error.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
//
// flare16x core
// Developed 2019 by Benedikt Muessig <[email protected]>
// Licensed under GPLv3
//
// error.c: Error handling functions
//
#include <stddef.h>
#include "error.h"
// A list of error strings to represent the error codes
const char* const flare16x_error_names[FLARE16X_ERROR_COUNT] = {
// FLARE16X_ERROR_NONE
"no error",
// FLARE16X_ERROR_NULL
"invalid null pointer",
// FLARE16X_ERROR_MALLOC
"memory allocation failed",
// FLARE16X_ERROR_LEAK
"memory leak avoided",
// FLARE16X_ERROR_RANGE
"invalid argument range",
// FLARE16X_ERROR_OPEN
"file open failed",
// FLARE16X_ERROR_IO
"I/O operation failed",
// FLARE16X_ERROR_SYNTAX
"syntax error",
// FLARE16X_ERROR_FORMAT
"file format error",
// FLARE16X_ERROR_IMAGE
"image size or feature error",
// FLARE16X_ERROR_UNKNOWN
"unknown value",
// FLARE16X_ERROR_ASSERT
"assert failed",
// FLARE16X_ERROR_CALLEE
"callee error",
// FLARE16X_ERROR_OTHER
"other unknown error"
};
// A list of error source strings to represent the error codes
const char* const flare16x_error_source_names[FLARE16X_ERROR_SOURCE_COUNT] = {
// FLARE16X_ERROR_SOURCE_GLOBAL
"global",
// FLARE16X_ERROR_SOURCE_BITMAP
"bitmap",
// FLARE16X_ERROR_SOURCE_CANVAS
"canvas",
// FLARE16X_ERROR_SOURCE_LOCATOR
"locator",
// FLARE16X_ERROR_SOURCE_OCR
"OCR",
// FLARE16X_ERROR_SOURCE_PALETTES
"palettes",
// FLARE16X_ERROR_SOURCE_THERMAL
"thermal"
};
// Returns a matching error name for the latest error on the stack
const char* flare16x_error_string(flare16x_error error)
{
error &= FLARE16X_ERROR_MASK;
if (error >= FLARE16X_ERROR_COUNT)
return "invalid error";
return flare16x_error_names[error];
}
// Returns a matching error source name for the latest error on the stack
const char* flare16x_error_source_string(flare16x_error error)
{
error &= FLARE16X_ERROR_SOURCE_MASK;
error >>= FLARE16X_ERROR_WIDTH;
if (error >= FLARE16X_ERROR_SOURCE_COUNT)
return "invalid error source";
return flare16x_error_source_names[error];
}
// Searches and returns the latest error from the stack and returns it
flare16x_error flare16x_error_first(flare16x_error error)
{
// Search through the errors until the most recent non-zero item is found
int stack_item;
for (stack_item = FLARE16x_ERROR_CAPACITY - 1; stack_item >= 0; stack_item--)
{
flare16x_error current_error;
current_error = error >> ((FLARE16X_ERROR_WIDTH + FLARE16X_ERROR_SOURCE_WIDTH) * stack_item) &
(FLARE16X_ERROR_SOURCE_MASK | FLARE16X_ERROR_MASK);
if (current_error != FLARE16X_ERROR_NONE)
return current_error;
}
// If no error could be found, return none
return FLARE16X_ERROR_NONE;
}
// Retrieves the latest error from the stack and returns it
flare16x_error flare16x_error_latest(flare16x_error error)
{
return error & (FLARE16X_ERROR_SOURCE_MASK | FLARE16X_ERROR_MASK);
}
// Pops an error from the error stack and returns it
flare16x_error flare16x_error_pop(flare16x_error* error)
{
if (error == NULL)
return FLARE16X_ERROR_NULL;
flare16x_error latest_error = *error & (FLARE16X_ERROR_SOURCE_MASK | FLARE16X_ERROR_MASK);
*error = *error >> (FLARE16X_ERROR_WIDTH + FLARE16X_ERROR_SOURCE_WIDTH);
return latest_error;
}
// Pushes a new error onto the error stack (and perhaps removes the first error on overflow)
void flare16x_error_push(flare16x_error new_error, flare16x_error* prev_errors)
{
if (prev_errors == NULL)
return;
*prev_errors = (*prev_errors << (FLARE16X_ERROR_WIDTH + FLARE16X_ERROR_SOURCE_WIDTH)) |
(new_error & (FLARE16X_ERROR_SOURCE_MASK | FLARE16X_ERROR_MASK));
}
// Pushes a new error onto a copy of the error stack and returns it
flare16x_error flare16x_error_wrap(flare16x_error new_error, flare16x_error prev_errors)
{
return (prev_errors << (FLARE16X_ERROR_WIDTH + FLARE16X_ERROR_SOURCE_WIDTH)) |
(new_error & (FLARE16X_ERROR_SOURCE_MASK | FLARE16X_ERROR_MASK));
}