-// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
//
-// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// 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, or (at your option) any later version.
//
-// 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
// Autor : Alexander A. BORODIN
// Module : SALOME
//
#include "Basics_DirUtils.hxx"
-
+#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifndef WIN32
# include <sys/stat.h>
# include <dirent.h>
+# include <unistd.h>
#else
# include <windows.h>
# include <time.h>
#endif
-using namespace std;
-
#ifdef WIN32
# define _separator_ '\\'
#else
namespace Kernel_Utils
{
- string GetBaseName( const std::string& file_path )
+ std::string GetBaseName( const std::string& file_path )
{
int pos = file_path.rfind( _separator_ );
if ( pos >= 0 )
- return pos < file_path.size()-1 ? file_path.substr( pos+1 ) : "";
+ return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
return file_path;
}
- string GetTmpDirByEnv( const std::string& tmp_path_env )
+ 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 )
{
- string dir;
char* val = getenv( tmp_path_env.c_str() );
- val ? dir = string( val ) : "";
+ std::string dir = val ? val : "";
return GetTmpDirByPath( dir );
}
- string GetTmpDirByPath( const std::string& tmp_path )
+ std::string GetTmpDirByPath( const std::string& tmp_path )
{
- string aTmpDir = tmp_path;
+ std::string aTmpDir = tmp_path;
if ( aTmpDir == "" )
{
#ifdef WIN32
{
Tmp_dir = getenv("TMP");
if (Tmp_dir == NULL)
- aTmpDir = string("C:\\");
+ aTmpDir = std::string("C:\\");
else
- aTmpDir = string(Tmp_dir);
+ aTmpDir = std::string(Tmp_dir);
}
else
- aTmpDir = string(Tmp_dir);
+ aTmpDir = std::string(Tmp_dir);
#else
- aTmpDir = string("/tmp/");
+ aTmpDir = std::string("/tmp/");
#endif
}
int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
char buffer[127];
sprintf(buffer, "%d", aRND);
- string aSubDir(buffer);
- if(aSubDir.size() <= 1) aSubDir = string("123409876");
+ std::string aSubDir(buffer);
+ if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
aTmpDir += aSubDir; //Get RND sub directory
- string aDir = aTmpDir;
+ std::string aDir = aTmpDir;
if(IsExists(aDir)) {
for(aRND = 0; IsExists(aDir); aRND++) {
// function : GetTempDir
// purpose : Returns a temp directory to store created files like "/tmp/sub_dir/"
//============================================================================
- string GetTmpDir()
+ std::string GetTmpDir()
{
return GetTmpDirByPath( "" );
}
// function : GetTempFileName
// purpose : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
//============================================================================
- string GetTmpFileName()
+ std::string GetTmpFileName()
{
- string tmpDir = GetTmpDir();
- string aFilePath = "";
+ std::string tmpDir = GetTmpDir();
+ std::string aFilePath = "";
if(IsExists(tmpDir)) {
srand((unsigned int)time(NULL));
int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
char buffer[127];
sprintf(buffer, "%d", aRND);
- string aSubDir(buffer);
- if(aSubDir.size() <= 1) aSubDir = string("123409876");
+ std::string aSubDir(buffer);
+ if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
aFilePath = tmpDir;
for(aRND = 0; IsExists(aFilePath); aRND++) {
// function : IsExists
// purpose : Returns True(False) if the path (not)exists
//============================================================================
- bool IsExists(const string& thePath)
+ bool IsExists(const std::string& thePath)
{
#ifdef WIN32
if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
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
//============================================================================
- string GetDirByPath(const string& thePath)
+ std::string GetDirByPath(const std::string& thePath)
{
if (thePath.empty())
return "";
- string path = thePath;
- string::size_type length = path.length();
+ std::string path = thePath;
+ std::string::size_type length = path.length();
//detect all separators in Unix format
- for ( int i = 0; i < length; i++ )
+ for ( unsigned int i = 0; i < length; i++ )
{
if( path[i] == '/' )
path[i] = '|';
}
//detect all separators in Windows format
- for ( int i = 0; i < length; i++ )
+ for ( unsigned int i = 0; i < length; i++ )
{
if( path[i] == '\\' )
path[i] = '|';
}
- string::size_type pos = path.rfind('|');
- if ( pos == string::npos )
+ std::string::size_type pos = path.rfind('|');
+ if ( pos == std::string::npos )
{
#ifdef WIN32
//check for disk letter ( C: )
}
length = path.length();
- for ( int i = 0; i < length; i++ )
+ for ( unsigned int i = 0; i < length; i++ )
{
if( path[i] == '|' )
path[i] = _separator_;
// purpose : Returns True(False) if the path (not) empty
// Also returns False if the path is not valid
//============================================================================
- bool IsEmptyDir(const string& thePath)
+ bool IsEmptyDir(const std::string& thePath)
{
if ( thePath.empty() || !IsExists(thePath))
return false;
result = true; //empty if no file found
while ((dirp = readdir(dp)) != NULL && result )
{
- string file_name(dirp->d_name);
+ std::string file_name(dirp->d_name);
result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
}
closedir(dp);