Skip to content
Snippets Groups Projects
Commit 057b7e5a authored by Tom Fischer's avatar Tom Fischer
Browse files

[BL || A/IO] Subst. geometryCleanup and clearData by cleanupVectorElements.

parent 38a624f8
No related branches found
No related tags found
No related merge requests found
...@@ -64,20 +64,6 @@ bool isCommentLine(std::string const& str) ...@@ -64,20 +64,6 @@ bool isCommentLine(std::string const& str)
return (str.substr(0, 1) == "#"); 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 /// Parses current section until END-tag is reached
bool skipToEND(std::ifstream& in) bool skipToEND(std::ifstream& in)
{ {
...@@ -537,7 +523,7 @@ MeshLib::Mesh* createMesh(std::ifstream& in, DataType type, ...@@ -537,7 +523,7 @@ MeshLib::Mesh* createMesh(std::ifstream& in, DataType type,
return new MeshLib::Mesh(mesh_name, nodes, elems, mesh_prop); return new MeshLib::Mesh(mesh_name, nodes, elems, mesh_prop);
} }
ERR("Error parsing {:s} {:s}.", dataType2ShortString(type), mesh_name); ERR("Error parsing {:s} {:s}.", dataType2ShortString(type), mesh_name);
clearData(nodes, elems); BaseLib::cleanupVectorElements(nodes, elems);
return nullptr; return nullptr;
} }
......
...@@ -350,16 +350,6 @@ bool SwmmInterface::readLinksAsPolylines(std::ifstream &in, ...@@ -350,16 +350,6 @@ bool SwmmInterface::readLinksAsPolylines(std::ifstream &in,
return true; 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, bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
GeoLib::GEOObjects &geo_objects, bool add_subcatchments) GeoLib::GEOObjects &geo_objects, bool add_subcatchments)
{ {
...@@ -380,27 +370,24 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name, ...@@ -380,27 +370,24 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
std::string geo_name = BaseLib::extractBaseNameWithoutExtension(inp_file_name); std::string geo_name = BaseLib::extractBaseNameWithoutExtension(inp_file_name);
std::string line; std::string line;
while ( std::getline(in, line) ) while (std::getline(in, line))
{ {
if (line == "[COORDINATES]") if (line == "[COORDINATES]" || line == "[VERTICES]" ||
{ line == "[SYMBOLS]")
if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names))
return geometryCleanup(*points, *lines);
}
if (line == "[VERTICES]")
{ {
if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names)) if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names))
return geometryCleanup(*points, *lines); {
BaseLib::cleanupVectorElements(*points, *lines);
return false;
}
} }
if (line == "[Polygons]" && add_subcatchments) if (line == "[Polygons]" && add_subcatchments)
{ {
if (!readPolygons(in, *lines, line_names, *points, pnt_names)) if (!readPolygons(in, *lines, line_names, *points, pnt_names))
return geometryCleanup(*points, *lines); {
} BaseLib::cleanupVectorElements(*points, *lines);
if (line == "[SYMBOLS]") return false;
{ }
if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names))
return geometryCleanup(*points, *lines);
} }
} }
...@@ -411,8 +398,9 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name, ...@@ -411,8 +398,9 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
} }
if (points->size() != pnt_names.size()) if (points->size() != pnt_names.size())
{ {
ERR ("Length of point vector and point name vector do not match."); ERR("Length of point vector and point name vector do not match.");
return geometryCleanup(*points, *lines); BaseLib::cleanupVectorElements(*points, *lines);
return false;
} }
auto name_id_map = std::make_unique<std::map<std::string, std::size_t>>(); 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, ...@@ -429,31 +417,43 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
in.clear(); in.clear();
in.seekg(0, in.beg); in.seekg(0, in.beg);
while ( std::getline(in, line) ) while (std::getline(in, line))
{ {
if (line == "[JUNCTIONS]") if (line == "[JUNCTIONS]")
{ {
INFO ("Reading point elevation..."); INFO ("Reading point elevation...");
if (!addPointElevation(in, *points, *name_id_map)) if (!addPointElevation(in, *points, *name_id_map))
return geometryCleanup(*points, *lines); {
BaseLib::cleanupVectorElements(*points, *lines);
return false;
}
} }
if (line == "[CONDUITS]") if (line == "[CONDUITS]")
{ {
INFO ("Reading conduits..."); INFO ("Reading conduits...");
if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map)) if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map))
return geometryCleanup(*points, *lines); {
BaseLib::cleanupVectorElements(*points, *lines);
return false;
}
} }
else if (line == "[PUMPS]") else if (line == "[PUMPS]")
{ {
INFO ("Reading pumps..."); INFO ("Reading pumps...");
if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map)) if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map))
return geometryCleanup(*points, *lines); {
BaseLib::cleanupVectorElements(*points, *lines);
return false;
}
} }
else if (line == "[WEIRS]") else if (line == "[WEIRS]")
{ {
INFO ("Reading weirs..."); INFO ("Reading weirs...");
if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map)) if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map))
return geometryCleanup(*points, *lines); {
BaseLib::cleanupVectorElements(*points, *lines);
return false;
}
} }
} }
......
...@@ -284,4 +284,20 @@ std::size_t findIndex(Container const& container, ...@@ -284,4 +284,20 @@ std::size_t findIndex(Container const& container,
} }
return std::distance(container.begin(), it); 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 } // namespace BaseLib
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment