Salome HOME
ADD a end user module (services.py) to help the manipulation of SALOME KERNEL service...
[modules/kernel.git] / src / Basics / Basics_DirUtils.cxx
index e6e6deb29d616d7df62d00f399f463a049be3a39..4ff026829a1b8fe48b786e27edb753bb2b25b3b6 100644 (file)
@@ -1,20 +1,20 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  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
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 //  File   : Basics_DirUtils.cxx
@@ -50,11 +50,18 @@ namespace Kernel_Utils
     return file_path;
   }
 
+  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 ) : "";
+    return std::string(".");
+  }
+
   std::string GetTmpDirByEnv( const std::string& tmp_path_env )
   {
-    std::string dir;
     char* val = getenv( tmp_path_env.c_str() );
-    val ? dir = std::string( val ) : "";
+    std::string dir = val ? val : "";
     return GetTmpDirByPath( dir );
   }
 
@@ -165,6 +172,26 @@ namespace Kernel_Utils
     return true;
   }
 
+  //============================================================================
+  // function : IsWritable
+  // purpose  : Returns True(False) if the path is (not) writable
+  //============================================================================ 
+  bool IsWritable(const std::string& thePath) 
+  {
+#ifdef WIN32 
+    if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
+      if (  GetLastError () == FILE_ATTRIBUTE_READONLY ) {
+        return false;
+      }
+    }
+#else 
+    int status = access(thePath.c_str(),W_OK); 
+    if (status != 0) return false;
+#endif
+    return true;
+  }
+
+
   //============================================================================
   // function : GetDirByPath
   // purpose  : Returns directory by path and converts it to native system format