Ignore leading whitespaces in STL files

Treat STL files as binary if non-printable characters appear in first
80 bytes
This commit is contained in:
Matthias Hochsteger 2019-09-30 11:54:00 +02:00
parent b867caf01c
commit 7becf20ebd

View File

@ -8,6 +8,7 @@
#include "stlgeom.hpp" #include "stlgeom.hpp"
#include <vector> #include <vector>
#include <cctype>
namespace netgen namespace netgen
{ {
@ -340,14 +341,28 @@ STLGeometry * STLTopology ::Load (istream & ist)
{ {
// Check if the file starts with "solid". If not, the file is binary // Check if the file starts with "solid". If not, the file is binary
{ {
char buf[5+1]; // binary header is 80 bytes, so don't load more than that
FIOReadStringE(ist,buf,5); constexpr int buflen = 80;
if (strcmp(buf, "solid") != 0) char buf[buflen+1];
{ FIOReadStringE(ist,buf,buflen);
for (auto i : Range(5))
// ignore whitespaces at start of line
int istart;
for (istart=0; istart<buflen-5; istart++)
if(std::isblank(buf[istart])==0)
break;
for (auto i : Range(buflen))
ist.unget(); ist.unget();
// does not start with "solid" -> binary file
if (strncmp(buf+istart, "solid", 5) != 0)
return LoadBinary(ist); return LoadBinary(ist);
}
// Check if there is a non-printable character in first 80 bytes
for (auto i : Range(istart, buflen))
if(std::isprint(buf[i])==0 && buf[i]!='\n')
return LoadBinary(ist);
} }
STLGeometry * geom = new STLGeometry(); STLGeometry * geom = new STLGeometry();
@ -361,6 +376,7 @@ STLGeometry * STLTopology ::Load (istream & ist)
int cntface = 0; int cntface = 0;
int vertex = 0; int vertex = 0;
bool badnormals = false; bool badnormals = false;
ist >> buf; // skip first line
while (ist.good()) while (ist.good())
{ {