From 89b0f03a283d7caea66819473a4f232665319332 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Wed, 7 Jan 2009 12:25:21 +0000
Subject: [PATCH] more std::string

---
 Common/GmshMessage.cpp |  2 +-
 Common/OS.cpp          | 24 ++++++++++++------------
 Common/OS.h            |  7 +++----
 Common/OpenFile.cpp    |  4 +---
 Fltk/menuWindow.cpp    | 14 +++++++-------
 Fltk/messageWindow.cpp |  2 +-
 Fltk/optionWindow.cpp  |  4 ++--
 7 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/Common/GmshMessage.cpp b/Common/GmshMessage.cpp
index b5a871779d..a067e4b5cf 100644
--- a/Common/GmshMessage.cpp
+++ b/Common/GmshMessage.cpp
@@ -76,7 +76,7 @@ void Msg::Init(int argc, char **argv)
 void Msg::Exit(int level)
 {
   // delete the temp file
-  if(!_commRank) UnlinkFile((CTX.home_dir + CTX.tmp_filename).c_str());
+  if(!_commRank) UnlinkFile(CTX.home_dir + CTX.tmp_filename);
 
   // exit directly on abnormal program termination (level != 0). We
   // used to call abort() to flush open streams, but on modern OSes
diff --git a/Common/OS.cpp b/Common/OS.cpp
index 32777c911e..c0855f3596 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -59,7 +59,7 @@ void SleepInSeconds(double s)
 #endif
 }
 
-void GetResources(double *s, long *mem)
+static void GetResources(double *s, long *mem)
 {
 #if !defined(WIN32) || defined(__CYGWIN__)
   static struct rusage r;
@@ -118,23 +118,23 @@ std::string GetHostName()
   return std::string(host);
 }
 
-int UnlinkFile(const char *filename)
+int UnlinkFile(std::string fileName)
 {
 #if !defined(WIN32) || defined(__CYGWIN__)
-  return unlink(filename);
+  return unlink(fileName.c_str());
 #else
-  return _unlink(filename);
+  return _unlink(fileName.c_str());
 #endif
 }
 
-int StatFile(const char *filename)
+int StatFile(std::string fileName)
 {
 #if !defined(WIN32) || defined(__CYGWIN__)
   struct stat buf;
-  return stat(filename, &buf);
+  return stat(fileName.c_str(), &buf);
 #else
   struct _stat buf;
-  return _stat(filename, &buf);
+  return _stat(fileName.c_str(), &buf);
 #endif
 }
 
@@ -153,15 +153,15 @@ int KillProcess(int pid)
   return 1;
 }
 
-int SystemCall(const char *command)
+int SystemCall(std::string command)
 {
 #if defined(WIN32)
   STARTUPINFO suInfo;
   PROCESS_INFORMATION prInfo;
   memset(&suInfo, 0, sizeof(suInfo));
   suInfo.cb = sizeof(suInfo);
-  Msg::Info("Calling '%s'", command);
-  CreateProcess(NULL, (char*)command, NULL, NULL, FALSE,
+  Msg::Info("Calling '%s'", command.c_str());
+  CreateProcess(NULL, (char*)command.c_str(), NULL, NULL, FALSE,
                 NORMAL_PRIORITY_CLASS, NULL, NULL, &suInfo, &prInfo);
   return 0;
 #else
@@ -169,8 +169,8 @@ int SystemCall(const char *command)
     Msg::Error("Could not find /bin/sh: aborting system call");
     return 1;
   }
-  Msg::Info("Calling '%s'", command);
-  return system(command);
+  Msg::Info("Calling '%s'", command.c_str());
+  return system(command.c_str());
 #endif
 }
 
diff --git a/Common/OS.h b/Common/OS.h
index 727087c9a3..d70bb523ee 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -10,14 +10,13 @@
 
 double GetTimeInSeconds();
 void SleepInSeconds(double s);
-void GetResources(double *s, long *mem);
 void CheckResources();
 double Cpu();
 int GetProcessId();
 std::string GetHostName();
-int UnlinkFile(const char *name);
-int StatFile(const char *filename);
+int UnlinkFile(std::string fileName);
+int StatFile(std::string fileName);
 int KillProcess(int pid);
-int SystemCall(const char *command);
+int SystemCall(std::string command);
 
 #endif
diff --git a/Common/OpenFile.cpp b/Common/OpenFile.cpp
index e9172c8690..8f0a86f9c7 100644
--- a/Common/OpenFile.cpp
+++ b/Common/OpenFile.cpp
@@ -249,9 +249,7 @@ int MergeFile(std::string fileName, bool warnIfMissing)
       // terms of gzFile, but until then, this is better than nothing
       if(fl_choice("File '%s' is in gzip format.\n\nDo you want to uncompress it?", 
                    "Cancel", "Uncompress", NULL, fileName.c_str())){
-        char tmp[256];
-        sprintf(tmp, "gunzip -c %s > %s", fileName.c_str(), no_ext);
-        if(SystemCall(tmp))
+        if(SystemCall(std::string("gunzip -c ") + fileName + " > " + no_ext))
           Msg::Error("Failed to uncompress `%s': check directory permissions", 
                      fileName.c_str());
         SetProjectName(no_ext);
diff --git a/Fltk/menuWindow.cpp b/Fltk/menuWindow.cpp
index eeeca7afad..ec18c3321c 100644
--- a/Fltk/menuWindow.cpp
+++ b/Fltk/menuWindow.cpp
@@ -55,10 +55,10 @@ static void file_new_cb(Fl_Widget *w, void *data)
  test:
   if(file_chooser(0, 1, "New", "*")) {
     std::string name = file_chooser_get_name(1);
-    if(!StatFile(name.c_str())){
+    if(!StatFile(name)){
       if(fl_choice("File '%s' already exists.\n\nDo you want to erase it?",
                    "Cancel", "Erase", NULL, name.c_str()))
-        UnlinkFile(name.c_str());
+        UnlinkFile(name);
       else
         goto test;
     }
@@ -299,7 +299,7 @@ static void file_save_as_cb(Fl_Widget *w, void *data)
   if(file_chooser(0, 1, "Save As", pat)) {
     std::string name = file_chooser_get_name(1);
     if(CTX.confirm_overwrite) {
-      if(!StatFile(name.c_str()))
+      if(!StatFile(name))
         if(!fl_choice("File '%s' already exists.\n\nDo you want to replace it?", 
                       "Cancel", "Replace", NULL, name.c_str()))
           goto test;
@@ -323,7 +323,7 @@ static void file_rename_cb(Fl_Widget *w, void *data)
   if(file_chooser(0, 1, "Rename", "*", GModel::current()->getFileName().c_str())) {
     std::string name = file_chooser_get_name(1);
     if(CTX.confirm_overwrite) {
-      if(!StatFile(name.c_str()))
+      if(!StatFile(name))
         if(!fl_choice("File '%s' already exists.\n\nDo you want to replace it?", 
                       "Cancel", "Replace", NULL, name.c_str()))
           goto test;
@@ -1441,7 +1441,7 @@ static void mesh_save_cb(Fl_Widget *w, void *data)
   std::string name = CTX.output_filename;
   if(name.empty()) name = GetDefaultFileName(CTX.mesh.format);
   if(CTX.confirm_overwrite) {
-    if(!StatFile(name.c_str()))
+    if(!StatFile(name))
       if(!fl_choice("File '%s' already exists.\n\nDo you want to replace it?",
                     "Cancel", "Replace", NULL, name.c_str()))
         return;
@@ -1904,7 +1904,7 @@ static void view_reload(int index)
   if(index >= 0 && index < (int)PView::list.size()){
     PView *p = PView::list[index];
 
-    if(StatFile(p->getData()->getFileName().c_str())){
+    if(StatFile(p->getData()->getFileName())){
       Msg::Error("File '%s' does not exist", p->getData()->getFileName().c_str());
       return;
     }
@@ -2010,7 +2010,7 @@ static void view_save_as(int index, const char *title, int format)
   if(file_chooser(0, 1, title, "*", view->getData()->getFileName().c_str())){
     std::string name = file_chooser_get_name(1);
     if(CTX.confirm_overwrite) {
-      if(!StatFile(name.c_str()))
+      if(!StatFile(name))
         if(!fl_choice("File '%s' already exists.\n\nDo you want to replace it?",
                       "Cancel", "Replace", NULL, name.c_str()))
           goto test;
diff --git a/Fltk/messageWindow.cpp b/Fltk/messageWindow.cpp
index 5bdc3cdf67..41d2a6fdb7 100644
--- a/Fltk/messageWindow.cpp
+++ b/Fltk/messageWindow.cpp
@@ -56,7 +56,7 @@ static void message_save_cb(Fl_Widget *w, void *data)
   if(file_chooser(0, 1, "Save", "*")) {
     std::string name = file_chooser_get_name(1);
     if(CTX.confirm_overwrite) {
-      if(!StatFile(name.c_str()))
+      if(!StatFile(name))
         if(!fl_choice("File '%s' already exists.\n\nDo you want to replace it?", 
                       "Cancel", "Replace", NULL, name.c_str()))
           goto test;
diff --git a/Fltk/optionWindow.cpp b/Fltk/optionWindow.cpp
index 026df7adde..4ddf7926bf 100644
--- a/Fltk/optionWindow.cpp
+++ b/Fltk/optionWindow.cpp
@@ -153,8 +153,8 @@ void options_save_cb(Fl_Widget *w, void *data)
 static void options_restore_defaults_cb(Fl_Widget *w, void *data)
 {
   // not sure if we have to remove the file...
-  UnlinkFile((CTX.home_dir + CTX.session_filename).c_str());
-  UnlinkFile((CTX.home_dir + CTX.options_filename).c_str());
+  UnlinkFile(CTX.home_dir + CTX.session_filename);
+  UnlinkFile(CTX.home_dir + CTX.options_filename);
   ReInit_Options(0);
   Init_Options_GUI(0);
   if(GUI::instance()->menu->module->value() == 3) // hack to refresh the buttons
-- 
GitLab