gmio/src/gmio_stl/stl_io.c

163 lines
4.7 KiB
C
Raw Normal View History

/****************************************************************************
2015-05-28 15:40:24 +08:00
** gmio
2015-05-01 00:19:45 +08:00
** Copyright Fougue (2 Mar. 2015)
2015-07-13 17:42:03 +08:00
** contact@fougue.pro
**
** This software is a reusable library whose purpose is to provide complete
** I/O support for various CAD file formats (eg. STL)
**
** This software is governed by the CeCILL-B license under French law and
** abiding by the rules of distribution of free software. You can use,
** modify and/ or redistribute the software under the terms of the CeCILL-B
** license as circulated by CEA, CNRS and INRIA at the following URL
2015-03-30 15:05:25 +08:00
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
****************************************************************************/
#include "stl_io.h"
#include "stl_error.h"
#include "internal/stla_write.h"
#include "internal/stlb_write.h"
#include "../gmio_core/error.h"
#include "../gmio_core/internal/byte_codec.h"
#include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_stream.h"
2015-12-10 01:51:03 +08:00
int gmio_stl_read(struct gmio_stl_read_args* args)
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
2015-12-17 19:31:30 +08:00
const enum gmio_stl_format format =
2015-12-10 01:51:03 +08:00
gmio_stl_get_format(&args->core.stream);
2015-12-17 19:31:30 +08:00
switch (format) {
case GMIO_STL_FORMAT_ASCII: {
2015-12-10 01:51:03 +08:00
error = gmio_stla_read(args);
break;
}
case GMIO_STL_FORMAT_BINARY_BE: {
2015-12-10 01:51:03 +08:00
error = gmio_stlb_read(args, GMIO_ENDIANNESS_BIG);
break;
}
case GMIO_STL_FORMAT_BINARY_LE: {
2015-12-10 01:51:03 +08:00
error = gmio_stlb_read(args, GMIO_ENDIANNESS_LITTLE);
break;
}
case GMIO_STL_FORMAT_UNKNOWN: {
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
2015-12-10 01:51:03 +08:00
break;
}
} /* end switch() */
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
}
2015-12-10 01:51:03 +08:00
int gmio_stl_read_file(
struct gmio_stl_read_args* args, const char* filepath)
{
int error = GMIO_ERROR_OK;
2015-12-10 01:51:03 +08:00
if (args != NULL) {
FILE* file = fopen(filepath, "rb");
if (file != NULL) {
args->core.stream = gmio_stream_stdio(file);
error = gmio_stl_read(args);
fclose(file);
}
else {
error = GMIO_ERROR_STDIO;
}
}
else {
2015-12-10 01:51:03 +08:00
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
}
2015-12-17 19:31:30 +08:00
int gmio_stl_write(
struct gmio_stl_write_args* args, enum gmio_stl_format format)
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(&args->core.memblock);
2015-12-17 19:31:30 +08:00
switch (format) {
case GMIO_STL_FORMAT_ASCII: {
2015-12-10 01:51:03 +08:00
error = gmio_stla_write(args);
break;
}
case GMIO_STL_FORMAT_BINARY_BE: {
2015-12-10 01:51:03 +08:00
error = gmio_stlb_write(args, GMIO_ENDIANNESS_BIG);
break;
}
case GMIO_STL_FORMAT_BINARY_LE: {
2015-12-10 01:51:03 +08:00
error = gmio_stlb_write(args, GMIO_ENDIANNESS_LITTLE);
break;
}
case GMIO_STL_FORMAT_UNKNOWN: {
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
}
} /* end switch() */
gmio_memblock_helper_release(&mblock_helper);
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
2015-12-10 01:51:03 +08:00
return error;
}
2015-12-10 01:51:03 +08:00
int gmio_stl_write_file(
2015-12-17 19:31:30 +08:00
struct gmio_stl_write_args* args,
enum gmio_stl_format format,
const char* filepath)
2015-12-10 01:51:03 +08:00
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
FILE* file = fopen(filepath, "wb");
if (file != NULL) {
args->core.stream = gmio_stream_stdio(file);
2015-12-17 19:31:30 +08:00
error = gmio_stl_write(args, format);
2015-12-10 01:51:03 +08:00
fclose(file);
}
else {
error = GMIO_ERROR_STDIO;
}
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
}
static const struct gmio_stlb_header internal_stlb_zero_header = {0};
int gmio_stlb_write_header(
struct gmio_stream *stream,
enum gmio_endianness byte_order,
const struct gmio_stlb_header *header,
uint32_t facet_count)
{
uint8_t facet_count_bytes[sizeof(uint32_t)];
const struct gmio_stlb_header* non_null_header =
header != NULL ? header : &internal_stlb_zero_header;
/* Write 80-byte header */
if (gmio_stream_write(stream, non_null_header, GMIO_STLB_HEADER_SIZE, 1)
!= 1)
{
return GMIO_ERROR_STREAM;
}
/* Write facet count */
if (byte_order == GMIO_ENDIANNESS_LITTLE)
gmio_encode_uint32_le(facet_count, facet_count_bytes);
else
gmio_encode_uint32_be(facet_count, facet_count_bytes);
if (gmio_stream_write(stream, facet_count_bytes, sizeof(uint32_t), 1) != 1)
return GMIO_ERROR_STREAM;
return GMIO_ERROR_OK;
}