libstl-c

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

bench.c (1029B)


      1#include "stl.h"
      2
      3#include <err.h>
      4#include <stdio.h>
      5#include <stdlib.h>
      6
      7static char buf[BUFSIZ];
      8
      9static char *stl_err[] = {
     10	STL_STRERROR_INIT
     11};
     12
     13int
     14main(int argc, const char **argv)
     15{
     16	struct stl_result res;
     17	struct stl stl;
     18	size_t count;
     19	int n, rc;
     20
     21	stl_init(&stl, STL_TYPE_DETECT);
     22
     23	count = 0;
     24	while (1) {
     25		n = fread(buf, 1, BUFSIZ, stdin);
     26		if (n <= 0) errx(1, "truncated input");
     27
     28		while (1) {
     29			rc = stl_feed(&stl, &res, buf, n);
     30			if (rc == STL_DONE) break;
     31			if (rc == STL_INCOMPLETE) break;
     32			if (rc != STL_OK) errx(1, "libstl: %s", stl_err[rc]);
     33
     34			switch (res.type) {
     35			case STL_RES_FILETYPE:
     36				printf("filetype: %s\n",
     37					res.filetype == STL_TYPE_ASCII
     38					? "ascii" : "binary");
     39				break;
     40			case STL_RES_HEADER:
     41				printf("header: %s\n", res.header.str);
     42				break;
     43			case STL_RES_SOLID_NAME:
     44				printf("name: %s\n", res.solid_name.str);
     45				break;
     46			case STL_RES_TRIANGLE:
     47				count += 1;
     48				break;
     49			}
     50		}
     51
     52		if (rc == STL_DONE)
     53			break;
     54	}
     55
     56	printf("triangles: %lu\n", count);
     57}