From 5b53950deff44fd35d0e0e95b544e6f20eff9e59 Mon Sep 17 00:00:00 2001 From: hasufell Date: Tue, 13 May 2014 14:05:02 +0200 Subject: [PATCH] More error handling improvements --- src/filereader.c | 6 +++++- src/gl_draw.c | 2 +- src/half_edge.c | 16 +++++++++++++--- src/half_edge.h | 14 +++++++++++++- src/print.c | 15 +++++++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/filereader.c b/src/filereader.c index faedcff..03ec95a 100644 --- a/src/filereader.c +++ b/src/filereader.c @@ -48,9 +48,13 @@ HE_obj *read_obj_file(char const * const filename) char *string = NULL; /* file content */ HE_obj *obj = NULL; + if (!filename || !*filename) + return NULL; + /* read the whole file into string */ string = read_file(filename); - if (!filename || !*filename || !string || !*string) + + if (!string) return NULL; obj = parse_obj(string); diff --git a/src/gl_draw.c b/src/gl_draw.c index 5bec569..6a20f7b 100644 --- a/src/gl_draw.c +++ b/src/gl_draw.c @@ -351,7 +351,7 @@ void init(char const * const filename) if (!obj) ABORT("Failed to read object file \"%s\"!", filename); - normalize_object(obj); + NORMALIZE_OBJECT(obj); day = 0; year = 0; diff --git a/src/half_edge.c b/src/half_edge.c index 3fd7637..8b41830 100644 --- a/src/half_edge.c +++ b/src/half_edge.c @@ -77,7 +77,7 @@ static bool get_all_emanating_edges(HE_vert const * const vert, HE_edge **edge_array; HE_edge **tmp_ptr; - if (!vert) + if (!edge_array_out || !vert || !ec_out) return false; edge_array = malloc(sizeof(HE_edge*) * approx_ec); @@ -254,9 +254,13 @@ float get_normalized_scale_factor(HE_obj const * const obj) * * @param obj the object we want to scale [mod] */ -void normalize_object(HE_obj *obj) +bool normalize_object(HE_obj *obj) { float scale_factor; + + if (!obj) + return false; + scale_factor = get_normalized_scale_factor(obj); for (uint32_t i = 0; i < obj->vc; i++) { @@ -264,6 +268,8 @@ void normalize_object(HE_obj *obj) obj->vertices[i].vec->y = obj->vertices[i].vec->y * scale_factor; obj->vertices[i].vec->z = obj->vertices[i].vec->z * scale_factor; } + + return true; } /** @@ -335,7 +341,8 @@ HE_obj *parse_obj(char const * const obj_string) /* exceeds 3 dimensions, malformed vertice */ if (strtok_r(NULL, " ", &str_ptr_space)) - return NULL; + ABORT("Failure in parse_obj(),\n" + "malformed vertice, exceeds 2 dimensions!\n"); /* parse faces */ } else if (!strcmp(str_tmp_ptr, "f")) { @@ -432,6 +439,9 @@ HE_obj *parse_obj(char const * const obj_string) */ void delete_object(HE_obj *obj) { + if (!obj) + return; + for (uint32_t i = 0; i < obj->vc; i++) free(obj->vertices[i].vec); free(obj->edges); diff --git a/src/half_edge.h b/src/half_edge.h index d913898..96766cb 100644 --- a/src/half_edge.h +++ b/src/half_edge.h @@ -82,6 +82,18 @@ } \ } +/** + * Fault intolerant macro. Will abort the program if the called + * function failed. + */ +#define NORMALIZE_OBJECT(...) \ +{ \ + if (!normalize_object(__VA_ARGS__)) { \ + fprintf(stderr, "Failure in normalize_object()!\n"); \ + abort(); \ + } \ +} + /** * Typedef for the plain faces @@ -182,7 +194,7 @@ bool face_normal(HE_edge const * const edge, bool vec_normal(HE_vert const * const vert, vector *vec); bool find_center(HE_obj const * const obj, vector *vec); float get_normalized_scale_factor(HE_obj const * const obj); -void normalize_object(HE_obj *obj); +bool normalize_object(HE_obj *obj); HE_obj *parse_obj(char const * const filename); void delete_object(HE_obj *obj); diff --git a/src/print.c b/src/print.c index baad87a..9ae52a1 100644 --- a/src/print.c +++ b/src/print.c @@ -38,6 +38,9 @@ */ void print_edges(HE_obj *obj) { + if (!obj) + return; + for (uint32_t i = 0; i < obj->ec; i++) { printf("edge vertices %i:\n", i); printf(" x: %f\n", obj->edges[i].vert->vec->x); @@ -54,6 +57,9 @@ void print_edges(HE_obj *obj) */ void print_vertices(HE_obj *obj) { + if (!obj) + return; + printf("vertices: %d\n", obj->vc); for (uint32_t i = 0; i < obj->vc; i++) { printf("x[%d]: %f\n", i, obj->vertices[i].vec->x); @@ -71,6 +77,9 @@ void print_vertices(HE_obj *obj) */ void print_faces(HE_obj *obj) { + if (!obj) + return; + for (uint32_t i = 0; i < obj->fc; i++) { printf("face edge vertice %i:\n", i); printf(" x: %f\n", obj->faces[i].edge->vert->vec->x); @@ -89,6 +98,9 @@ void print_faces(HE_obj *obj) */ void print_plain_faces(FACE face, uint32_t fc) { + if (!face || !fc) + return; + printf("plain faces:\n"); for (uint32_t i = 0; i < fc - 1; i++) { uint32_t j = 0; @@ -109,6 +121,9 @@ void print_plain_faces(FACE face, uint32_t fc) */ void print_vector(vector *vec) { + if (!vec) + return; + printf("vector:\n" "x %f\n" "y %f\n"