diff --git a/Code/Common/otbSystem.cxx b/Code/Common/otbSystem.cxx index 4ad545a5b2ca06c564090def4cded346f3ca0998..8a287eba38deacb2e3b2097bb9dd977bb50db109 100755 --- a/Code/Common/otbSystem.cxx +++ b/Code/Common/otbSystem.cxx @@ -77,6 +77,11 @@ System::GetRootName( const std::string& filename ) return( filename ); } +bool System::IsAFileName(std::string pszPath) +{ + return( ! IsADirName(pszPath) ); +} + #if defined(WIN32) || defined(WIN32CE) @@ -84,6 +89,32 @@ System::GetRootName( const std::string& filename ) WIN32 / MSVC++ implementation *====================================================================*/ +bool System::IsADirName(std::string pszPath) +{ + struct _finddata_t c_file; + long hFile; + bool isADir(false); + std::string pszFileSpec; + std::string path(pszPath); + + if (pszPath.empty() == true) + path = "."; + + pszFileSpec = path + "\\*.*"; + + if ( (hFile = _findfirst( pszFileSpec.c_str(), &c_file )) != -1L ) + { + isADir = true; + _findclose( hFile ); + } + else + { + isADir = false; + } + + return isADir; +} + std::vector<std::string> System::Readdir(std::string pszPath) { struct _finddata_t c_file; @@ -116,6 +147,29 @@ std::vector<std::string> System::Readdir(std::string pszPath) POSIX (Unix) implementation *====================================================================*/ +bool System::IsADirName(std::string pszPath) +{ + bool isADir(false); + DIR *hDir; + struct dirent *psDirEntry; + std::string path(pszPath); + + if (pszPath.empty() == true) + path = "."; + + if ( (hDir = opendir(path.c_str())) != NULL ) + { + isADir = true; + closedir( hDir ); + } + else + { + isADir = false; + } + + return isADir; +} + /** * Read names in a directory. * diff --git a/Code/Common/otbSystem.h b/Code/Common/otbSystem.h index 03085ed782a6ec698610ef3d2cd73c74823583ba..2def5fe13cabcfa7edce64ed000963744117cfb0 100755 --- a/Code/Common/otbSystem.h +++ b/Code/Common/otbSystem.h @@ -40,6 +40,10 @@ public: /** Standard class typedefs. */ typedef System Self; + /** Return true if the parameter 'pszPath' specify a directory name) */ + static bool IsADirName(std::string pszPath); + /** Return true if the parameter 'pszPath' specify a file name) */ + static bool IsAFileName(std::string pszPath); /** Get the extension of the file name */ static std::string GetExtension( const std::string& filename ); /** Get the root name */ diff --git a/Code/IO/otbImageFileReader.h b/Code/IO/otbImageFileReader.h index 5839720ff0841dbcb477b78b31474b501b9cf91a..50b4a223fb2929b9771e412d8402ade063a325ea 100755 --- a/Code/IO/otbImageFileReader.h +++ b/Code/IO/otbImageFileReader.h @@ -85,6 +85,13 @@ protected: private: + /** Test whether the given filename exist and it is readable, + this is intended to be called before attempting to use + ImageIO classes for actually reading the file. If the file + doesn't exist or it is not readable, and exception with an + approriate message will be thrown. */ + void TestFileExistanceAndReadability(); + ImageFileReader(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented diff --git a/Code/IO/otbImageFileReader.txx b/Code/IO/otbImageFileReader.txx index 770ab0f1b04429481446524e8db33087ca63752e..24c90d67d6bdae980c2972bf4a8c15a8685a96fc 100755 --- a/Code/IO/otbImageFileReader.txx +++ b/Code/IO/otbImageFileReader.txx @@ -26,6 +26,7 @@ #include "itkVectorImage.h" #include "otbMacro.h" +#include "otbSystem.h" #include "otbImageIOFactory.h" #include <itksys/SystemTools.hxx> @@ -347,6 +348,45 @@ ImageFileReader<TOutputImage> } +template <class TOutputImage> +void +ImageFileReader<TOutputImage> +::TestFileExistanceAndReadability() +{ + // Test if the file exists. + if( ! itksys::SystemTools::FileExists( this->m_FileName.c_str() ) ) + { + itk::ImageFileReaderException e(__FILE__, __LINE__); + itk::OStringStream msg; + msg <<"The file doesn't exists. " + << std::endl << "Filename = " << this->m_FileName + << std::endl; + e.SetDescription(msg.str().c_str()); + throw e; + return; + } + + // Test if the file can be open for reading access. + //Only if m_FileName speciy a filname (not a dirname) + if( otb::System::IsAFileName( this->m_FileName ) == true ) + { + std::ifstream readTester; + readTester.open( this->m_FileName.c_str() ); + if( readTester.fail() ) + { + readTester.close(); + itk::OStringStream msg; + msg <<"The file couldn't be opened for reading. " + << std::endl << "Filename: " << this->m_FileName + << std::endl; + itk::ImageFileReaderException e(__FILE__, __LINE__,msg.str().c_str(),ITK_LOCATION); + throw e; + return; + + } + readTester.close(); + } +} } //namespace otb