From 30553db7c86a70e43f956d385d317a1dfad9b2d0 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 3 Jun 2023 14:47:52 -0400 Subject: feat: optable errors handled with error handlers, not just by printing --- stack.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'stack.c') diff --git a/stack.c b/stack.c index 3fadaf4..575e38a 100644 --- a/stack.c +++ b/stack.c @@ -23,10 +23,6 @@ void stack_free(stack* s) { #define ERROR_STACK_OVERFLOW "Error: Stack Overflow" #define ERROR_OUTPUT_BUFFER_OVERFLOW "Error: Output would overflow buffer if printed" -static void stack_handleerror(stack* s, char* errormessage) { - s->errorhandler(errormessage); -} - stackitem stack_pop(stack* s) { if (s->size > 0) { s->size = s->size - 1; @@ -34,7 +30,7 @@ stackitem stack_pop(stack* s) { return si; } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); return 0; } } @@ -44,7 +40,7 @@ stackitem stack_peek(stack* s) { return s->start[s->size - 1]; } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); return 0; } } @@ -52,7 +48,7 @@ stackitem stack_peek(stack* s) { void stack_push(stack *s, stackitem si) { // fprintf(stderr, "pushing %d", si); if (s->size >= s->maxsize) { - stack_handleerror(s, ERROR_STACK_OVERFLOW); + s->errorhandler(ERROR_STACK_OVERFLOW); } else { s->start[s->size] = si; s->size = s->size + 1; @@ -69,18 +65,18 @@ int stack_depth(stack* s) { void stack_tostringappend(stack* s, int sbmaxlen, char* sb) { int i = strlen(sb); if (i >= sbmaxlen) { - stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW); + s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW); } sprintf(&(sb[i]), "<%d>", s->size); i = strlen(sb); if (i >= sbmaxlen) { - stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW); + s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW); } for (int j = 0; j < s->size; j++) { sprintf(&(sb[i]), " %d", s->start[j]); i = strlen(sb); if (i >= sbmaxlen) { - stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW); + s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW); } } sprintf(&(sb[i]), "\n"); @@ -98,7 +94,7 @@ void stack_roll(stack* s) { s->start[i] = newtop; } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); } } @@ -109,7 +105,7 @@ void stack_pick(stack *s) { stack_push(s, s->start[maxindex - posfromtop]); } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); } } -- cgit v1.2.3