45
45
#endif
46
46
47
47
#include < algorithm>
48
+ #include < cctype>
48
49
#include < cstdint>
49
50
#include < fstream>
50
51
#include < locale>
@@ -122,7 +123,11 @@ namespace DX
122
123
if (!InFile)
123
124
break ;
124
125
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' #' )
126
131
{
127
132
// Comment
128
133
}
@@ -372,6 +377,11 @@ namespace DX
372
377
materials.emplace_back (mat);
373
378
}
374
379
}
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
+ }
375
385
else
376
386
{
377
387
#ifdef _DEBUG
@@ -437,6 +447,7 @@ namespace DX
437
447
InFile.imbue (std::locale::classic ());
438
448
439
449
auto curMaterial = materials.end ();
450
+ bool foundmat = false ;
440
451
441
452
for (;; )
442
453
{
@@ -445,8 +456,19 @@ namespace DX
445
456
if (!InFile)
446
457
break ;
447
458
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" ))
449
469
{
470
+ foundmat = true ;
471
+
450
472
// Switching active materials
451
473
wchar_t strName[MAX_PATH] = {};
452
474
InFile.width (MAX_PATH);
@@ -462,16 +484,17 @@ namespace DX
462
484
}
463
485
}
464
486
}
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
+ }
465
492
466
493
// The rest of the commands rely on an active material
467
494
if (curMaterial == materials.end ())
468
495
continue ;
469
496
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" ))
475
498
{
476
499
// Ambient color
477
500
float r, g, b;
@@ -570,7 +593,7 @@ namespace DX
570
593
571
594
InFile.close ();
572
595
573
- return S_OK;
596
+ return (foundmat) ? S_OK : E_FAIL ;
574
597
}
575
598
576
599
void Clear ()
0 commit comments