From 01e133c6b550786c63fe2886fe4252ecefa19567 Mon Sep 17 00:00:00 2001 From: MetaIO Maintainers Date: Wed, 3 Jul 2024 13:53:30 -0400 Subject: [PATCH] MetaIO 2024-07-03 (d269b397) Code extracted from: https://github.com/Kitware/MetaIO.git at commit d269b3972910919ad22f2b179a13c189b176deef (master). --- .../MetaIO/src/MetaIO/src/metaScene.cxx | 47 +++++++++---------- .../MetaIO/src/MetaIO/src/metaUtils.cxx | 27 +++++------ .../MetaIO/src/MetaIO/src/metaUtils.h | 2 +- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaScene.cxx b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaScene.cxx index e28b544ac94..1edb52bacd6 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaScene.cxx +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaScene.cxx @@ -166,10 +166,10 @@ MetaScene::Read(const char * _headerName) } const std::string objectType = MET_ReadType(*m_ReadStream); - if (!strncmp(objectType.c_str(), "Tube", 4) || ((objectType.empty()) && !strcmp(suf, "tre"))) + if (objectType.find("Tube") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "tre"))) { - char * subtype = MET_ReadSubType(*m_ReadStream); - if (!strncmp(subtype, "Vessel", 6)) + const std::string subtype = MET_ReadSubType(*m_ReadStream); + if (subtype.find("Vessel") != std::string::npos) { auto * vesseltube = new MetaVesselTube(); vesseltube->APIVersion(m_APIVersion); @@ -177,7 +177,7 @@ MetaScene::Read(const char * _headerName) vesseltube->ReadStream(m_NDims, m_ReadStream); m_ObjectList.push_back(vesseltube); } - else if (!strncmp(subtype, "DTI", 3)) + else if (subtype.find("DTI") != std::string::npos) { auto * dtitube = new MetaDTITube(); dtitube->APIVersion(m_APIVersion); @@ -193,10 +193,9 @@ MetaScene::Read(const char * _headerName) tube->ReadStream(m_NDims, m_ReadStream); m_ObjectList.push_back(tube); } - delete[] subtype; } - else if (!strncmp(objectType.c_str(), "Transform", 9)) + else if (objectType.find("Transform") != std::string::npos) { auto * transform = new MetaTransform(); transform->APIVersion(m_APIVersion); @@ -205,7 +204,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(transform); } - else if (!strncmp(objectType.c_str(), "TubeGraph", 9)) + else if (objectType.find("TubeGraph") != std::string::npos) { auto * tubeGraph = new MetaTubeGraph(); tubeGraph->APIVersion(m_APIVersion); @@ -214,7 +213,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(tubeGraph); } - else if (!strncmp(objectType.c_str(), "Ellipse", 7) || ((objectType.empty()) && !strcmp(suf, "elp"))) + else if (objectType.find("Ellipse") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "elp"))) { auto * ellipse = new MetaEllipse(); ellipse->APIVersion(m_APIVersion); @@ -223,7 +222,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(ellipse); } - else if (!strncmp(objectType.c_str(), "Contour", 7) || ((objectType.empty()) && !strcmp(suf, "ctr"))) + else if (objectType.find("Contour") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "ctr"))) { auto * contour = new MetaContour(); contour->APIVersion(m_APIVersion); @@ -232,7 +231,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(contour); } - else if (!strncmp(objectType.c_str(), "Arrow", 5)) + else if (objectType.find("Arrow") != std::string::npos) { auto * arrow = new MetaArrow(); arrow->APIVersion(m_APIVersion); @@ -241,7 +240,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(arrow); } - else if (!strncmp(objectType.c_str(), "Gaussian", 8) || ((objectType.empty()) && !strcmp(suf, "gau"))) + else if (objectType.find("Gaussian") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "gau"))) { auto * gaussian = new MetaGaussian(); gaussian->APIVersion(m_APIVersion); @@ -250,7 +249,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(gaussian); } - else if (!strncmp(objectType.c_str(), "Image", 5) || + else if (objectType.find("Image") != std::string::npos || ((objectType.empty()) && (!strcmp(suf, "mhd") || !strcmp(suf, "mha")))) { auto * image = new MetaImage(); @@ -261,7 +260,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(image); } - else if (!strncmp(objectType.c_str(), "Blob", 4) || ((objectType.empty()) && !strcmp(suf, "blb"))) + else if (objectType.find("Blob") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "blb"))) { auto * blob = new MetaBlob(); blob->APIVersion(m_APIVersion); @@ -270,7 +269,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(blob); } - else if (!strncmp(objectType.c_str(), "Landmark", 8) || ((objectType.empty()) && !strcmp(suf, "ldm"))) + else if (objectType.find("Landmark") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "ldm"))) { auto * landmark = new MetaLandmark(); landmark->APIVersion(m_APIVersion); @@ -279,7 +278,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(landmark); } - else if (!strncmp(objectType.c_str(), "Surface", 5) || ((objectType.empty()) && !strcmp(suf, "suf"))) + else if (objectType.find("Surface") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "suf"))) { auto * surface = new MetaSurface(); surface->APIVersion(m_APIVersion); @@ -288,7 +287,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(surface); } - else if (!strncmp(objectType.c_str(), "Line", 4) || ((objectType.empty()) && !strcmp(suf, "lin"))) + else if (objectType.find("Line") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "lin"))) { auto * line = new MetaLine(); line->APIVersion(m_APIVersion); @@ -297,7 +296,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(line); } - else if (!strncmp(objectType.c_str(), "Group", 5) || ((objectType.empty()) && !strcmp(suf, "grp"))) + else if (objectType.find("Group") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "grp"))) { auto * group = new MetaGroup(); group->APIVersion(m_APIVersion); @@ -306,7 +305,7 @@ MetaScene::Read(const char * _headerName) m_ObjectList.push_back(group); } - else if (!strncmp(objectType.c_str(), "AffineTransform", 15) || ((objectType.empty()) && !strcmp(suf, "trn"))) + else if (objectType.find("AffineTransform") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "trn"))) { auto * group = new MetaGroup(); group->APIVersion(m_APIVersion); @@ -314,7 +313,7 @@ MetaScene::Read(const char * _headerName) group->ReadStream(m_NDims, m_ReadStream); m_ObjectList.push_back(group); } - else if (!strncmp(objectType.c_str(), "Mesh", 4) || ((objectType.empty()) && !strcmp(suf, "msh"))) + else if (objectType.find("Mesh") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "msh"))) { auto * mesh = new MetaMesh(); mesh->APIVersion(m_APIVersion); @@ -322,7 +321,7 @@ MetaScene::Read(const char * _headerName) mesh->ReadStream(m_NDims, m_ReadStream); m_ObjectList.push_back(mesh); } - else if (!strncmp(objectType.c_str(), "FEMObject", 9) || ((objectType.empty()) && !strcmp(suf, "fem"))) + else if (objectType.find("FEMObject") != std::string::npos || ((objectType.empty()) && !strcmp(suf, "fem"))) { auto * femobject = new MetaFEMObject(); femobject->APIVersion(m_APIVersion); @@ -438,6 +437,10 @@ MetaScene::M_SetupWriteFields() MET_FieldRecordType * mF; + mF = new MET_FieldRecordType; + MET_InitWriteField(mF, "ObjectType", MET_STRING, strlen(m_ObjectTypeName), m_ObjectTypeName); + m_Fields.push_back(mF); + if (m_FileFormatVersion > 0) { mF = new MET_FieldRecordType; @@ -452,10 +455,6 @@ MetaScene::M_SetupWriteFields() m_Fields.push_back(mF); } - mF = new MET_FieldRecordType; - MET_InitWriteField(mF, "ObjectType", MET_STRING, strlen(m_ObjectTypeName), m_ObjectTypeName); - m_Fields.push_back(mF); - mF = new MET_FieldRecordType; MET_InitWriteField(mF, "NDims", MET_INT, m_NDims); m_Fields.push_back(mF); diff --git a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.cxx b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.cxx index abbf7574f73..26706e80523 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.cxx +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.cxx @@ -183,7 +183,7 @@ MET_ReadType(std::istream & _fp) // // Read the subtype of the object // -char * +std::string MET_ReadSubType(std::istream & _fp) { std::streampos pos = _fp.tellg(); @@ -193,25 +193,23 @@ MET_ReadSubType(std::istream & _fp) MET_InitReadField(mF, "ObjectType", MET_STRING, false); mF->required = false; fields.push_back(mF); + mF = new MET_FieldRecordType; + MET_InitReadField(mF, "ObjectSubType", MET_STRING, false); + mF->required = false; + fields.push_back(mF); MET_Read(_fp, &fields, '=', true); + _fp.seekg(pos); - // Find the line right after the ObjectType - char s[1024]; - _fp.getline(s, 500); - std::string value = s; - size_t position = value.find('='); - if (position != std::string::npos) + if (mF->defined) { - value = value.substr(position + 2, value.size() - position); + std::string value = reinterpret_cast(mF->value); + delete mF; + return value; } - _fp.seekg(pos); - char * ret = new char[value.size() + 1]; - strncpy(ret, value.c_str(), value.size()); - ret[value.size()] = '\0'; delete mF; - return ret; + return std::string(); } @@ -1165,6 +1163,7 @@ MET_Read(std::istream & fp, MET_SeperatorChar = _met_SeperatorChar; + unsigned int linecount = 0; while (!fp.eof()) { int i = 0; @@ -1426,7 +1425,7 @@ MET_Read(std::istream & fp, fp.getline(s, 500); } } - if (oneLine) + if (oneLine && ++linecount > 3) { return MET_IsComplete(fields); } diff --git a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.h b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.h index 8f85d3a1dbf..2d2ad50ef20 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.h +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaUtils.h @@ -455,7 +455,7 @@ std::string MET_ReadType(std::istream & _fp); METAIO_EXPORT -char * +std::string MET_ReadSubType(std::istream & _fp); # if (METAIO_USE_NAMESPACE)