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