From 1ff67d7bf6a13208c364375a6141493219110a14 Mon Sep 17 00:00:00 2001 From: Thomas Feuvrier <thomas.feuvrier@c-s.fr> Date: Wed, 19 Jul 2006 14:50:19 +0000 Subject: [PATCH] =?UTF-8?q?Corrections=20sur=20la=20m=C3=A9thode=20TestFil?= =?UTF-8?q?eReadability=20:=20m=C3=A9thode=20surcharg=C3=A9=20dans=20otb::?= =?UTF-8?q?ImageFileReader,=20pour=20=C3=A9viter=20de=20faire=20un=20open?= =?UTF-8?q?=20sur=20un=20filename=20qui=20d=C3=A9signe=20un=20r=C3=A9perto?= =?UTF-8?q?ire.=20Pourquoi=20:=20parceque=20sous=20Windows,=20ca=20sort=20?= =?UTF-8?q?en=20ERREUR.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Code/Common/otbSystem.cxx | 54 ++++++++++++++++++++++++++++++++++ Code/Common/otbSystem.h | 4 +++ Code/IO/otbImageFileReader.h | 7 +++++ Code/IO/otbImageFileReader.txx | 40 +++++++++++++++++++++++++ 4 files changed, 105 insertions(+) diff --git a/Code/Common/otbSystem.cxx b/Code/Common/otbSystem.cxx index 4ad545a5b2..8a287eba38 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 03085ed782..2def5fe13c 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 5839720ff0..50b4a223fb 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 770ab0f1b0..24c90d67d6 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 -- GitLab