libstl-c

C STL file-format library
git clone https://git.sinitax.com/sinitax/libstl-c
Log | Files | Refs | LICENSE | sfeed.txt

stl.h (1308B)


      1#pragma once
      2
      3#include <stdbool.h>
      4#include <stdlib.h>
      5
      6#define STL_BUFMAX 256
      7
      8#define STL_STRERROR_INIT \
      9	[STL_OK] = "Success", \
     10	[STL_DONE] = "Done processing", \
     11	[STL_INVALID] = "Invalid stl file", \
     12	[STL_INVALID_ARG] = "Invalid argument", \
     13	[STL_INCOMPLETE] = "Unexpected end of file" \
     14
     15enum {
     16	STL_OK,
     17	STL_DONE,
     18	STL_INVALID,
     19	STL_INVALID_ARG,
     20	STL_INCOMPLETE,
     21};
     22
     23enum {
     24	STL_TYPE_DETECT,
     25	STL_TYPE_ASCII,
     26	STL_TYPE_BINARY,
     27};
     28
     29enum {
     30	STL_RES_SOLID_NAME,
     31	STL_RES_HEADER,
     32	STL_RES_TRIANGLE,
     33	STL_RES_FILETYPE
     34};
     35
     36struct stl_vertex {
     37	union {
     38		struct {
     39			float x, y, z;
     40		};
     41		struct {
     42			float dim[3];
     43		};
     44	};
     45};
     46
     47struct stl_triangle {
     48	struct stl_vertex vtx[3];
     49	struct stl_vertex normal;
     50};
     51
     52struct stl_span {
     53	const char *str;
     54	size_t len;
     55};
     56
     57struct stl_result {
     58	int type;
     59	union {
     60		struct stl_span solid_name;
     61		struct stl_span header;
     62		struct stl_triangle tri;
     63		int filetype;
     64	};
     65};
     66
     67struct stl {
     68	char buf[STL_BUFMAX];
     69	size_t buflen;
     70
     71	const void *chunk;
     72	const void *pos;
     73	size_t nleft;
     74
     75	struct stl_triangle tri;
     76
     77	int type;
     78	union {
     79		struct {
     80			int state;
     81		} ascii;
     82		struct {
     83			int state;
     84			size_t index;
     85			size_t count;
     86		} bin;
     87	};
     88};
     89
     90void stl_init(struct stl *stl, int type);
     91int stl_feed(struct stl *stl, struct stl_result *res,
     92	const void *chunk, size_t size);