stl/stl.c
josh ffe4f07ace stl: added the beginning of a viewer
git-svn-id: svn://anubis/misc/stl@5 bd8a9e45-a331-0410-811e-c64571078777
2007-06-03 02:38:40 +00:00

40 lines
941 B
C

#include <sys/stat.h> /* struct stat */
#include <stdlib.h> /* malloc() */
#include <stdio.h> /* fprintf() */
#include "stl.h"
/* load a STL file and return a pointer to the stl_t struct */
stl_t * stl_load(const char * filename)
{
struct stat s;
if (stat(filename, &s))
{
fprintf(stderr, "Couldn't stat '%s'\n", filename);
return NULL;
}
if (s.st_size < sizeof(stl_t))
{
fprintf(stderr, "Size of '%s' only %d bytes!\n", filename, s.st_size);
return NULL;
}
FILE *fil;
if ((fil = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "Couldn't open '%s'\n", filename);
return NULL;
}
stl_t * stl = (stl_t *) malloc(s.st_size);
fread(stl, 1, s.st_size, fil);
fclose(fil);
if (s.st_size <
sizeof(stl_t) + sizeof(stl_face_t) * (stl_num_faces(stl)-1))
{
fprintf(stderr, "Size of '%s' only %d bytes, but has %d faces!\n",
filename, s.st_size, stl_num_faces(stl));
free(stl);
return NULL;
}
return stl;
}