More error handling improvements

This commit is contained in:
hasufell 2014-05-13 14:05:02 +02:00
parent a1d3c3947e
commit 5b53950def
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
5 changed files with 47 additions and 6 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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"