diff --git a/src/gmio_core/endian.c b/src/gmio_core/endian.c index 4c4d138..4c22553 100644 --- a/src/gmio_core/endian.c +++ b/src/gmio_core/endian.c @@ -23,22 +23,22 @@ typedef union { - uint32_t integer; - uint8_t bytes[4]; + uint32_t integer; + uint8_t bytes[4]; } _internal_gmio_int_bytes_32_convert_t; gmio_endianness_t gmio_host_endianness() { - _internal_gmio_int_bytes_32_convert_t conv; + _internal_gmio_int_bytes_32_convert_t conv; - conv.integer = 0x01020408; - if (conv.bytes[0] == 0x08 && conv.bytes[3] == 0x01) - return GMIO_LITTLE_ENDIAN; - else if (conv.bytes[0] == 0x01 && conv.bytes[3] == 0x08) - return GMIO_BIG_ENDIAN; - else if (conv.bytes[1] == 0x08 && conv.bytes[2] == 0x01) - return GMIO_MIDDLE_ENDIAN; - else - return GMIO_OTHER_ENDIAN; + conv.integer = 0x01020408; + if (conv.bytes[0] == 0x08 && conv.bytes[3] == 0x01) + return GMIO_LITTLE_ENDIAN; + else if (conv.bytes[0] == 0x01 && conv.bytes[3] == 0x08) + return GMIO_BIG_ENDIAN; + else if (conv.bytes[1] == 0x08 && conv.bytes[2] == 0x01) + return GMIO_MIDDLE_ENDIAN; + else + return GMIO_OTHER_ENDIAN; } diff --git a/src/gmio_core/endian.h b/src/gmio_core/endian.h index 238155a..1debaaf 100644 --- a/src/gmio_core/endian.h +++ b/src/gmio_core/endian.h @@ -25,10 +25,10 @@ GMIO_C_LINKAGE_BEGIN /*! This enum identifies common endianness (byte order) of computer memory */ enum gmio_endianness { - GMIO_LITTLE_ENDIAN, - GMIO_BIG_ENDIAN, - GMIO_MIDDLE_ENDIAN, - GMIO_OTHER_ENDIAN + GMIO_LITTLE_ENDIAN, + GMIO_BIG_ENDIAN, + GMIO_MIDDLE_ENDIAN, + GMIO_OTHER_ENDIAN }; typedef enum gmio_endianness gmio_endianness_t; diff --git a/src/gmio_core/error.c b/src/gmio_core/error.c index 0bc4843..1425b5f 100644 --- a/src/gmio_core/error.c +++ b/src/gmio_core/error.c @@ -19,10 +19,10 @@ gmio_bool_t gmio_no_error(int code) { - return code == GMIO_NO_ERROR; + return code == GMIO_NO_ERROR; } gmio_bool_t gmio_error(int code) { - return code != GMIO_NO_ERROR; + return code != GMIO_NO_ERROR; } diff --git a/src/gmio_core/error.h b/src/gmio_core/error.h index 07993ae..49c0883 100644 --- a/src/gmio_core/error.h +++ b/src/gmio_core/error.h @@ -25,23 +25,23 @@ GMIO_C_LINKAGE_BEGIN /*! This enum defines common errors */ enum gmio_error { - /*! No error occurred, success */ - GMIO_NO_ERROR = 0, + /*! No error occurred, success */ + GMIO_NO_ERROR = 0, - /*! Pointer on argument gmio_transfer_t is NULL */ - GMIO_NULL_TRANSFER_ERROR = -1, + /*! Pointer on argument gmio_transfer_t is NULL */ + GMIO_NULL_TRANSFER_ERROR = -1, - /*! Pointer on argument buffer is NULL */ - GMIO_NULL_BUFFER_ERROR = -2, + /*! Pointer on argument buffer is NULL */ + GMIO_NULL_BUFFER_ERROR = -2, - /*! Argument buffer's size is too small */ - GMIO_INVALID_BUFFER_SIZE_ERROR = -3, + /*! Argument buffer's size is too small */ + GMIO_INVALID_BUFFER_SIZE_ERROR = -3, - /*! An error occurred with the argument gmio_stream_t (check gmio_stream_error()) */ - GMIO_STREAM_ERROR = -4, + /*! An error occurred with the argument gmio_stream_t (check gmio_stream_error()) */ + GMIO_STREAM_ERROR = -4, - /*! Operation was stopped by user (gmio_task_control::handle_progress_func() returned GMIO_FALSE) */ - GMIO_TASK_STOPPED_ERROR = -5 + /*! Operation was stopped by user (gmio_task_control::handle_progress_func() returned GMIO_FALSE) */ + GMIO_TASK_STOPPED_ERROR = -5 }; typedef enum gmio_error gmio_error_t; diff --git a/src/gmio_core/global.h b/src/gmio_core/global.h index e8398f6..270febe 100644 --- a/src/gmio_core/global.h +++ b/src/gmio_core/global.h @@ -80,8 +80,8 @@ typedef int gmio_bool_t; /*! This enum defines true/false boolean values */ enum gmio_bool_value { - GMIO_FALSE = 0, - GMIO_TRUE = 1 + GMIO_FALSE = 0, + GMIO_TRUE = 1 }; #endif /* GMIO_HAVE_STDBOOL_H */ diff --git a/src/gmio_core/internal/byte_codec.h b/src/gmio_core/internal/byte_codec.h index 0ae611c..2ba40ef 100644 --- a/src/gmio_core/internal/byte_codec.h +++ b/src/gmio_core/internal/byte_codec.h @@ -25,61 +25,61 @@ /*! Read a 16bit integer from memory-location \p bytes (little-endian byte order) */ GMIO_INLINE static uint16_t gmio_decode_uint16_le(const uint8_t* bytes) { - /* |0 |1 | */ - /* |BB|AA| -> 0xAABB */ - return (bytes[1] << 8) | bytes[0]; + /* |0 |1 | */ + /* |BB|AA| -> 0xAABB */ + return (bytes[1] << 8) | bytes[0]; } /*! Read a 16bit integer from memory-location \p bytes (big-endian byte order) */ GMIO_INLINE static uint16_t gmio_decode_uint16_be(const uint8_t* bytes) { - /* |0 |1 | */ - /* |AA|BB| -> 0xAABB */ - return (bytes[0] << 8) | bytes[1]; + /* |0 |1 | */ + /* |AA|BB| -> 0xAABB */ + return (bytes[0] << 8) | bytes[1]; } /*! Write 16bit integer \p val to the memory location at \p bytes in little-endian byte order */ GMIO_INLINE static void gmio_encode_uint16_le(uint16_t val, uint8_t* bytes) { - bytes[0] = val & 0xFF; - bytes[1] = (val >> 8) & 0xFF; + bytes[0] = val & 0xFF; + bytes[1] = (val >> 8) & 0xFF; } /*! Read a 32bit integer from memory-location \p bytes (little-endian byte order) */ GMIO_INLINE static uint32_t gmio_decode_uint32_le(const uint8_t* bytes) { - /* |DD|CC|BB|AA| -> 0xAABBCCDD */ - return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); + /* |DD|CC|BB|AA| -> 0xAABBCCDD */ + return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); } /*! Read a 32bit integer from memory-location \p bytes (mixed-endian byte order) */ GMIO_INLINE static uint32_t gmio_decode_uint32_me(const uint8_t* bytes) { - /* |DD|CC|BB|AA| -> 0xCCDDAABB */ - return (bytes[0] << 16) | (bytes[1] << 24) | (bytes[3] << 8) | bytes[2]; + /* |DD|CC|BB|AA| -> 0xCCDDAABB */ + return (bytes[0] << 16) | (bytes[1] << 24) | (bytes[3] << 8) | bytes[2]; } /*! Read a 32bit integer from memory-location \p bytes (big-endian byte order) */ GMIO_INLINE static uint32_t gmio_decode_uint32_be(const uint8_t* bytes) { - /* |DD|CC|BB|AA| -> 0xDDCCBBAA */ - return bytes[3] | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24); + /* |DD|CC|BB|AA| -> 0xDDCCBBAA */ + return bytes[3] | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24); } /*! Write 32bit integer \p val to the memory location at \p bytes in little-endian byte order */ GMIO_INLINE static void gmio_encode_uint32_le(uint32_t val, uint8_t* bytes) { - bytes[0] = val & 0xFF; - bytes[1] = (val >> 8) & 0xFF; - bytes[2] = (val >> 16) & 0xFF; - bytes[3] = (val >> 24) & 0xFF; + bytes[0] = val & 0xFF; + bytes[1] = (val >> 8) & 0xFF; + bytes[2] = (val >> 16) & 0xFF; + bytes[3] = (val >> 24) & 0xFF; } /*! Write 32bit integer \p val to the memory location at \p bytes in big-endian byte order */ GMIO_INLINE static void gmio_encode_uint32_be(uint32_t val, uint8_t* bytes) { - bytes[0] = (val >> 24) & 0xFF; - bytes[1] = (val >> 16) & 0xFF; - bytes[2] = (val >> 8) & 0xFF; - bytes[3] = val & 0xFF; + bytes[0] = (val >> 24) & 0xFF; + bytes[1] = (val >> 16) & 0xFF; + bytes[2] = (val >> 8) & 0xFF; + bytes[3] = val & 0xFF; } diff --git a/src/gmio_core/internal/byte_swap.h b/src/gmio_core/internal/byte_swap.h index ba85ed1..0129cd7 100644 --- a/src/gmio_core/internal/byte_swap.h +++ b/src/gmio_core/internal/byte_swap.h @@ -30,11 +30,11 @@ GMIO_INLINE static uint16_t gmio_uint16_bswap(uint16_t val) { #ifdef GMIO_HAVE_GCC_BUILTIN_BSWAP16_FUNC - return __builtin_bswap16(val); + return __builtin_bswap16(val); #elif defined(GMIO_HAVE_MSVC_BUILTIN_BSWAP_FUNC) - return _byteswap_ushort(val); + return _byteswap_ushort(val); #else - return ((val & 0x00FF) << 8) | ((val >> 8) & 0x00FF); + return ((val & 0x00FF) << 8) | ((val >> 8) & 0x00FF); #endif } @@ -42,14 +42,14 @@ GMIO_INLINE static uint16_t gmio_uint16_bswap(uint16_t val) GMIO_INLINE static uint32_t gmio_uint32_bswap(uint32_t val) { #ifdef GMIO_HAVE_GCC_BUILTIN_BSWAP32_FUNC - return __builtin_bswap32(val); + return __builtin_bswap32(val); #elif defined(GMIO_HAVE_MSVC_BUILTIN_BSWAP_FUNC) - return _byteswap_ulong(val); + return _byteswap_ulong(val); #else - return - ((val & 0x000000FF) << 24) - | ((val & 0x0000FF00) << 8) - | ((val >> 8) & 0x0000FF00) - | ((val >> 24) & 0x000000FF); + return + ((val & 0x000000FF) << 24) + | ((val & 0x0000FF00) << 8) + | ((val >> 8) & 0x0000FF00) + | ((val >> 24) & 0x000000FF); #endif } diff --git a/src/gmio_core/internal/convert.c b/src/gmio_core/internal/convert.c index e381ccc..971f765 100644 --- a/src/gmio_core/internal/convert.c +++ b/src/gmio_core/internal/convert.c @@ -19,14 +19,14 @@ gmio_real32_t gmio_convert_real32(uint32_t val) { - gmio_uint32_float_t conv; - conv.as_uint32 = val; - return conv.as_float; + gmio_uint32_float_t conv; + conv.as_uint32 = val; + return conv.as_float; } uint32_t gmio_convert_uint32(gmio_real32_t val) { - gmio_uint32_float_t conv; - conv.as_float = val; - return conv.as_uint32; + gmio_uint32_float_t conv; + conv.as_float = val; + return conv.as_uint32; } diff --git a/src/gmio_core/internal/convert.h b/src/gmio_core/internal/convert.h index 6eb2750..6a1bd74 100644 --- a/src/gmio_core/internal/convert.h +++ b/src/gmio_core/internal/convert.h @@ -22,8 +22,8 @@ union gmio_uint32_float { - uint32_t as_uint32; - float as_float; + uint32_t as_uint32; + float as_float; }; typedef union gmio_uint32_float gmio_uint32_float_t; diff --git a/src/gmio_core/internal/string_parse.c b/src/gmio_core/internal/string_parse.c index dc149a2..52ffd8f 100644 --- a/src/gmio_core/internal/string_parse.c +++ b/src/gmio_core/internal/string_parse.c @@ -23,112 +23,112 @@ void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t *it) { - /* Trick: declaring the buffer exhausted will actually trigger the first call to + /* Trick: declaring the buffer exhausted will actually trigger the first call to * gmio_stream_read() inside gmio_next_char() */ - it->buffer.len = 0; - it->buffer_pos = it->buffer.max_len; - gmio_next_char(it); + it->buffer.len = 0; + it->buffer_pos = it->buffer.max_len; + gmio_next_char(it); } char *gmio_current_char(const gmio_string_stream_fwd_iterator_t *it) { - if (it != NULL && it->buffer_pos < it->buffer.len) - return it->buffer.ptr + it->buffer_pos; - return NULL; + if (it != NULL && it->buffer_pos < it->buffer.len) + return it->buffer.ptr + it->buffer_pos; + return NULL; } char *gmio_next_char(gmio_string_stream_fwd_iterator_t *it) { - if (it == NULL) - return NULL; + if (it == NULL) + return NULL; - if ((it->buffer_pos + 1) < it->buffer.len) { - ++(it->buffer_pos); - return it->buffer.ptr + it->buffer_pos; - } - else { - if (gmio_stream_error(it->stream) != 0 || gmio_stream_at_end(it->stream)) - return NULL; - - /* Read next chunk of data */ - it->buffer_pos = 0; - it->buffer.len = gmio_stream_read(it->stream, it->buffer.ptr, sizeof(char), it->buffer.max_len); - if (gmio_stream_error(it->stream) == 0) { - if (it->stream_read_hook != NULL) - it->stream_read_hook(it->cookie, &it->buffer); - return it->buffer.ptr; + if ((it->buffer_pos + 1) < it->buffer.len) { + ++(it->buffer_pos); + return it->buffer.ptr + it->buffer_pos; } - } + else { + if (gmio_stream_error(it->stream) != 0 || gmio_stream_at_end(it->stream)) + return NULL; - return NULL; + /* Read next chunk of data */ + it->buffer_pos = 0; + it->buffer.len = gmio_stream_read(it->stream, it->buffer.ptr, sizeof(char), it->buffer.max_len); + if (gmio_stream_error(it->stream) == 0) { + if (it->stream_read_hook != NULL) + it->stream_read_hook(it->cookie, &it->buffer); + return it->buffer.ptr; + } + } + + return NULL; } void gmio_skip_spaces(gmio_string_stream_fwd_iterator_t *it) { - const char* curr_char = gmio_current_char(it); - while (curr_char != NULL && isspace(*curr_char)) - curr_char = gmio_next_char(it); + const char* curr_char = gmio_current_char(it); + while (curr_char != NULL && isspace(*curr_char)) + curr_char = gmio_next_char(it); } int gmio_eat_word(gmio_string_stream_fwd_iterator_t *it, gmio_string_buffer_t *buffer) { - const char* stream_curr_char = NULL; - int isspace_res = 0; - size_t i = 0; + const char* stream_curr_char = NULL; + int isspace_res = 0; + size_t i = 0; - if (buffer == NULL || buffer->ptr == NULL) - return -1; + if (buffer == NULL || buffer->ptr == NULL) + return -1; - buffer->len = 0; - gmio_skip_spaces(it); - stream_curr_char = gmio_current_char(it); + buffer->len = 0; + gmio_skip_spaces(it); + stream_curr_char = gmio_current_char(it); - while (i < buffer->max_len && stream_curr_char != NULL && isspace_res == 0) { - isspace_res = isspace(*stream_curr_char); - if (isspace_res == 0) { - buffer->ptr[i] = *stream_curr_char; - stream_curr_char = gmio_next_char(it); - ++i; + while (i < buffer->max_len && stream_curr_char != NULL && isspace_res == 0) { + isspace_res = isspace(*stream_curr_char); + if (isspace_res == 0) { + buffer->ptr[i] = *stream_curr_char; + stream_curr_char = gmio_next_char(it); + ++i; + } } - } - if (i < buffer->max_len) { - buffer->ptr[i] = 0; /* End string with terminating null byte */ - buffer->len = i; - if (stream_curr_char != NULL || gmio_stream_at_end(it->stream)) - return 0; - return -2; - } - return -3; + if (i < buffer->max_len) { + buffer->ptr[i] = 0; /* End string with terminating null byte */ + buffer->len = i; + if (stream_curr_char != NULL || gmio_stream_at_end(it->stream)) + return 0; + return -2; + } + return -3; } int gmio_get_real32(const char *str, gmio_real32_t *value_ptr) { - char* end_ptr; /* for strtod() */ + char* end_ptr; /* for strtod() */ #ifdef GMIO_HAVE_STRTOF_FUNC - *value_ptr = strtof(str, &end_ptr); /* Requires C99 */ + *value_ptr = strtof(str, &end_ptr); /* Requires C99 */ #else - *value_ptr = (gmio_real32_t)strtod(str, &end_ptr); + *value_ptr = (gmio_real32_t)strtod(str, &end_ptr); #endif - if (end_ptr == str || errno == ERANGE) - return -1; + if (end_ptr == str || errno == ERANGE) + return -1; - return 0; + return 0; } gmio_bool_t gmio_checked_next_chars(gmio_string_stream_fwd_iterator_t *it, const char *str) { - size_t pos = 0; - const char* curr_char = gmio_current_char(it); - gmio_bool_t same = curr_char != NULL && *curr_char == *str; + size_t pos = 0; + const char* curr_char = gmio_current_char(it); + gmio_bool_t same = curr_char != NULL && *curr_char == *str; - while (same) { - curr_char = gmio_next_char(it); - same = curr_char != NULL && *curr_char == str[++pos]; - } + while (same) { + curr_char = gmio_next_char(it); + same = curr_char != NULL && *curr_char == str[++pos]; + } - return same; + return same; } diff --git a/src/gmio_core/internal/string_parse.h b/src/gmio_core/internal/string_parse.h index 9d6563d..e174011 100644 --- a/src/gmio_core/internal/string_parse.h +++ b/src/gmio_core/internal/string_parse.h @@ -23,21 +23,21 @@ struct gmio_string_buffer { - char* ptr; /*!< Buffer contents */ - size_t len; /*!< Size(length) of current contents */ - size_t max_len; /*!< Maximum contents size(length) */ + char* ptr; /*!< Buffer contents */ + size_t len; /*!< Size(length) of current contents */ + size_t max_len; /*!< Maximum contents size(length) */ }; typedef struct gmio_string_buffer gmio_string_buffer_t; struct gmio_string_stream_fwd_iterator { - gmio_stream_t* stream; - gmio_string_buffer_t buffer; - size_t buffer_pos; /*!< Position indicator in buffer */ + gmio_stream_t* stream; + gmio_string_buffer_t buffer; + size_t buffer_pos; /*!< Position indicator in buffer */ - void* cookie; - void (*stream_read_hook)(void* cookie, const gmio_string_buffer_t* str_buffer); + void* cookie; + void (*stream_read_hook)(void* cookie, const gmio_string_buffer_t* str_buffer); }; typedef struct gmio_string_stream_fwd_iterator gmio_string_stream_fwd_iterator_t; diff --git a/src/gmio_core/stream.c b/src/gmio_core/stream.c index 12c5c58..ceb5766 100644 --- a/src/gmio_core/stream.c +++ b/src/gmio_core/stream.c @@ -23,17 +23,17 @@ void gmio_stream_set_null(gmio_stream_t* stream) { - memset(stream, 0, sizeof(gmio_stream_t)); + memset(stream, 0, sizeof(gmio_stream_t)); } static gmio_bool_t gmio_stream_stdio_at_end(void* cookie) { - return feof((FILE*) cookie); + return feof((FILE*) cookie); } static int gmio_stream_stdio_error(void* cookie) { - return ferror((FILE*) cookie); + return ferror((FILE*) cookie); } static size_t gmio_stream_stdio_read(void* cookie, @@ -41,7 +41,7 @@ static size_t gmio_stream_stdio_read(void* cookie, size_t item_size, size_t item_count) { - return fread(ptr, item_size, item_count, (FILE*) cookie); + return fread(ptr, item_size, item_count, (FILE*) cookie); } static size_t gmio_stream_stdio_write(void* cookie, @@ -49,42 +49,42 @@ static size_t gmio_stream_stdio_write(void* cookie, size_t item_size, size_t item_count) { - return fwrite(ptr, item_size, item_count, (FILE*) cookie); + return fwrite(ptr, item_size, item_count, (FILE*) cookie); } void gmio_stream_set_stdio(gmio_stream_t* stream, FILE* file) { - stream->cookie = file; - stream->at_end_func = gmio_stream_stdio_at_end; - stream->error_func = gmio_stream_stdio_error; - stream->read_func = gmio_stream_stdio_read; - stream->write_func = gmio_stream_stdio_write; + stream->cookie = file; + stream->at_end_func = gmio_stream_stdio_at_end; + stream->error_func = gmio_stream_stdio_error; + stream->read_func = gmio_stream_stdio_read; + stream->write_func = gmio_stream_stdio_write; } gmio_bool_t gmio_stream_at_end(gmio_stream_t* stream) { - if (stream != NULL && stream->at_end_func != NULL) - return stream->at_end_func(stream->cookie); - return GMIO_FALSE; + if (stream != NULL && stream->at_end_func != NULL) + return stream->at_end_func(stream->cookie); + return GMIO_FALSE; } int gmio_stream_error(gmio_stream_t* stream) { - if (stream != NULL && stream->error_func != NULL) - return stream->error_func(stream->cookie); - return 0; + if (stream != NULL && stream->error_func != NULL) + return stream->error_func(stream->cookie); + return 0; } size_t gmio_stream_read(gmio_stream_t* stream, void *ptr, size_t size, size_t count) { - if (stream != NULL && stream->read_func != NULL) - return stream->read_func(stream->cookie, ptr, size, count); - return 0; + if (stream != NULL && stream->read_func != NULL) + return stream->read_func(stream->cookie, ptr, size, count); + return 0; } size_t gmio_stream_write(gmio_stream_t* stream, const void *ptr, size_t size, size_t count) { - if (stream != NULL && stream->write_func != NULL) - return stream->write_func(stream->cookie, ptr, size, count); - return 0; + if (stream != NULL && stream->write_func != NULL) + return stream->write_func(stream->cookie, ptr, size, count); + return 0; } diff --git a/src/gmio_core/stream.h b/src/gmio_core/stream.h index 63fa3fd..17f4d2c 100644 --- a/src/gmio_core/stream.h +++ b/src/gmio_core/stream.h @@ -37,11 +37,11 @@ GMIO_C_LINKAGE_BEGIN */ struct gmio_stream { - /*! \brief Opaque pointer on the user stream, passed as first argument to + /*! \brief Opaque pointer on the user stream, passed as first argument to * hook functions */ - void* cookie; + void* cookie; - /*! \brief Pointer on a function that checks end-of-stream indicator + /*! \brief Pointer on a function that checks end-of-stream indicator * * Checks whether the end-of-stream indicator associated with stream * pointed by \p cookie is set, returning GMIO_TRUE if is. @@ -49,9 +49,9 @@ struct gmio_stream * The function should behaves like C standard [feof()] * (http://pubs.opengroup.org/onlinepubs/007904975/functions/feof.html) */ - gmio_bool_t (*at_end_func)(void* cookie); + gmio_bool_t (*at_end_func)(void* cookie); - /*! \brief Pointer on a function that checks error indicator + /*! \brief Pointer on a function that checks error indicator * * Checks if the error indicator associated with stream pointed by \p cookie * is set, returning a value different from zero if it is. @@ -59,9 +59,9 @@ struct gmio_stream * The function should behaves like C standard [ferror()] * (http://pubs.opengroup.org/onlinepubs/007904975/functions/ferror.html) */ - int (*error_func)(void* cookie); + int (*error_func)(void* cookie); - /*! \brief Pointer on a function that reads block of data from stream + /*! \brief Pointer on a function that reads block of data from stream * * \details Reads an array of \p count elements, each one with a size of \p size * bytes, from the stream pointed by \p cookie and stores them in the block @@ -72,9 +72,9 @@ struct gmio_stream * * \returns The total number of elements successfully read */ - size_t (*read_func)(void* cookie, void* ptr, size_t size, size_t count); + size_t (*read_func)(void* cookie, void* ptr, size_t size, size_t count); - /*! \brief Pointer on a function that writes block of data to stream + /*! \brief Pointer on a function that writes block of data to stream * * \details Writes an array of \p count elements, each one with a size of \p size * bytes, from the block of memory pointed by \p ptr to the current position @@ -85,7 +85,7 @@ struct gmio_stream * * \returns The total number of elements successfully written */ - size_t (*write_func)(void* cookie, const void* ptr, size_t size, size_t count); + size_t (*write_func)(void* cookie, const void* ptr, size_t size, size_t count); }; typedef struct gmio_stream gmio_stream_t; diff --git a/src/gmio_core/task_control.c b/src/gmio_core/task_control.c index e2d5e16..3fc067c 100644 --- a/src/gmio_core/task_control.c +++ b/src/gmio_core/task_control.c @@ -21,7 +21,7 @@ gmio_bool_t gmio_task_control_is_stop_requested(const gmio_task_control_t* task_ctrl) { - if (task_ctrl != NULL && task_ctrl->is_stop_requested_func != NULL) - return task_ctrl->is_stop_requested_func(task_ctrl->cookie); - return GMIO_FALSE; + if (task_ctrl != NULL && task_ctrl->is_stop_requested_func != NULL) + return task_ctrl->is_stop_requested_func(task_ctrl->cookie); + return GMIO_FALSE; } diff --git a/src/gmio_core/task_control.h b/src/gmio_core/task_control.h index 4013d39..b9e6300 100644 --- a/src/gmio_core/task_control.h +++ b/src/gmio_core/task_control.h @@ -28,15 +28,15 @@ GMIO_C_LINKAGE_BEGIN */ struct gmio_task_control { - /*! Opaque pointer on a user task object, passed as first argument to hook functions */ - void* cookie; + /*! Opaque pointer on a user task object, passed as first argument to hook functions */ + void* cookie; - /*! \brief Pointer on a function that says if the current task must stop + /*! \brief Pointer on a function that says if the current task must stop * * If GMIO_TRUE is returned then the current task should abort as soon as possible, otherwise it * can continue execution. */ - gmio_bool_t (*is_stop_requested_func)(void* cookie); + gmio_bool_t (*is_stop_requested_func)(void* cookie); }; typedef struct gmio_task_control gmio_task_control_t; diff --git a/src/gmio_core/transfer.h b/src/gmio_core/transfer.h index db92301..0256d2a 100644 --- a/src/gmio_core/transfer.h +++ b/src/gmio_core/transfer.h @@ -25,17 +25,17 @@ /*! Defines objects required for any transfer(read/write) operation */ struct gmio_transfer { - /*! The stream object to be used for I/O */ - gmio_stream_t stream; + /*! The stream object to be used for I/O */ + gmio_stream_t stream; - /*! The optional object used to control execution of the transfer */ - gmio_task_control_t task_control; + /*! The optional object used to control execution of the transfer */ + gmio_task_control_t task_control; - /*! Pointer on a memory buffer used by the transfer for stream operations */ - void* buffer; + /*! Pointer on a memory buffer used by the transfer for stream operations */ + void* buffer; - /*! Size (in bytes) of the memory buffer */ - size_t buffer_size; + /*! Size (in bytes) of the memory buffer */ + size_t buffer_size; }; typedef struct gmio_transfer gmio_transfer_t; diff --git a/src/gmio_stl/internal/stl_rw_common.c b/src/gmio_stl/internal/stl_rw_common.c index 2054c29..3255f0d 100644 --- a/src/gmio_stl/internal/stl_rw_common.c +++ b/src/gmio_stl/internal/stl_rw_common.c @@ -22,41 +22,41 @@ gmio_bool_t gmio_check_transfer(int *error, const gmio_transfer_t* trsf) { - if (trsf == NULL) { - *error = GMIO_NULL_TRANSFER_ERROR; - } - else { - if (trsf->buffer == NULL) - *error = GMIO_NULL_BUFFER_ERROR; - else if (trsf->buffer_size == 0) - *error = GMIO_INVALID_BUFFER_SIZE_ERROR; - } + if (trsf == NULL) { + *error = GMIO_NULL_TRANSFER_ERROR; + } + else { + if (trsf->buffer == NULL) + *error = GMIO_NULL_BUFFER_ERROR; + else if (trsf->buffer_size == 0) + *error = GMIO_INVALID_BUFFER_SIZE_ERROR; + } - return gmio_no_error(*error); + return gmio_no_error(*error); } gmio_bool_t gmio_stl_check_mesh(int *error, const gmio_stl_mesh_t* mesh) { - if (mesh == NULL - || (mesh->triangle_count > 0 && mesh->get_triangle_func == NULL)) - { - *error = GMIO_STL_WRITE_NULL_GET_TRIANGLE_FUNC_ERROR; - } + if (mesh == NULL + || (mesh->triangle_count > 0 && mesh->get_triangle_func == NULL)) + { + *error = GMIO_STL_WRITE_NULL_GET_TRIANGLE_FUNC_ERROR; + } - return gmio_no_error(*error); + return gmio_no_error(*error); } gmio_bool_t gmio_stlb_check_params(int *error, const gmio_transfer_t *trsf, gmio_endianness_t byte_order) { - if (!gmio_check_transfer(error, trsf)) - return GMIO_FALSE; + if (!gmio_check_transfer(error, trsf)) + return GMIO_FALSE; - if (trsf->buffer_size < GMIO_STLB_MIN_CONTENTS_SIZE) - *error = GMIO_INVALID_BUFFER_SIZE_ERROR; - if (byte_order != GMIO_LITTLE_ENDIAN && byte_order != GMIO_BIG_ENDIAN) - *error = GMIO_STLB_UNSUPPORTED_BYTE_ORDER_ERROR; + if (trsf->buffer_size < GMIO_STLB_MIN_CONTENTS_SIZE) + *error = GMIO_INVALID_BUFFER_SIZE_ERROR; + if (byte_order != GMIO_LITTLE_ENDIAN && byte_order != GMIO_BIG_ENDIAN) + *error = GMIO_STLB_UNSUPPORTED_BYTE_ORDER_ERROR; - return gmio_no_error(*error); + return gmio_no_error(*error); } diff --git a/src/gmio_stl/internal/stl_rw_common.h b/src/gmio_stl/internal/stl_rw_common.h index aa8b24d..8b16f35 100644 --- a/src/gmio_stl/internal/stl_rw_common.h +++ b/src/gmio_stl/internal/stl_rw_common.h @@ -26,9 +26,9 @@ struct gmio_stlb_readwrite_helper { - uint32_t facet_count; - uint32_t i_facet_offset; - void (*fix_endian_func)(gmio_stl_triangle_t* tri); + uint32_t facet_count; + uint32_t i_facet_offset; + void (*fix_endian_func)(gmio_stl_triangle_t* tri); }; typedef struct gmio_stlb_readwrite_helper gmio_stlb_readwrite_helper_t; diff --git a/src/gmio_stl/internal/stlb_byte_swap.c b/src/gmio_stl/internal/stlb_byte_swap.c index fb54631..3f75de7 100644 --- a/src/gmio_stl/internal/stlb_byte_swap.c +++ b/src/gmio_stl/internal/stlb_byte_swap.c @@ -21,11 +21,11 @@ void gmio_stl_triangle_bswap(gmio_stl_triangle_t* triangle) { - int i; - uint32_t* uintcoord_ptr = (uint32_t*)&(triangle->normal.x); + int i; + uint32_t* uintcoord_ptr = (uint32_t*)&(triangle->normal.x); - for (i = 0; i < 12; ++i) - *(uintcoord_ptr + i) = gmio_uint32_bswap(*(uintcoord_ptr + i)); - if (triangle->attribute_byte_count != 0) - triangle->attribute_byte_count = gmio_uint16_bswap(triangle->attribute_byte_count); + for (i = 0; i < 12; ++i) + *(uintcoord_ptr + i) = gmio_uint32_bswap(*(uintcoord_ptr + i)); + if (triangle->attribute_byte_count != 0) + triangle->attribute_byte_count = gmio_uint16_bswap(triangle->attribute_byte_count); } diff --git a/src/gmio_stl/stl_error.h b/src/gmio_stl/stl_error.h index c59ebf9..f7d97bb 100644 --- a/src/gmio_stl/stl_error.h +++ b/src/gmio_stl/stl_error.h @@ -24,27 +24,27 @@ enum { GMIO_STL_ERROR_TAG = 0x11000000 }; /*! This enum defines the various error codes reported by STL read/write functions */ enum gmio_stl_error { - /*! Common STL write error indicating gmio_stl_mesh::get_triangle_func() pointer is NULL */ - GMIO_STL_WRITE_NULL_GET_TRIANGLE_FUNC_ERROR = GMIO_STL_ERROR_TAG + 1, + /*! Common STL write error indicating gmio_stl_mesh::get_triangle_func() pointer is NULL */ + GMIO_STL_WRITE_NULL_GET_TRIANGLE_FUNC_ERROR = GMIO_STL_ERROR_TAG + 1, - /* Specific error codes returned by STL_ascii read function */ + /* Specific error codes returned by STL_ascii read function */ - /*! Parsing error occured in gmio_stla_read() due to malformed STL ascii input */ - GMIO_STLA_READ_PARSE_ERROR = GMIO_STL_ERROR_TAG + 100, + /*! Parsing error occured in gmio_stla_read() due to malformed STL ascii input */ + GMIO_STLA_READ_PARSE_ERROR = GMIO_STL_ERROR_TAG + 100, - /*! Invalid max number of decimal significants digits for gmio_stla_write(), must be in [1..9] */ - GMIO_STLA_WRITE_INVALID_REAL32_PREC_ERROR = GMIO_STL_ERROR_TAG + 101, + /*! Invalid max number of decimal significants digits for gmio_stla_write(), must be in [1..9] */ + GMIO_STLA_WRITE_INVALID_REAL32_PREC_ERROR = GMIO_STL_ERROR_TAG + 101, - /* Specific error codes returned by STL_binary read/write functions */ + /* Specific error codes returned by STL_binary read/write functions */ - /*! The byte order argument supplied is not supported, must be little or big endian */ - GMIO_STLB_UNSUPPORTED_BYTE_ORDER_ERROR = GMIO_STL_ERROR_TAG + 300, + /*! The byte order argument supplied is not supported, must be little or big endian */ + GMIO_STLB_UNSUPPORTED_BYTE_ORDER_ERROR = GMIO_STL_ERROR_TAG + 300, - /*! Error occured when reading header data in gmio_stlb_read() */ - GMIO_STLB_READ_HEADER_WRONG_SIZE_ERROR = GMIO_STL_ERROR_TAG + 301, + /*! Error occured when reading header data in gmio_stlb_read() */ + GMIO_STLB_READ_HEADER_WRONG_SIZE_ERROR = GMIO_STL_ERROR_TAG + 301, - /*! Error occured when reading facet count in gmio_stlb_read() */ - GMIO_STLB_READ_FACET_COUNT_ERROR = GMIO_STL_ERROR_TAG + 302 + /*! Error occured when reading facet count in gmio_stlb_read() */ + GMIO_STLB_READ_FACET_COUNT_ERROR = GMIO_STL_ERROR_TAG + 302 }; typedef enum gmio_stl_error gmio_stl_error_t; diff --git a/src/gmio_stl/stl_format.c b/src/gmio_stl/stl_format.c index c45e5e2..936afa0 100644 --- a/src/gmio_stl/stl_format.c +++ b/src/gmio_stl/stl_format.c @@ -30,47 +30,47 @@ enum { _INTERNAL_GMIO_FIXED_BUFFER_SIZE = 512 }; gmio_stl_format_t gmio_stl_get_format(gmio_stream_t *stream, size_t data_size) { - char fixed_buffer[_INTERNAL_GMIO_FIXED_BUFFER_SIZE]; - size_t read_size = 0; + char fixed_buffer[_INTERNAL_GMIO_FIXED_BUFFER_SIZE]; + size_t read_size = 0; - if (stream == NULL || data_size == 0) - return GMIO_STL_UNKNOWN_FORMAT; + if (stream == NULL || data_size == 0) + return GMIO_STL_UNKNOWN_FORMAT; - /* Read a chunk of bytes from stream, then try to find format from that */ - memset(fixed_buffer, 0, _INTERNAL_GMIO_FIXED_BUFFER_SIZE); - read_size = gmio_stream_read(stream, &fixed_buffer, 1, _INTERNAL_GMIO_FIXED_BUFFER_SIZE); - read_size = read_size < _INTERNAL_GMIO_FIXED_BUFFER_SIZE ? read_size : - _INTERNAL_GMIO_FIXED_BUFFER_SIZE; + /* Read a chunk of bytes from stream, then try to find format from that */ + memset(fixed_buffer, 0, _INTERNAL_GMIO_FIXED_BUFFER_SIZE); + read_size = gmio_stream_read(stream, &fixed_buffer, 1, _INTERNAL_GMIO_FIXED_BUFFER_SIZE); + read_size = read_size < _INTERNAL_GMIO_FIXED_BUFFER_SIZE ? read_size : + _INTERNAL_GMIO_FIXED_BUFFER_SIZE; - /* Binary STL ? */ - if (read_size >= (GMIO_STLB_HEADER_SIZE + 4)) { - /* Try with little-endian format */ - uint32_t facet_count = gmio_decode_uint32_le((const uint8_t*)fixed_buffer + 80); + /* Binary STL ? */ + if (read_size >= (GMIO_STLB_HEADER_SIZE + 4)) { + /* Try with little-endian format */ + uint32_t facet_count = gmio_decode_uint32_le((const uint8_t*)fixed_buffer + 80); - if ((GMIO_STLB_HEADER_SIZE + 4 + facet_count*GMIO_STLB_TRIANGLE_RAWSIZE) == data_size) - return GMIO_STL_BINARY_LE_FORMAT; + if ((GMIO_STLB_HEADER_SIZE + 4 + facet_count*GMIO_STLB_TRIANGLE_RAWSIZE) == data_size) + return GMIO_STL_BINARY_LE_FORMAT; - /* Try with byte-reverted facet count */ - facet_count = gmio_uint32_bswap(facet_count); - if ((GMIO_STLB_HEADER_SIZE + 4 + facet_count*GMIO_STLB_TRIANGLE_RAWSIZE) == data_size) - return GMIO_STL_BINARY_BE_FORMAT; - } - - /* ASCII STL ? */ - { - /* Skip spaces at beginning */ - size_t pos = 0; - while (pos < read_size && isspace(fixed_buffer[pos])) - ++pos; - - /* Next token (if exists) must match "solid " */ - if (pos < _INTERNAL_GMIO_FIXED_BUFFER_SIZE - && strncmp(fixed_buffer + pos, "solid ", 6) == 0) - { - return GMIO_STL_ASCII_FORMAT; + /* Try with byte-reverted facet count */ + facet_count = gmio_uint32_bswap(facet_count); + if ((GMIO_STLB_HEADER_SIZE + 4 + facet_count*GMIO_STLB_TRIANGLE_RAWSIZE) == data_size) + return GMIO_STL_BINARY_BE_FORMAT; } - } - /* Fallback case */ - return GMIO_STL_UNKNOWN_FORMAT; + /* ASCII STL ? */ + { + /* Skip spaces at beginning */ + size_t pos = 0; + while (pos < read_size && isspace(fixed_buffer[pos])) + ++pos; + + /* Next token (if exists) must match "solid " */ + if (pos < _INTERNAL_GMIO_FIXED_BUFFER_SIZE + && strncmp(fixed_buffer + pos, "solid ", 6) == 0) + { + return GMIO_STL_ASCII_FORMAT; + } + } + + /* Fallback case */ + return GMIO_STL_UNKNOWN_FORMAT; } diff --git a/src/gmio_stl/stl_format.h b/src/gmio_stl/stl_format.h index d9d97f4..d5f5642 100644 --- a/src/gmio_stl/stl_format.h +++ b/src/gmio_stl/stl_format.h @@ -26,10 +26,10 @@ GMIO_C_LINKAGE_BEGIN /*! This enums defines the various STL formats */ enum gmio_stl_format { - GMIO_STL_ASCII_FORMAT, /*!< STL ASCII (text) */ - GMIO_STL_BINARY_LE_FORMAT, /*!< STL binary (little-endian) */ - GMIO_STL_BINARY_BE_FORMAT, /*!< STL binary (big-endian) */ - GMIO_STL_UNKNOWN_FORMAT + GMIO_STL_ASCII_FORMAT, /*!< STL ASCII (text) */ + GMIO_STL_BINARY_LE_FORMAT, /*!< STL binary (little-endian) */ + GMIO_STL_BINARY_BE_FORMAT, /*!< STL binary (big-endian) */ + GMIO_STL_UNKNOWN_FORMAT }; typedef enum gmio_stl_format gmio_stl_format_t; diff --git a/src/gmio_stl/stl_io.h b/src/gmio_stl/stl_io.h index 3fa044f..6e929c4 100644 --- a/src/gmio_stl/stl_io.h +++ b/src/gmio_stl/stl_io.h @@ -39,7 +39,7 @@ GMIO_C_LINKAGE_BEGIN */ struct gmio_stla_read_options { - void* dummy; /*!< Structs must have at least one member in strict ISO-C90 */ + void* dummy; /*!< Structs must have at least one member in strict ISO-C90 */ }; typedef struct gmio_stla_read_options gmio_stla_read_options_t; @@ -56,11 +56,11 @@ int gmio_stla_read(gmio_stl_mesh_creator_t* creator, */ struct gmio_stla_write_options { - /*! May be NULL to generate default name */ - const char* solid_name; + /*! May be NULL to generate default name */ + const char* solid_name; - /*! The maximum number of significant digits(set to 9 if options == NULL) */ - uint8_t real32_prec; + /*! The maximum number of significant digits(set to 9 if options == NULL) */ + uint8_t real32_prec; }; typedef struct gmio_stla_write_options gmio_stla_write_options_t; @@ -89,8 +89,8 @@ int gmio_stla_write(const gmio_stl_mesh_t* mesh, */ struct gmio_stlb_read_options { - /*! Set to host byte order if not specified (ie. options == NULL) */ - gmio_endianness_t byte_order; + /*! Set to host byte order if not specified (ie. options == NULL) */ + gmio_endianness_t byte_order; }; typedef struct gmio_stlb_read_options gmio_stlb_read_options_t; @@ -106,11 +106,11 @@ int gmio_stlb_read(gmio_stl_mesh_creator_t* creator, */ struct gmio_stlb_write_options { - /*! Header data consisting of 80 bytes */ - const uint8_t* header_data; + /*! Header data consisting of 80 bytes */ + const uint8_t* header_data; - /*! Set to host byte order if not specified (ie. options == NULL) */ - gmio_endianness_t byte_order; + /*! Set to host byte order if not specified (ie. options == NULL) */ + gmio_endianness_t byte_order; }; typedef struct gmio_stlb_write_options gmio_stlb_write_options_t; diff --git a/src/gmio_stl/stl_mesh.h b/src/gmio_stl/stl_mesh.h index 19aa184..8864a08 100644 --- a/src/gmio_stl/stl_mesh.h +++ b/src/gmio_stl/stl_mesh.h @@ -24,14 +24,14 @@ /*! Provides an interface for accessing the underlying(hidden) user mesh */ struct gmio_stl_mesh { - /*! Opaque pointer on the user mesh, passed as first argument to hook functions */ - const void* cookie; + /*! Opaque pointer on the user mesh, passed as first argument to hook functions */ + const void* cookie; - /*! Number of triangles in the mesh */ - uint32_t triangle_count; + /*! Number of triangles in the mesh */ + uint32_t triangle_count; - /*! Pointer on a function that stores the mesh triangle of index \p tri_id into \p triangle */ - void (*get_triangle_func)(const void* cookie, uint32_t tri_id, gmio_stl_triangle_t* triangle); + /*! Pointer on a function that stores the mesh triangle of index \p tri_id into \p triangle */ + void (*get_triangle_func)(const void* cookie, uint32_t tri_id, gmio_stl_triangle_t* triangle); }; typedef struct gmio_stl_mesh gmio_stl_mesh_t; diff --git a/src/gmio_stl/stl_mesh_creator.h b/src/gmio_stl/stl_mesh_creator.h index 8d11ed1..c9ce9ff 100644 --- a/src/gmio_stl/stl_mesh_creator.h +++ b/src/gmio_stl/stl_mesh_creator.h @@ -26,44 +26,44 @@ */ struct gmio_stl_mesh_creator { - /*! \brief Opaque pointer on the user mesh, passed as first argument to hook + /*! \brief Opaque pointer on the user mesh, passed as first argument to hook * functions */ - void* cookie; + void* cookie; - /* All function pointers are optional (ie can be set to NULL) */ + /* All function pointers are optional (ie can be set to NULL) */ - /*! \brief Pointer on a function that handles declaration of a solid of name + /*! \brief Pointer on a function that handles declaration of a solid of name * \p solid_name * * Optional function useful only with STL ascii (ie. gmio_stla_read()) */ - void (*ascii_begin_solid_func)(void* cookie, const char* solid_name); + void (*ascii_begin_solid_func)(void* cookie, const char* solid_name); - /*! \brief Pointer on a function that handles declaration of a mesh with + /*! \brief Pointer on a function that handles declaration of a mesh with * \p tri_count number of triangles * * Optional function useful only with STL binary (ie. gmio_stlb_read()). * * The argument \p header contains the header data(80 bytes) */ - void (*binary_begin_solid_func)(void* cookie, uint32_t tri_count, const uint8_t* header); + void (*binary_begin_solid_func)(void* cookie, uint32_t tri_count, const uint8_t* header); - /*! \brief Pointer on a function that adds a triangle to the user mesh + /*! \brief Pointer on a function that adds a triangle to the user mesh * * The argument \p triangle is the triangle to be added, note that * gmio_stl_triangle_t::attribute_byte_count is meaningless for STL ascii. * * The argument \p tri_id is the index of the mesh triangle */ - void (*add_triangle_func)(void* cookie, uint32_t tri_id, const gmio_stl_triangle_t* triangle); + void (*add_triangle_func)(void* cookie, uint32_t tri_id, const gmio_stl_triangle_t* triangle); - /*! \brief Pointer on a function that finalizes creation of the user mesh + /*! \brief Pointer on a function that finalizes creation of the user mesh * * Optional function called at the end of the read process, ie. after all * triangles have been added */ - void (*end_solid_func)(void* cookie); + void (*end_solid_func)(void* cookie); }; typedef struct gmio_stl_mesh_creator gmio_stl_mesh_creator_t; diff --git a/src/gmio_stl/stl_triangle.h b/src/gmio_stl/stl_triangle.h index e8f5a8e..9f1d34b 100644 --- a/src/gmio_stl/stl_triangle.h +++ b/src/gmio_stl/stl_triangle.h @@ -25,9 +25,9 @@ */ struct gmio_stl_coords { - gmio_real32_t x; - gmio_real32_t y; - gmio_real32_t z; + gmio_real32_t x; + gmio_real32_t y; + gmio_real32_t z; }; typedef struct gmio_stl_coords gmio_stl_coords_t; @@ -37,11 +37,11 @@ typedef struct gmio_stl_coords gmio_stl_coords_t; */ struct gmio_stl_triangle { - gmio_stl_coords_t normal; - gmio_stl_coords_t v1; - gmio_stl_coords_t v2; - gmio_stl_coords_t v3; - uint16_t attribute_byte_count; /*!< Useful only for STL binary format */ + gmio_stl_coords_t normal; + gmio_stl_coords_t v1; + gmio_stl_coords_t v2; + gmio_stl_coords_t v3; + uint16_t attribute_byte_count; /*!< Useful only for STL binary format */ }; typedef struct gmio_stl_triangle gmio_stl_triangle_t; diff --git a/src/gmio_stl/stla_read.c b/src/gmio_stl/stla_read.c index 78dfdc8..27b8a44 100644 --- a/src/gmio_stl/stla_read.c +++ b/src/gmio_stl/stla_read.c @@ -78,326 +78,326 @@ /* gmio_stream_fwd_iterator_stla_cookie */ typedef struct { - gmio_task_control_t* task_control; - size_t stream_offset; - gmio_bool_t is_stop_requested; + gmio_task_control_t* task_control; + size_t stream_offset; + gmio_bool_t is_stop_requested; } _internal_gmio_fwd_iterator_cookie_t; /* gmio_stla_token */ typedef enum { - ENDFACET_token, - ENDLOOP_token, - ENDSOLID_token, - FACET_token, - ID_token, - FLOAT_token = ID_token, - LOOP_token, - NORMAL_token, - OUTER_token, - SOLID_token, - VERTEX_token, - empty_token, - unknown_token + ENDFACET_token, + ENDLOOP_token, + ENDSOLID_token, + FACET_token, + ID_token, + FLOAT_token = ID_token, + LOOP_token, + NORMAL_token, + OUTER_token, + SOLID_token, + VERTEX_token, + empty_token, + unknown_token } gmio_stla_token_t; /* gmio_stla_parse_data */ typedef struct { - gmio_stla_token_t token; - gmio_bool_t error; - gmio_string_stream_fwd_iterator_t stream_iterator; - _internal_gmio_fwd_iterator_cookie_t stream_iterator_cookie; - gmio_string_buffer_t string_buffer; - gmio_stl_mesh_creator_t* creator; + gmio_stla_token_t token; + gmio_bool_t error; + gmio_string_stream_fwd_iterator_t stream_iterator; + _internal_gmio_fwd_iterator_cookie_t stream_iterator_cookie; + gmio_string_buffer_t string_buffer; + gmio_stl_mesh_creator_t* creator; } gmio_stla_parse_data_t; static void gmio_stream_fwd_iterator_stla_read_hook(void* cookie, const gmio_string_buffer_t* buffer) { - _internal_gmio_fwd_iterator_cookie_t* tcookie = (_internal_gmio_fwd_iterator_cookie_t*)(cookie); - const gmio_task_control_t* ctrl = tcookie != NULL ? tcookie->task_control : NULL; - if (ctrl != NULL) - tcookie->is_stop_requested = gmio_task_control_is_stop_requested(ctrl); - if (tcookie != NULL) - tcookie->stream_offset += buffer->len; + _internal_gmio_fwd_iterator_cookie_t* tcookie = (_internal_gmio_fwd_iterator_cookie_t*)(cookie); + const gmio_task_control_t* ctrl = tcookie != NULL ? tcookie->task_control : NULL; + if (ctrl != NULL) + tcookie->is_stop_requested = gmio_task_control_is_stop_requested(ctrl); + if (tcookie != NULL) + tcookie->stream_offset += buffer->len; } GMIO_INLINE static gmio_bool_t parsing_can_continue(const gmio_stla_parse_data_t* data) { - if (!data->error && !data->stream_iterator_cookie.is_stop_requested) - return GMIO_TRUE; - return GMIO_FALSE; + if (!data->error && !data->stream_iterator_cookie.is_stop_requested) + return GMIO_TRUE; + return GMIO_FALSE; } GMIO_INLINE static const char* current_token_as_identifier(const gmio_stla_parse_data_t* data) { - return data->token == ID_token ? data->string_buffer.ptr : ""; + return data->token == ID_token ? data->string_buffer.ptr : ""; } GMIO_INLINE static int get_current_token_as_real32(const gmio_stla_parse_data_t* data, gmio_real32_t* value_ptr) { - if (data->token == FLOAT_token) - return gmio_get_real32(data->string_buffer.ptr, value_ptr); - return -3; + if (data->token == FLOAT_token) + return gmio_get_real32(data->string_buffer.ptr, value_ptr); + return -3; } GMIO_INLINE static void parsing_error(gmio_stla_parse_data_t* data) { - data->error = GMIO_TRUE; - data->token = unknown_token; - fprintf(stderr, - "\n[gmio_stla_read()] parsing_error, token: \"%s\"\n", - data->string_buffer.ptr); + data->error = GMIO_TRUE; + data->token = unknown_token; + fprintf(stderr, + "\n[gmio_stla_read()] parsing_error, token: \"%s\"\n", + data->string_buffer.ptr); } static gmio_stla_token_t parsing_find_token(const gmio_string_buffer_t* str_buffer) { - const char* word = str_buffer->ptr; - const size_t word_len = str_buffer->len; + const char* word = str_buffer->ptr; + const size_t word_len = str_buffer->len; - /* Get rid of ill-formed token */ - if (word_len == 0) - return empty_token; + /* Get rid of ill-formed token */ + if (word_len == 0) + return empty_token; - /* Try to find non "endXxx" token */ - if (word_len >= 4) { - switch (word[0]) { - case 'f': - case 'F': - if (strcmp(word + 1, "acet") == 0) - return FACET_token; - break; - case 'l': - case 'L': - if (strcmp(word + 1, "oop") == 0) - return LOOP_token; - break; - case 'n': - case 'N': - if (strcmp(word + 1, "ormal") == 0) - return NORMAL_token; - break; - case 'o': - case 'O': - if (strcmp(word + 1, "uter") == 0) - return OUTER_token; - break; - case 's': - case 'S': - if (strcmp(word + 1, "olid") == 0) - return SOLID_token; - break; - case 'v': - case 'V': - if (strcmp(word + 1, "ertex") == 0) - return VERTEX_token; - break; - default: - break; + /* Try to find non "endXxx" token */ + if (word_len >= 4) { + switch (word[0]) { + case 'f': + case 'F': + if (strcmp(word + 1, "acet") == 0) + return FACET_token; + break; + case 'l': + case 'L': + if (strcmp(word + 1, "oop") == 0) + return LOOP_token; + break; + case 'n': + case 'N': + if (strcmp(word + 1, "ormal") == 0) + return NORMAL_token; + break; + case 'o': + case 'O': + if (strcmp(word + 1, "uter") == 0) + return OUTER_token; + break; + case 's': + case 'S': + if (strcmp(word + 1, "olid") == 0) + return SOLID_token; + break; + case 'v': + case 'V': + if (strcmp(word + 1, "ertex") == 0) + return VERTEX_token; + break; + default: + break; + } } - } - /* Might be "end..." token */ - if (word_len >= 7 && strncmp(word, "end", 3) == 0) { - switch (word[3]) { - case 'f': - case 'F': - if (strcmp(word + 4, "acet") == 0) - return ENDFACET_token; - break; - case 'l': - case 'L': - if (strcmp(word + 4, "oop") == 0) - return ENDLOOP_token; - break; - case 's': - case 'S': - if (strcmp(word + 4, "olid") == 0) - return ENDSOLID_token; - break; - default: - break; + /* Might be "end..." token */ + if (word_len >= 7 && strncmp(word, "end", 3) == 0) { + switch (word[3]) { + case 'f': + case 'F': + if (strcmp(word + 4, "acet") == 0) + return ENDFACET_token; + break; + case 'l': + case 'L': + if (strcmp(word + 4, "oop") == 0) + return ENDLOOP_token; + break; + case 's': + case 'S': + if (strcmp(word + 4, "olid") == 0) + return ENDSOLID_token; + break; + default: + break; + } } - } - return ID_token; + return ID_token; } static void parsing_advance(gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - if (gmio_eat_word(&data->stream_iterator, &data->string_buffer) == 0) - data->token = parsing_find_token(&data->string_buffer); - else - data->token = unknown_token; + if (gmio_eat_word(&data->stream_iterator, &data->string_buffer) == 0) + data->token = parsing_find_token(&data->string_buffer); + else + data->token = unknown_token; - if (data->token == unknown_token) - parsing_error(data); + if (data->token == unknown_token) + parsing_error(data); } static void parsing_eat_token(gmio_stla_token_t token, gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - if (data->token == token) - parsing_advance(data); - else - parsing_error(data); + if (data->token == token) + parsing_advance(data); + else + parsing_error(data); } static void parse_solidname_beg(gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - switch (data->token) { - case ENDSOLID_token: - case FACET_token: - case ID_token: - break; - default: - parsing_error(data); - } + switch (data->token) { + case ENDSOLID_token: + case FACET_token: + case ID_token: + break; + default: + parsing_error(data); + } } static void parse_solidname_end(gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - switch (data->token) { - case SOLID_token: - case ID_token: - case empty_token: - break; - default: - parsing_error(data); - } + switch (data->token) { + case SOLID_token: + case ID_token: + case empty_token: + break; + default: + parsing_error(data); + } } static void parse_beginsolid(gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - switch (data->token) { - case SOLID_token: { - parsing_eat_token(SOLID_token, data); - parse_solidname_beg(data); - if (parsing_can_continue(data) - && data->creator != NULL - && data->creator->ascii_begin_solid_func != NULL) - { - data->creator->ascii_begin_solid_func(data->creator->cookie, - current_token_as_identifier(data)); + switch (data->token) { + case SOLID_token: { + parsing_eat_token(SOLID_token, data); + parse_solidname_beg(data); + if (parsing_can_continue(data) + && data->creator != NULL + && data->creator->ascii_begin_solid_func != NULL) + { + data->creator->ascii_begin_solid_func(data->creator->cookie, + current_token_as_identifier(data)); + } + if (data->token == ID_token) + parsing_eat_token(ID_token, data); + break; } - if (data->token == ID_token) - parsing_eat_token(ID_token, data); - break; - } - default: - parsing_error(data); - } /* end switch */ + default: + parsing_error(data); + } /* end switch */ } static void parse_endsolid(gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - switch (data->token) { - case ENDSOLID_token: { - parsing_eat_token(ENDSOLID_token, data); - parse_solidname_end(data); - if (parsing_can_continue(data) - && data->creator != NULL - && data->creator->end_solid_func != NULL) - { - data->creator->end_solid_func(data->creator->cookie/*, current_token_as_identifier(data)*/); + switch (data->token) { + case ENDSOLID_token: { + parsing_eat_token(ENDSOLID_token, data); + parse_solidname_end(data); + if (parsing_can_continue(data) + && data->creator != NULL + && data->creator->end_solid_func != NULL) + { + data->creator->end_solid_func(data->creator->cookie/*, current_token_as_identifier(data)*/); + } + if (data->token == ID_token) + parsing_eat_token(ID_token, data); + break; } - if (data->token == ID_token) - parsing_eat_token(ID_token, data); - break; - } - default: - parsing_error(data); - } /* end switch */ + default: + parsing_error(data); + } /* end switch */ } static void parse_xyz_coords(gmio_stla_parse_data_t* data, gmio_stl_coords_t* coords) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - switch (data->token) { - case FLOAT_token: { - if (get_current_token_as_real32(data, &coords->x) != 0) - parsing_error(data); - parsing_eat_token(FLOAT_token, data); - if (get_current_token_as_real32(data, &coords->y) != 0) - parsing_error(data); - parsing_eat_token(FLOAT_token, data); - if (get_current_token_as_real32(data, &coords->z) != 0) - parsing_error(data); - parsing_eat_token(FLOAT_token, data); - break; - } - default: - break; - } /* end switch */ + switch (data->token) { + case FLOAT_token: { + if (get_current_token_as_real32(data, &coords->x) != 0) + parsing_error(data); + parsing_eat_token(FLOAT_token, data); + if (get_current_token_as_real32(data, &coords->y) != 0) + parsing_error(data); + parsing_eat_token(FLOAT_token, data); + if (get_current_token_as_real32(data, &coords->z) != 0) + parsing_error(data); + parsing_eat_token(FLOAT_token, data); + break; + } + default: + break; + } /* end switch */ } static void parse_facet(gmio_stla_parse_data_t* data, gmio_stl_triangle_t* facet) { - parsing_eat_token(FACET_token, data); - parsing_eat_token(NORMAL_token, data); - parse_xyz_coords(data, &facet->normal); + parsing_eat_token(FACET_token, data); + parsing_eat_token(NORMAL_token, data); + parse_xyz_coords(data, &facet->normal); - parsing_eat_token(OUTER_token, data); - parsing_eat_token(LOOP_token, data); + parsing_eat_token(OUTER_token, data); + parsing_eat_token(LOOP_token, data); - parsing_eat_token(VERTEX_token, data); - parse_xyz_coords(data, &facet->v1); - parsing_eat_token(VERTEX_token, data); - parse_xyz_coords(data, &facet->v2); - parsing_eat_token(VERTEX_token, data); - parse_xyz_coords(data, &facet->v3); + parsing_eat_token(VERTEX_token, data); + parse_xyz_coords(data, &facet->v1); + parsing_eat_token(VERTEX_token, data); + parse_xyz_coords(data, &facet->v2); + parsing_eat_token(VERTEX_token, data); + parse_xyz_coords(data, &facet->v3); - parsing_eat_token(ENDLOOP_token, data); - parsing_eat_token(ENDFACET_token, data); + parsing_eat_token(ENDLOOP_token, data); + parsing_eat_token(ENDFACET_token, data); } static void parse_facets(gmio_stla_parse_data_t* data) { - uint32_t i_facet_offset = 0; - gmio_stl_triangle_t facet; + uint32_t i_facet_offset = 0; + gmio_stl_triangle_t facet; - while (data->token == FACET_token && parsing_can_continue(data)) { - parse_facet(data, &facet); - if (data->creator != NULL && data->creator->add_triangle_func != NULL) - data->creator->add_triangle_func(data->creator->cookie, i_facet_offset, &facet); - ++i_facet_offset; - } + while (data->token == FACET_token && parsing_can_continue(data)) { + parse_facet(data, &facet); + if (data->creator != NULL && data->creator->add_triangle_func != NULL) + data->creator->add_triangle_func(data->creator->cookie, i_facet_offset, &facet); + ++i_facet_offset; + } } static void parse_solid(gmio_stla_parse_data_t* data) { - if (!parsing_can_continue(data)) - return; + if (!parsing_can_continue(data)) + return; - if (data->token == SOLID_token) { - parse_beginsolid(data); - parse_facets(data); - parse_endsolid(data); - } - else { - parsing_error(data); - } + if (data->token == SOLID_token) { + parse_beginsolid(data); + parse_facets(data); + parse_endsolid(data); + } + else { + parsing_error(data); + } } enum { GMIO_STLA_READ_STRING_BUFFER_LEN = 512 }; @@ -406,41 +406,41 @@ int gmio_stla_read(gmio_stl_mesh_creator_t* creator, gmio_transfer_t* trsf, const gmio_stla_read_options_t* options) { - char fixed_buffer[GMIO_STLA_READ_STRING_BUFFER_LEN]; - gmio_stla_parse_data_t parse_data; - GMIO_UNUSED(options); + char fixed_buffer[GMIO_STLA_READ_STRING_BUFFER_LEN]; + gmio_stla_parse_data_t parse_data; + GMIO_UNUSED(options); - { /* Check validity of input parameters */ - int error = GMIO_NO_ERROR; - if (!gmio_check_transfer(&error, trsf)) - return error; - } + { /* Check validity of input parameters */ + int error = GMIO_NO_ERROR; + if (!gmio_check_transfer(&error, trsf)) + return error; + } - parse_data.token = unknown_token; - parse_data.error = GMIO_FALSE; + parse_data.token = unknown_token; + parse_data.error = GMIO_FALSE; - parse_data.stream_iterator_cookie.task_control = &trsf->task_control; - parse_data.stream_iterator_cookie.stream_offset = 0; - parse_data.stream_iterator_cookie.is_stop_requested = GMIO_FALSE; + parse_data.stream_iterator_cookie.task_control = &trsf->task_control; + parse_data.stream_iterator_cookie.stream_offset = 0; + parse_data.stream_iterator_cookie.is_stop_requested = GMIO_FALSE; - parse_data.stream_iterator.stream = &trsf->stream; - parse_data.stream_iterator.buffer.ptr = trsf->buffer; - parse_data.stream_iterator.buffer.max_len = trsf->buffer_size; - parse_data.stream_iterator.cookie = &parse_data.stream_iterator_cookie; - parse_data.stream_iterator.stream_read_hook = gmio_stream_fwd_iterator_stla_read_hook; - gmio_string_stream_fwd_iterator_init(&parse_data.stream_iterator); + parse_data.stream_iterator.stream = &trsf->stream; + parse_data.stream_iterator.buffer.ptr = trsf->buffer; + parse_data.stream_iterator.buffer.max_len = trsf->buffer_size; + parse_data.stream_iterator.cookie = &parse_data.stream_iterator_cookie; + parse_data.stream_iterator.stream_read_hook = gmio_stream_fwd_iterator_stla_read_hook; + gmio_string_stream_fwd_iterator_init(&parse_data.stream_iterator); - parse_data.string_buffer.ptr = &fixed_buffer[0]; - parse_data.string_buffer.len = 0; - parse_data.string_buffer.max_len = GMIO_STLA_READ_STRING_BUFFER_LEN; + parse_data.string_buffer.ptr = &fixed_buffer[0]; + parse_data.string_buffer.len = 0; + parse_data.string_buffer.max_len = GMIO_STLA_READ_STRING_BUFFER_LEN; - parse_data.creator = creator; + parse_data.creator = creator; - parsing_advance(&parse_data); - parse_solid(&parse_data); + parsing_advance(&parse_data); + parse_solid(&parse_data); - if (parse_data.error) - return GMIO_STLA_READ_PARSE_ERROR; - return parse_data.stream_iterator_cookie.is_stop_requested ? GMIO_TASK_STOPPED_ERROR : - GMIO_NO_ERROR; + if (parse_data.error) + return GMIO_STLA_READ_PARSE_ERROR; + return parse_data.stream_iterator_cookie.is_stop_requested ? GMIO_TASK_STOPPED_ERROR : + GMIO_NO_ERROR; } diff --git a/src/gmio_stl/stla_write.c b/src/gmio_stl/stla_write.c index 4df54bc..7648847 100644 --- a/src/gmio_stl/stla_write.c +++ b/src/gmio_stl/stla_write.c @@ -51,24 +51,24 @@ enum { GMIO_STLA_SOLID_NAME_MAX_LEN = 512 }; static char* gmio_write_string(char* buffer, const char* str) { - const char* safe_str = str != NULL ? str : ""; - strcpy(buffer, safe_str); - return buffer + strlen(safe_str); + const char* safe_str = str != NULL ? str : ""; + strcpy(buffer, safe_str); + return buffer + strlen(safe_str); } static char* gmio_write_string_eol(char* buffer, const char* str) { - const char* safe_str = str != NULL ? str : ""; - const size_t len = strlen(safe_str); - strncpy(buffer, safe_str, len); - buffer[len] = '\n'; - return buffer + len + 1; + const char* safe_str = str != NULL ? str : ""; + const size_t len = strlen(safe_str); + strncpy(buffer, safe_str, len); + buffer[len] = '\n'; + return buffer + len + 1; } static char* gmio_write_eol(char* buffer) { - *buffer = '\n'; - return buffer + 1; + *buffer = '\n'; + return buffer + 1; } /*static char* gmio_write_space(char* buffer) @@ -79,21 +79,21 @@ static char* gmio_write_eol(char* buffer) static char* gmio_write_nspaces(char* buffer, int n) { - const int offset = n; - while (n > 0) - *(buffer + (--n)) = ' '; - return buffer + offset; + const int offset = n; + while (n > 0) + *(buffer + (--n)) = ' '; + return buffer + offset; } static char* gmio_write_stdio_format(char* buffer, uint8_t prec) { - int prec_len = 0; + int prec_len = 0; - buffer[0] = '%'; - buffer[1] = '.'; - prec_len = sprintf(buffer + 2, "%i", prec); - buffer[2 + prec_len] = 'E'; - return buffer + 3 + prec_len; + buffer[0] = '%'; + buffer[1] = '.'; + prec_len = sprintf(buffer + 2, "%i", prec); + buffer[2 + prec_len] = 'E'; + return buffer + 3 + prec_len; } /*static char* gmio_write_real32_string(char* buffer, const char* format, gmio_real32_t val) @@ -105,106 +105,106 @@ static char* gmio_write_coords(char* buffer, const char* coords_format, const gmio_stl_coords_t* coords) { - return buffer + sprintf(buffer, coords_format, coords->x, coords->y, coords->z); + return buffer + sprintf(buffer, coords_format, coords->x, coords->y, coords->z); } static gmio_bool_t gmio_transfer_flush_buffer(gmio_transfer_t* trsf, size_t n) { - return gmio_stream_write(&trsf->stream, trsf->buffer, sizeof(char), n) == n; + return gmio_stream_write(&trsf->stream, trsf->buffer, sizeof(char), n) == n; } int gmio_stla_write(const gmio_stl_mesh_t* mesh, gmio_transfer_t* trsf, const gmio_stla_write_options_t* options) { - const char* solid_name = options != NULL ? options->solid_name : NULL; - const uint8_t real32_prec = options != NULL ? options->real32_prec : 9; - const uint32_t total_facet_count = mesh != NULL ? mesh->triangle_count : 0; - const uint32_t buffer_facet_count = - trsf != NULL ? - gmio_size_to_uint32(trsf->buffer_size / GMIO_STLA_FACET_SIZE_P2) : 0; - uint32_t ifacet = 0; - char* buffer_iterator = trsf != NULL ? trsf->buffer : NULL; - char coords_format[64]; - int error = GMIO_NO_ERROR; + const char* solid_name = options != NULL ? options->solid_name : NULL; + const uint8_t real32_prec = options != NULL ? options->real32_prec : 9; + const uint32_t total_facet_count = mesh != NULL ? mesh->triangle_count : 0; + const uint32_t buffer_facet_count = + trsf != NULL ? + gmio_size_to_uint32(trsf->buffer_size / GMIO_STLA_FACET_SIZE_P2) : 0; + uint32_t ifacet = 0; + char* buffer_iterator = trsf != NULL ? trsf->buffer : NULL; + char coords_format[64]; + int error = GMIO_NO_ERROR; + + /* Check validity of input parameters */ + gmio_check_transfer(&error, trsf); + gmio_stl_check_mesh(&error, mesh); + if (real32_prec == 0 || real32_prec > 9) + error = GMIO_STLA_WRITE_INVALID_REAL32_PREC_ERROR; + if (trsf->buffer_size < GMIO_STLA_FACET_SIZE_P2) + error = GMIO_INVALID_BUFFER_SIZE_ERROR; + if (gmio_error(error)) + return error; + + { /* Create XYZ coords format string (for normal and vertex coords) */ + char* coords_format_iterator = coords_format; + coords_format_iterator = gmio_write_stdio_format(coords_format_iterator, real32_prec); + coords_format_iterator = gmio_write_nspaces(coords_format_iterator, 2); + coords_format_iterator = gmio_write_stdio_format(coords_format_iterator, real32_prec); + coords_format_iterator = gmio_write_nspaces(coords_format_iterator, 2); + coords_format_iterator = gmio_write_stdio_format(coords_format_iterator, real32_prec); + *coords_format_iterator = 0; /* Write terminating null byte */ + /* TODO: check the "format" string can contain the given precision */ + } + + /* Write solid declaration */ + { + buffer_iterator = gmio_write_string(buffer_iterator, "solid "); + buffer_iterator = gmio_write_string_eol(buffer_iterator, solid_name); + if (!gmio_transfer_flush_buffer(trsf, buffer_iterator - (char*)trsf->buffer)) + return GMIO_STREAM_ERROR; + } + + /* Write solid's facets */ + for (ifacet = 0; + ifacet < total_facet_count && gmio_no_error(error); + ifacet += buffer_facet_count) + { + const uint32_t clamped_facet_count = _GMIO_INTERNAL_MIN(ifacet + buffer_facet_count, + total_facet_count); + gmio_stl_triangle_t tri; + uint32_t ibuffer_facet; + + /* Writing of facets is buffered */ + buffer_iterator = trsf->buffer; + for (ibuffer_facet = ifacet; ibuffer_facet < clamped_facet_count; ++ibuffer_facet) { + mesh->get_triangle_func(mesh->cookie, ibuffer_facet, &tri); + buffer_iterator = gmio_write_string(buffer_iterator, "facet normal "); + buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.normal); + buffer_iterator = gmio_write_eol(buffer_iterator); + + buffer_iterator = gmio_write_string_eol(buffer_iterator, " outer loop"); + buffer_iterator = gmio_write_string(buffer_iterator, " vertex "); + buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.v1); + buffer_iterator = gmio_write_eol(buffer_iterator); + buffer_iterator = gmio_write_string(buffer_iterator, " vertex "); + buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.v2); + buffer_iterator = gmio_write_eol(buffer_iterator); + buffer_iterator = gmio_write_string(buffer_iterator, " vertex "); + buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.v3); + buffer_iterator = gmio_write_eol(buffer_iterator); + buffer_iterator = gmio_write_string_eol(buffer_iterator, " endloop"); + + buffer_iterator = gmio_write_string_eol(buffer_iterator, "endfacet"); + } /* end for (ibuffer_facet) */ + + if (!gmio_transfer_flush_buffer(trsf, buffer_iterator - (char*)trsf->buffer)) + error = GMIO_STREAM_ERROR; + + /* Task control */ + if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control)) + error = GMIO_TASK_STOPPED_ERROR; + } /* end for (ifacet) */ + + /* Write end of solid */ + if (gmio_no_error(error)) { + buffer_iterator = gmio_write_string(trsf->buffer, "endsolid "); + buffer_iterator = gmio_write_string_eol(buffer_iterator, solid_name); + if (!gmio_transfer_flush_buffer(trsf, buffer_iterator - (char*)trsf->buffer)) + error = GMIO_STREAM_ERROR; + } - /* Check validity of input parameters */ - gmio_check_transfer(&error, trsf); - gmio_stl_check_mesh(&error, mesh); - if (real32_prec == 0 || real32_prec > 9) - error = GMIO_STLA_WRITE_INVALID_REAL32_PREC_ERROR; - if (trsf->buffer_size < GMIO_STLA_FACET_SIZE_P2) - error = GMIO_INVALID_BUFFER_SIZE_ERROR; - if (gmio_error(error)) return error; - - { /* Create XYZ coords format string (for normal and vertex coords) */ - char* coords_format_iterator = coords_format; - coords_format_iterator = gmio_write_stdio_format(coords_format_iterator, real32_prec); - coords_format_iterator = gmio_write_nspaces(coords_format_iterator, 2); - coords_format_iterator = gmio_write_stdio_format(coords_format_iterator, real32_prec); - coords_format_iterator = gmio_write_nspaces(coords_format_iterator, 2); - coords_format_iterator = gmio_write_stdio_format(coords_format_iterator, real32_prec); - *coords_format_iterator = 0; /* Write terminating null byte */ - /* TODO: check the "format" string can contain the given precision */ - } - - /* Write solid declaration */ - { - buffer_iterator = gmio_write_string(buffer_iterator, "solid "); - buffer_iterator = gmio_write_string_eol(buffer_iterator, solid_name); - if (!gmio_transfer_flush_buffer(trsf, buffer_iterator - (char*)trsf->buffer)) - return GMIO_STREAM_ERROR; - } - - /* Write solid's facets */ - for (ifacet = 0; - ifacet < total_facet_count && gmio_no_error(error); - ifacet += buffer_facet_count) - { - const uint32_t clamped_facet_count = _GMIO_INTERNAL_MIN(ifacet + buffer_facet_count, - total_facet_count); - gmio_stl_triangle_t tri; - uint32_t ibuffer_facet; - - /* Writing of facets is buffered */ - buffer_iterator = trsf->buffer; - for (ibuffer_facet = ifacet; ibuffer_facet < clamped_facet_count; ++ibuffer_facet) { - mesh->get_triangle_func(mesh->cookie, ibuffer_facet, &tri); - buffer_iterator = gmio_write_string(buffer_iterator, "facet normal "); - buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.normal); - buffer_iterator = gmio_write_eol(buffer_iterator); - - buffer_iterator = gmio_write_string_eol(buffer_iterator, " outer loop"); - buffer_iterator = gmio_write_string(buffer_iterator, " vertex "); - buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.v1); - buffer_iterator = gmio_write_eol(buffer_iterator); - buffer_iterator = gmio_write_string(buffer_iterator, " vertex "); - buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.v2); - buffer_iterator = gmio_write_eol(buffer_iterator); - buffer_iterator = gmio_write_string(buffer_iterator, " vertex "); - buffer_iterator = gmio_write_coords(buffer_iterator, coords_format, &tri.v3); - buffer_iterator = gmio_write_eol(buffer_iterator); - buffer_iterator = gmio_write_string_eol(buffer_iterator, " endloop"); - - buffer_iterator = gmio_write_string_eol(buffer_iterator, "endfacet"); - } /* end for (ibuffer_facet) */ - - if (!gmio_transfer_flush_buffer(trsf, buffer_iterator - (char*)trsf->buffer)) - error = GMIO_STREAM_ERROR; - - /* Task control */ - if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control)) - error = GMIO_TASK_STOPPED_ERROR; - } /* end for (ifacet) */ - - /* Write end of solid */ - if (gmio_no_error(error)) { - buffer_iterator = gmio_write_string(trsf->buffer, "endsolid "); - buffer_iterator = gmio_write_string_eol(buffer_iterator, solid_name); - if (!gmio_transfer_flush_buffer(trsf, buffer_iterator - (char*)trsf->buffer)) - error = GMIO_STREAM_ERROR; - } - - return error; } diff --git a/src/gmio_stl/stlb_read.c b/src/gmio_stl/stlb_read.c index c8283c8..110c5bc 100644 --- a/src/gmio_stl/stlb_read.c +++ b/src/gmio_stl/stlb_read.c @@ -32,111 +32,111 @@ GMIO_INLINE static void read_triangle_memcpy(const uint8_t* buffer, gmio_stl_triangle_t* triangle) { - /* *triangle = *((gmio_stl_triangle_t*)(buffer)); */ - memcpy(triangle, buffer, GMIO_STLB_TRIANGLE_RAWSIZE); + /* *triangle = *((gmio_stl_triangle_t*)(buffer)); */ + memcpy(triangle, buffer, GMIO_STLB_TRIANGLE_RAWSIZE); } static void gmio_stlb_read_facets(gmio_stl_mesh_creator_t* creator, const uint8_t* buffer, const gmio_stlb_readwrite_helper_t* rparams) { - const uint32_t facet_count = rparams->facet_count; - const uint32_t i_facet_offset = rparams->i_facet_offset; - gmio_stl_triangle_t triangle; - uint32_t buffer_offset = 0; - uint32_t i_facet = 0; + const uint32_t facet_count = rparams->facet_count; + const uint32_t i_facet_offset = rparams->i_facet_offset; + gmio_stl_triangle_t triangle; + uint32_t buffer_offset = 0; + uint32_t i_facet = 0; - if (creator == NULL || creator->add_triangle_func == NULL) - return; + if (creator == NULL || creator->add_triangle_func == NULL) + return; - for (i_facet = 0; i_facet < facet_count; ++i_facet) { - /* Decode data */ - read_triangle_memcpy(buffer + buffer_offset, &triangle); - buffer_offset += GMIO_STLB_TRIANGLE_RAWSIZE; + for (i_facet = 0; i_facet < facet_count; ++i_facet) { + /* Decode data */ + read_triangle_memcpy(buffer + buffer_offset, &triangle); + buffer_offset += GMIO_STLB_TRIANGLE_RAWSIZE; - if (rparams->fix_endian_func != NULL) - rparams->fix_endian_func(&triangle); + if (rparams->fix_endian_func != NULL) + rparams->fix_endian_func(&triangle); - /* Declare triangle */ - creator->add_triangle_func(creator->cookie, i_facet_offset + i_facet, &triangle); - } + /* Declare triangle */ + creator->add_triangle_func(creator->cookie, i_facet_offset + i_facet, &triangle); + } } int gmio_stlb_read(gmio_stl_mesh_creator_t *creator, gmio_transfer_t* trsf, const gmio_stlb_read_options_t* options) { - const gmio_endianness_t host_byte_order = gmio_host_endianness(); - const gmio_endianness_t byte_order = - options != NULL ? options->byte_order : host_byte_order; - const uint32_t max_facet_count_per_read = - gmio_size_to_uint32(trsf->buffer_size / GMIO_STLB_TRIANGLE_RAWSIZE); - gmio_stlb_readwrite_helper_t rparams = {0}; - uint8_t header_data[GMIO_STLB_HEADER_SIZE]; - uint32_t total_facet_count = 0; /* Count of facets as declared in the stream */ - int error = GMIO_NO_ERROR; /* Helper variable to store function result error code */ + const gmio_endianness_t host_byte_order = gmio_host_endianness(); + const gmio_endianness_t byte_order = + options != NULL ? options->byte_order : host_byte_order; + const uint32_t max_facet_count_per_read = + gmio_size_to_uint32(trsf->buffer_size / GMIO_STLB_TRIANGLE_RAWSIZE); + gmio_stlb_readwrite_helper_t rparams = {0}; + uint8_t header_data[GMIO_STLB_HEADER_SIZE]; + uint32_t total_facet_count = 0; /* Count of facets as declared in the stream */ + int error = GMIO_NO_ERROR; /* Helper variable to store function result error code */ - /* Check validity of input parameters */ - if (!gmio_stlb_check_params(&error, trsf, byte_order)) - return error; + /* Check validity of input parameters */ + if (!gmio_stlb_check_params(&error, trsf, byte_order)) + return error; - /* Initialize rparams */ - if (host_byte_order != byte_order) - rparams.fix_endian_func = gmio_stl_triangle_bswap; + /* Initialize rparams */ + if (host_byte_order != byte_order) + rparams.fix_endian_func = gmio_stl_triangle_bswap; - /* Read header */ - if (gmio_stream_read(&trsf->stream, header_data, 1, GMIO_STLB_HEADER_SIZE) - != GMIO_STLB_HEADER_SIZE) - { - return GMIO_STLB_READ_HEADER_WRONG_SIZE_ERROR; - } - - /* Read facet count */ - if (gmio_stream_read(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1) - return GMIO_STLB_READ_FACET_COUNT_ERROR; - - memcpy(&total_facet_count, trsf->buffer, sizeof(uint32_t)); - if (host_byte_order != byte_order) - total_facet_count = gmio_uint32_bswap(total_facet_count); - - /* Callback to notify triangle count and header data */ - if (creator != NULL && creator->binary_begin_solid_func != NULL) - creator->binary_begin_solid_func(creator->cookie, total_facet_count, header_data); - - /* Read triangles */ - while (gmio_no_error(error) - && rparams.i_facet_offset < total_facet_count) - { - rparams.facet_count = - gmio_size_to_uint32( - gmio_stream_read( - &trsf->stream, - trsf->buffer, - GMIO_STLB_TRIANGLE_RAWSIZE, - max_facet_count_per_read)); - if (gmio_stream_error(&trsf->stream) != 0) - error = GMIO_STREAM_ERROR; - else if (rparams.facet_count > 0) - error = GMIO_NO_ERROR; - else - break; /* Exit if no facet to read */ - - if (gmio_no_error(error)) { - gmio_stlb_read_facets(creator, trsf->buffer, &rparams); - rparams.i_facet_offset += rparams.facet_count; - if (gmio_task_control_is_stop_requested(&trsf->task_control)) - error = GMIO_TASK_STOPPED_ERROR; + /* Read header */ + if (gmio_stream_read(&trsf->stream, header_data, 1, GMIO_STLB_HEADER_SIZE) + != GMIO_STLB_HEADER_SIZE) + { + return GMIO_STLB_READ_HEADER_WRONG_SIZE_ERROR; } - } /* end while */ - if (gmio_no_error(error) - && creator != NULL - && creator->end_solid_func != NULL) - { - creator->end_solid_func(creator->cookie); - } + /* Read facet count */ + if (gmio_stream_read(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1) + return GMIO_STLB_READ_FACET_COUNT_ERROR; - if (gmio_no_error(error) && rparams.i_facet_offset != total_facet_count) - error = GMIO_STLB_READ_FACET_COUNT_ERROR; - return error; + memcpy(&total_facet_count, trsf->buffer, sizeof(uint32_t)); + if (host_byte_order != byte_order) + total_facet_count = gmio_uint32_bswap(total_facet_count); + + /* Callback to notify triangle count and header data */ + if (creator != NULL && creator->binary_begin_solid_func != NULL) + creator->binary_begin_solid_func(creator->cookie, total_facet_count, header_data); + + /* Read triangles */ + while (gmio_no_error(error) + && rparams.i_facet_offset < total_facet_count) + { + rparams.facet_count = + gmio_size_to_uint32( + gmio_stream_read( + &trsf->stream, + trsf->buffer, + GMIO_STLB_TRIANGLE_RAWSIZE, + max_facet_count_per_read)); + if (gmio_stream_error(&trsf->stream) != 0) + error = GMIO_STREAM_ERROR; + else if (rparams.facet_count > 0) + error = GMIO_NO_ERROR; + else + break; /* Exit if no facet to read */ + + if (gmio_no_error(error)) { + gmio_stlb_read_facets(creator, trsf->buffer, &rparams); + rparams.i_facet_offset += rparams.facet_count; + if (gmio_task_control_is_stop_requested(&trsf->task_control)) + error = GMIO_TASK_STOPPED_ERROR; + } + } /* end while */ + + if (gmio_no_error(error) + && creator != NULL + && creator->end_solid_func != NULL) + { + creator->end_solid_func(creator->cookie); + } + + if (gmio_no_error(error) && rparams.i_facet_offset != total_facet_count) + error = GMIO_STLB_READ_FACET_COUNT_ERROR; + return error; } diff --git a/src/gmio_stl/stlb_write.c b/src/gmio_stl/stlb_write.c index fb1f0c9..a694a0b 100644 --- a/src/gmio_stl/stlb_write.c +++ b/src/gmio_stl/stlb_write.c @@ -32,98 +32,98 @@ GMIO_INLINE static void write_triangle_memcpy(const gmio_stl_triangle_t* triangle, uint8_t* buffer) { - memcpy(buffer, triangle, GMIO_STLB_TRIANGLE_RAWSIZE); + memcpy(buffer, triangle, GMIO_STLB_TRIANGLE_RAWSIZE); } static void gmio_stlb_write_facets(const gmio_stl_mesh_t* mesh, uint8_t* buffer, const gmio_stlb_readwrite_helper_t* wparams) { - const uint32_t facet_count = wparams->facet_count; - const uint32_t i_facet_offset = wparams->i_facet_offset; - gmio_stl_triangle_t triangle; - uint32_t buffer_offset = 0; - uint32_t i_facet = 0; + const uint32_t facet_count = wparams->facet_count; + const uint32_t i_facet_offset = wparams->i_facet_offset; + gmio_stl_triangle_t triangle; + uint32_t buffer_offset = 0; + uint32_t i_facet = 0; - if (mesh == NULL || mesh->get_triangle_func == NULL) - return; + if (mesh == NULL || mesh->get_triangle_func == NULL) + return; - triangle.attribute_byte_count = 0; - for (i_facet = i_facet_offset; i_facet < (i_facet_offset + facet_count); ++i_facet) { - mesh->get_triangle_func(mesh->cookie, i_facet, &triangle); + triangle.attribute_byte_count = 0; + for (i_facet = i_facet_offset; i_facet < (i_facet_offset + facet_count); ++i_facet) { + mesh->get_triangle_func(mesh->cookie, i_facet, &triangle); - if (wparams->fix_endian_func != NULL) - wparams->fix_endian_func(&triangle); + if (wparams->fix_endian_func != NULL) + wparams->fix_endian_func(&triangle); - write_triangle_memcpy(&triangle, buffer + buffer_offset); + write_triangle_memcpy(&triangle, buffer + buffer_offset); - buffer_offset += GMIO_STLB_TRIANGLE_RAWSIZE; - } /* end for */ + buffer_offset += GMIO_STLB_TRIANGLE_RAWSIZE; + } /* end for */ } int gmio_stlb_write(const gmio_stl_mesh_t* mesh, gmio_transfer_t* trsf, const gmio_stlb_write_options_t* options) { - const gmio_endianness_t host_byte_order = gmio_host_endianness(); - const gmio_endianness_t byte_order = options != NULL ? options->byte_order : host_byte_order; - const uint8_t* header_data = options != NULL ? options->header_data : NULL; - gmio_stlb_readwrite_helper_t wparams = {0}; - const uint32_t facet_count = mesh != NULL ? mesh->triangle_count : 0; - uint32_t i_facet = 0; - int error = GMIO_NO_ERROR; + const gmio_endianness_t host_byte_order = gmio_host_endianness(); + const gmio_endianness_t byte_order = options != NULL ? options->byte_order : host_byte_order; + const uint8_t* header_data = options != NULL ? options->header_data : NULL; + gmio_stlb_readwrite_helper_t wparams = {0}; + const uint32_t facet_count = mesh != NULL ? mesh->triangle_count : 0; + uint32_t i_facet = 0; + int error = GMIO_NO_ERROR; - /* Check validity of input parameters */ - gmio_stl_check_mesh(&error, mesh); - gmio_stlb_check_params(&error, trsf, byte_order); - if (gmio_error(error)) - return error; + /* Check validity of input parameters */ + gmio_stl_check_mesh(&error, mesh); + gmio_stlb_check_params(&error, trsf, byte_order); + if (gmio_error(error)) + return error; - /* Initialize wparams */ - if (host_byte_order != byte_order) - wparams.fix_endian_func = gmio_stl_triangle_bswap; - wparams.facet_count = gmio_size_to_uint32(trsf->buffer_size / GMIO_STLB_TRIANGLE_RAWSIZE); + /* Initialize wparams */ + if (host_byte_order != byte_order) + wparams.fix_endian_func = gmio_stl_triangle_bswap; + wparams.facet_count = gmio_size_to_uint32(trsf->buffer_size / GMIO_STLB_TRIANGLE_RAWSIZE); - /* Write header */ - if (header_data == NULL) { - /* Use buffer to store an empty header (filled with zeroes) */ - memset(trsf->buffer, 0, GMIO_STLB_HEADER_SIZE); - header_data = (const uint8_t*)trsf->buffer; - } - if (gmio_stream_write(&trsf->stream, header_data, GMIO_STLB_HEADER_SIZE, 1) != 1) - return GMIO_STREAM_ERROR; - - /* Write facet count */ - if (byte_order == GMIO_LITTLE_ENDIAN) - gmio_encode_uint32_le(facet_count, trsf->buffer); - else - gmio_encode_uint32_be(facet_count, trsf->buffer); - if (gmio_stream_write(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1) - return GMIO_STREAM_ERROR; - - /* Write triangles */ - for (i_facet = 0; - i_facet < facet_count && gmio_no_error(error); - i_facet += wparams.facet_count) - { - /* Write to buffer */ - wparams.facet_count = _GMIO_INTERNAL_MIN(wparams.facet_count, - facet_count - wparams.i_facet_offset); - - gmio_stlb_write_facets(mesh, trsf->buffer, &wparams); - wparams.i_facet_offset += wparams.facet_count; - - /* Write buffer to stream */ - if (gmio_stream_write(&trsf->stream, trsf->buffer, GMIO_STLB_TRIANGLE_RAWSIZE, wparams.facet_count) - != wparams.facet_count) - { - error = GMIO_STREAM_ERROR; + /* Write header */ + if (header_data == NULL) { + /* Use buffer to store an empty header (filled with zeroes) */ + memset(trsf->buffer, 0, GMIO_STLB_HEADER_SIZE); + header_data = (const uint8_t*)trsf->buffer; } + if (gmio_stream_write(&trsf->stream, header_data, GMIO_STLB_HEADER_SIZE, 1) != 1) + return GMIO_STREAM_ERROR; - /* Task control */ - if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control)) - error = GMIO_TASK_STOPPED_ERROR; - } /* end for */ + /* Write facet count */ + if (byte_order == GMIO_LITTLE_ENDIAN) + gmio_encode_uint32_le(facet_count, trsf->buffer); + else + gmio_encode_uint32_be(facet_count, trsf->buffer); + if (gmio_stream_write(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1) + return GMIO_STREAM_ERROR; - return error; + /* Write triangles */ + for (i_facet = 0; + i_facet < facet_count && gmio_no_error(error); + i_facet += wparams.facet_count) + { + /* Write to buffer */ + wparams.facet_count = _GMIO_INTERNAL_MIN(wparams.facet_count, + facet_count - wparams.i_facet_offset); + + gmio_stlb_write_facets(mesh, trsf->buffer, &wparams); + wparams.i_facet_offset += wparams.facet_count; + + /* Write buffer to stream */ + if (gmio_stream_write(&trsf->stream, trsf->buffer, GMIO_STLB_TRIANGLE_RAWSIZE, wparams.facet_count) + != wparams.facet_count) + { + error = GMIO_STREAM_ERROR; + } + + /* Task control */ + if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control)) + error = GMIO_TASK_STOPPED_ERROR; + } /* end for */ + + return error; } diff --git a/tests/stream_buffer.c b/tests/stream_buffer.c index 8cfd7c1..706fc3a 100644 --- a/tests/stream_buffer.c +++ b/tests/stream_buffer.c @@ -21,14 +21,14 @@ static gmio_bool_t gmio_stream_buffer_at_end(void* cookie) { - const gmio_buffer_t* buff = (const gmio_buffer_t*)cookie; - return buff->pos >= buff->len; + const gmio_buffer_t* buff = (const gmio_buffer_t*)cookie; + return buff->pos >= buff->len; } static int gmio_stream_buffer_error(void* cookie) { - const gmio_buffer_t* buff = (const gmio_buffer_t*)cookie; - return buff == NULL || buff->pos > buff->len; + const gmio_buffer_t* buff = (const gmio_buffer_t*)cookie; + return buff == NULL || buff->pos > buff->len; } static size_t gmio_stream_buffer_read(void* cookie, @@ -36,21 +36,21 @@ static size_t gmio_stream_buffer_read(void* cookie, size_t item_size, size_t item_count) { - if (item_size > 0 && item_count > 0) { - gmio_buffer_t* buff = (gmio_buffer_t*)cookie; - const size_t buff_remaining_size = buff->len - buff->pos; - const size_t wanted_read_size = item_size * item_count; - const size_t next_read_size = wanted_read_size <= buff_remaining_size ? wanted_read_size : - buff_remaining_size; - const size_t next_read_item_count = next_read_size / item_size; + if (item_size > 0 && item_count > 0) { + gmio_buffer_t* buff = (gmio_buffer_t*)cookie; + const size_t buff_remaining_size = buff->len - buff->pos; + const size_t wanted_read_size = item_size * item_count; + const size_t next_read_size = wanted_read_size <= buff_remaining_size ? wanted_read_size : + buff_remaining_size; + const size_t next_read_item_count = next_read_size / item_size; - memcpy(ptr, (const char*)buff->ptr + buff->pos, next_read_item_count * item_size); - buff->pos += next_read_item_count * item_size; - return next_read_item_count; - } - else { - return 0; - } + memcpy(ptr, (const char*)buff->ptr + buff->pos, next_read_item_count * item_size); + buff->pos += next_read_item_count * item_size; + return next_read_item_count; + } + else { + return 0; + } } static size_t gmio_stream_buffer_write(void* cookie, @@ -58,28 +58,28 @@ static size_t gmio_stream_buffer_write(void* cookie, size_t item_size, size_t item_count) { - if (item_size > 0 && item_count > 0) { - gmio_buffer_t* buff = (gmio_buffer_t*)cookie; - const size_t buff_remaining_size = buff->len - buff->pos; - const size_t wanted_write_size = item_size * item_count; - const size_t next_write_size = wanted_write_size <= buff_remaining_size ? wanted_write_size : - buff_remaining_size; - const size_t next_write_item_count = next_write_size / item_size; + if (item_size > 0 && item_count > 0) { + gmio_buffer_t* buff = (gmio_buffer_t*)cookie; + const size_t buff_remaining_size = buff->len - buff->pos; + const size_t wanted_write_size = item_size * item_count; + const size_t next_write_size = wanted_write_size <= buff_remaining_size ? wanted_write_size : + buff_remaining_size; + const size_t next_write_item_count = next_write_size / item_size; - memcpy((char*)buff->ptr + buff->pos, ptr, next_write_item_count * item_size); - buff->pos += next_write_item_count * item_size; - return next_write_item_count; - } - else { - return 0; - } + memcpy((char*)buff->ptr + buff->pos, ptr, next_write_item_count * item_size); + buff->pos += next_write_item_count * item_size; + return next_write_item_count; + } + else { + return 0; + } } void gmio_stream_set_buffer(gmio_stream_t *stream, gmio_buffer_t *buff) { - stream->cookie = buff; - stream->at_end_func = gmio_stream_buffer_at_end; - stream->error_func = gmio_stream_buffer_error; - stream->read_func = gmio_stream_buffer_read; - stream->write_func = gmio_stream_buffer_write; + stream->cookie = buff; + stream->at_end_func = gmio_stream_buffer_at_end; + stream->error_func = gmio_stream_buffer_error; + stream->read_func = gmio_stream_buffer_read; + stream->write_func = gmio_stream_buffer_write; } diff --git a/tests/stream_buffer.h b/tests/stream_buffer.h index 4cbb0b5..e42f60e 100644 --- a/tests/stream_buffer.h +++ b/tests/stream_buffer.h @@ -22,9 +22,9 @@ typedef struct { - void* ptr; - size_t len; - size_t pos; + void* ptr; + size_t len; + size_t pos; } gmio_buffer_t; void gmio_stream_set_buffer(gmio_stream_t* stream, gmio_buffer_t* buff); diff --git a/tests/test_internal.c b/tests/test_internal.c index ca8aab4..8fcbc13 100644 --- a/tests/test_internal.c +++ b/tests/test_internal.c @@ -28,107 +28,107 @@ const char* test_internal__byte_swap() { - UTEST_ASSERT(gmio_uint16_bswap(0x1122) == 0x2211); - UTEST_ASSERT(gmio_uint32_bswap(0x11223344) == 0x44332211); - return NULL; + UTEST_ASSERT(gmio_uint16_bswap(0x1122) == 0x2211); + UTEST_ASSERT(gmio_uint32_bswap(0x11223344) == 0x44332211); + return NULL; } const char* test_internal__byte_codec() { - { /* decode */ - const uint8_t data[] = { 0x11, 0x22, 0x33, 0x44 }; - UTEST_ASSERT(gmio_decode_uint16_le(data) == 0x2211); - UTEST_ASSERT(gmio_decode_uint16_be(data) == 0x1122); - UTEST_ASSERT(gmio_decode_uint32_le(data) == 0x44332211); - UTEST_ASSERT(gmio_decode_uint32_be(data) == 0x11223344); - } + { /* decode */ + const uint8_t data[] = { 0x11, 0x22, 0x33, 0x44 }; + UTEST_ASSERT(gmio_decode_uint16_le(data) == 0x2211); + UTEST_ASSERT(gmio_decode_uint16_be(data) == 0x1122); + UTEST_ASSERT(gmio_decode_uint32_le(data) == 0x44332211); + UTEST_ASSERT(gmio_decode_uint32_be(data) == 0x11223344); + } - { /* encode */ - uint8_t data[] = { 0, 0, 0, 0 }; - gmio_encode_uint16_le(0x1122, data); - UTEST_ASSERT(data[0] == 0x22 && data[1] == 0x11); - gmio_encode_uint32_le(0x11223344, data); - UTEST_ASSERT(data[0] == 0x44 && data[1] == 0x33 && data[2] == 0x22 && data[3] == 0x11); - gmio_encode_uint32_be(0x11223344, data); - UTEST_ASSERT(data[3] == 0x44 && data[2] == 0x33 && data[1] == 0x22 && data[0] == 0x11); - } + { /* encode */ + uint8_t data[] = { 0, 0, 0, 0 }; + gmio_encode_uint16_le(0x1122, data); + UTEST_ASSERT(data[0] == 0x22 && data[1] == 0x11); + gmio_encode_uint32_le(0x11223344, data); + UTEST_ASSERT(data[0] == 0x44 && data[1] == 0x33 && data[2] == 0x22 && data[3] == 0x11); + gmio_encode_uint32_be(0x11223344, data); + UTEST_ASSERT(data[3] == 0x44 && data[2] == 0x33 && data[1] == 0x22 && data[0] == 0x11); + } - return NULL; + return NULL; } const char* test_internal__string_parse() { - char text[] = - "Une citation,\to je crois qu'elle est de moi :" - "Parfois le chemin est rude.\n" - "pi : 3.1415926535897932384626433832795"; + char text[] = + "Une citation,\to je crois qu'elle est de moi :" + "Parfois le chemin est rude.\n" + "pi : 3.1415926535897932384626433832795"; - { - gmio_buffer_t buff = {0}; - gmio_stream_t stream = {0}; + { + gmio_buffer_t buff = {0}; + gmio_stream_t stream = {0}; - char small_fwd_it_str[4]; - char fwd_it_str[32]; - gmio_string_stream_fwd_iterator_t fwd_it = {0}; + char small_fwd_it_str[4]; + char fwd_it_str[32]; + gmio_string_stream_fwd_iterator_t fwd_it = {0}; - char copy_str[128]; - gmio_string_buffer_t copy_strbuff; + char copy_str[128]; + gmio_string_buffer_t copy_strbuff; - buff.ptr = text; - buff.len = strlen(text); - buff.pos = 0; - gmio_stream_set_buffer(&stream, &buff); + buff.ptr = text; + buff.len = strlen(text); + buff.pos = 0; + gmio_stream_set_buffer(&stream, &buff); - fwd_it.stream = &stream; - fwd_it.buffer.ptr = fwd_it_str; - fwd_it.buffer.max_len = sizeof(fwd_it_str); - gmio_string_stream_fwd_iterator_init(&fwd_it); + fwd_it.stream = &stream; + fwd_it.buffer.ptr = fwd_it_str; + fwd_it.buffer.max_len = sizeof(fwd_it_str); + gmio_string_stream_fwd_iterator_init(&fwd_it); - copy_strbuff.ptr = copy_str; - copy_strbuff.max_len = sizeof(copy_str); + copy_strbuff.ptr = copy_str; + copy_strbuff.max_len = sizeof(copy_str); - UTEST_ASSERT(gmio_current_char(&fwd_it) != NULL); - UTEST_ASSERT(*gmio_current_char(&fwd_it) == 'U'); + UTEST_ASSERT(gmio_current_char(&fwd_it) != NULL); + UTEST_ASSERT(*gmio_current_char(&fwd_it) == 'U'); - UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); - /* printf("\ncopy_strbuff.ptr = \"%s\"\n", copy_strbuff.ptr); */ - UTEST_ASSERT(strcmp(copy_strbuff.ptr, "Une") == 0); + UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); + /* printf("\ncopy_strbuff.ptr = \"%s\"\n", copy_strbuff.ptr); */ + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "Une") == 0); - UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); - UTEST_ASSERT(strcmp(copy_strbuff.ptr, "citation,") == 0); + UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "citation,") == 0); - UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); - UTEST_ASSERT(strcmp(copy_strbuff.ptr, "o") == 0); + UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "o") == 0); - UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); - UTEST_ASSERT(strcmp(copy_strbuff.ptr, "je") == 0); + UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "je") == 0); - gmio_skip_spaces(&fwd_it); - UTEST_ASSERT(gmio_next_char(&fwd_it) != NULL); - UTEST_ASSERT(*gmio_current_char(&fwd_it) == 'r'); + gmio_skip_spaces(&fwd_it); + UTEST_ASSERT(gmio_next_char(&fwd_it) != NULL); + UTEST_ASSERT(*gmio_current_char(&fwd_it) == 'r'); - /* Test with very small string buffer */ - buff.pos = 0; - fwd_it.buffer.ptr = small_fwd_it_str; - fwd_it.buffer.max_len = sizeof(small_fwd_it_str); - gmio_string_stream_fwd_iterator_init(&fwd_it); + /* Test with very small string buffer */ + buff.pos = 0; + fwd_it.buffer.ptr = small_fwd_it_str; + fwd_it.buffer.max_len = sizeof(small_fwd_it_str); + gmio_string_stream_fwd_iterator_init(&fwd_it); - UTEST_ASSERT(*gmio_current_char(&fwd_it) == 'U'); - UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); - UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); - UTEST_ASSERT(strcmp(copy_strbuff.ptr, "citation,") == 0); - } + UTEST_ASSERT(*gmio_current_char(&fwd_it) == 'U'); + UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(gmio_eat_word(&fwd_it, ©_strbuff) == 0); + UTEST_ASSERT(strcmp(copy_strbuff.ptr, "citation,") == 0); + } - return NULL; + return NULL; } const char* all_tests() { - UTEST_SUITE_START(); - UTEST_RUN(test_internal__byte_swap); - UTEST_RUN(test_internal__byte_codec); - UTEST_RUN(test_internal__string_parse); - return NULL; + UTEST_SUITE_START(); + UTEST_RUN(test_internal__byte_swap); + UTEST_RUN(test_internal__byte_codec); + UTEST_RUN(test_internal__string_parse); + return NULL; } UTEST_MAIN(all_tests) diff --git a/tests/test_platform.c b/tests/test_platform.c index 75ebf80..d298c17 100644 --- a/tests/test_platform.c +++ b/tests/test_platform.c @@ -24,44 +24,44 @@ const char* test_platform__alignment() { - UTEST_ASSERT(offsetof(gmio_stl_coords_t, x) == 0); - UTEST_ASSERT(offsetof(gmio_stl_coords_t, y) == 4); - UTEST_ASSERT(offsetof(gmio_stl_coords_t, z) == 8); - UTEST_ASSERT(sizeof(gmio_stl_coords_t) == GMIO_STL_COORDS_RAWSIZE); - - UTEST_ASSERT(offsetof(gmio_stl_triangle_t, normal) == 0); - UTEST_ASSERT(offsetof(gmio_stl_triangle_t, v1) == GMIO_STL_COORDS_RAWSIZE); - UTEST_ASSERT(offsetof(gmio_stl_triangle_t, v2) == 2*GMIO_STL_COORDS_RAWSIZE); - UTEST_ASSERT(offsetof(gmio_stl_triangle_t, v3) == 3*GMIO_STL_COORDS_RAWSIZE); - UTEST_ASSERT(offsetof(gmio_stl_triangle_t, attribute_byte_count) == 4*GMIO_STL_COORDS_RAWSIZE); - UTEST_ASSERT(sizeof(gmio_stl_triangle_t) >= GMIO_STLB_TRIANGLE_RAWSIZE); + UTEST_ASSERT(offsetof(gmio_stl_coords_t, x) == 0); + UTEST_ASSERT(offsetof(gmio_stl_coords_t, y) == 4); + UTEST_ASSERT(offsetof(gmio_stl_coords_t, z) == 8); + UTEST_ASSERT(sizeof(gmio_stl_coords_t) == GMIO_STL_COORDS_RAWSIZE); - return NULL; + UTEST_ASSERT(offsetof(gmio_stl_triangle_t, normal) == 0); + UTEST_ASSERT(offsetof(gmio_stl_triangle_t, v1) == GMIO_STL_COORDS_RAWSIZE); + UTEST_ASSERT(offsetof(gmio_stl_triangle_t, v2) == 2*GMIO_STL_COORDS_RAWSIZE); + UTEST_ASSERT(offsetof(gmio_stl_triangle_t, v3) == 3*GMIO_STL_COORDS_RAWSIZE); + UTEST_ASSERT(offsetof(gmio_stl_triangle_t, attribute_byte_count) == 4*GMIO_STL_COORDS_RAWSIZE); + UTEST_ASSERT(sizeof(gmio_stl_triangle_t) >= GMIO_STLB_TRIANGLE_RAWSIZE); + + return NULL; } const char* test_platform__global_h() { - UTEST_ASSERT(sizeof(int8_t) == 1); - UTEST_ASSERT(sizeof(uint8_t) == 1); - - UTEST_ASSERT(sizeof(int16_t) == 2); - UTEST_ASSERT(sizeof(uint16_t) == 2); - - UTEST_ASSERT(sizeof(int32_t) == 4); - UTEST_ASSERT(sizeof(uint32_t) == 4); + UTEST_ASSERT(sizeof(int8_t) == 1); + UTEST_ASSERT(sizeof(uint8_t) == 1); - UTEST_ASSERT(sizeof(gmio_real32_t) == 4); - UTEST_ASSERT(sizeof(gmio_real64_t) == 8); - - return NULL; + UTEST_ASSERT(sizeof(int16_t) == 2); + UTEST_ASSERT(sizeof(uint16_t) == 2); + + UTEST_ASSERT(sizeof(int32_t) == 4); + UTEST_ASSERT(sizeof(uint32_t) == 4); + + UTEST_ASSERT(sizeof(gmio_real32_t) == 4); + UTEST_ASSERT(sizeof(gmio_real64_t) == 8); + + return NULL; } const char* all_tests() { - UTEST_SUITE_START(); - UTEST_RUN(test_platform__alignment); - UTEST_RUN(test_platform__global_h); - return NULL; + UTEST_SUITE_START(); + UTEST_RUN(test_platform__alignment); + UTEST_RUN(test_platform__global_h); + return NULL; } UTEST_MAIN(all_tests) diff --git a/tests/utest_lib.h b/tests/utest_lib.h index de2ae56..1e6fee0 100644 --- a/tests/utest_lib.h +++ b/tests/utest_lib.h @@ -25,18 +25,18 @@ #define UTEST_MAIN(name) \ int main(int argc, char *argv[]) {\ - const char *result = NULL; \ - \ - printf("----\nRUNNING: %s\n", argv[0]);\ - result = name();\ - if (result != NULL) {\ - printf("\n\nFAILED: %s\n", result);\ - }\ - else {\ - printf("\n\nALL TESTS PASSED\n");\ - }\ - printf("Tests run: %d\n", tests_run);\ - exit(result != NULL);\ + const char *result = NULL; \ + \ + printf("----\nRUNNING: %s\n", argv[0]);\ + result = name();\ + if (result != NULL) {\ + printf("\n\nFAILED: %s\n", result);\ + }\ + else {\ + printf("\n\nALL TESTS PASSED\n");\ + }\ + printf("Tests run: %d\n", tests_run);\ + exit(result != NULL);\ }