diff --git a/src/exception_t.c b/src/exception_t.c index b5cea82..6ab3607 100644 --- a/src/exception_t.c +++ b/src/exception_t.c @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "exception_t.h" @@ -6,12 +9,13 @@ exception_t* exception_init() { exception_t *e = (exception_t*) malloc(sizeof(exception_t)); e->type = NO_ERROR; - e->msg = NULL; + e->msg = malloc(CHAR_MAX); return e; } void exception_free(exception_t *e) { + free(e->msg); free(e); } @@ -19,3 +23,22 @@ int exception_null(const exception_t *e) { return e->type == NO_ERROR; } + +char* +exception_prestr(const exception_t *e) { + switch(e->type) { + case NO_ERROR: return "No error\n"; + case NO_FILE: return "No file found"; + case XML_FILE_MISSING: return "XML file missing"; + case XML_READ_FILE_FAIL: return "XML file reading failed"; + case MD5SUM_GEN_FAIL: return "MD5sum generation failed"; + case MD5SUM_VERIFY_FAIL: return "MD5sum verification failed"; + } +} + +char* +exception_str(const exception_t *e, char* msg) { + if (e->type != NO_ERROR) sprintf(msg, "%s: %s\n", exception_prestr(e), e->msg); + else sprintf(msg, "%s\n", exception_prestr(e)); + return msg; +} diff --git a/src/exception_t.h b/src/exception_t.h index 5be2cb7..d54525f 100644 --- a/src/exception_t.h +++ b/src/exception_t.h @@ -25,6 +25,6 @@ int exception_null(const exception_t *e); char* -exception_str(exception_t *e); +exception_str(const exception_t *e, char* msg); #endif