Move tryPossibleName and loadFromFile functions into File namespace

This commit is contained in:
Abs62 2012-01-30 17:11:41 +04:00
parent 25bca9076b
commit 99fe9474ac
5 changed files with 60 additions and 78 deletions

View file

@ -302,18 +302,6 @@ sptr< Dictionary::DataRequest > DictdDictionary::getArticle( wstring const & wor
} // anonymous namespace
static bool tryPossibleName( string const & name, string & copyTo )
{
if ( File::exists( name ) )
{
copyTo = name;
return true;
}
else
return false;
}
vector< sptr< Dictionary::Class > > makeDictionaries(
vector< string > const & fileNames,
string const & indicesDir,
@ -340,8 +328,8 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
dictFiles.push_back( string() );
if ( !tryPossibleName( baseName + "dict", dictFiles[ 1 ] ) &&
!tryPossibleName( baseName + "dict.dz", dictFiles[ 1 ] ) )
if ( !File::tryPossibleName( baseName + "dict", dictFiles[ 1 ] ) &&
!File::tryPossibleName( baseName + "dict.dz", dictFiles[ 1 ] ) )
{
// No corresponding .dict file, skipping
continue;

47
dsl.cc
View file

@ -1084,19 +1084,6 @@ sptr< Dictionary::DataRequest > DslDictionary::getArticle( wstring const & word,
return new DslArticleRequest( word, alts, *this );
}
void loadFromFile( string const & n, vector< char > & data )
{
File::Class f( n, "rb" );
f.seekEnd();
data.resize( f.tell() );
f.rewind();
f.read( &data.front(), data.size() );
}
//// DslDictionary::getResource()
class DslResourceRequest;
@ -1191,7 +1178,7 @@ void DslResourceRequest::run()
{
Mutex::Lock _( dataMutex );
loadFromFile( n, data );
File::loadFromFile( n, data );
}
catch( File::exCantOpen & )
{
@ -1203,7 +1190,7 @@ void DslResourceRequest::run()
{
Mutex::Lock _( dataMutex );
loadFromFile( n, data );
File::loadFromFile( n, data );
}
catch( File::exCantOpen & )
{
@ -1275,18 +1262,6 @@ sptr< Dictionary::DataRequest > DslDictionary::getResource( string const & name
} // anonymous namespace
static bool tryPossibleName( string const & name, string & copyTo )
{
if ( File::exists( name ) )
{
copyTo = name;
return true;
}
else
return false;
}
#if 0
static void findCorrespondingFiles( string const & ifo,
string & idx, string & dict, string & syn,
@ -1366,11 +1341,11 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
string abrvFileName;
if ( tryPossibleName( baseName + "_abrv.dsl", abrvFileName ) ||
tryPossibleName( baseName + "_abrv.dsl.dz", abrvFileName ) ||
tryPossibleName( baseName + "_ABRV.DSL", abrvFileName ) ||
tryPossibleName( baseName + "_ABRV.DSL.DZ", abrvFileName ) ||
tryPossibleName( baseName + "_ABRV.DSL.dz", abrvFileName ) )
if ( File::tryPossibleName( baseName + "_abrv.dsl", abrvFileName ) ||
File::tryPossibleName( baseName + "_abrv.dsl.dz", abrvFileName ) ||
File::tryPossibleName( baseName + "_ABRV.DSL", abrvFileName ) ||
File::tryPossibleName( baseName + "_ABRV.DSL.DZ", abrvFileName ) ||
File::tryPossibleName( baseName + "_ABRV.DSL.dz", abrvFileName ) )
dictFiles.push_back( abrvFileName );
string dictId = Dictionary::makeDictionaryId( dictFiles );
@ -1379,10 +1354,10 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
string zipFileName;
if ( tryPossibleName( baseName + ".dsl.files.zip", zipFileName ) ||
tryPossibleName( baseName + ".dsl.dz.files.zip", zipFileName ) ||
tryPossibleName( baseName + ".DSL.FILES.ZIP", zipFileName ) ||
tryPossibleName( baseName + ".DSL.DZ.FILES.ZIP", zipFileName ) )
if ( File::tryPossibleName( baseName + ".dsl.files.zip", zipFileName ) ||
File::tryPossibleName( baseName + ".dsl.dz.files.zip", zipFileName ) ||
File::tryPossibleName( baseName + ".DSL.FILES.ZIP", zipFileName ) ||
File::tryPossibleName( baseName + ".DSL.DZ.FILES.ZIP", zipFileName ) )
dictFiles.push_back( zipFileName );
string indexFile = indicesDir + dictId;

25
file.cc
View file

@ -5,6 +5,7 @@
#include <cstring>
#include <cerrno>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
@ -25,6 +26,30 @@ enum
WriteBufferSize = 65536
};
bool tryPossibleName( std::string const & name, std::string & copyTo )
{
if ( File::exists( name ) )
{
copyTo = name;
return true;
}
else
return false;
}
void loadFromFile( std::string const & n, std::vector< char > & data )
{
File::Class f( n, "rb" );
f.seekEnd();
data.resize( f.tell() );
f.rewind();
f.read( &data.front(), data.size() );
}
bool exists( char const * filename ) throw()
{
#ifdef __WIN32

View file

@ -6,6 +6,7 @@
#include <cstdio>
#include <string>
#include <vector>
#include "ex.hh"
/// A simple wrapper over FILE * operations with added write-buffering,
@ -20,6 +21,11 @@ DEF_EX( exWriteError, "Error writing to the file", Ex )
DEF_EX( exSeekError, "File seek error", Ex )
/// Checks if the file exists or not.
bool tryPossibleName( std::string const & name, std::string & copyTo );
void loadFromFile( std::string const & n, std::vector< char > & data );
bool exists( char const * filename ) throw();
inline bool exists( std::string const & filename ) throw()

View file

@ -835,48 +835,36 @@ Ifo::Ifo( File::Class & f ):
} // anonymous namespace
static bool tryPossibleName( string const & name, string & copyTo )
{
if ( File::exists( name ) )
{
copyTo = name;
return true;
}
else
return false;
}
static void findCorrespondingFiles( string const & ifo,
string & idx, string & dict, string & syn )
{
string base( ifo, 0, ifo.size() - 3 );
if ( !(
tryPossibleName( base + "idx", idx ) ||
tryPossibleName( base + "idx.gz", idx ) ||
tryPossibleName( base + "idx.dz", idx ) ||
tryPossibleName( base + "IDX", idx ) ||
tryPossibleName( base + "IDX.GZ", idx ) ||
tryPossibleName( base + "IDX.DZ", idx )
File::tryPossibleName( base + "idx", idx ) ||
File::tryPossibleName( base + "idx.gz", idx ) ||
File::tryPossibleName( base + "idx.dz", idx ) ||
File::tryPossibleName( base + "IDX", idx ) ||
File::tryPossibleName( base + "IDX.GZ", idx ) ||
File::tryPossibleName( base + "IDX.DZ", idx )
) )
throw exNoIdxFile( ifo );
if ( !(
tryPossibleName( base + "dict", dict ) ||
tryPossibleName( base + "dict.dz", dict ) ||
tryPossibleName( base + "DICT", dict ) ||
tryPossibleName( base + "dict.DZ", dict )
File::tryPossibleName( base + "dict", dict ) ||
File::tryPossibleName( base + "dict.dz", dict ) ||
File::tryPossibleName( base + "DICT", dict ) ||
File::tryPossibleName( base + "dict.DZ", dict )
) )
throw exNoDictFile( ifo );
if ( !(
tryPossibleName( base + "syn", syn ) ||
tryPossibleName( base + "syn.gz", syn ) ||
tryPossibleName( base + "syn.dz", syn ) ||
tryPossibleName( base + "SYN", syn ) ||
tryPossibleName( base + "SYN.GZ", syn ) ||
tryPossibleName( base + "SYN.DZ", syn )
File::tryPossibleName( base + "syn", syn ) ||
File::tryPossibleName( base + "syn.gz", syn ) ||
File::tryPossibleName( base + "syn.dz", syn ) ||
File::tryPossibleName( base + "SYN", syn ) ||
File::tryPossibleName( base + "SYN.GZ", syn ) ||
File::tryPossibleName( base + "SYN.DZ", syn )
) )
syn.clear();
}