2
0
Эх сурвалжийг харах

Obj: support line continuations with \ (fixes #91)

Alexander Gessler 12 жил өмнө
parent
commit
e80886f12c
1 өөрчлөгдсөн 22 нэмэгдсэн , 6 устгасан
  1. 22 6
      code/ObjFileParser.cpp

+ 22 - 6
code/ObjFileParser.cpp

@@ -200,6 +200,8 @@ void ObjFileParser::copyNextWord(char *pBuffer, size_t length)
 			break;
 		++m_DataIt;
 	}
+
+	ai_assert(index < length);
 	pBuffer[index] = '\0';
 }
 
@@ -207,16 +209,30 @@ void ObjFileParser::copyNextWord(char *pBuffer, size_t length)
 // Copy the next line into a temporary buffer
 void ObjFileParser::copyNextLine(char *pBuffer, size_t length)
 {
-	size_t index = 0;
-	while (m_DataIt != m_DataItEnd)
+	size_t index = 0u;
+
+	// some OBJ files have line continuations using \ (such as in C++ et al)
+	bool continuation = false;
+	for (;m_DataIt != m_DataItEnd && index < length-1; ++m_DataIt) 
 	{
-		if (*m_DataIt == '\n' || *m_DataIt == '\r' || index == length-1)
+		const char c = *m_DataIt;
+		if (c == '\\') {
+			continuation = true;
+			continue;
+		}
+		
+		if (c == '\n' || c == '\r') {
+			if(continuation) {
+				pBuffer[ index++ ] = ' ';
+				continue;
+			}
 			break;
+		}
 
-		pBuffer[ index ] = *m_DataIt;
-		++index;
-		++m_DataIt;
+		continuation = false;
+		pBuffer[ index++ ] = c;
 	}
+	ai_assert(index < length);
 	pBuffer[ index ] = '\0';
 }