4545#endif
4646
4747#include < algorithm>
48+ #include < cctype>
4849#include < cstdint>
4950#include < fstream>
5051#include < locale>
@@ -122,7 +123,11 @@ namespace DX
122123 if (!InFile)
123124 break ;
124125
125- if (*strCommand.c_str () == L' #' )
126+ if (strCommand.empty () || *strCommand.c_str () == 0 )
127+ {
128+ continue ;
129+ }
130+ else if (*strCommand.c_str () == L' #' )
126131 {
127132 // Comment
128133 }
@@ -372,6 +377,11 @@ namespace DX
372377 materials.emplace_back (mat);
373378 }
374379 }
380+ else if (!std::isprint (*strCommand.c_str ()))
381+ {
382+ // non-printable characters outside of comments mean this is not a text file
383+ return E_FAIL;
384+ }
375385 else
376386 {
377387 #ifdef _DEBUG
@@ -437,6 +447,7 @@ namespace DX
437447 InFile.imbue (std::locale::classic ());
438448
439449 auto curMaterial = materials.end ();
450+ bool foundmat = false ;
440451
441452 for (;; )
442453 {
@@ -445,8 +456,19 @@ namespace DX
445456 if (!InFile)
446457 break ;
447458
448- if (0 == wcscmp (strCommand.c_str (), L" newmtl" ))
459+ if (strCommand.empty () || *strCommand.c_str () == 0 )
460+ continue ;
461+
462+ if (*strCommand.c_str () == L' #' )
463+ {
464+ // Comment
465+ InFile.ignore (1000 , L' \n ' );
466+ continue ;
467+ }
468+ else if (0 == wcscmp (strCommand.c_str (), L" newmtl" ))
449469 {
470+ foundmat = true ;
471+
450472 // Switching active materials
451473 wchar_t strName[MAX_PATH] = {};
452474 InFile.width (MAX_PATH);
@@ -462,16 +484,17 @@ namespace DX
462484 }
463485 }
464486 }
487+ else if (!std::isprint (*strCommand.c_str ()))
488+ {
489+ // non-printable characters outside of comments mean this is not a text file
490+ return E_FAIL;
491+ }
465492
466493 // The rest of the commands rely on an active material
467494 if (curMaterial == materials.end ())
468495 continue ;
469496
470- if (0 == wcscmp (strCommand.c_str (), L" #" ))
471- {
472- // Comment
473- }
474- else if (0 == wcscmp (strCommand.c_str (), L" Ka" ))
497+ if (0 == wcscmp (strCommand.c_str (), L" Ka" ))
475498 {
476499 // Ambient color
477500 float r, g, b;
@@ -570,7 +593,7 @@ namespace DX
570593
571594 InFile.close ();
572595
573- return S_OK;
596+ return (foundmat) ? S_OK : E_FAIL ;
574597 }
575598
576599 void Clear ()
0 commit comments