From 288f9a8c26eae26d88fd0a9e6e7c4b71d2beecde Mon Sep 17 00:00:00 2001 From: Dmitri Naumov <github@naumov.de> Date: Mon, 22 Mar 2021 12:55:47 +0100 Subject: [PATCH] Replace Writer::writeToFile with writeStringToFile Eliminating the function also reveals a copy of the data being made internally. --- .../DataExplorer/DataView/SaveMeshDialog.cpp | 3 ++- Applications/DataExplorer/mainwindow.cpp | 16 ++++++++++------ Applications/FileIO/Legacy/createSurface.cpp | 2 +- Applications/FileIO/SWMM/SWMMInterface.cpp | 4 ++-- Applications/FileIO/XmlIO/Qt/XmlPrjInterface.cpp | 8 +++++--- Applications/FileIO/writeGeometryToFile.cpp | 2 +- .../Utils/FileConverter/ConvertSHPToGLI.cpp | 4 ++-- Applications/Utils/FileConverter/VTK2OGS.cpp | 2 +- Applications/Utils/GeoTools/MoveGeometry.cpp | 3 ++- .../Utils/GeoTools/TriangulatePolyline.cpp | 2 +- .../Utils/MeshEdit/MapGeometryToMeshSurface.cpp | 3 ++- .../MeshGeoTools/VerticalSliceFromLayers.cpp | 5 +++-- .../Utils/OGSFileConverter/OGSFileConverter.cpp | 4 ++-- .../Utils/SWMMConverter/SWMMConverter.cpp | 2 +- BaseLib/IO/Writer.cpp | 5 ----- BaseLib/IO/Writer.h | 3 --- MeshLib/IO/writeMeshToFile.cpp | 2 +- Tests/FileIO/TestBoostGmlInterface.cpp | 3 ++- Tests/FileIO/TestCsvWriter.cpp | 2 +- Tests/FileIO_Qt/TestQtGmlInterface.cpp | 3 ++- 20 files changed, 41 insertions(+), 37 deletions(-) diff --git a/Applications/DataExplorer/DataView/SaveMeshDialog.cpp b/Applications/DataExplorer/DataView/SaveMeshDialog.cpp index a46c5c6e88f..ecb3acc8d4b 100644 --- a/Applications/DataExplorer/DataView/SaveMeshDialog.cpp +++ b/Applications/DataExplorer/DataView/SaveMeshDialog.cpp @@ -87,7 +87,8 @@ void SaveMeshDialog::accept() { MeshLib::IO::Legacy::MeshIO meshIO; meshIO.setMesh(&_mesh); - meshIO.writeToFile(file_name.toStdString()); + BaseLib::IO::writeStringToFile(meshIO.writeToString(), + file_name.toStdString()); } LastSavedFileDirectory::setDir(file_name); diff --git a/Applications/DataExplorer/mainwindow.cpp b/Applications/DataExplorer/mainwindow.cpp index 3162fc3dc77..da7f10411de 100644 --- a/Applications/DataExplorer/mainwindow.cpp +++ b/Applications/DataExplorer/mainwindow.cpp @@ -478,7 +478,8 @@ void MainWindow::save() if (fi.suffix().toLower() == "prj") { XmlPrjInterface xml(_project); - xml.writeToFile(fileName.toStdString()); + BaseLib::IO::writeStringToFile(xml.writeToString(), + fileName.toStdString()); } else if (fi.suffix().toLower() == "geo") { @@ -495,7 +496,8 @@ void MainWindow::save() FileIO::GMSH::MeshDensityAlgorithm::FixedMeshDensity, point_density, station_density, max_pnts_per_leaf, selected_geometries, false, false); - bool const success = gmsh_io.writeToFile(fileName.toStdString()); + bool const success = BaseLib::IO::writeStringToFile( + gmsh_io.writeToString(), fileName.toStdString()); if (!success) { @@ -917,14 +919,14 @@ void MainWindow::writeGeometryToFile(QString gliName, QString fileName) #endif GeoLib::IO::XmlGmlInterface xml(_project.getGEOObjects()); xml.export_name = gliName.toStdString(); - xml.writeToFile(fileName.toStdString()); + BaseLib::IO::writeStringToFile(xml.writeToString(), fileName.toStdString()); } void MainWindow::writeStationListToFile(QString listName, QString fileName) { GeoLib::IO::XmlStnInterface xml(_project.getGEOObjects()); xml.export_name = listName.toStdString(); - xml.writeToFile(fileName.toStdString()); + BaseLib::IO::writeStringToFile(xml.writeToString(), fileName.toStdString()); } void MainWindow::mapGeometry(const std::string &geo_name) @@ -1061,13 +1063,15 @@ void MainWindow::callGMSH(std::vector<std::string> & selectedGeometries, _project.getGEOObjects(), true, FileIO::GMSH::MeshDensityAlgorithm::AdaptiveMeshDensity, param2, param3, param1, selectedGeometries, false, false); - gmsh_io.writeToFile(fileName.toStdString()); + BaseLib::IO::writeStringToFile(gmsh_io.writeToString(), + fileName.toStdString()); } else { // homogeneous meshing selected FileIO::GMSH::GMSHInterface gmsh_io( _project.getGEOObjects(), true, FileIO::GMSH::MeshDensityAlgorithm::FixedMeshDensity, param4, param3, param1, selectedGeometries, false, false); - gmsh_io.writeToFile(fileName.toStdString()); + BaseLib::IO::writeStringToFile(gmsh_io.writeToString(), + fileName.toStdString()); } if (system(nullptr) != 0) // command processor available diff --git a/Applications/FileIO/Legacy/createSurface.cpp b/Applications/FileIO/Legacy/createSurface.cpp index ffc73e69482..72cb2f6bf63 100644 --- a/Applications/FileIO/Legacy/createSurface.cpp +++ b/Applications/FileIO/Legacy/createSurface.cpp @@ -81,7 +81,7 @@ bool createSurface(GeoLib::Polyline const& ply, auto geo_file = fs::temp_directory_path() /= BaseLib::randomString(32); auto msh_file = fs::temp_directory_path() /= BaseLib::randomString(32); - gmsh_io.writeToFile(geo_file); + BaseLib::IO::writeStringToFile(gmsh_io.writeToString(), geo_file); // Using GMSH's vtk output here so we don't have to deal with GMSH and it's // various file format versions here std::string gmsh_command = diff --git a/Applications/FileIO/SWMM/SWMMInterface.cpp b/Applications/FileIO/SWMM/SWMMInterface.cpp index 094ae5f1140..43029fc1323 100644 --- a/Applications/FileIO/SWMM/SWMMInterface.cpp +++ b/Applications/FileIO/SWMM/SWMMInterface.cpp @@ -1321,7 +1321,7 @@ bool SwmmInterface::writeCsvForTimestep(std::string const& file_name, SwmmObject ERR ("No data to write"); return false; } - csv.writeToFile(file_name); + BaseLib::IO::writeStringToFile(csv.writeToString(), file_name); return true; } @@ -1343,7 +1343,7 @@ bool SwmmInterface::writeCsvForObject(std::string const& file_name, SwmmObject o ERR ("No data to write"); return false; } - csv.writeToFile(file_name); + BaseLib::IO::writeStringToFile(csv.writeToString(), file_name); return true; } diff --git a/Applications/FileIO/XmlIO/Qt/XmlPrjInterface.cpp b/Applications/FileIO/XmlIO/Qt/XmlPrjInterface.cpp index 88b82638c43..20452c12552 100644 --- a/Applications/FileIO/XmlIO/Qt/XmlPrjInterface.cpp +++ b/Applications/FileIO/XmlIO/Qt/XmlPrjInterface.cpp @@ -334,7 +334,7 @@ T* XmlPrjInterface::parseCondition( int XmlPrjInterface::writeToFile(const std::string& filename) { _filename = filename; - return BaseLib::IO::Writer::writeToFile(filename); + return BaseLib::IO::writeStringToFile(writeToString(), filename); } bool XmlPrjInterface::write() @@ -376,7 +376,8 @@ bool XmlPrjInterface::write() // write gml file GeoLib::IO::XmlGmlInterface gml(geo_objects); gml.export_name = name; - if (gml.writeToFile(std::string(path + name + ".gml"))) + if (BaseLib::IO::writeStringToFile(gml.writeToString(), + std::string(path + name + ".gml"))) { // write entry in project file QDomElement geo_tag = doc.createElement("geometry"); @@ -399,7 +400,8 @@ bool XmlPrjInterface::write() GeoLib::IO::XmlStnInterface stn(geo_objects); stn.export_name = name; - if (stn.writeToFile(path + name + ".stn")) + if (BaseLib::IO::writeStringToFile(stn.writeToString(), + path + name + ".stn")) { // write entry in project file QDomElement stn_tag = doc.createElement("stations"); diff --git a/Applications/FileIO/writeGeometryToFile.cpp b/Applications/FileIO/writeGeometryToFile.cpp index a83a9a7ee48..23e8f25bc55 100644 --- a/Applications/FileIO/writeGeometryToFile.cpp +++ b/Applications/FileIO/writeGeometryToFile.cpp @@ -26,7 +26,7 @@ void writeGeometryToFile(std::string const& geo_name, if (extension == ".gml" || extension == ".GML") { GeoLib::IO::BoostXmlGmlInterface xml(geo_objs); xml.export_name = geo_name; - xml.writeToFile(fname); + BaseLib::IO::writeStringToFile(xml.writeToString(), fname); } else if (extension == "gli" || extension == "GLI") { FileIO::Legacy::writeGLIFileV4(fname, geo_name, geo_objs); } else { diff --git a/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp b/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp index 48f86b33385..48c7362a712 100644 --- a/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp +++ b/Applications/Utils/FileConverter/ConvertSHPToGLI.cpp @@ -93,11 +93,11 @@ void convertPoints (DBFHandle dbf_handle, if (station) { GeoLib::IO::XmlStnInterface xml (geo_objs); xml.export_name = points_group_name; - xml.writeToFile(out_fname); + BaseLib::IO::writeStringToFile(xml.writeToString(), out_fname); } else { GeoLib::IO::XmlGmlInterface xml (geo_objs); xml.export_name = points_group_name; - xml.writeToFile(out_fname); + BaseLib::IO::writeStringToFile(xml.writeToString(), out_fname); } } diff --git a/Applications/Utils/FileConverter/VTK2OGS.cpp b/Applications/Utils/FileConverter/VTK2OGS.cpp index e671dfab758..1d1f767f931 100644 --- a/Applications/Utils/FileConverter/VTK2OGS.cpp +++ b/Applications/Utils/FileConverter/VTK2OGS.cpp @@ -47,7 +47,7 @@ int main (int argc, char* argv[]) MeshLib::IO::Legacy::MeshIO meshIO; meshIO.setMesh(mesh); - meshIO.writeToFile(mesh_out.getValue()); + BaseLib::IO::writeStringToFile(meshIO.writeToString(), mesh_out.getValue()); return EXIT_SUCCESS; } diff --git a/Applications/Utils/GeoTools/MoveGeometry.cpp b/Applications/Utils/GeoTools/MoveGeometry.cpp index 9625e6ad00c..87c60334622 100644 --- a/Applications/Utils/GeoTools/MoveGeometry.cpp +++ b/Applications/Utils/GeoTools/MoveGeometry.cpp @@ -93,7 +93,8 @@ int main(int argc, char *argv[]) } xml.export_name = geo_name; - xml.writeToFile(geo_output_arg.getValue()); + BaseLib::IO::writeStringToFile(xml.writeToString(), + geo_output_arg.getValue()); return EXIT_SUCCESS; } diff --git a/Applications/Utils/GeoTools/TriangulatePolyline.cpp b/Applications/Utils/GeoTools/TriangulatePolyline.cpp index fc602705625..8eeb8170aba 100644 --- a/Applications/Utils/GeoTools/TriangulatePolyline.cpp +++ b/Applications/Utils/GeoTools/TriangulatePolyline.cpp @@ -145,7 +145,7 @@ int main(int argc, char *argv[]) // write new file xml.export_name = geo_name; - xml.writeToFile(output_arg.getValue()); + BaseLib::IO::writeStringToFile(xml.writeToString(), output_arg.getValue()); INFO ("...done."); return EXIT_SUCCESS; diff --git a/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp b/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp index 66716e73318..0047666847f 100644 --- a/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp +++ b/Applications/Utils/MeshEdit/MapGeometryToMeshSurface.cpp @@ -82,7 +82,8 @@ int main (int argc, char* argv[]) { GeoLib::IO::BoostXmlGmlInterface xml_io(geometries); xml_io.export_name = geo_name; - xml_io.writeToFile(output_geometry_fname.getValue()); + BaseLib::IO::writeStringToFile(xml_io.writeToString(), + output_geometry_fname.getValue()); } return EXIT_SUCCESS; } diff --git a/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp b/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp index a439d3f308c..22db8db946c 100644 --- a/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp +++ b/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp @@ -198,7 +198,7 @@ void consolidateGeometry(GeoLib::GEOObjects& geo, std::string const filename(output_name + ".gml"); GeoLib::IO::XmlGmlInterface xml(geo); xml.export_name = merged_geo_name; - xml.writeToFile(filename); + BaseLib::IO::writeStringToFile(xml.writeToString(), filename); geo.removePolylineVec(merged_geo_name); geo.removePointVec(merged_geo_name); @@ -224,7 +224,8 @@ MeshLib::Mesh* generateMesh(GeoLib::GEOObjects& geo, geo, true, FileIO::GMSH::MeshDensityAlgorithm::FixedMeshDensity, res, 0, 0, gmsh_geo, false, false); gmsh_io.writePhysicalGroups(true); - bool const success = gmsh_io.writeToFile(gmsh_geo_name); + bool const success = + BaseLib::IO::writeStringToFile(gmsh_io.writeToString(), gmsh_geo_name); std::string const gmsh_mesh_name = output_name + ".msh"; std::string gmsh_command = "gmsh -2 -algo meshadapt " + gmsh_geo_name; diff --git a/Applications/Utils/OGSFileConverter/OGSFileConverter.cpp b/Applications/Utils/OGSFileConverter/OGSFileConverter.cpp index 8119ad349fb..f40de784429 100644 --- a/Applications/Utils/OGSFileConverter/OGSFileConverter.cpp +++ b/Applications/Utils/OGSFileConverter/OGSFileConverter.cpp @@ -114,7 +114,7 @@ void OGSFileConverter::convertGLI2GML(const QStringList &input, const QString &o std::string const geo_name = BaseLib::extractBaseName(input_string.toStdString()); xml.export_name = geo_name; - xml.writeToFile(output_str); + BaseLib::IO::writeStringToFile(xml.writeToString(), output_str); geo_objects.removeSurfaceVec(geo_name); geo_objects.removePolylineVec(geo_name); geo_objects.removePointVec(geo_name); @@ -156,7 +156,7 @@ void OGSFileConverter::convertVTU2MSH(const QStringList &input, const QString &o } MeshLib::IO::Legacy::MeshIO meshIO; meshIO.setMesh(mesh); - meshIO.writeToFile(output_str); + BaseLib::IO::writeStringToFile(meshIO.writeToString(), output_str); delete mesh; } OGSError::box("File conversion finished"); diff --git a/Applications/Utils/SWMMConverter/SWMMConverter.cpp b/Applications/Utils/SWMMConverter/SWMMConverter.cpp index 09eb89bbf82..1fa4fd9781a 100644 --- a/Applications/Utils/SWMMConverter/SWMMConverter.cpp +++ b/Applications/Utils/SWMMConverter/SWMMConverter.cpp @@ -32,7 +32,7 @@ int writeGeoOutput(std::string input_file, std::string output_file) GeoLib::IO::BoostXmlGmlInterface xml(geo_objects); xml.export_name = BaseLib::extractBaseNameWithoutExtension(input_file); - xml.writeToFile(output_file); + BaseLib::IO::writeStringToFile(xml.writeToString(), output_file); return 0; } diff --git a/BaseLib/IO/Writer.cpp b/BaseLib/IO/Writer.cpp index 350d98d448b..be8c08e720e 100644 --- a/BaseLib/IO/Writer.cpp +++ b/BaseLib/IO/Writer.cpp @@ -43,11 +43,6 @@ std::string Writer::writeToString() return std::string(""); } -int Writer::writeToFile(std::filesystem::path const& file_path) -{ - return writeStringToFile(writeToString(), file_path); -} - int writeStringToFile(std::string content, std::filesystem::path const& file_path) { diff --git a/BaseLib/IO/Writer.h b/BaseLib/IO/Writer.h index a7bbd691fb3..1c682d9ac72 100644 --- a/BaseLib/IO/Writer.h +++ b/BaseLib/IO/Writer.h @@ -36,9 +36,6 @@ public: /// @brief Writes the object to a string. std::string writeToString(); - /// @brief Writes the object to the given file. - int writeToFile(std::filesystem::path const& file_path); - protected: /// @brief Writes the object to the internal stream. /// This method must be implemented by a subclass. diff --git a/MeshLib/IO/writeMeshToFile.cpp b/MeshLib/IO/writeMeshToFile.cpp index 377aa6f79b0..1adb694ad0d 100644 --- a/MeshLib/IO/writeMeshToFile.cpp +++ b/MeshLib/IO/writeMeshToFile.cpp @@ -26,7 +26,7 @@ int writeMeshToFile(const MeshLib::Mesh& mesh, { MeshLib::IO::Legacy::MeshIO meshIO; meshIO.setMesh(&mesh); - meshIO.writeToFile(file_path); + BaseLib::IO::writeStringToFile(meshIO.writeToString(), file_path); return 0; } if (file_path.extension().string() == ".vtu") diff --git a/Tests/FileIO/TestBoostGmlInterface.cpp b/Tests/FileIO/TestBoostGmlInterface.cpp index bfe7aa59641..94e9591c016 100644 --- a/Tests/FileIO/TestBoostGmlInterface.cpp +++ b/Tests/FileIO/TestBoostGmlInterface.cpp @@ -30,7 +30,8 @@ TEST_F(TestGmlInterface, BoostXmlGmlWriterReaderTest) GeoLib::IO::BoostXmlGmlInterface xml(geo_objects); xml.export_name = geo_name; - int result = xml.writeToFile(test_data_file); + int result = + BaseLib::IO::writeStringToFile(xml.writeToString(), test_data_file); EXPECT_EQ(result, 1); // remove the written data from the data structures diff --git a/Tests/FileIO/TestCsvWriter.cpp b/Tests/FileIO/TestCsvWriter.cpp index 9cacb6717f9..b37d9ed9fa4 100644 --- a/Tests/FileIO/TestCsvWriter.cpp +++ b/Tests/FileIO/TestCsvWriter.cpp @@ -48,7 +48,7 @@ TEST(CsvWriter, WriteReadTest) ASSERT_EQ(3, csv.getNArrays()); csv.addIndexVectorForWriting(str_vec.size()); ASSERT_EQ(4, csv.getNArrays()); - int result = csv.writeToFile(test_file); + int result = BaseLib::IO::writeStringToFile(csv.writeToString(), test_file); ASSERT_EQ(1, result); std::vector<std::string> str_result; diff --git a/Tests/FileIO_Qt/TestQtGmlInterface.cpp b/Tests/FileIO_Qt/TestQtGmlInterface.cpp index 6eaedba8178..e45f337faf8 100644 --- a/Tests/FileIO_Qt/TestQtGmlInterface.cpp +++ b/Tests/FileIO_Qt/TestQtGmlInterface.cpp @@ -30,7 +30,8 @@ TEST_F(TestGmlInterface, QtXmlGmlWriterReaderTest) GeoLib::IO::XmlGmlInterface xml(geo_objects); xml.export_name = geo_name; - int result = xml.writeToFile(test_data_file); + int result = + BaseLib::IO::writeStringToFile(xml.writeToString(), test_data_file); EXPECT_EQ(result, 1); // remove the written data from the data structures -- GitLab