From 91ad292dda42d7ecaece9013c288410aa0ae1322 Mon Sep 17 00:00:00 2001
From: Norihiro Watanabe <norihiro.watanabe@ufz.de>
Date: Tue, 30 Oct 2012 09:29:45 +0100
Subject: [PATCH] follow the review comments

- remove functions to manipulate file names because they exist in StringTools.h
- move implementations into cpp
- fix type, etc.
---
 BaseLib/FileTools.cpp |  63 ++++++++++++++++++++++++
 BaseLib/FileTools.h   | 112 ++++++------------------------------------
 2 files changed, 78 insertions(+), 97 deletions(-)
 create mode 100644 BaseLib/FileTools.cpp

diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp
new file mode 100644
index 00000000000..16d0ee3d59c
--- /dev/null
+++ b/BaseLib/FileTools.cpp
@@ -0,0 +1,63 @@
+/**
+ * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.org/project/license
+ *
+ *
+ * \file FileTools.cpp
+ *
+ * Created on 2010-04-26 by Lars Bilke
+ *
+ */
+
+#include "FileTools.h"
+
+#include <sys/stat.h>
+
+namespace BaseLib
+{
+
+/**
+ * Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html
+ */
+bool IsFileExisting(const std::string &strFilename)
+{
+	struct stat stFileInfo;
+	bool blnReturn;
+	int intStat;
+
+	// Attempt to get the file attributes
+	intStat = stat(strFilename.c_str(),&stFileInfo);
+
+	if(intStat == 0)
+	{
+		// We were able to get the file attributes
+		// so the file obviously exists.
+		blnReturn = true;
+	}
+	else
+	{
+		// We were not able to get the file attributes.
+		// This may mean that we don't have permission to
+		// access the folder which contains this file. If you
+		// need to do that level of checking, lookup the
+		// return values of stat which will give you
+		// more details on why stat failed.
+		blnReturn = false;
+	}
+
+	return(blnReturn);
+}
+
+/**
+ * \brief truncate a file
+ */
+void truncateFile( std::string const& filename)
+{
+    std::ofstream ofs(filename.c_str(), std::ios_base::trunc);
+    ofs.close();
+}
+
+} // end namespace BaseLib
+
diff --git a/BaseLib/FileTools.h b/BaseLib/FileTools.h
index 831b2ce2893..2858e11f79b 100644
--- a/BaseLib/FileTools.h
+++ b/BaseLib/FileTools.h
@@ -17,117 +17,35 @@
 
 #include <string>
 #include <fstream>
-#include <sys/stat.h>
 
-namespace BaseLib {
-/**
- * Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html
- */
-static bool IsFileExisting(const std::string &strFilename)
-{
-	struct stat stFileInfo;
-	bool blnReturn;
-	int intStat;
-
-	// Attempt to get the file attributes
-	intStat = stat(strFilename.c_str(),&stFileInfo);
-
-	if(intStat == 0)
-	{
-		// We were able to get the file attributes
-		// so the file obviously exists.
-		blnReturn = true;
-	}
-	else
-	{
-		// We were not able to get the file attributes.
-		// This may mean that we don't have permission to
-		// access the folder which contains this file. If you
-		// need to do that level of checking, lookup the
-		// return values of stat which will give you
-		// more details on why stat failed.
-		blnReturn = false;
-	}
-
-	return(blnReturn);
-}
-
-/**
- * \brief return a directory path
- */
-static std::string getFileDirecotryPath(const std::string &file_path)
+namespace BaseLib
 {
-    std::size_t indexChWin, indexChLinux;
-    indexChWin = indexChLinux = 0;
-    indexChWin = file_path.find_last_of('\\');
-    indexChLinux = file_path.find_last_of('/');
-    //
-    std::string dir_path;
-    if(indexChWin != std::string::npos)
-        dir_path = file_path.substr(0,indexChWin); // + "\\";
-    else if(indexChLinux != std::string::npos)
-        dir_path = file_path.substr(0,indexChLinux); // + "/";
-
-    return dir_path;
-}
 
 /**
- * \brief return a file base name
+ * \brief Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html
+ *
+ * \param strFilename         the file name
  */
-static std::string getFileBaseName(const std::string &file_path)
-{
-    std::size_t indexChWin, indexChLinux;
-    indexChWin = indexChLinux = 0;
-    indexChWin = file_path.find_last_of('\\');
-    indexChLinux = file_path.find_last_of('/');
-    //
-    std::string dir_path;
-    if(indexChWin != std::string::npos)
-        dir_path = file_path.substr(indexChWin+1, file_path.length());
-    else if(indexChLinux != std::string::npos)
-        dir_path = file_path.substr(indexChLinux+1, file_path.length());
-    else
-        dir_path = file_path;
-
-    return dir_path;
-}
-
-/**
- * \brief return a file name with or without file extensions
- */ 
-static std::string getFileNameFromPath(const std::string &str, bool with_extension)
-{
-    std::string::size_type beg1 = str.find_last_of('/');
-    std::string::size_type beg2 = str.find_last_of('\\');
-    std::string::size_type beg;
-    if (beg1 == std::string::npos && beg2 == std::string::npos) beg = -1;
-    else if (beg1 == std::string::npos) beg = beg2;
-    else if (beg2 == std::string::npos) beg = beg1;
-    else beg = (beg1<beg2) ? beg2 : beg1;
-    std::string file ( str.substr(beg+1) );
-    if (with_extension) return file;
-    // cut extension
-    std::string::size_type end  = file.find_last_of('.');
-    return file.substr(0,end);
-}
+bool IsFileExisting(const std::string &strFilename);
 
 /**
- * \brief write value in binary
+ * \brief write value as binary into the given output stream
+ *
+ * \tparam T    data type of the value
+ * \param out   output stream
+ * \param val   value
  */
-template <typename T> void write_value_binary(std::fstream &fin, T val)
+template <typename T> void writeValueBinary(std::ostream &out, T const& val)
 {
-    fin.write((const char*)&val, sizeof(T));
+    out.write((const char*)&val, sizeof(T));
 }
 
 /**
  * \brief truncate a file
+ *
+ * \param file_path         the file name
  */
-static void truncateFile(const std::string &filename)
-{
-    std::ofstream ofs;
-    ofs.open(filename.c_str(), std::ios_base::trunc);
-    ofs.close();
-}
+void truncateFile( std::string const& file_path);
 
 } // end namespace BaseLib
 
-- 
GitLab