From 4db12b8121c0b32df8b8671045013c857beb191f Mon Sep 17 00:00:00 2001 From: biouman Date: Fri, 27 Apr 2001 04:12:25 +0000 Subject: --- exceptions.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 exceptions.c (limited to 'exceptions.c') diff --git a/exceptions.c b/exceptions.c new file mode 100644 index 0000000..8333b82 --- /dev/null +++ b/exceptions.c @@ -0,0 +1,81 @@ +/* + * + * Gestionnaire d'exceptions + * + */ + +#include +#include +#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define _(x) x +#endif +#include "exceptions.h" + +char *contexts[128]; +int clevel = 0; + +char *Estrdup(char *o) +{ + char *r; + + if (o) { + if (!(r = strdup(o))) { + exception(1, _("Out of memory.")); + } + } else { + return NULL; + } + return r; +} + +void *Emalloc(size_t s) +{ + void *r; + + if (s) { + if (!(r = malloc(s))) { + exception(1, _("Out of memory.")); + } + } else { + return NULL; + } + return r; +} + +void pushcontext(char *c) +{ + if (clevel == 128) { + exception(1, _("Too much error contexts during pushcontext().")); + } + contexts[clevel++] = Estrdup(c); +} + +void popcontext(void) +{ + if (clevel == 0) { + exception(1, _("Error context empty, but popcontext() called.")); + } + free(contexts[--clevel]); +} + +void flushcontext(void) +{ + while (clevel) { + popcontext(); + } +} + +void exception(int level, char *msg) +{ + int i; + + fprintf(stderr, "Error detected. Showing context.\n"); + for (i = 0; i < clevel; i++) { + fprintf(stderr, " (%i) - %s\n", i, contexts[i]); + } + fprintf(stderr, " Error description: %s\n", msg); + exit(level); +} -- cgit v1.2.3