From f9f57516e4f14d2c2c5e88420b73987eab0d84c0 Mon Sep 17 00:00:00 2001 From: abd Date: Thu, 9 Oct 2008 10:39:45 +0000 Subject: [PATCH] Implementations of common functions in Kernel_Utils namespace: -GetDirByPath: parse in path string a valid directory name -IsEmptyDir: detect of empty directory status --- src/Basics/Basics_DirUtils.cxx | 115 +++++++++++++++++++++++++++++++-- src/Basics/Basics_DirUtils.hxx | 8 +++ 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/src/Basics/Basics_DirUtils.cxx b/src/Basics/Basics_DirUtils.cxx index 3cb8e4dd6..ef43f4890 100644 --- a/src/Basics/Basics_DirUtils.cxx +++ b/src/Basics/Basics_DirUtils.cxx @@ -29,6 +29,7 @@ #ifndef WIN32 # include +# include #else # include # include @@ -108,13 +109,17 @@ namespace Kernel_Utils //============================================================================ // function : GetTempDir - // purpose : Return a temp directory to store created files like "/tmp/sub_dir/" + // purpose : Returns a temp directory to store created files like "/tmp/sub_dir/" //============================================================================ 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() { string tmpDir = GetTmpDir(); @@ -136,7 +141,10 @@ namespace Kernel_Utils return aFilePath; } - + //============================================================================ + // function : IsExists + // purpose : Returns True(False) if the path (not)exists + //============================================================================ bool IsExists(const string& thePath) { #ifdef WIN32 @@ -151,5 +159,104 @@ namespace Kernel_Utils #endif return true; } - + + //============================================================================ + // function : GetDirByPath + // purpose : Returns directory by path and converts it to native system format + //============================================================================ + string GetDirByPath(const string& thePath) + { + if (thePath.empty()) + return ""; + string path = thePath; + string::size_type length = path.length(); + + //detect all separators in Unix format + for ( int i = 0; i < length; i++ ) + { + if( path[i] == '/' ) + path[i] = '|'; + } + + //detect all separators in Windows format + for ( int i = 0; i < length; i++ ) + { + if( path[i] == '\\' ) + path[i] = '|'; + } + + + string::size_type pos = path.rfind('|'); + if ( pos == string::npos ) + { +#ifdef WIN32 + //check for disk letter ( C: ) + if ( path.length() == 2 && path[1] == ':' ) + path += _separator_; +#else + //not valid path + return ""; +#endif + } + else + { + //remove right subdirectory or filename from path + path = path.substr( 0, pos ); + } + + length = path.length(); + for ( int i = 0; i < length; i++ ) + { + if( path[i] == '|' ) + path[i] = _separator_; + } + return path; + } + + //============================================================================ + // function : IsEmptyDir + // purpose : Returns True(False) if the path (not) empty + // Also returns False if the path is not valid + //============================================================================ + bool IsEmptyDir(const string& thePath) + { + if ( thePath.empty() || !IsExists(thePath)) + return false; + + bool result = false; + +#ifdef WIN32 + WIN32_FIND_DATA aFileData; + HANDLE hFile = FindFirstFile( path.c_str(), &aFileData ); + if ( hFile == INVALID_HANDLE_VALUE ) + { + //empty dir + result = true; + } + else + { + //close serching. path is not empty + FindClose( hFile ); + } +#else + DIR *dp; + struct dirent *dirp; + if((dp = opendir(thePath.c_str())) == NULL) + { + //Could not open directory + return false; + } + else + { + result = true; //empty if no file found + while ((dirp = readdir(dp)) != NULL && result ) + { + string file_name(dirp->d_name); + result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false + } + closedir(dp); + } +#endif + return result; + } } diff --git a/src/Basics/Basics_DirUtils.hxx b/src/Basics/Basics_DirUtils.hxx index 90e42b57f..5a64a84f9 100644 --- a/src/Basics/Basics_DirUtils.hxx +++ b/src/Basics/Basics_DirUtils.hxx @@ -46,12 +46,20 @@ namespace Kernel_Utils // /tmp/something/ for Unix or c:\something\ for WIN32 BASICS_EXPORT std::string GetTmpDir(); + // Returns the unique temporary file name without any extension // /tmp/something/file for Unix or c:\something\file for WIN32 BASICS_EXPORT std::string GetTmpFileName(); // Returns True(False) if the path (not)exists BASICS_EXPORT bool IsExists( const std::string& path ); + + // Returns directory by path and converts it to native system format + BASICS_EXPORT std::string GetDirByPath( const std::string& path ); + + // Returns True(False) if the path (not) empty + // Also returns False if the path is not valid + BASICS_EXPORT bool IsEmptyDir( const std::string& path ); } #endif -- 2.39.2