From dbe36c0eb15eaf8beeb8d8632deefce058cc6c5a Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Tue, 28 Jan 2014 19:05:18 +0100 Subject: [PATCH] libstl: add stl_format.h,c --- src/libstl/stl_format.c | 52 +++++++++++++++++++++++++++++++++++++++++ src/libstl/stl_format.h | 18 ++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/libstl/stl_format.c create mode 100644 src/libstl/stl_format.h diff --git a/src/libstl/stl_format.c b/src/libstl/stl_format.c new file mode 100644 index 0000000..e7d2dd5 --- /dev/null +++ b/src/libstl/stl_format.c @@ -0,0 +1,52 @@ +#include "stl_format.h" + +#include "../endian.h" +#include "../internal/byte_codec.h" +#include "../internal/byte_swap.h" +#include "stlb_triangle.h" + +#include +#include + +#define _INTERNAL_FOUG_FIXED_BUFFER_SIZE 512 + +foug_stl_format_t foug_stl_get_format(foug_stream_t *stream, size_t data_size) +{ + char fixed_buffer[_INTERNAL_FOUG_FIXED_BUFFER_SIZE]; + size_t byte_read = 0; + + if (stream == NULL || data_size == 0) + return FOUG_STL_UNKNOWN_FORMAT; + + memset(fixed_buffer, 0, _INTERNAL_FOUG_FIXED_BUFFER_SIZE); + byte_read = foug_stream_read(stream, &fixed_buffer, 1, _INTERNAL_FOUG_FIXED_BUFFER_SIZE); + + /* Binary STL ? */ + if (byte_read >= (FOUG_STLB_HEADER_SIZE + 4)) { + /* Try with little-endian format */ + uint32_t facet_count = foug_decode_uint32_le((const uint8_t*)fixed_buffer + 80); + + if ((FOUG_STLB_HEADER_SIZE + 4 + facet_count*FOUG_STLB_TRIANGLE_RAWSIZE) == data_size) + return FOUG_STL_BINARY_LE_FORMAT; + + /* Try with byte-reverted facet count */ + facet_count = foug_uint32_bswap(facet_count); + if ((FOUG_STLB_HEADER_SIZE + 4 + facet_count*FOUG_STLB_TRIANGLE_RAWSIZE) == data_size) + return FOUG_STL_BINARY_BE_FORMAT; + } + + /* ASCII STL ? */ + { + size_t pos = 0; + while (isspace(fixed_buffer[pos]) && pos < _INTERNAL_FOUG_FIXED_BUFFER_SIZE) + ++pos; + if (pos == _INTERNAL_FOUG_FIXED_BUFFER_SIZE) + return FOUG_STL_ASCII_FORMAT; + + if (strcmp(fixed_buffer + pos, "solid ") == 0) + return FOUG_STL_ASCII_FORMAT; + } + + /* Fallback case */ + return FOUG_STL_UNKNOWN_FORMAT; +} diff --git a/src/libstl/stl_format.h b/src/libstl/stl_format.h new file mode 100644 index 0000000..3048121 --- /dev/null +++ b/src/libstl/stl_format.h @@ -0,0 +1,18 @@ +#ifndef FOUG_LIBSTL_FORMAT_H +#define FOUG_LIBSTL_FORMAT_H + +#include "stl_global.h" +#include "../stream.h" + +typedef enum +{ + FOUG_STL_ASCII_FORMAT, /*!< STL ASCII (text) */ + FOUG_STL_BINARY_LE_FORMAT, /*!< STL binary (little-endian) */ + FOUG_STL_BINARY_BE_FORMAT, /*!< STL binary (big-endian) */ + FOUG_STL_UNKNOWN_FORMAT +} foug_stl_format_t; + +FOUG_DATAX_LIBSTL_EXPORT +foug_stl_format_t foug_stl_get_format(foug_stream_t* stream, size_t data_size); + +#endif /* FOUG_LIBSTL_FORMAT_H */