Improve error handling

Basically be less fault tolerant. Added a lot of upper-case function
macros to use for fault-intolerant calls.
This commit is contained in:
hasufell 2014-05-13 13:47:13 +02:00
parent a7c3fe4f82
commit a1d3c3947e
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
5 changed files with 188 additions and 65 deletions

View File

@ -77,9 +77,12 @@ char *read_file(char const * const filename)
if (fd != -1) {
/* read and copy chunks */
while ((n = read(fd, buf, STD_FILE_BUF)) > 0) {
while ((n = read(fd, buf, STD_FILE_BUF)) != 0) {
char *tmp_ptr = NULL;
if (n == -1)
ABORT("Failed while reading file descriptor %d\n", fd);
str_size += n; /* count total bytes read */
tmp_ptr = (char*) realloc( /* allocate correct size */
@ -90,12 +93,13 @@ char *read_file(char const * const filename)
string = tmp_ptr;
/* append buffer to string */
memcpy(string + (str_size - n), buf, n);
memcpy(string + (str_size - n), buf, (size_t)n);
}
/* add trailing NULL byte */
string[str_size] = '\0';
close(fd);
if (close(fd))
ABORT("Failed to close file descripter %d\n", fd);
return string;
} else {

View File

@ -72,7 +72,8 @@ static void draw_normals(HE_obj const * const obj)
vector vec;
for (uint32_t i = 0; i < obj->vc; i++) {
if ((vec_normal(&(obj->vertices[i]), &vec))) {
VEC_NORMAL(&(obj->vertices[i]), &vec);
glPushMatrix();
glLineWidth(3);
@ -88,10 +89,6 @@ static void draw_normals(HE_obj const * const obj)
glEnd();
glPopMatrix();
} else {
fprintf(stderr, "Failed drawing the normals!\n");
return;
}
}
}
@ -147,10 +144,7 @@ static void draw_obj(int32_t const myxrot,
vector center_vert;
if (!find_center(obj, &center_vert)) {
fprintf(stderr, "Failed drawing the object!\n");
return;
}
FIND_CENTER(obj, &center_vert);
/* increment rotation, if any */
xrot += myxrot;

View File

@ -36,10 +36,24 @@
#include <string.h>
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define GET_ALL_EMANATING_EDGES(...) \
{ \
if (!get_all_emanating_edges(__VA_ARGS__)) { \
fprintf(stderr, "Failure in get_all_emanating_edges()!\n"); \
abort(); \
} \
}
/*
* static declarations
*/
static HE_edge **get_all_emanating_edges(HE_vert const * const vert,
static bool get_all_emanating_edges(HE_vert const * const vert,
HE_edge ***edge_array_out,
uint32_t *ec_out);
@ -48,20 +62,26 @@ static HE_edge **get_all_emanating_edges(HE_vert const * const vert,
* to that array with the size of ec_out.
*
* @param vert the vertice to get the emanating edges of
* @param edge_array_out address of the 2d edge array to save
* the result in [out]
* @param ec_out the edge counter is saved here [out]
* @return pointer to an array of half-edges, size ec_out
*/
static HE_edge **get_all_emanating_edges(HE_vert const * const vert,
static bool get_all_emanating_edges(HE_vert const * const vert,
HE_edge ***edge_array_out,
uint32_t *ec_out)
{
uint32_t ec = 0, /* edge count */
rc = 0; /* realloc count */
uint32_t const approx_ec = 20; /* allocation chunk */
HE_edge **edge_array = malloc(sizeof(HE_edge*) * approx_ec);
HE_edge **edge_array;
HE_edge **tmp_ptr;
if (!vert)
return NULL;
return false;
edge_array = malloc(sizeof(HE_edge*) * approx_ec);
CHECK_PTR_VAL(edge_array);
HE_edge *edge = vert->edge;
@ -83,10 +103,11 @@ static HE_edge **get_all_emanating_edges(HE_vert const * const vert,
} while (edge != vert->edge);
/* this is the real size, not the x[ec] value */
*ec_out = ec;
/* set out-pointers */
*edge_array_out = edge_array;
*ec_out = ec; /* this is the real size, not the x[ec] value */
return edge_array;
return true;
}
/**
@ -104,24 +125,18 @@ bool face_normal(HE_edge const * const edge,
he_vec2,
he_base;
if (!(copy_vector(edge->next->vert->vec, &he_base)))
if (!edge || !vec)
return false;
/* calculate vector between vertices */
if (!(sub_vectors(edge->next->next->vert->vec, &he_base, &he_vec1)))
return false;
if (!(sub_vectors(edge->vert->vec, &he_base, &he_vec2)))
return false;
COPY_VECTOR(edge->next->vert->vec, &he_base);
/* vector product */
if (!(vector_product(&he_vec1,
&he_vec2,
vec)))
return false;
/* calculate vectors between the vertices */
SUB_VECTORS(edge->next->next->vert->vec, &he_base, &he_vec1);
SUB_VECTORS(edge->vert->vec, &he_base, &he_vec2);
/* normalize vector */
if (!(normalize_vector(vec, vec)))
return false;
VECTOR_PRODUCT(&he_vec1, &he_vec2, vec);
NORMALIZE_VECTOR(vec, vec);
return true;
}
@ -135,43 +150,30 @@ bool face_normal(HE_edge const * const edge,
*/
bool vec_normal(HE_vert const * const vert, vector *vec)
{
HE_edge **edge_array;
HE_edge **edge_array = NULL;
uint32_t ec;
vector he_base;
if (!vert || !vec)
return false;
/* get all emanating edges */
if (!(edge_array = get_all_emanating_edges(vert, &ec)))
return false;
if (!(copy_vector(edge_array[0]->vert->vec, &he_base)))
return false;
/* avoid side effects due to junk data */
if (!(set_null_vector(vec)))
return false;
GET_ALL_EMANATING_EDGES(vert, &edge_array, &ec);
COPY_VECTOR(edge_array[0]->vert->vec, &he_base);
SET_NULL_VECTOR(vec); /* set to null for later summation */
/* iterate over all edges, get the normalized
* face vector and add those up */
for (uint32_t i = 0; i < ec; i++) {
vector new_vec;
/* get face normal */
if (!(face_normal(edge_array[i], &new_vec)))
return false;
if (!(add_vectors(vec, &new_vec, vec)))
return false;
FACE_NORMAL(edge_array[i], &new_vec);
ADD_VECTORS(vec, &new_vec, vec);
}
/* normalize the result */
if (!(normalize_vector(vec, vec)))
return false;
NORMALIZE_VECTOR(vec, vec);
free(edge_array);
return true;
}

View File

@ -34,6 +34,55 @@
#include <stdint.h>
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define GET_ALL_EMANATING_EDGES(...) \
{ \
if (!get_all_emanating_edges(__VA_ARGS__)) { \
fprintf(stderr, "Failure in get_all_emanating_edges()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define FACE_NORMAL(...) \
{ \
if (!face_normal(__VA_ARGS__)) { \
fprintf(stderr, "Failure in face_normal()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define VEC_NORMAL(...) \
{ \
if (!vec_normal(__VA_ARGS__)) { \
fprintf(stderr, "Failure in vec_normal()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define FIND_CENTER(...) \
{ \
if (!find_center(__VA_ARGS__)) { \
fprintf(stderr, "Failure in find_center()!\n"); \
abort(); \
} \
}
/**
* Typedef for the plain faces
* that are not yet converted to real HE_face.

View File

@ -28,6 +28,80 @@
#include <stdbool.h>
#include <stdio.h>
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define VECTOR_PRODUCT(...) \
{ \
if (!vector_product(__VA_ARGS__)) { \
fprintf(stderr, "Failure in vector_product()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define ADD_VECTORS(...) \
{ \
if (!add_vectors(__VA_ARGS__)) { \
fprintf(stderr, "Failure in add_vectors()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define SUB_VECTORS(...) \
{ \
if (!sub_vectors(__VA_ARGS__)) { \
fprintf(stderr, "Failure in sub_vectors()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define NORMALIZE_VECTOR(...) \
{ \
if (!normalize_vector(__VA_ARGS__)) { \
fprintf(stderr, "Failure in normalize_vector()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define COPY_VECTOR(...) \
{ \
if (!copy_vector(__VA_ARGS__)) { \
fprintf(stderr, "Failure in copy_vector()!\n"); \
abort(); \
} \
}
/**
* Fault intolerant macro. Will abort the program if the called
* function failed.
*/
#define SET_NULL_VECTOR(...) \
{ \
if (!set_null_vector(__VA_ARGS__)) { \
fprintf(stderr, "Failure in set_null_vector()!\n"); \
abort(); \
} \
}
typedef struct vector vector;