diff options
Diffstat (limited to 'fitz/except.c')
-rw-r--r-- | fitz/except.c | 71 |
1 files changed, 52 insertions, 19 deletions
diff --git a/fitz/except.c b/fitz/except.c index 348b950e..4055367d 100644 --- a/fitz/except.c +++ b/fitz/except.c @@ -1,3 +1,4 @@ +#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -6,44 +7,76 @@ static void do_throw(fz_except_context *ex) { - if(ex->depth >= 0) - longjmp(ex->stack[ex->depth].buffer, 1); - else { - printf("Uncaught exception: %s\n", ex->except.mess); - exit(EXIT_FAILURE); /* Bale through normal channels */ - } + assert(ex != NULL); + if(ex->depth >= 0) + longjmp(ex->stack[ex->depth].buffer, 1); + else { + printf("Uncaught exception: %s\n", ex->except.mess); + exit(EXIT_FAILURE); /* Bale through normal channels */ + } } void fz_throw(fz_context *ctx, char *fmt, ...) { - va_list args; + va_list args; - va_start(args, fmt); - vsprintf(ctx->except->except.mess, fmt, args); - va_end(args); - do_throw(ctx->except); + va_start(args, fmt); + vsprintf(ctx->except->except.mess, fmt, args); + va_end(args); + do_throw(ctx->except); } fz_except *fz_caught(fz_context *ctx) { - return &ctx->except->except; + assert(ctx != NULL); + assert(ctx->except != NULL); + return &ctx->except->except; } void fz_rethrow(fz_context *ctx) { - do_throw(ctx->except); + assert(ctx != NULL); + do_throw(ctx->except); } void fz_except_xxx_push(fz_except_context *ex) { - if(ex->depth+1 >= MAXDEPTH) { - fprintf(stderr, "Exception stack overflow!\n"); - exit(EXIT_FAILURE); /* Bale through normal channels */ - } - ex->depth++; + assert(ex != NULL); + if(ex->depth+1 >= MAXDEPTH) { + fprintf(stderr, "Exception stack overflow!\n"); + exit(EXIT_FAILURE); /* Bale through normal channels */ + } + ex->depth++; } void fz_var_xxx(void *e) { - /* Do nothing */ + /* Do nothing */ +} + +fz_error fz_except_init(fz_context *ctx) +{ + fz_except_context *ex; + + assert(ctx != NULL); + if (ctx->except != NULL) + return fz_okay; + ex = fz_malloc_nothrow(ctx, sizeof(fz_except_context)); + if (ex == NULL) + return fz_error_make("Failed to initialise exception context"); + ex->depth = -1; + ctx->except = ex; + + return fz_okay; +} + +void fz_except_fin(fz_context *ctx) +{ + assert(ctx != NULL); + + if (ctx->except == NULL) + return; + assert(ctx->except->depth == -1); + free(ctx->except); + ctx->except = NULL; } |