From e6bcf895e116e285b998dd115da452fd6bc41e55 Mon Sep 17 00:00:00 2001
From: Norihiro Watanabe <norihiro.watanabe@ufz.de>
Date: Thu, 25 Oct 2012 23:55:10 +0200
Subject: [PATCH] add some functions to manipulate file paths and to write
 files

---
 BaseLib/FileTools.h | 82 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 80 insertions(+), 2 deletions(-)

diff --git a/BaseLib/FileTools.h b/BaseLib/FileTools.h
index 1020374ad38..831b2ce2893 100644
--- a/BaseLib/FileTools.h
+++ b/BaseLib/FileTools.h
@@ -15,14 +15,15 @@
 #ifndef FILETOOLS_H
 #define FILETOOLS_H
 
-// ** INCLUDES **
+#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(std::string strFilename)
+static bool IsFileExisting(const std::string &strFilename)
 {
 	struct stat stFileInfo;
 	bool blnReturn;
@@ -51,6 +52,83 @@ static bool IsFileExisting(std::string strFilename)
 	return(blnReturn);
 }
 
+/**
+ * \brief return a directory path
+ */
+static std::string getFileDirecotryPath(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(0,indexChWin); // + "\\";
+    else if(indexChLinux != std::string::npos)
+        dir_path = file_path.substr(0,indexChLinux); // + "/";
+
+    return dir_path;
+}
+
+/**
+ * \brief return a file base 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);
+}
+
+/**
+ * \brief write value in binary
+ */
+template <typename T> void write_value_binary(std::fstream &fin, T val)
+{
+    fin.write((const char*)&val, sizeof(T));
+}
+
+/**
+ * \brief truncate a file
+ */
+static void truncateFile(const std::string &filename)
+{
+    std::ofstream ofs;
+    ofs.open(filename.c_str(), std::ios_base::trunc);
+    ofs.close();
+}
+
 } // end namespace BaseLib
 
 #endif // FILETOOLS_H
-- 
GitLab