17
17
# include < charconv>
18
18
#endif
19
19
20
+ #include < functional>
20
21
#include < iomanip>
21
22
#include < stdexcept>
22
23
@@ -2346,7 +2347,7 @@ bool CDxfRead::ReadCircle()
2346
2347
return false ;
2347
2348
}
2348
2349
2349
- void CDxfRead::ReadMText ()
2350
+ bool CDxfRead::ReadMText ()
2350
2351
{
2351
2352
Dxf_MTEXT text;
2352
2353
bool withinAcadColumnInfo = false ;
@@ -2367,7 +2368,7 @@ void CDxfRead::ReadMText()
2367
2368
2368
2369
text.str = this ->toUtf8 (text.str );
2369
2370
OnReadMText (text);
2370
- return ;
2371
+ return true ;
2371
2372
}
2372
2373
2373
2374
get_line ();
@@ -2501,9 +2502,11 @@ void CDxfRead::ReadMText()
2501
2502
break ;
2502
2503
}
2503
2504
}
2505
+
2506
+ return false ;
2504
2507
}
2505
2508
2506
- void CDxfRead::ReadText ()
2509
+ bool CDxfRead::ReadText ()
2507
2510
{
2508
2511
Dxf_TEXT text;
2509
2512
@@ -2513,7 +2516,7 @@ void CDxfRead::ReadText()
2513
2516
if (n == 0 ) {
2514
2517
ResolveColorIndex ();
2515
2518
OnReadText (text);
2516
- return ;
2519
+ return true ;
2517
2520
}
2518
2521
2519
2522
get_line ();
@@ -2563,6 +2566,8 @@ void CDxfRead::ReadText()
2563
2566
break ;
2564
2567
}
2565
2568
}
2569
+
2570
+ return false ;
2566
2571
}
2567
2572
2568
2573
bool CDxfRead::ReadEllipse ()
@@ -2898,6 +2903,32 @@ bool CDxfRead::ReadSolid()
2898
2903
return false ;
2899
2904
}
2900
2905
2906
+ bool CDxfRead::ReadSection ()
2907
+ {
2908
+ m_section_name.clear ();
2909
+ get_line ();
2910
+ get_line ();
2911
+ if (m_str != " ENTITIES" )
2912
+ m_section_name = m_str;
2913
+
2914
+ m_block_name.clear ();
2915
+ return true ;
2916
+ }
2917
+
2918
+ bool CDxfRead::ReadTable ()
2919
+ {
2920
+ get_line ();
2921
+ get_line ();
2922
+ return true ;
2923
+ }
2924
+
2925
+ bool CDxfRead::ReadEndSec ()
2926
+ {
2927
+ m_section_name.clear ();
2928
+ m_block_name.clear ();
2929
+ return true ;
2930
+ }
2931
+
2901
2932
bool CDxfRead::ReadPolyLine ()
2902
2933
{
2903
2934
Dxf_POLYLINE polyline;
@@ -3181,7 +3212,7 @@ void CDxfRead::put_line(const std::string& value)
3181
3212
m_unused_line = value;
3182
3213
}
3183
3214
3184
- bool CDxfRead::ReadUnits ()
3215
+ bool CDxfRead::ReadInsUnits ()
3185
3216
{
3186
3217
get_line (); // Skip to next line.
3187
3218
get_line (); // Skip to next line.
@@ -3196,6 +3227,17 @@ bool CDxfRead::ReadUnits()
3196
3227
}
3197
3228
}
3198
3229
3230
+ bool CDxfRead::ReadMeasurement ()
3231
+ {
3232
+ get_line ();
3233
+ get_line ();
3234
+ const int n = stringToInt (m_str, StringToErrorMode::ReturnErrorValue);
3235
+ if (n == 0 )
3236
+ m_measurement_inch = true ;
3237
+
3238
+ return true ;
3239
+ }
3240
+
3199
3241
bool CDxfRead::ReadLayer ()
3200
3242
{
3201
3243
std::string layername;
@@ -3293,7 +3335,7 @@ bool CDxfRead::ReadStyle()
3293
3335
return false ;
3294
3336
}
3295
3337
3296
- bool CDxfRead::ReadVersion ()
3338
+ bool CDxfRead::ReadAcadVer ()
3297
3339
{
3298
3340
static const std::vector<std::string> VersionNames = {
3299
3341
// This table is indexed by eDXFVersion_t - (ROlder+1)
@@ -3331,7 +3373,7 @@ bool CDxfRead::ReadVersion()
3331
3373
return ResolveEncoding ();
3332
3374
}
3333
3375
3334
- bool CDxfRead::ReadDWGCodePage ()
3376
+ bool CDxfRead::ReadDwgCodePage ()
3335
3377
{
3336
3378
get_line ();
3337
3379
get_line ();
@@ -3379,186 +3421,83 @@ void CDxfRead::DoRead(bool ignore_errors)
3379
3421
{
3380
3422
m_ignore_errors = ignore_errors;
3381
3423
m_gcount = 0 ;
3382
- if (m_fail) {
3424
+ if (m_fail)
3383
3425
return ;
3384
- }
3426
+
3427
+ std::unordered_map<std::string, std::function<bool ()>> mapHeaderVarHandler;
3428
+ mapHeaderVarHandler.insert ({ " $INSUNITS" , [=]{ return ReadInsUnits (); } });
3429
+ mapHeaderVarHandler.insert ({ " $MEASUREMENT" , [=]{ return ReadMeasurement (); } });
3430
+ mapHeaderVarHandler.insert ({ " $ACADVER" , [=]{ return ReadAcadVer (); } });
3431
+ mapHeaderVarHandler.insert ({ " $DWGCODEPAGE" , [=]{ return ReadDwgCodePage (); } });
3432
+
3433
+ std::unordered_map<std::string, std::function<bool ()>> mapEntityHandler;
3434
+ mapEntityHandler.insert ({ " ARC" , [=]{ return ReadArc (); } });
3435
+ mapEntityHandler.insert ({ " BLOCK" , [=]{ return ReadBlockInfo (); } });
3436
+ mapEntityHandler.insert ({ " CIRCLE" , [=]{ return ReadCircle (); } });
3437
+ mapEntityHandler.insert ({ " DIMENSION" , [=]{ return ReadDimension (); } });
3438
+ mapEntityHandler.insert ({ " ELLIPSE" , [=]{ return ReadEllipse (); } });
3439
+ mapEntityHandler.insert ({ " INSERT" , [=]{ return ReadInsert (); } });
3440
+ mapEntityHandler.insert ({ " LAYER" , [=]{ return ReadLayer (); } });
3441
+ mapEntityHandler.insert ({ " LINE" , [=]{ return ReadLine (); } });
3442
+ mapEntityHandler.insert ({ " LWPOLYLINE" , [=]{ return ReadLwPolyLine (); } });
3443
+ mapEntityHandler.insert ({ " MTEXT" , [=]{ return ReadMText (); } });
3444
+ mapEntityHandler.insert ({ " POINT" , [=]{ return ReadPoint (); } });
3445
+ mapEntityHandler.insert ({ " POLYLINE" , [=]{ return ReadPolyLine (); } });
3446
+ mapEntityHandler.insert ({ " SECTION" , [=]{ return ReadSection (); } });
3447
+ mapEntityHandler.insert ({ " SOLID" , [=]{ return ReadSolid (); } });
3448
+ mapEntityHandler.insert ({ " SPLINE" , [=]{ return ReadSpline (); } });
3449
+ mapEntityHandler.insert ({ " STYLE" , [=]{ return ReadStyle (); } });
3450
+ mapEntityHandler.insert ({ " TEXT" , [=]{ return ReadText (); } });
3451
+ mapEntityHandler.insert ({ " TABLE" , [=]{ return ReadTable (); } });
3452
+ mapEntityHandler.insert ({ " ENDSEC" , [=]{ return ReadEndSec (); } });
3385
3453
3386
3454
get_line ();
3387
3455
3388
3456
ScopedCLocale _ (LC_NUMERIC);
3389
3457
while (!m_ifs.eof ()) {
3390
3458
m_ColorIndex = ColorBylayer; // Default
3391
3459
3392
- if (m_str == " $INSUNITS" ) {
3393
- if (!ReadUnits ()) {
3394
- return ;
3395
- }
3396
- continue ;
3397
- } // End if - then
3398
-
3399
- if (m_str == " $MEASUREMENT" ) {
3400
- get_line ();
3401
- get_line ();
3402
- const int n = stringToInt (m_str, StringToErrorMode::ReturnErrorValue);
3403
- if (n == 0 )
3404
- m_measurement_inch = true ;
3405
-
3406
- continue ;
3407
- } // End if - then
3408
-
3409
- if (m_str == " $ACADVER" ) {
3410
- if (!ReadVersion ()) {
3411
- return ;
3412
- }
3413
- continue ;
3414
- } // End if - then
3415
-
3416
- if (m_str == " $DWGCODEPAGE" ) {
3417
- if (!ReadDWGCodePage ()) {
3418
- return ;
3460
+ { // Handle header variable
3461
+ auto itHandler = mapHeaderVarHandler.find (m_str);
3462
+ if (itHandler != mapHeaderVarHandler.cend ()) {
3463
+ const auto & fn = itHandler->second ;
3464
+ if (fn ())
3465
+ continue ;
3466
+ else
3467
+ return ;
3419
3468
}
3420
- continue ;
3421
- } // End if - then
3469
+ }
3422
3470
3423
3471
if (m_str == " 0" ) {
3424
3472
get_line ();
3425
3473
if (m_str == " 0" )
3426
3474
get_line (); // Skip again
3427
3475
3428
- if (m_str == " SECTION" ) {
3429
- m_section_name.clear ();
3430
- get_line ();
3431
- get_line ();
3432
- if (m_str != " ENTITIES" ) {
3433
- m_section_name = m_str;
3434
- }
3435
- m_block_name.clear ();
3436
-
3437
- } // End if - then
3438
- else if (m_str == " TABLE" ) {
3439
- get_line ();
3440
- get_line ();
3441
- }
3442
-
3443
- else if (m_str == " LAYER" ) {
3444
- if (!ReadLayer ()) {
3445
- this ->ReportError (" DXF::DoRead() - Failed to read layer" );
3446
- // return; Some objects or tables can have "LAYER" as name...
3447
- }
3448
- continue ;
3449
- }
3450
-
3451
- else if (m_str == " STYLE" ) {
3452
- ReadStyle ();
3453
- continue ;
3454
- }
3455
-
3456
- else if (m_str == " BLOCK" ) {
3457
- if (!ReadBlockInfo ()) {
3458
- this ->ReportError (" DXF::DoRead()f - Failed to read block info" );
3459
- return ;
3460
- }
3461
- continue ;
3462
- } // End if - then
3463
-
3464
- else if (m_str == " ENDSEC" ) {
3465
- m_section_name.clear ();
3466
- m_block_name.clear ();
3467
- } // End if - then
3468
- else if (m_str == " LINE" ) {
3469
- if (!ReadLine ()) {
3470
- this ->ReportError (" DXF::DoRead() - Failed to read line" );
3471
- return ;
3472
- }
3473
- continue ;
3474
- }
3475
- else if (m_str == " ARC" ) {
3476
- if (!ReadArc ()) {
3477
- this ->ReportError (" DXF::DoRead() - Failed to read arc" );
3478
- return ;
3479
- }
3480
- continue ;
3481
- }
3482
- else if (m_str == " CIRCLE" ) {
3483
- if (!ReadCircle ()) {
3484
- this ->ReportError (" DXF::DoRead() - Failed to read circle" );
3485
- return ;
3486
- }
3487
- continue ;
3488
- }
3489
- else if (m_str == " MTEXT" ) {
3476
+ auto itHandler = mapEntityHandler.find (m_str);
3477
+ if (itHandler != mapEntityHandler.cend ()) {
3478
+ const auto & fn = itHandler->second ;
3479
+ bool okRead = false ;
3480
+ std::string exceptionMsg;
3490
3481
try {
3491
- ReadMText ();
3482
+ okRead = fn ();
3492
3483
} catch (const std::runtime_error& err) {
3493
- this ->ReportError (" DXF::DoRead() - Failed to read MTEXT\n Error: " + std::string (err.what ()));
3494
- return ;
3484
+ exceptionMsg = err.what ();
3495
3485
}
3496
- continue ;
3497
- }
3498
- else if (m_str == " TEXT" ) {
3499
- try {
3500
- ReadText ();
3501
- } catch (const std::runtime_error& err) {
3502
- this ->ReportError (" DXF::DoRead() - Failed to read TEXT\n Error: " + std::string (err.what ()));
3503
- return ;
3504
- }
3505
- continue ;
3506
- }
3507
- else if (m_str == " ELLIPSE" ) {
3508
- if (!ReadEllipse ()) {
3509
- this ->ReportError (" DXF::DoRead() - Failed to read ellipse" );
3510
- return ;
3511
- }
3512
- continue ;
3513
- }
3514
- else if (m_str == " SPLINE" ) {
3515
- if (!ReadSpline ()) {
3516
- this ->ReportError (" DXF::DoRead() - Failed to read spline" );
3517
- return ;
3518
- }
3519
- continue ;
3520
- }
3521
- else if (m_str == " LWPOLYLINE" ) {
3522
- if (!ReadLwPolyLine ()) {
3523
- this ->ReportError (" DXF::DoRead() - Failed to read LW Polyline" );
3524
- return ;
3525
- }
3526
- continue ;
3527
- }
3528
- else if (m_str == " POLYLINE" ) {
3529
- if (!ReadPolyLine ()) {
3530
- this ->ReportError (" DXF::DoRead() - Failed to read Polyline" );
3531
- return ;
3532
- }
3533
- continue ;
3534
- }
3535
- else if (m_str == " POINT" ) {
3536
- if (!ReadPoint ()) {
3537
- this ->ReportError (" DXF::DoRead() - Failed to read Point" );
3538
- return ;
3539
- }
3540
- continue ;
3541
- }
3542
- else if (m_str == " INSERT" ) {
3543
- if (!ReadInsert ()) {
3544
- this ->ReportError (" DXF::DoRead() - Failed to read Insert" );
3545
- return ;
3546
- }
3547
- continue ;
3548
- }
3549
- else if (m_str == " DIMENSION" ) {
3550
- if (!ReadDimension ()) {
3551
- this ->ReportError (" DXF::DoRead() - Failed to read Dimension" );
3552
- return ;
3486
+
3487
+ if (okRead) {
3488
+ continue ;
3553
3489
}
3554
- continue ;
3555
- }
3556
- else if (m_str == " SOLID" ) {
3557
- if (!ReadSolid ()) {
3558
- this ->ReportError (" DXF::DoRead() - Failed to read Solid" );
3559
- return ;
3490
+ else {
3491
+ std::string errMsg = " DXF::DoRead() - Failed to read " + m_str;
3492
+ if (!exceptionMsg.empty ())
3493
+ errMsg += " \n Error: " + exceptionMsg;
3494
+
3495
+ this ->ReportError (errMsg);
3496
+ if (m_str == " LAYER" ) // Some objects or tables can have "LAYER" as name...
3497
+ continue ;
3498
+ else
3499
+ return ;
3560
3500
}
3561
- continue ;
3562
3501
}
3563
3502
}
3564
3503
0 commit comments