Utility/OBJParser: Abort parsing when there's more than one error per two line

Former-commit-id: 37075de6e7d10996af4c30ffcf74ead74b27905f [formerly 25597dd190b82dc40de1eaf7f4c75c9b058784ec]
Former-commit-id: 5383d58fd326b61cce4c415707dd06a0376e6dbe
This commit is contained in:
Lynix 2016-07-29 13:37:44 +02:00
parent b273a08571
commit f9d6f27e92
3 changed files with 45 additions and 23 deletions

View File

@ -86,7 +86,7 @@ namespace Nz
inline void Error(const String& message); inline void Error(const String& message);
inline void Flush() const; inline void Flush() const;
inline void Warning(const String& message); inline void Warning(const String& message);
inline void UnrecognizedLine(bool error = false); inline bool UnrecognizedLine(bool error = false);
std::vector<Mesh> m_meshes; std::vector<Mesh> m_meshes;
std::vector<String> m_materials; std::vector<String> m_materials;
@ -99,6 +99,7 @@ namespace Nz
mutable StringStream m_outputStream; mutable StringStream m_outputStream;
bool m_keepLastLine; bool m_keepLastLine;
unsigned int m_lineCount; unsigned int m_lineCount;
unsigned int m_errorCount;
}; };
} }

View File

@ -168,7 +168,7 @@ namespace Nz
NazaraWarning(message + " at line #" + String::Number(m_lineCount)); NazaraWarning(message + " at line #" + String::Number(m_lineCount));
} }
inline void OBJParser::UnrecognizedLine(bool error) inline bool OBJParser::UnrecognizedLine(bool error)
{ {
String message = "Unrecognized \"" + m_currentLine + '"'; String message = "Unrecognized \"" + m_currentLine + '"';
@ -176,6 +176,16 @@ namespace Nz
Error(message); Error(message);
else else
Warning(message); Warning(message);
m_errorCount++;
if (m_lineCount > 20 && (m_errorCount * 100 / m_lineCount) > 50)
{
NazaraError("Aborting parsing because of error percentage");
return false; //< Abort parsing if error percentage is too high
}
return true;
} }
} }

View File

@ -97,7 +97,8 @@ namespace Nz
if (m_currentLine.GetSize() < 7) // Since we only treat triangles, this is the minimum length of a face line (f 1 2 3) if (m_currentLine.GetSize() < 7) // Since we only treat triangles, this is the minimum length of a face line (f 1 2 3)
{ {
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
break; break;
} }
@ -106,7 +107,8 @@ namespace Nz
if (vertexCount < 3) if (vertexCount < 3)
{ {
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
break; break;
} }
@ -138,7 +140,8 @@ namespace Nz
if (std::sscanf(&m_currentLine[pos], "%d%n", &p, &offset) != 1) if (std::sscanf(&m_currentLine[pos], "%d%n", &p, &offset) != 1)
{ {
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
error = true; error = true;
break; break;
@ -217,7 +220,8 @@ namespace Nz
case 'm': //< MTLLib case 'm': //< MTLLib
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
if (m_currentLine.GetWord(0).ToLower() != "mtllib") if (m_currentLine.GetWord(0).ToLower() != "mtllib")
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
m_mtlLib = m_currentLine.SubString(m_currentLine.GetWordPosition(1)); m_mtlLib = m_currentLine.SubString(m_currentLine.GetWordPosition(1));
@ -229,7 +233,8 @@ namespace Nz
if (m_currentLine.GetSize() <= 2 || m_currentLine[1] != ' ') if (m_currentLine.GetSize() <= 2 || m_currentLine[1] != ' ')
{ {
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
break; break;
} }
@ -238,7 +243,8 @@ namespace Nz
if (objectName.IsEmpty()) if (objectName.IsEmpty())
{ {
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
break; break;
} }
@ -254,17 +260,20 @@ namespace Nz
{ {
String param = m_currentLine.SubString(2); String param = m_currentLine.SubString(2);
if (param != "all" && param != "on" && param != "off" && !param.IsNumber()) if (param != "all" && param != "on" && param != "off" && !param.IsNumber())
UnrecognizedLine(); {
if (!UnrecognizedLine())
return false;
}
} }
else else if (!UnrecognizedLine())
UnrecognizedLine(); return false;
break; break;
#endif #endif
case 'u': //< Usemtl case 'u': //< Usemtl
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
if (m_currentLine.GetWord(0) != "usemtl") if (m_currentLine.GetWord(0) != "usemtl" && !UnrecognizedLine())
UnrecognizedLine(); return false;
#endif #endif
matName = m_currentLine.SubString(m_currentLine.GetWordPosition(1)); matName = m_currentLine.SubString(m_currentLine.GetWordPosition(1));
@ -272,7 +281,8 @@ namespace Nz
if (matName.IsEmpty()) if (matName.IsEmpty())
{ {
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
break; break;
} }
@ -288,8 +298,8 @@ namespace Nz
if (paramCount >= 1) if (paramCount >= 1)
m_positions.push_back(vertex); m_positions.push_back(vertex);
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
else else if (!UnrecognizedLine())
UnrecognizedLine(); false;
#endif #endif
} }
else if (word == "vn") else if (word == "vn")
@ -299,8 +309,8 @@ namespace Nz
if (paramCount == 3) if (paramCount == 3)
m_normals.push_back(normal); m_normals.push_back(normal);
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
else else if (!UnrecognizedLine())
UnrecognizedLine(); false;
#endif #endif
} }
else if (word == "vt") else if (word == "vt")
@ -310,13 +320,13 @@ namespace Nz
if (paramCount >= 2) if (paramCount >= 2)
m_texCoords.push_back(uvw); m_texCoords.push_back(uvw);
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
else else if (!UnrecognizedLine())
UnrecognizedLine(); false;
#endif #endif
} }
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
else else if (!UnrecognizedLine())
UnrecognizedLine(); false;
#endif #endif
break; break;
@ -324,7 +334,8 @@ namespace Nz
default: default:
#if NAZARA_UTILITY_STRICT_RESOURCE_PARSING #if NAZARA_UTILITY_STRICT_RESOURCE_PARSING
UnrecognizedLine(); if (!UnrecognizedLine())
return false;
#endif #endif
break; break;
} }