diff --git a/Applications/FileIO/GocadIO/GocadAsciiReader.cpp b/Applications/FileIO/GocadIO/GocadAsciiReader.cpp index 0a0fad11df1ffa4b5c57f7f34d860c3acc64eac5..26147fa5b8a0e6c30457dc4782498de84835c99a 100644 --- a/Applications/FileIO/GocadIO/GocadAsciiReader.cpp +++ b/Applications/FileIO/GocadIO/GocadAsciiReader.cpp @@ -64,20 +64,6 @@ bool isCommentLine(std::string const& str) return (str.substr(0, 1) == "#"); } -/// Clears data vectors if an error occured -void clearData(std::vector<MeshLib::Node*> const& nodes, - std::vector<MeshLib::Element*> const& elems) -{ - for (MeshLib::Element* e : elems) - { - delete e; - } - for (MeshLib::Node* n : nodes) - { - delete n; - } -} - /// Parses current section until END-tag is reached bool skipToEND(std::ifstream& in) { @@ -537,7 +523,7 @@ MeshLib::Mesh* createMesh(std::ifstream& in, DataType type, return new MeshLib::Mesh(mesh_name, nodes, elems, mesh_prop); } ERR("Error parsing {:s} {:s}.", dataType2ShortString(type), mesh_name); - clearData(nodes, elems); + BaseLib::cleanupVectorElements(nodes, elems); return nullptr; } diff --git a/Applications/FileIO/SWMM/SWMMInterface.cpp b/Applications/FileIO/SWMM/SWMMInterface.cpp index 072afa331289b49bd23a8ca59f07cb0f9b382398..a4810875431510eebda4e6915aa8eb8011df5d84 100644 --- a/Applications/FileIO/SWMM/SWMMInterface.cpp +++ b/Applications/FileIO/SWMM/SWMMInterface.cpp @@ -350,16 +350,6 @@ bool SwmmInterface::readLinksAsPolylines(std::ifstream &in, return true; } -/// Deletes the geometric objects and returns false -bool geometryCleanup(std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Polyline*> &lines) -{ - for (auto line : lines) - delete line; - for (auto point : points) - delete point; - return false; -} - bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name, GeoLib::GEOObjects &geo_objects, bool add_subcatchments) { @@ -380,27 +370,24 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name, std::string geo_name = BaseLib::extractBaseNameWithoutExtension(inp_file_name); std::string line; - while ( std::getline(in, line) ) + while (std::getline(in, line)) { - if (line == "[COORDINATES]") - { - if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names)) - return geometryCleanup(*points, *lines); - } - if (line == "[VERTICES]") + if (line == "[COORDINATES]" || line == "[VERTICES]" || + line == "[SYMBOLS]") { if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names)) - return geometryCleanup(*points, *lines); + { + BaseLib::cleanupVectorElements(*points, *lines); + return false; + } } if (line == "[Polygons]" && add_subcatchments) { if (!readPolygons(in, *lines, line_names, *points, pnt_names)) - return geometryCleanup(*points, *lines); - } - if (line == "[SYMBOLS]") - { - if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names)) - return geometryCleanup(*points, *lines); + { + BaseLib::cleanupVectorElements(*points, *lines); + return false; + } } } @@ -411,8 +398,9 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name, } if (points->size() != pnt_names.size()) { - ERR ("Length of point vector and point name vector do not match."); - return geometryCleanup(*points, *lines); + ERR("Length of point vector and point name vector do not match."); + BaseLib::cleanupVectorElements(*points, *lines); + return false; } auto name_id_map = std::make_unique<std::map<std::string, std::size_t>>(); @@ -429,31 +417,43 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name, in.clear(); in.seekg(0, in.beg); - while ( std::getline(in, line) ) + while (std::getline(in, line)) { if (line == "[JUNCTIONS]") { INFO ("Reading point elevation..."); if (!addPointElevation(in, *points, *name_id_map)) - return geometryCleanup(*points, *lines); + { + BaseLib::cleanupVectorElements(*points, *lines); + return false; + } } if (line == "[CONDUITS]") { INFO ("Reading conduits..."); if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map)) - return geometryCleanup(*points, *lines); + { + BaseLib::cleanupVectorElements(*points, *lines); + return false; + } } else if (line == "[PUMPS]") { INFO ("Reading pumps..."); if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map)) - return geometryCleanup(*points, *lines); + { + BaseLib::cleanupVectorElements(*points, *lines); + return false; + } } else if (line == "[WEIRS]") { INFO ("Reading weirs..."); if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map)) - return geometryCleanup(*points, *lines); + { + BaseLib::cleanupVectorElements(*points, *lines); + return false; + } } } diff --git a/BaseLib/Algorithm.h b/BaseLib/Algorithm.h index 62a47c1dc849201021cf2e99b2de22784dc08a05..a01a42ad5e121dfa7d279906d5adf1f9bfb7ecb6 100644 --- a/BaseLib/Algorithm.h +++ b/BaseLib/Algorithm.h @@ -284,4 +284,20 @@ std::size_t findIndex(Container const& container, } return std::distance(container.begin(), it); } + +/** Util function to cleanup vectors */ +template <typename T1, typename T2> +void cleanupVectorElements(std::vector<T1*> const& items, + std::vector<T2*> const& dependent_items) +{ + for (auto dependent_item : dependent_items) + { + delete dependent_item; + } + for (auto item : items) + { + delete item; + } +} + } // namespace BaseLib