+ Introduce File::exists() and use that instead of trying to just open the file.

This commit is contained in:
Konstantin Isakov 2009-05-17 22:22:10 +00:00
parent ba9b13d9e6
commit ecc3d39fdb
5 changed files with 27 additions and 18 deletions

View file

@ -301,18 +301,14 @@ sptr< Dictionary::DataRequest > DictdDictionary::getArticle( wstring const & wor
static bool tryPossibleName( string const & name, string & copyTo ) static bool tryPossibleName( string const & name, string & copyTo )
{ {
try if ( File::exists( name ) )
{ {
File::Class f( name, "rb" );
copyTo = name; copyTo = name;
return true; return true;
} }
catch( ... ) else
{
return false; return false;
}
} }
vector< sptr< Dictionary::Class > > makeDictionaries( vector< sptr< Dictionary::Class > > makeDictionaries(

View file

@ -1276,18 +1276,14 @@ sptr< Dictionary::DataRequest > DslDictionary::getResource( string const & name
static bool tryPossibleName( string const & name, string & copyTo ) static bool tryPossibleName( string const & name, string & copyTo )
{ {
try if ( File::exists( name ) )
{ {
File::Class f( name, "rb" );
copyTo = name; copyTo = name;
return true; return true;
} }
catch( ... ) else
{
return false; return false;
}
} }
#if 0 #if 0

View file

@ -6,6 +6,10 @@
#include <cstring> #include <cstring>
#include <cerrno> #include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace File { namespace File {
enum enum
@ -15,6 +19,17 @@ enum
WriteBufferSize = 65536 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 ) void Class::open( char const * filename, char const * mode ) throw( exCantOpen )
{ {
f = fopen( filename, mode ); f = fopen( filename, mode );

View file

@ -19,6 +19,12 @@ DEF_EX( exReadError, "Error reading from the file", Ex )
DEF_EX( exWriteError, "Error writing to the file", Ex ) DEF_EX( exWriteError, "Error writing to the file", Ex )
DEF_EX( exSeekError, "File seek error", 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 class Class
{ {
FILE * f; FILE * f;

View file

@ -815,18 +815,14 @@ Ifo::Ifo( File::Class & f ):
static bool tryPossibleName( string const & name, string & copyTo ) static bool tryPossibleName( string const & name, string & copyTo )
{ {
try if ( File::exists( name ) )
{ {
File::Class f( name, "rb" );
copyTo = name; copyTo = name;
return true; return true;
} }
catch( ... ) else
{
return false; return false;
}
} }
static void findCorrespondingFiles( string const & ifo, static void findCorrespondingFiles( string const & ifo,