added binary stl file size check after ascii check
This commit is contained in:
@@ -456,31 +456,61 @@ _STLReader_::
|
|||||||
check_stl_type(const std::string& _filename) const
|
check_stl_type(const std::string& _filename) const
|
||||||
{
|
{
|
||||||
|
|
||||||
// open file
|
// open file
|
||||||
std::ifstream ifs (_filename.c_str(), std::ifstream::binary);
|
std::ifstream ifs (_filename.c_str(), std::ifstream::binary);
|
||||||
if(!ifs.good())
|
if(!ifs.good())
|
||||||
{
|
{
|
||||||
omerr() << "could not open file" << _filename << std::endl;
|
omerr() << "could not open file" << _filename << std::endl;
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//find first non whitespace character
|
//find first non whitespace character
|
||||||
std::string line = "";
|
std::string line = "";
|
||||||
std::size_t firstChar;
|
std::size_t firstChar;
|
||||||
while(line.empty() && ifs.good())
|
while(line.empty() && ifs.good())
|
||||||
{
|
{
|
||||||
std::getline(ifs,line);
|
std::getline(ifs,line);
|
||||||
firstChar = line.find_first_not_of("\t ");
|
firstChar = line.find_first_not_of("\t ");
|
||||||
}
|
}
|
||||||
|
|
||||||
//check for ascii keyword solid
|
//check for ascii keyword solid
|
||||||
if(strnicmp("solid",&line[firstChar],5) == 0)
|
if(strnicmp("solid",&line[firstChar],5) == 0)
|
||||||
{
|
{
|
||||||
return STLA;
|
return STLA;
|
||||||
}
|
}
|
||||||
|
ifs.close();
|
||||||
|
|
||||||
//if the file does not start with solid it is STLB
|
//if the file does not start with solid it is probably STLB
|
||||||
return STLB;
|
//check the file size to verify it.
|
||||||
|
|
||||||
|
//open the file
|
||||||
|
FILE* in = fopen(_filename.c_str(), "rb");
|
||||||
|
if (!in) return NONE;
|
||||||
|
|
||||||
|
// determine endian mode
|
||||||
|
union { unsigned int i; unsigned char c[4]; } endian_test;
|
||||||
|
endian_test.i = 1;
|
||||||
|
bool swapFlag = (endian_test.c[3] == 1);
|
||||||
|
|
||||||
|
|
||||||
|
// read number of triangles
|
||||||
|
char dummy[100];
|
||||||
|
fread(dummy, 1, 80, in);
|
||||||
|
size_t nT = read_int(in, swapFlag);
|
||||||
|
|
||||||
|
|
||||||
|
// compute file size from nT
|
||||||
|
size_t binary_size = 84 + nT*50;
|
||||||
|
|
||||||
|
// get actual file size
|
||||||
|
size_t file_size(0);
|
||||||
|
rewind(in);
|
||||||
|
while (!feof(in))
|
||||||
|
file_size += fread(dummy, 1, 100, in);
|
||||||
|
fclose(in);
|
||||||
|
|
||||||
|
// if sizes match -> it's STLB
|
||||||
|
return (binary_size == file_size ? STLB : NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user