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:
parent
a7c3fe4f82
commit
a1d3c3947e
@ -77,9 +77,12 @@ char *read_file(char const * const filename)
|
|||||||
|
|
||||||
if (fd != -1) {
|
if (fd != -1) {
|
||||||
/* read and copy chunks */
|
/* 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;
|
char *tmp_ptr = NULL;
|
||||||
|
|
||||||
|
if (n == -1)
|
||||||
|
ABORT("Failed while reading file descriptor %d\n", fd);
|
||||||
|
|
||||||
str_size += n; /* count total bytes read */
|
str_size += n; /* count total bytes read */
|
||||||
|
|
||||||
tmp_ptr = (char*) realloc( /* allocate correct size */
|
tmp_ptr = (char*) realloc( /* allocate correct size */
|
||||||
@ -90,12 +93,13 @@ char *read_file(char const * const filename)
|
|||||||
string = tmp_ptr;
|
string = tmp_ptr;
|
||||||
|
|
||||||
/* append buffer to string */
|
/* append buffer to string */
|
||||||
memcpy(string + (str_size - n), buf, n);
|
memcpy(string + (str_size - n), buf, (size_t)n);
|
||||||
}
|
}
|
||||||
/* add trailing NULL byte */
|
/* add trailing NULL byte */
|
||||||
string[str_size] = '\0';
|
string[str_size] = '\0';
|
||||||
|
|
||||||
close(fd);
|
if (close(fd))
|
||||||
|
ABORT("Failed to close file descripter %d\n", fd);
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
} else {
|
} else {
|
||||||
|
@ -72,7 +72,8 @@ static void draw_normals(HE_obj const * const obj)
|
|||||||
vector vec;
|
vector vec;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < obj->vc; i++) {
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
||||||
if ((vec_normal(&(obj->vertices[i]), &vec))) {
|
VEC_NORMAL(&(obj->vertices[i]), &vec);
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
|
||||||
glLineWidth(3);
|
glLineWidth(3);
|
||||||
@ -88,10 +89,6 @@ static void draw_normals(HE_obj const * const obj)
|
|||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
glPopMatrix();
|
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;
|
vector center_vert;
|
||||||
|
|
||||||
if (!find_center(obj, ¢er_vert)) {
|
FIND_CENTER(obj, ¢er_vert);
|
||||||
fprintf(stderr, "Failed drawing the object!\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* increment rotation, if any */
|
/* increment rotation, if any */
|
||||||
xrot += myxrot;
|
xrot += myxrot;
|
||||||
|
@ -36,10 +36,24 @@
|
|||||||
#include <string.h>
|
#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 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);
|
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.
|
* to that array with the size of ec_out.
|
||||||
*
|
*
|
||||||
* @param vert the vertice to get the emanating edges of
|
* @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]
|
* @param ec_out the edge counter is saved here [out]
|
||||||
* @return pointer to an array of half-edges, size ec_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_out)
|
||||||
{
|
{
|
||||||
uint32_t ec = 0, /* edge count */
|
uint32_t ec = 0, /* edge count */
|
||||||
rc = 0; /* realloc count */
|
rc = 0; /* realloc count */
|
||||||
uint32_t const approx_ec = 20; /* allocation chunk */
|
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;
|
HE_edge **tmp_ptr;
|
||||||
|
|
||||||
if (!vert)
|
if (!vert)
|
||||||
return NULL;
|
return false;
|
||||||
|
|
||||||
|
edge_array = malloc(sizeof(HE_edge*) * approx_ec);
|
||||||
|
CHECK_PTR_VAL(edge_array);
|
||||||
|
|
||||||
HE_edge *edge = vert->edge;
|
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);
|
} while (edge != vert->edge);
|
||||||
|
|
||||||
/* this is the real size, not the x[ec] value */
|
/* set out-pointers */
|
||||||
*ec_out = ec;
|
*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_vec2,
|
||||||
he_base;
|
he_base;
|
||||||
|
|
||||||
if (!(copy_vector(edge->next->vert->vec, &he_base)))
|
if (!edge || !vec)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* calculate vector between vertices */
|
COPY_VECTOR(edge->next->vert->vec, &he_base);
|
||||||
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;
|
|
||||||
|
|
||||||
/* vector product */
|
/* calculate vectors between the vertices */
|
||||||
if (!(vector_product(&he_vec1,
|
SUB_VECTORS(edge->next->next->vert->vec, &he_base, &he_vec1);
|
||||||
&he_vec2,
|
SUB_VECTORS(edge->vert->vec, &he_base, &he_vec2);
|
||||||
vec)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
/* normalize vector */
|
VECTOR_PRODUCT(&he_vec1, &he_vec2, vec);
|
||||||
if (!(normalize_vector(vec, vec)))
|
|
||||||
return false;
|
NORMALIZE_VECTOR(vec, vec);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -135,43 +150,30 @@ bool face_normal(HE_edge const * const edge,
|
|||||||
*/
|
*/
|
||||||
bool vec_normal(HE_vert const * const vert, vector *vec)
|
bool vec_normal(HE_vert const * const vert, vector *vec)
|
||||||
{
|
{
|
||||||
HE_edge **edge_array;
|
HE_edge **edge_array = NULL;
|
||||||
uint32_t ec;
|
uint32_t ec;
|
||||||
vector he_base;
|
vector he_base;
|
||||||
|
|
||||||
if (!vert || !vec)
|
if (!vert || !vec)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* get all emanating edges */
|
GET_ALL_EMANATING_EDGES(vert, &edge_array, &ec);
|
||||||
if (!(edge_array = get_all_emanating_edges(vert, &ec)))
|
COPY_VECTOR(edge_array[0]->vert->vec, &he_base);
|
||||||
return false;
|
SET_NULL_VECTOR(vec); /* set to null for later summation */
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
/* iterate over all edges, get the normalized
|
/* iterate over all edges, get the normalized
|
||||||
* face vector and add those up */
|
* face vector and add those up */
|
||||||
for (uint32_t i = 0; i < ec; i++) {
|
for (uint32_t i = 0; i < ec; i++) {
|
||||||
vector new_vec;
|
vector new_vec;
|
||||||
|
|
||||||
/* get face normal */
|
FACE_NORMAL(edge_array[i], &new_vec);
|
||||||
if (!(face_normal(edge_array[i], &new_vec)))
|
ADD_VECTORS(vec, &new_vec, vec);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!(add_vectors(vec, &new_vec, vec)))
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* normalize the result */
|
/* normalize the result */
|
||||||
if (!(normalize_vector(vec, vec)))
|
NORMALIZE_VECTOR(vec, vec);
|
||||||
return false;
|
|
||||||
|
|
||||||
free(edge_array);
|
free(edge_array);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +34,55 @@
|
|||||||
#include <stdint.h>
|
#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
|
* Typedef for the plain faces
|
||||||
* that are not yet converted to real HE_face.
|
* that are not yet converted to real HE_face.
|
||||||
|
74
src/vector.h
74
src/vector.h
@ -28,6 +28,80 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
#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;
|
typedef struct vector vector;
|
||||||
|
Loading…
Reference in New Issue
Block a user