Salome HOME
Merge branch 'V9_7_BR'
[modules/kernel.git] / src / Basics / Basics_DirUtils.cxx
index daa4b8f55567d6ed3cb697e7a232b363b17baedd..01d32f935d8902f17bf74715fdbf756af696504b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -27,6 +27,8 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include <algorithm>
+
 #ifndef WIN32
 # include <sys/stat.h>
 # include <dirent.h>
@@ -52,22 +54,22 @@ namespace Kernel_Utils
   std::string GetBaseName( const std::string& file_path, const bool with_extension )
   {
     std::string tmp_str = file_path;
-    int pos = file_path.rfind( _separator_ );
-    if ( pos >= 0 )
-      tmp_str = pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
+    auto pos = file_path.rfind( _separator_ );
+    if ( pos != std::string::npos )
+      tmp_str = pos < file_path.size()-1 ? file_path.substr( pos+1 ) : "";
 
     pos = tmp_str.rfind( _extension_ );
-    if( !with_extension && pos >= 0 )
-      tmp_str = pos < (int)tmp_str.size()-1 ? tmp_str.substr( 0, pos ) : "";
+    if( !with_extension && pos != std::string::npos )
+      tmp_str = pos < tmp_str.size()-1 ? tmp_str.substr( 0, pos ) : "";
 
     return tmp_str;
   }
 
   std::string GetDirName( const std::string& file_path )
   {
-    int pos = file_path.rfind( _separator_ );
-    if ( pos >= 0 )
-      return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
+    auto pos = file_path.rfind( _separator_ );
+    if ( pos != std::string::npos )
+      return pos < file_path.size()-1 ? file_path.substr(0, pos ) : "";
     return std::string(".");
   }
 
@@ -188,8 +190,8 @@ namespace Kernel_Utils
   std::string AddExtension( const std::string& name )
   {
     std::string tmp_str = name;
-    int pos = tmp_str.rfind( _extension_ );
-    if( pos < 0 )
+    auto pos = tmp_str.rfind( _extension_ );
+    if( pos == std::string::npos )
       return tmp_str.append( _extension_ );
     return tmp_str;
   }
@@ -339,4 +341,28 @@ namespace Kernel_Utils
 #endif
     return result;
   }
+
+  //============================================================================
+  // function : BackSlashToSlash  
+  // purpose  : Convert back slash to slash
+  //============================================================================ 
+  std::string BackSlashToSlash(const std::string& path) {
+         std::string res = path;
+         std::replace(res.begin(), res.end(), '\\', '/');
+         return res;
+  }
+
+
+  //============================================================================
+  // function : BackSlashToSlash  
+  // purpose  : Convert back slash to slash
+  //============================================================================ 
+  std::string HomePath() {
+#ifdef WIN32
+    std::string homedir = getenv("USERPROFILE");
+#else
+    std::string homedir = getenv("HOME");       
+#endif
+    return homedir;
+  }
 }