Skip to content

Commit f48ae1c

Browse files
committed
IO_Dxf: factorize code in CDxfRead::DoRead()
1 parent 472d501 commit f48ae1c

File tree

2 files changed

+118
-172
lines changed

2 files changed

+118
-172
lines changed

src/io_dxf/dxf.cpp

+106-167
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# include <charconv>
1818
#endif
1919

20+
#include <functional>
2021
#include <iomanip>
2122
#include <stdexcept>
2223

@@ -2346,7 +2347,7 @@ bool CDxfRead::ReadCircle()
23462347
return false;
23472348
}
23482349

2349-
void CDxfRead::ReadMText()
2350+
bool CDxfRead::ReadMText()
23502351
{
23512352
Dxf_MTEXT text;
23522353
bool withinAcadColumnInfo = false;
@@ -2367,7 +2368,7 @@ void CDxfRead::ReadMText()
23672368

23682369
text.str = this->toUtf8(text.str);
23692370
OnReadMText(text);
2370-
return;
2371+
return true;
23712372
}
23722373

23732374
get_line();
@@ -2501,9 +2502,11 @@ void CDxfRead::ReadMText()
25012502
break;
25022503
}
25032504
}
2505+
2506+
return false;
25042507
}
25052508

2506-
void CDxfRead::ReadText()
2509+
bool CDxfRead::ReadText()
25072510
{
25082511
Dxf_TEXT text;
25092512

@@ -2513,7 +2516,7 @@ void CDxfRead::ReadText()
25132516
if (n == 0) {
25142517
ResolveColorIndex();
25152518
OnReadText(text);
2516-
return;
2519+
return true;
25172520
}
25182521

25192522
get_line();
@@ -2563,6 +2566,8 @@ void CDxfRead::ReadText()
25632566
break;
25642567
}
25652568
}
2569+
2570+
return false;
25662571
}
25672572

25682573
bool CDxfRead::ReadEllipse()
@@ -2898,6 +2903,32 @@ bool CDxfRead::ReadSolid()
28982903
return false;
28992904
}
29002905

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+
29012932
bool CDxfRead::ReadPolyLine()
29022933
{
29032934
Dxf_POLYLINE polyline;
@@ -3181,7 +3212,7 @@ void CDxfRead::put_line(const std::string& value)
31813212
m_unused_line = value;
31823213
}
31833214

3184-
bool CDxfRead::ReadUnits()
3215+
bool CDxfRead::ReadInsUnits()
31853216
{
31863217
get_line(); // Skip to next line.
31873218
get_line(); // Skip to next line.
@@ -3196,6 +3227,17 @@ bool CDxfRead::ReadUnits()
31963227
}
31973228
}
31983229

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+
31993241
bool CDxfRead::ReadLayer()
32003242
{
32013243
std::string layername;
@@ -3293,7 +3335,7 @@ bool CDxfRead::ReadStyle()
32933335
return false;
32943336
}
32953337

3296-
bool CDxfRead::ReadVersion()
3338+
bool CDxfRead::ReadAcadVer()
32973339
{
32983340
static const std::vector<std::string> VersionNames = {
32993341
// This table is indexed by eDXFVersion_t - (ROlder+1)
@@ -3331,7 +3373,7 @@ bool CDxfRead::ReadVersion()
33313373
return ResolveEncoding();
33323374
}
33333375

3334-
bool CDxfRead::ReadDWGCodePage()
3376+
bool CDxfRead::ReadDwgCodePage()
33353377
{
33363378
get_line();
33373379
get_line();
@@ -3379,186 +3421,83 @@ void CDxfRead::DoRead(bool ignore_errors)
33793421
{
33803422
m_ignore_errors = ignore_errors;
33813423
m_gcount = 0;
3382-
if (m_fail) {
3424+
if (m_fail)
33833425
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(); } });
33853453

33863454
get_line();
33873455

33883456
ScopedCLocale _(LC_NUMERIC);
33893457
while (!m_ifs.eof()) {
33903458
m_ColorIndex = ColorBylayer; // Default
33913459

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;
34193468
}
3420-
continue;
3421-
} // End if - then
3469+
}
34223470

34233471
if (m_str == "0") {
34243472
get_line();
34253473
if (m_str == "0")
34263474
get_line(); // Skip again
34273475

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;
34903481
try {
3491-
ReadMText();
3482+
okRead = fn();
34923483
} catch (const std::runtime_error& err) {
3493-
this->ReportError("DXF::DoRead() - Failed to read MTEXT\nError: " + std::string(err.what()));
3494-
return;
3484+
exceptionMsg = err.what();
34953485
}
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\nError: " + 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;
35533489
}
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 += "\nError: " + 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;
35603500
}
3561-
continue;
35623501
}
35633502
}
35643503

0 commit comments

Comments
 (0)