libstl: add stl_format.h,c

This commit is contained in:
Hugues Delorme 2014-01-28 19:05:18 +01:00
parent c11804fcea
commit dbe36c0eb1
2 changed files with 70 additions and 0 deletions

52
src/libstl/stl_format.c Normal file
View File

@ -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 <ctype.h>
#include <string.h>
#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;
}

18
src/libstl/stl_format.h Normal file
View File

@ -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 */