Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean: rename File::Class to what it is really used. #1437

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/btreeidx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ string const & BtreeDictionary::ensureInitDone()
return empty;
}

void BtreeIndex::openIndex( IndexInfo const & indexInfo, File::Class & file, QMutex & mutex )
void BtreeIndex::openIndex( IndexInfo const & indexInfo, File::Index & file, QMutex & mutex )
{
indexNodeSize = indexInfo.btreeMaxElements;
rootOffset = indexInfo.rootOffset;
Expand Down Expand Up @@ -754,7 +754,7 @@ void BtreeIndex::antialias( wstring const & str, vector< WordArticleLink > & cha
/// leaf nodes.
static uint32_t buildBtreeNode( IndexedWords::const_iterator & nextIndex,
size_t indexSize,
File::Class & file,
File::Index & file,
size_t maxElements,
uint32_t & lastLeafLinkOffset )
{
Expand Down Expand Up @@ -984,7 +984,7 @@ void IndexedWords::addSingleWord( wstring const & index_word, uint32_t articleOf
operator[]( Utf8::encode( folded ) ).emplace_back( Utf8::encode( word ), articleOffset );
}

IndexInfo buildIndex( IndexedWords const & indexedWords, File::Class & file )
IndexInfo buildIndex( IndexedWords const & indexedWords, File::Index & file )
{
size_t indexSize = indexedWords.size();
auto nextIndex = indexedWords.begin();
Expand Down
6 changes: 3 additions & 3 deletions src/btreeidx.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public:
/// Opens the index. The file reference is saved to be used for
/// subsequent lookups.
/// The mutex is the one to be locked when working with the file.
void openIndex( IndexInfo const &, File::Class &, QMutex & );
void openIndex( IndexInfo const &, File::Index &, QMutex & );

/// Finds articles that match the given string. A case-insensitive search
/// is performed.
Expand Down Expand Up @@ -140,7 +140,7 @@ protected:
protected:

QMutex * idxFileMutex;
File::Class * idxFile;
File::Index * idxFile;

private:

Expand Down Expand Up @@ -276,7 +276,7 @@ struct IndexedWords: public map< string, vector< WordArticleLink > >
/// Builds the index, as a compressed btree. Returns IndexInfo.
/// All the data is stored to the given file, beginning from its current
/// position.
IndexInfo buildIndex( IndexedWords const &, File::Class & file );
IndexInfo buildIndex( IndexedWords const &, File::Index & file );

} // namespace BtreeIndexing

Expand Down
4 changes: 2 additions & 2 deletions src/chunkedstorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum {
ChunkMaxSize = 65536 // Can't be more since it would overflow the address
};

Writer::Writer( File::Class & f ):
Writer::Writer( File::Index & f ):
file( f ),
chunkStarted( false ),
bufferUsed( 0 )
Expand Down Expand Up @@ -115,7 +115,7 @@ uint32_t Writer::finish()
return offset;
}

Reader::Reader( File::Class & f, uint32_t offset ):
Reader::Reader( File::Index & f, uint32_t offset ):
file( f )
{
file.seek( offset );
Expand Down
8 changes: 4 additions & 4 deletions src/chunkedstorage.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ DEF_EX( mapFailed, "Failed to map/unmap the file", Ex )
class Writer
{
vector< uint32_t > offsets;
File::Class & file;
File::Index & file;
size_t scratchPadOffset, scratchPadSize;

public:
explicit Writer( File::Class & );
explicit Writer( File::Index & );

/// Starts new block. Returns its address.
uint32_t startNewBlock();
Expand Down Expand Up @@ -72,12 +72,12 @@ private:
class Reader
{
vector< uint32_t > offsets;
File::Class & file;
File::Index & file;

public:
/// Creates reader by giving it a file to read from and the offset returned
/// by Writer::finish().
Reader( File::Class &, uint32_t );
Reader( File::Index &, uint32_t );

/// Reads the block previously written by Writer, identified by its address.
/// Uses the user-provided storage to load the entire chunk, and then to
Expand Down
40 changes: 20 additions & 20 deletions src/common/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@

void loadFromFile( std::string const & filename, std::vector< char > & data )
{
File::Class f( filename, "rb" );
File::Index f( filename, "rb" );
auto size = f.file().size(); // QFile::size() obtains size via statx on Linux
data.resize( size );
f.read( data.data(), size );
}

void Class::open( char const * mode )
void Index::open( char const * mode )
{
QFile::OpenMode openMode = QIODevice::Text;

Expand Down Expand Up @@ -74,26 +74,26 @@
throw exCantOpen( f.fileName().toStdString() + ": " + f.errorString().toUtf8().data() );
}

Class::Class( std::string_view filename, char const * mode )
Index::Index( std::string_view filename, char const * mode )
{
f.setFileName( QString::fromUtf8( filename.data(), filename.size() ) );
open( mode );
}

void Class::read( void * buf, qint64 size )
void Index::read( void * buf, qint64 size )
{
if ( f.read( static_cast< char * >( buf ), size ) != size ) {
throw exReadError();
}
}

size_t Class::readRecords( void * buf, qint64 size, qint64 count )
size_t Index::readRecords( void * buf, qint64 size, qint64 count )
{
qint64 result = f.read( static_cast< char * >( buf ), size * count );
return result < 0 ? result : result / size;
}

void Class::write( void const * buf, qint64 size )
void Index::write( void const * buf, qint64 size )
{
if ( 0 == size ) {
return;
Expand All @@ -106,13 +106,13 @@
f.write( static_cast< char const * >( buf ), size );
}

size_t Class::writeRecords( void const * buf, qint64 size, qint64 count )
size_t Index::writeRecords( void const * buf, qint64 size, qint64 count )
{
qint64 result = f.write( static_cast< const char * >( buf ), size * count );
return result < 0 ? result : result / size;
}

char * Class::gets( char * s, int size, bool stripNl )
char * Index::gets( char * s, int size, bool stripNl )

Check failure on line 115 in src/common/file.cc

View check run for this annotation

codefactor.io / CodeFactor

src/common/file.cc#L115

Use of gets/_getts (CWE-120, CWE-20) (flawfinder7-gets)
{
qint64 len = f.readLine( s, size );
char * result = len > 0 ? s : nullptr;
Expand All @@ -134,7 +134,7 @@
return result;
}

std::string Class::gets( bool stripNl )
std::string Index::gets( bool stripNl )

Check failure on line 137 in src/common/file.cc

View check run for this annotation

codefactor.io / CodeFactor

src/common/file.cc#L137

Use of gets/_getts (CWE-120, CWE-20) (flawfinder7-gets)
{
char buf[ 1024 ];

Expand All @@ -144,61 +144,61 @@
return { buf };
}

QByteArray Class::readall()
QByteArray Index::readall()
{
return f.readAll();
};


void Class::seek( qint64 offset )
void Index::seek( qint64 offset )
{
if ( !f.seek( offset ) )
throw exSeekError();
}

uchar * Class::map( qint64 offset, qint64 size )
uchar * Index::map( qint64 offset, qint64 size )
{
return f.map( offset, size );
}

bool Class::unmap( uchar * address )
bool Index::unmap( uchar * address )
{
return f.unmap( address );
}


void Class::seekEnd()
void Index::seekEnd()
{
if ( !f.seek( f.size() ) )
throw exSeekError();
}

void Class::rewind()
void Index::rewind()
{
seek( 0 );
}

qint64 Class::tell()
qint64 Index::tell()
{
return f.pos();
}

bool Class::eof() const
bool Index::eof() const
{
return f.atEnd();
}

QFile & Class::file()
QFile & Index::file()
{
return f;
}

void Class::close()
void Index::close()
{
f.close();
}

Class::~Class() noexcept
Index::~Index() noexcept
{
f.close();
}
Expand Down
14 changes: 5 additions & 9 deletions src/common/file.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
#include <vector>
#include <QMutex>

/// A wrapper over QFile with some GD specific functions
/// and exception throwing which are required for older coded to work correctly
/// Consider the wrapped QFile as private implementation in the `Pimpl Idiom`
///
/// Note: this is used *only* in code related to `Dictionary::CLass` to manage dict files.
/// In other places, just use QFile directly.
/// File utilities
namespace File {

DEF_EX( Ex, "File exception", std::exception )
Expand All @@ -40,15 +35,16 @@ inline bool exists( std::string_view filename ) noexcept
return QFileInfo::exists( QString::fromUtf8( filename.data(), filename.size() ) );
};

class Class
/// Exclusivly used for processing GD's index files
class Index
{
QFile f;

public:
QMutex lock;

// Create QFile Object and open() it.
Class( std::string_view filename, char const * mode );
Index( std::string_view filename, char const * mode );

/// QFile::read & QFile::write , but with exception throwing
void read( void * buf, qint64 size );
Expand Down Expand Up @@ -115,7 +111,7 @@ public:
/// Closes the file. No further operations are valid.
void close();

~Class() noexcept;
~Index() noexcept;

private:
// QFile::open but with fopen-like mode settings.
Expand Down
10 changes: 5 additions & 5 deletions src/dict/aard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ __attribute__( ( packed ) )

bool indexIsOldOrBad( string const & indexFile )
{
File::Class idx( indexFile, "rb" );
File::Index idx( indexFile, "rb" );

IdxHeader header;

Expand Down Expand Up @@ -214,10 +214,10 @@ class AardDictionary: public BtreeIndexing::BtreeDictionary
{
QMutex idxMutex;
QMutex aardMutex;
File::Class idx;
File::Index idx;
IdxHeader idxHeader;
ChunkedStorage::Reader chunks;
File::Class df;
File::Index df;

public:

Expand Down Expand Up @@ -774,7 +774,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
}
}

File::Class df( fileName, "rb" );
File::Index df( fileName, "rb" );

AAR_header dictHeader;

Expand Down Expand Up @@ -839,7 +839,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f

initializing.indexingDictionary( dictName );

File::Class idx( indexFile, "wb" );
File::Index idx( indexFile, "wb" );
IdxHeader idxHeader;
memset( &idxHeader, 0, sizeof( idxHeader ) );

Expand Down
14 changes: 7 additions & 7 deletions src/dict/bgl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ __attribute__( ( packed ) )

bool indexIsOldOrBad( string const & indexFile )
{
File::Class idx( indexFile, "rb" );
File::Index idx( indexFile, "rb" );

IdxHeader header;

Expand Down Expand Up @@ -175,7 +175,7 @@ DEF_EX( exChunkIndexOutOfRange, "Chunk index is out of range", Dictionary::Ex )
class BglDictionary: public BtreeIndexing::BtreeDictionary
{
QMutex idxMutex;
File::Class idx;
File::Index idx;
IdxHeader idxHeader;
ChunkedStorage::Reader chunks;

Expand Down Expand Up @@ -809,7 +809,7 @@ class BglResourceRequest: public Dictionary::DataRequest
{

QMutex & idxMutex;
File::Class & idx;
File::Index & idx;
uint32_t resourceListOffset, resourcesCount;
string name;

Expand All @@ -819,7 +819,7 @@ class BglResourceRequest: public Dictionary::DataRequest
public:

BglResourceRequest( QMutex & idxMutex_,
File::Class & idx_,
File::Index & idx_,
uint32_t resourceListOffset_,
uint32_t resourcesCount_,
string const & name_ ):
Expand Down Expand Up @@ -953,12 +953,12 @@ void BglDictionary::replaceCharsetEntities( string & text )

class ResourceHandler: public Babylon::ResourceHandler
{
File::Class & idxFile;
File::Index & idxFile;
list< pair< string, uint32_t > > resources;

public:

ResourceHandler( File::Class & idxFile_ ):
ResourceHandler( File::Index & idxFile_ ):
idxFile( idxFile_ )
{
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f

initializing.indexingDictionary( b.title() );

File::Class idx( indexFile, "wb" );
File::Index idx( indexFile, "wb" );

IdxHeader idxHeader;

Expand Down
Loading
Loading