This repository has been archived on 2021-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
modetw/src/exception_t.c

45 lines
1002 B
C

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include "exception_t.h"
exception_t*
exception_init() {
exception_t *e = (exception_t*) malloc(sizeof(exception_t));
e->type = NO_ERROR;
e->msg = malloc(CHAR_MAX);
return e;
}
void
exception_free(exception_t *e) {
free(e->msg);
free(e);
}
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;
}