Revamp error checking
This commit is contained in:
parent
4bc3af5a1c
commit
fecc9fb478
15
src/error.h
15
src/error.h
@ -4,12 +4,15 @@
|
|||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
|
||||||
/* Common errors */
|
/* Common errors */
|
||||||
#define FOUG_DATAX_NO_ERROR 0
|
typedef enum
|
||||||
#define FOUG_DATAX_NULL_TRANSFER_ERROR -1
|
{
|
||||||
#define FOUG_DATAX_NULL_BUFFER_ERROR -2
|
FOUG_DATAX_NO_ERROR = 0,
|
||||||
#define FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR -3
|
FOUG_DATAX_NULL_TRANSFER_ERROR = -1,
|
||||||
#define FOUG_DATAX_STREAM_ERROR -4
|
FOUG_DATAX_NULL_BUFFER_ERROR = -2,
|
||||||
#define FOUG_DATAX_TASK_STOPPED_ERROR -5
|
FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR = -3,
|
||||||
|
FOUG_DATAX_STREAM_ERROR = -4,
|
||||||
|
FOUG_DATAX_TASK_STOPPED_ERROR = -5
|
||||||
|
} foug_datax_error_t;
|
||||||
|
|
||||||
FOUG_LIB_EXPORT foug_bool_t foug_datax_no_error(int code);
|
FOUG_LIB_EXPORT foug_bool_t foug_datax_no_error(int code);
|
||||||
FOUG_LIB_EXPORT foug_bool_t foug_datax_error(int code);
|
FOUG_LIB_EXPORT foug_bool_t foug_datax_error(int code);
|
||||||
|
@ -50,6 +50,11 @@ typedef unsigned long long uint64_t;
|
|||||||
typedef int foug_bool_t;
|
typedef int foug_bool_t;
|
||||||
typedef float foug_real32_t;
|
typedef float foug_real32_t;
|
||||||
typedef double foug_real64_t;
|
typedef double foug_real64_t;
|
||||||
|
enum foug_bool_value
|
||||||
|
{
|
||||||
|
FOUG_FALSE = 0,
|
||||||
|
FOUG_TRUE = 1
|
||||||
|
};
|
||||||
|
|
||||||
#ifndef FOUG_INLINE
|
#ifndef FOUG_INLINE
|
||||||
# if defined(__GNUC__)
|
# if defined(__GNUC__)
|
||||||
|
45
src/internal/libstl/stl_rw_common.c
Normal file
45
src/internal/libstl/stl_rw_common.c
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include "stl_rw_common.h"
|
||||||
|
|
||||||
|
#include "../../error.h"
|
||||||
|
#include "../../libstl/stl_error.h"
|
||||||
|
|
||||||
|
foug_bool_t foug_check_transfer(int *error, const foug_transfer_t* trsf)
|
||||||
|
{
|
||||||
|
if (trsf == NULL) {
|
||||||
|
*error = FOUG_DATAX_NULL_TRANSFER_ERROR;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (trsf->buffer == NULL)
|
||||||
|
*error = FOUG_DATAX_NULL_BUFFER_ERROR;
|
||||||
|
else if (trsf->buffer_size == 0)
|
||||||
|
*error = FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return foug_datax_no_error(*error);
|
||||||
|
}
|
||||||
|
|
||||||
|
foug_bool_t foug_stl_check_geom(int *error, const foug_stl_geom_t* geom)
|
||||||
|
{
|
||||||
|
if (geom == NULL
|
||||||
|
|| (geom->triangle_count > 0 && geom->get_triangle_func == NULL))
|
||||||
|
{
|
||||||
|
*error = FOUG_STL_WRITE_NULL_GET_TRIANGLE_FUNC_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return foug_datax_no_error(*error);
|
||||||
|
}
|
||||||
|
|
||||||
|
foug_bool_t foug_stlb_check_params(int *error,
|
||||||
|
const foug_transfer_t *trsf,
|
||||||
|
foug_endianness_t byte_order)
|
||||||
|
{
|
||||||
|
if (!foug_check_transfer(error, trsf))
|
||||||
|
return FOUG_FALSE;
|
||||||
|
|
||||||
|
if (trsf->buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE)
|
||||||
|
*error = FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
||||||
|
if (byte_order != FOUG_LITTLE_ENDIAN && byte_order != FOUG_BIG_ENDIAN)
|
||||||
|
*error = FOUG_STLB_UNSUPPORTED_BYTE_ORDER_ERROR;
|
||||||
|
|
||||||
|
return foug_datax_no_error(*error);
|
||||||
|
}
|
25
src/internal/libstl/stl_rw_common.h
Normal file
25
src/internal/libstl/stl_rw_common.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef FOUG_INTERNAL_STL_RW_COMMON_H
|
||||||
|
#define FOUG_INTERNAL_STL_RW_COMMON_H
|
||||||
|
|
||||||
|
#include "../../global.h"
|
||||||
|
#include "../../endian.h"
|
||||||
|
#include "../../transfer.h"
|
||||||
|
#include "../../libstl/stl_geom.h"
|
||||||
|
#include "../../libstl/stl_triangle.h"
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint32_t facet_count;
|
||||||
|
uint32_t i_facet_offset;
|
||||||
|
void (*fix_endian_func)(foug_stl_triangle_t*);
|
||||||
|
} foug_stlb_readwrite_helper;
|
||||||
|
|
||||||
|
foug_bool_t foug_check_transfer(int* error, const foug_transfer_t* trsf);
|
||||||
|
|
||||||
|
foug_bool_t foug_stl_check_geom(int* error, const foug_stl_geom_t* geom);
|
||||||
|
|
||||||
|
foug_bool_t foug_stlb_check_params(int* error,
|
||||||
|
const foug_transfer_t* trsf,
|
||||||
|
foug_endianness_t byte_order);
|
||||||
|
|
||||||
|
#endif /* FOUG_INTERNAL_STLB_RW_COMMON_H */
|
@ -1,19 +0,0 @@
|
|||||||
#include "stlb_rw_common.h"
|
|
||||||
|
|
||||||
#include "../../error.h"
|
|
||||||
#include "../../libstl/stl_error.h"
|
|
||||||
|
|
||||||
int foug_stlb_check_params(const foug_transfer_t *trsf,
|
|
||||||
foug_endianness_t byte_order)
|
|
||||||
{
|
|
||||||
if (trsf == NULL)
|
|
||||||
return FOUG_DATAX_NULL_TRANSFER_ERROR;
|
|
||||||
if (trsf->buffer == NULL)
|
|
||||||
return FOUG_DATAX_NULL_BUFFER_ERROR;
|
|
||||||
if (trsf->buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE)
|
|
||||||
return FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
|
||||||
if (byte_order != FOUG_LITTLE_ENDIAN && byte_order != FOUG_BIG_ENDIAN)
|
|
||||||
return FOUG_STLB_READWRITE_UNSUPPORTED_BYTE_ORDER;
|
|
||||||
|
|
||||||
return FOUG_DATAX_NO_ERROR;
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
#ifndef FOUG_INTERNAL_STLB_RW_COMMON_H
|
|
||||||
#define FOUG_INTERNAL_STLB_RW_COMMON_H
|
|
||||||
|
|
||||||
#include "../../global.h"
|
|
||||||
#include "../../endian.h"
|
|
||||||
#include "../../transfer.h"
|
|
||||||
#include "../../libstl/stl_triangle.h"
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint32_t facet_count;
|
|
||||||
uint32_t i_facet_offset;
|
|
||||||
void (*fix_endian_func)(foug_stl_triangle_t*);
|
|
||||||
} foug_readwrite_helper;
|
|
||||||
|
|
||||||
int foug_stlb_check_params(const foug_transfer_t* trsf,
|
|
||||||
foug_endianness_t byte_order);
|
|
||||||
|
|
||||||
#endif /* FOUG_INTERNAL_STLB_RW_COMMON_H */
|
|
@ -1,24 +1,19 @@
|
|||||||
#ifndef FOUG_LIBSTL_STL_ERROR_H
|
#ifndef FOUG_LIBSTL_STL_ERROR_H
|
||||||
#define FOUG_LIBSTL_STL_ERROR_H
|
#define FOUG_LIBSTL_STL_ERROR_H
|
||||||
|
|
||||||
#define FOUG_STL_ERROR_TAG 0x11000000
|
#define FOUG_STL_ERROR_TAG 0x11000000
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FOUG_STL_WRITE_NULL_GET_TRIANGLE_FUNC_ERROR = FOUG_STL_ERROR_TAG + 1,
|
||||||
|
|
||||||
/* Specific error codes returned by foug_stla_read() */
|
/* Specific error codes returned by STL_ascii read function */
|
||||||
#define FOUG_STLA_READ_PARSE_ERROR (FOUG_STL_ERROR_TAG + 1)
|
FOUG_STLA_READ_PARSE_ERROR = FOUG_STL_ERROR_TAG + 100,
|
||||||
|
FOUG_STLA_WRITE_INVALID_REAL32_PREC_ERROR = FOUG_STL_ERROR_TAG + 101,
|
||||||
|
|
||||||
/* Specific error codes returned by foug_stla_write() */
|
/* Specific error codes returned by STL_binary read/write functions */
|
||||||
#define FOUG_STLA_WRITE_NULL_GET_TRIANGLE_FUNC (FOUG_STL_ERROR_TAG + 100)
|
FOUG_STLB_UNSUPPORTED_BYTE_ORDER_ERROR = FOUG_STL_ERROR_TAG + 300,
|
||||||
#define FOUG_STLA_WRITE_INVALID_REAL32_PRECISION (FOUG_STL_ERROR_TAG + 101)
|
FOUG_STLB_READ_HEADER_WRONG_SIZE_ERROR = FOUG_STL_ERROR_TAG + 301,
|
||||||
|
FOUG_STLB_READ_FACET_COUNT_ERROR = FOUG_STL_ERROR_TAG + 302
|
||||||
/* Specific error code common to foug_stlb_read() and foug_stlb_write() */
|
} foug_stl_rw_error_t;
|
||||||
#define FOUG_STLB_READWRITE_UNSUPPORTED_BYTE_ORDER (FOUG_STL_ERROR_TAG + 200)
|
|
||||||
|
|
||||||
/* Specific error codes returned by foug_stlb_read() */
|
|
||||||
#define FOUG_STLB_READ_HEADER_WRONG_SIZE_ERROR (FOUG_STL_ERROR_TAG + 300)
|
|
||||||
#define FOUG_STLB_READ_FACET_COUNT_ERROR (FOUG_STL_ERROR_TAG + 301)
|
|
||||||
#define FOUG_STLB_READ_UNSUPPORTED_BYTE_ORDER (FOUG_STL_ERROR_TAG + 302)
|
|
||||||
|
|
||||||
/* Specific error codes returned by foug_stlb_write() */
|
|
||||||
#define FOUG_STLB_WRITE_NULL_GET_TRIANGLE_FUNC (FOUG_STL_ERROR_TAG + 400)
|
|
||||||
|
|
||||||
#endif /* FOUG_LIBSTL_STL_ERROR_H */
|
#endif /* FOUG_LIBSTL_STL_ERROR_H */
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "stla_read.h"
|
#include "stla_read.h"
|
||||||
|
|
||||||
#include "../internal/ascii_parse.h"
|
#include "../internal/ascii_parse.h"
|
||||||
|
#include "../internal/libstl/stl_rw_common.h"
|
||||||
#include "../error.h"
|
#include "../error.h"
|
||||||
#include "stl_error.h"
|
#include "stl_error.h"
|
||||||
|
|
||||||
@ -384,12 +385,11 @@ int foug_stla_read(foug_stl_geom_creator_t *creator,
|
|||||||
char fixed_buffer[FOUG_STLA_READ_STRING_BUFFER_LEN];
|
char fixed_buffer[FOUG_STLA_READ_STRING_BUFFER_LEN];
|
||||||
foug_stla_parse_data_t parse_data;
|
foug_stla_parse_data_t parse_data;
|
||||||
|
|
||||||
if (trsf == NULL)
|
{ /* Check validity of input parameters */
|
||||||
return FOUG_DATAX_NULL_TRANSFER_ERROR;
|
int error = FOUG_DATAX_NO_ERROR;
|
||||||
if (trsf->buffer == NULL)
|
if (!foug_check_transfer(&error, trsf))
|
||||||
return FOUG_DATAX_NULL_BUFFER_ERROR;
|
return error;
|
||||||
if (trsf->buffer_size == 0)
|
}
|
||||||
return FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
|
||||||
|
|
||||||
parse_data.token = unknown_token;
|
parse_data.token = unknown_token;
|
||||||
parse_data.error = 0;
|
parse_data.error = 0;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "stla_write.h"
|
#include "stla_write.h"
|
||||||
|
|
||||||
#include "../error.h"
|
#include "../error.h"
|
||||||
|
#include "../internal/libstl/stl_rw_common.h"
|
||||||
#include "stl_error.h"
|
#include "stl_error.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -124,16 +125,17 @@ int foug_stla_write(foug_stl_geom_t* geom,
|
|||||||
char coords_format[64];
|
char coords_format[64];
|
||||||
int error = FOUG_DATAX_NO_ERROR;
|
int error = FOUG_DATAX_NO_ERROR;
|
||||||
|
|
||||||
|
/* Check validity of input parameters */
|
||||||
|
foug_check_transfer(&error, trsf);
|
||||||
|
foug_stl_check_geom(&error, geom);
|
||||||
if (real32_prec == 0 || real32_prec > 9)
|
if (real32_prec == 0 || real32_prec > 9)
|
||||||
return FOUG_STLA_WRITE_INVALID_REAL32_PRECISION;
|
error = FOUG_STLA_WRITE_INVALID_REAL32_PREC_ERROR;
|
||||||
if (buffer_iterator == NULL)
|
|
||||||
return FOUG_DATAX_NULL_BUFFER_ERROR;
|
|
||||||
if (trsf->buffer_size < FOUG_STLA_FACET_SIZE_P2)
|
if (trsf->buffer_size < FOUG_STLA_FACET_SIZE_P2)
|
||||||
return FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
error = FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
||||||
if (geom->get_triangle_func == NULL)
|
if (foug_datax_error(error))
|
||||||
return FOUG_STLA_WRITE_NULL_GET_TRIANGLE_FUNC;
|
return error;
|
||||||
|
|
||||||
{
|
{ /* Create XYZ coords format string (for normal and vertex coords) */
|
||||||
char* coords_format_iterator = coords_format;
|
char* coords_format_iterator = coords_format;
|
||||||
coords_format_iterator = foug_write_stdio_format(coords_format_iterator, real32_prec);
|
coords_format_iterator = foug_write_stdio_format(coords_format_iterator, real32_prec);
|
||||||
coords_format_iterator = foug_write_nspaces(coords_format_iterator, 2);
|
coords_format_iterator = foug_write_nspaces(coords_format_iterator, 2);
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#include "../internal/convert.h"
|
#include "../internal/convert.h"
|
||||||
#include "../internal/byte_swap.h"
|
#include "../internal/byte_swap.h"
|
||||||
|
#include "../internal/libstl/stl_rw_common.h"
|
||||||
#include "../internal/libstl/stlb_byte_swap.h"
|
#include "../internal/libstl/stlb_byte_swap.h"
|
||||||
#include "../internal/libstl/stlb_rw_common.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ static void read_triangle_alignsafe(const uint8_t* buffer, foug_stl_triangle_t*
|
|||||||
|
|
||||||
static void foug_stlb_read_facets(foug_stl_geom_creator_t* creator,
|
static void foug_stlb_read_facets(foug_stl_geom_creator_t* creator,
|
||||||
const uint8_t* buffer,
|
const uint8_t* buffer,
|
||||||
const foug_readwrite_helper* rparams)
|
const foug_stlb_readwrite_helper* rparams)
|
||||||
{
|
{
|
||||||
const uint32_t facet_count = rparams->facet_count;
|
const uint32_t facet_count = rparams->facet_count;
|
||||||
const uint32_t i_facet_offset = rparams->i_facet_offset;
|
const uint32_t i_facet_offset = rparams->i_facet_offset;
|
||||||
@ -67,18 +67,17 @@ int foug_stlb_read(foug_stl_geom_creator_t *creator,
|
|||||||
foug_endianness_t byte_order)
|
foug_endianness_t byte_order)
|
||||||
{
|
{
|
||||||
const foug_endianness_t host_byte_order = foug_host_endianness();
|
const foug_endianness_t host_byte_order = foug_host_endianness();
|
||||||
foug_readwrite_helper rparams;
|
foug_stlb_readwrite_helper rparams;
|
||||||
uint8_t header_data[FOUG_STLB_HEADER_SIZE];
|
uint8_t header_data[FOUG_STLB_HEADER_SIZE];
|
||||||
uint32_t total_facet_count = 0; /* Count of facets as declared in the stream */
|
uint32_t total_facet_count = 0; /* Count of facets as declared in the stream */
|
||||||
int error = FOUG_DATAX_NO_ERROR; /* Helper variable to store function result error code */
|
int error = FOUG_DATAX_NO_ERROR; /* Helper variable to store function result error code */
|
||||||
|
|
||||||
/* Check validity of input parameters */
|
/* Check validity of input parameters */
|
||||||
error = foug_stlb_check_params(trsf, byte_order);
|
if (!foug_stlb_check_params(&error, trsf, byte_order))
|
||||||
if (foug_datax_error(error))
|
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
/* Initialize rparams */
|
/* Initialize rparams */
|
||||||
memset(&rparams, 0, sizeof(foug_readwrite_helper));
|
memset(&rparams, 0, sizeof(foug_stlb_readwrite_helper));
|
||||||
if (host_byte_order != byte_order)
|
if (host_byte_order != byte_order)
|
||||||
rparams.fix_endian_func = foug_stl_triangle_bswap;
|
rparams.fix_endian_func = foug_stl_triangle_bswap;
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
#include "stl_error.h"
|
#include "stl_error.h"
|
||||||
|
|
||||||
#include "../internal/byte_codec.h"
|
#include "../internal/byte_codec.h"
|
||||||
|
#include "../internal/libstl/stl_rw_common.h"
|
||||||
#include "../internal/libstl/stlb_byte_swap.h"
|
#include "../internal/libstl/stlb_byte_swap.h"
|
||||||
#include "../internal/libstl/stlb_rw_common.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ static void write_triangle_alignsafe(const foug_stl_triangle_t* triangle, uint8_
|
|||||||
|
|
||||||
static void foug_stlb_write_facets(const foug_stl_geom_t* geom,
|
static void foug_stlb_write_facets(const foug_stl_geom_t* geom,
|
||||||
uint8_t* buffer,
|
uint8_t* buffer,
|
||||||
const foug_readwrite_helper* wparams)
|
const foug_stlb_readwrite_helper* wparams)
|
||||||
{
|
{
|
||||||
const uint32_t facet_count = wparams->facet_count;
|
const uint32_t facet_count = wparams->facet_count;
|
||||||
const uint32_t i_facet_offset = wparams->i_facet_offset;
|
const uint32_t i_facet_offset = wparams->i_facet_offset;
|
||||||
@ -65,20 +65,19 @@ int foug_stlb_write(const foug_stl_geom_t *geom,
|
|||||||
const uint8_t *header_data,
|
const uint8_t *header_data,
|
||||||
foug_endianness_t byte_order)
|
foug_endianness_t byte_order)
|
||||||
{
|
{
|
||||||
foug_readwrite_helper wparams;
|
foug_stlb_readwrite_helper wparams;
|
||||||
const uint32_t facet_count = geom != NULL ? geom->triangle_count : 0;
|
const uint32_t facet_count = geom != NULL ? geom->triangle_count : 0;
|
||||||
uint32_t i_facet = 0;
|
uint32_t i_facet = 0;
|
||||||
int error = FOUG_DATAX_NO_ERROR;
|
int error = FOUG_DATAX_NO_ERROR;
|
||||||
|
|
||||||
/* Check validity of input parameters */
|
/* Check validity of input parameters */
|
||||||
error = foug_stlb_check_params(trsf, byte_order);
|
foug_stl_check_geom(&error, geom);
|
||||||
|
foug_stlb_check_params(&error, trsf, byte_order);
|
||||||
if (foug_datax_error(error))
|
if (foug_datax_error(error))
|
||||||
return error;
|
return error;
|
||||||
if (geom == NULL || geom->get_triangle_func == NULL)
|
|
||||||
return FOUG_STLB_WRITE_NULL_GET_TRIANGLE_FUNC;
|
|
||||||
|
|
||||||
/* Initialize wparams */
|
/* Initialize wparams */
|
||||||
memset(&wparams, 0, sizeof(foug_readwrite_helper));
|
memset(&wparams, 0, sizeof(foug_stlb_readwrite_helper));
|
||||||
if (foug_host_endianness() != byte_order)
|
if (foug_host_endianness() != byte_order)
|
||||||
wparams.fix_endian_func = foug_stl_triangle_bswap;
|
wparams.fix_endian_func = foug_stl_triangle_bswap;
|
||||||
wparams.facet_count = trsf->buffer_size / FOUG_STLB_TRIANGLE_RAWSIZE;
|
wparams.facet_count = trsf->buffer_size / FOUG_STLB_TRIANGLE_RAWSIZE;
|
||||||
|
Loading…
Reference in New Issue
Block a user