+ 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,19 +301,15 @@ 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(
vector< string > const & fileNames,

View file

@ -1276,19 +1276,15 @@ 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
static void findCorrespondingFiles( string const & ifo,

View file

@ -6,6 +6,10 @@
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
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 );

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( 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;

View file

@ -815,19 +815,15 @@ 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,
string & idx, string & dict, string & syn )