-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
103 lines (94 loc) · 2.95 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
#include "containers.h"
#include <stdio.h>
static struct Error {
int code;
char *Message;
} ErrorMessagesTable[] = {
{CONTAINER_ERROR_BADARG, "Bad argument"},
{CONTAINER_ERROR_NOMEMORY, "Insufficient memory"},
{CONTAINER_ERROR_INDEX, "Index error"},
{CONTAINER_ERROR_READONLY, "Object is read only"},
{CONTAINER_INTERNAL_ERROR, "Internal error in container library"},
{CONTAINER_ERROR_OBJECT_CHANGED,"Iterator used with modified object"},
{CONTAINER_ERROR_NOT_EMPTY, "container is not empty"},
{CONTAINER_ERROR_FILE_READ, "read error in input stream"},
{CONTAINER_ERROR_FILE_WRITE, "write error in output stream"},
{CONTAINER_FULL, "Container is full"},
{CONTAINER_ASSERTION_FAILED, "Assertion failed"},
{CONTAINER_ERROR_NOENT, "File not found"},
{CONTAINER_ERROR_FILEOPEN, "Error opening the file"},
{CONTAINER_ERROR_INCOMPATIBLE, "Incompatible element sizes"},
{CONTAINER_ERROR_WRONGFILE, "Not a container stream"},
{CONTAINER_ERROR_NOTIMPLEMENTED,"Function not implemented for this container type"},
{CONTAINER_ERROR_BADPOINTER, "Debug_malloc: BAD POINTER******"},
{CONTAINER_ERROR_BUFFEROVERFLOW,"Debug_malloc: BUFFER OVERFLOW******"},
{CONTAINER_ERROR_WRONGELEMENT, "Wrong element passed to a list"},
{CONTAINER_ERROR_BADMASK, "Incorrect mask length"},
{0,"Unknown error"},
};
static struct Error *ErrorMessages = ErrorMessagesTable;
static struct ErrorList {
struct ErrorList *Next;
int code;
char *Message;
} *UserErrorMessages = NULL;
static int AddError(int code, char *message)
{
struct ErrorList *e = CurrentAllocator->calloc(1,sizeof(struct ErrorList));
if (e) {
e->Message = CurrentAllocator->malloc(1+strlen(message));
if (e->Message) {
strcpy(e->Message,message);
e->code = code;
e->Next = UserErrorMessages;
UserErrorMessages = e;
return 1;
}
CurrentAllocator->free(e);
}
return CONTAINER_ERROR_NOMEMORY;
}
static char *StrError(int errcode)
{
struct ErrorList *e = UserErrorMessages;
struct Error *E;
while (e) {
if (e->code == errcode)
return e->Message;
e = e->Next;
}
E = ErrorMessages;
while (E->code) {
if (E->code == errcode)
break;
E++;
}
return E->Message;
}
static void *ContainerRaiseError(const char *fnname,int errcode,...)
{
fprintf(stderr,"Container library: Error '%s' in function %s\n",StrError(errcode),fnname);
return NULL;
}
static void *EmptyErrorFunction(const char *fnname,int errcode,...) { return NULL; }
static ErrorFunction SetError(ErrorFunction n)
{
ErrorFunction old = iError.RaiseError;
if (n != NULL) {
iError.RaiseError = n;
}
return old;
}
static int BadArgError(const char *fname)
{
iError.RaiseError(fname,CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
ErrorInterface iError = {
ContainerRaiseError,
EmptyErrorFunction,
StrError,
SetError,
BadArgError,
AddError,
};