From 36aa5b435408ca05f5acaf49764a55b15c32e3cc Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@uliege.be>
Date: Thu, 24 Sep 2020 10:38:54 +0200
Subject: [PATCH] remove last uses of add_infile()

---
 Fltk/graphicWindow.cpp     |  2 +-
 Fltk/visibilityWindow.cpp  | 23 ++++++-----------------
 Geo/GeoStringInterface.cpp | 28 +++++++++++++++++++++++++++-
 Geo/GeoStringInterface.h   |  5 ++++-
 4 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/Fltk/graphicWindow.cpp b/Fltk/graphicWindow.cpp
index 3baaa64ad6..c2ce39ace4 100644
--- a/Fltk/graphicWindow.cpp
+++ b/Fltk/graphicWindow.cpp
@@ -1245,7 +1245,7 @@ static void geometry_elementary_set_factory_cb(Fl_Widget *w, void *data)
 {
   if(!data) return;
   std::string str((const char*)data);
-  add_infile("SetFactory(\"" + str + "\");", GModel::current()->getFileName());
+  set_factory(str, GModel::current()->getFileName());
   if(FlGui::available())
     Msg::StatusBar(false, "Setting %s factory", str.c_str());
 }
diff --git a/Fltk/visibilityWindow.cpp b/Fltk/visibilityWindow.cpp
index 09d0c61163..2779201c73 100644
--- a/Fltk/visibilityWindow.cpp
+++ b/Fltk/visibilityWindow.cpp
@@ -823,41 +823,30 @@ static void visibility_save_cb(Fl_Widget *w, void *data)
   for(GModel::riter it = m->firstRegion(); it != m->lastRegion(); it++)
     (*it)->getVisibility() ? state[3][1].push_back((*it)->tag()) :
                              state[3][0].push_back((*it)->tag());
-  char tmp[256];
-  const char *labels[4] = {"Point", "Curve", "Surface", "Volume"};
-  std::string str;
-  int mode;
-  int on = 0, off = 0;
+  std::vector<std::pair<int, int> > entities;
+  int mode = 0, on = 0, off = 0;
   for(int i = 0; i < 4; i++) {
     on += state[i][1].size();
     off += state[i][0].size();
   }
   if(on > off) {
-    add_infile("Show \"*\";", GModel::current()->getFileName());
+    add_visibility_all(1, GModel::current()->getFileName());
     if(!off) return;
-    str += "Hide {\n";
     mode = 0;
   }
   else {
-    add_infile("Hide \"*\";", GModel::current()->getFileName());
+    add_visibility_all(0, GModel::current()->getFileName());
     if(!on) return;
-    str += "Show {\n";
     mode = 1;
   }
   for(int i = 0; i < 4; i++) {
     if(state[i][mode].size()) {
-      str += labels[i];
-      str += "{";
       for(std::size_t j = 0; j < state[i][mode].size(); j++) {
-        if(j) str += ",";
-        sprintf(tmp, "%d", state[i][mode][j]);
-        str += tmp;
+        entities.push_back(std::pair<int, int>(i, state[i][mode][j]));
       }
-      str += "};\n";
     }
   }
-  str += "}\n";
-  add_infile(str, GModel::current()->getFileName());
+  add_visibility(mode, entities, GModel::current()->getFileName());
   Msg::StatusBar(true, "Done appending visibility info");
 }
 
diff --git a/Geo/GeoStringInterface.cpp b/Geo/GeoStringInterface.cpp
index 73968135fa..04d3f570c9 100644
--- a/Geo/GeoStringInterface.cpp
+++ b/Geo/GeoStringInterface.cpp
@@ -23,7 +23,7 @@
 #include "onelab.h"
 #endif
 
-void add_infile(const std::string &text, const std::string &fileNameOrEmpty)
+static void add_infile(const std::string &text, const std::string &fileNameOrEmpty)
 {
   const std::string &fileName = fileNameOrEmpty;
   if(fileName.empty()) {
@@ -178,6 +178,13 @@ static void check_occ(std::ostringstream &sstream)
 #endif
 }
 
+void set_factory(const std::string &factory, const std::string &fileName)
+{
+  std::ostringstream sstream;
+  sstream << "SetFactory(\"" << factory << "\");";
+  add_infile(sstream.str(), fileName);
+}
+
 void add_charlength(const std::string &fileName, const std::vector<int> &l,
                     const std::string &lc)
 {
@@ -722,3 +729,22 @@ void delete_entities(const std::string &fileName,
   sstream << "Delete {\n  " << dimTags2String(l) << "\n}";
   add_infile(sstream.str(), fileName);
 }
+
+void add_visibility_all(int mode, const std::string &fileName)
+{
+  if(mode)
+    add_infile("Show \"*\";", fileName);
+  else
+    add_infile("Hide \"*\";", fileName);
+}
+
+void add_visibility(int mode, const std::vector<std::pair<int, int> > &l,
+                    const std::string &fileName)
+{
+  std::ostringstream sstream;
+  if(mode)
+    sstream << "Show {\n  " << dimTags2String(l) << "\n}";
+  else
+    sstream << "Hide {\n  " << dimTags2String(l) << "\n}";
+  add_infile(sstream.str(), fileName);
+}
diff --git a/Geo/GeoStringInterface.h b/Geo/GeoStringInterface.h
index adcf360c50..877ebe68e7 100644
--- a/Geo/GeoStringInterface.h
+++ b/Geo/GeoStringInterface.h
@@ -10,7 +10,7 @@
 #include <vector>
 #include "ListUtils.h"
 
-void add_infile(const std::string &text, const std::string &fileName);
+void set_factory(const std::string &factory, const std::string &fileName);
 void add_charlength(const std::string &fileName, const std::vector<int> &l,
                     const std::string &lc);
 void add_recosurf(const std::string &fileName, const std::vector<int> &l);
@@ -131,5 +131,8 @@ void coherence(const std::string &fileName);
 void delete_entities(const std::string &fileName,
                      const std::vector<std::pair<int, int> > &l,
                      bool recursive);
+void add_visibility_all(int mode, const std::string &fileName);
+void add_visibility(int mode, const std::vector<std::pair<int, int> > &l,
+                    const std::string &fileName);
 
 #endif
-- 
GitLab