Skip to content

Commit

Permalink
Merge pull request #1165 from jingxue/master
Browse files Browse the repository at this point in the history
Improve the PLY parser to work around some issues on Mac OSX
  • Loading branch information
taketwo committed Feb 28, 2015
2 parents 850bc7d + 85153bf commit 5fbfe0e
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions io/src/ply/ply_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
if (!istream)
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: couldn't read the magic string");
return false;
}

if ((magic[0] != 'p') || (magic[1] != 'l') || (magic[2] != 'y'))
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: wrong magic string");
return false;
}

Expand Down Expand Up @@ -101,14 +101,14 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
std::string format_string, version;
char space_format_format_string, space_format_string_version;
stringstream >> space_format_format_string >> std::ws >> format_string >> space_format_string_version >> std::ws >> version >> std::ws;
stringstream >> space_format_format_string >> std::ws >> format_string >> space_format_string_version >> std::ws >> version;
if (!stringstream ||
!stringstream.eof () ||
!isspace (space_format_format_string) ||
!isspace (space_format_string_version))
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: failed to parse the format statement");
return false;
}
if (format_string == "ascii")
Expand All @@ -127,7 +127,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: unknown format");
}
return false;
}
Expand All @@ -143,7 +143,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: more than 1 format statement");
}
return false;
}
Expand All @@ -159,15 +159,15 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
std::string name;
std::size_t count;
char space_element_name, space_name_count;
stringstream >> space_element_name >> std::ws >> name >> space_name_count >> std::ws >> count >> std::ws;
stringstream >> space_element_name >> std::ws >> name >> space_name_count >> std::ws >> count;
if (!stringstream ||
!stringstream.eof () ||
!isspace (space_element_name) ||
!isspace (space_name_count))
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: invalid element statement");
}
return false;
}
Expand All @@ -184,7 +184,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: invalid elements");
}
return false;
}
Expand Down Expand Up @@ -212,15 +212,15 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: invalid property statement");
}
return false;
}
if (type_or_list != "list") {
std::string name;
std::string& type = type_or_list;
char space_type_name;
stringstream >> space_type_name >> std::ws >> name >> std::ws;
stringstream >> space_type_name >> std::ws >> name;
if (!stringstream || !isspace (space_type_name))
{
if (error_callback_)
Expand Down Expand Up @@ -288,7 +288,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: unknown type");
}
return false;
}
Expand All @@ -299,14 +299,14 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
std::string name;
std::string size_type_string, scalar_type_string;
char space_list_size_type, space_size_type_scalar_type, space_scalar_type_name;
stringstream >> space_list_size_type >> std::ws >> size_type_string >> space_size_type_scalar_type >> std::ws >> scalar_type_string >> space_scalar_type_name >> std::ws >> name >> std::ws;
stringstream >> space_list_size_type >> std::ws >> size_type_string >> space_size_type_scalar_type >> std::ws >> scalar_type_string >> space_scalar_type_name >> std::ws >> name;
if (!stringstream ||
!isspace (space_list_size_type) ||
!isspace (space_size_type_scalar_type) ||
!isspace (space_scalar_type_name))
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: invalid list statement");
return false;
}
if (number_of_element_statements == 0)
Expand Down Expand Up @@ -369,7 +369,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: unkonwn scalar type");
}
return false;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
else
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: unknown scalar type");
return false;
}
}
Expand Down Expand Up @@ -454,7 +454,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
else
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: unknown scalar type");
return false;
}
}
Expand Down Expand Up @@ -506,7 +506,7 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
if (number_of_format_statements == 0)
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: a format statement is required");
return false;
}

Expand Down Expand Up @@ -594,14 +594,19 @@ bool pcl::io::ply::ply_parser::parse (const std::string& filename)
}
}
}
if (istream.fail () || (istream.rdbuf ()->sgetc () != std::char_traits<char>::eof ()) || istream.bad ())
if (istream.fail () || istream.bad ())
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
error_callback_ (line_number_, "parse error: failed to read from the binary stream");
}
return false;
}
if (istream.rdbuf ()->sgetc () != std::char_traits<char>::eof ())
{
if (warning_callback_)
warning_callback_ (line_number_, "ignoring extra data at the end of binary stream");
}
return true;
}
}

0 comments on commit 5fbfe0e

Please sign in to comment.