From ecc3d39fdb90ca57c5b216bb0f4dba8748125998 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Sun, 17 May 2009 22:22:10 +0000 Subject: [PATCH] + Introduce File::exists() and use that instead of trying to just open the file. --- src/dictdfiles.cc | 8 ++------ src/dsl.cc | 8 ++------ src/file.cc | 15 +++++++++++++++ src/file.hh | 6 ++++++ src/stardict.cc | 8 ++------ 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/dictdfiles.cc b/src/dictdfiles.cc index 094d41e9..fedaaf3c 100644 --- a/src/dictdfiles.cc +++ b/src/dictdfiles.cc @@ -301,18 +301,14 @@ sptr< Dictionary::DataRequest > DictdDictionary::getArticle( wstring const & wor static bool tryPossibleName( string const & name, string & copyTo ) { - try + if ( File::exists( name ) ) { - File::Class f( name, "rb" ); - copyTo = name; return true; } - catch( ... ) - { + else return false; - } } vector< sptr< Dictionary::Class > > makeDictionaries( diff --git a/src/dsl.cc b/src/dsl.cc index 1c1adcd0..2dbb904f 100644 --- a/src/dsl.cc +++ b/src/dsl.cc @@ -1276,18 +1276,14 @@ sptr< Dictionary::DataRequest > DslDictionary::getResource( string const & name static bool tryPossibleName( string const & name, string & copyTo ) { - try + if ( File::exists( name ) ) { - File::Class f( name, "rb" ); - copyTo = name; return true; } - catch( ... ) - { + else return false; - } } #if 0 diff --git a/src/file.cc b/src/file.cc index b7273c7d..e4fe3178 100644 --- a/src/file.cc +++ b/src/file.cc @@ -6,6 +6,10 @@ #include #include +#include +#include +#include + namespace File { enum @@ -15,6 +19,17 @@ enum WriteBufferSize = 65536 }; +bool exists( char const * filename ) throw() +{ +#ifdef __WIN32 + struct _stat buf; + return _stat( filename, &buf ) == 0; +#else + struct stat buf; + return stat( filename, &buf ) == 0; +#endif +} + void Class::open( char const * filename, char const * mode ) throw( exCantOpen ) { f = fopen( filename, mode ); diff --git a/src/file.hh b/src/file.hh index 9f850615..10305ee6 100644 --- a/src/file.hh +++ b/src/file.hh @@ -19,6 +19,12 @@ DEF_EX( exReadError, "Error reading from the file", Ex ) DEF_EX( exWriteError, "Error writing to the file", Ex ) DEF_EX( exSeekError, "File seek error", Ex ) +/// Checks if the file exists or not. +bool exists( char const * filename ) throw(); + +inline bool exists( std::string const & filename ) throw() +{ return exists( filename.c_str() ); } + class Class { FILE * f; diff --git a/src/stardict.cc b/src/stardict.cc index 140cc32a..99e23cd6 100644 --- a/src/stardict.cc +++ b/src/stardict.cc @@ -815,18 +815,14 @@ Ifo::Ifo( File::Class & f ): static bool tryPossibleName( string const & name, string & copyTo ) { - try + if ( File::exists( name ) ) { - File::Class f( name, "rb" ); - copyTo = name; return true; } - catch( ... ) - { + else return false; - } } static void findCorrespondingFiles( string const & ifo,