2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
#ifndef GOLDENDICT_FILE_HH
|
|
|
|
#define GOLDENDICT_FILE_HH
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
#include "ex.hh"
|
|
|
|
#include "mutex.hh"
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2012-01-30 13:11:41 +00:00
|
|
|
#include <vector>
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
/// A simple wrapper over QFile with some convenient GD specific functions
|
|
|
|
/// 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.
|
2009-01-28 20:55:45 +00:00
|
|
|
namespace File {
|
|
|
|
|
|
|
|
DEF_EX( Ex, "File exception", std::exception )
|
|
|
|
DEF_EX_STR( exCantOpen, "Can't open", Ex )
|
|
|
|
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 )
|
2014-04-25 13:13:56 +00:00
|
|
|
DEF_EX( exAllocation, "Memory allocation error", Ex )
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
bool tryPossibleName( std::string const & name, std::string & copyTo );
|
|
|
|
|
2017-04-24 14:42:01 +00:00
|
|
|
bool tryPossibleZipName( std::string const & name, std::string & copyTo );
|
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
void loadFromFile( std::string const & filename, std::vector< char > & data );
|
2009-05-17 22:22:10 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
// QFileInfo::exists but used for std::string and char*
|
|
|
|
inline bool exists( std::string_view filename ) noexcept
|
|
|
|
{
|
|
|
|
return QFileInfo::exists( QString::fromUtf8( filename.data(), filename.size() ) );
|
|
|
|
};
|
2009-05-17 22:22:10 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
class Class
|
|
|
|
{
|
2014-02-26 14:22:12 +00:00
|
|
|
QFile f;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
public:
|
2022-06-18 01:09:40 +00:00
|
|
|
QMutex lock;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
// Create QFile Object and open() it.
|
|
|
|
Class( std::string_view filename, char const * mode );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
/// QFile::read & QFile::write , but with exception throwing
|
|
|
|
void read( void * buf, qint64 size );
|
|
|
|
void write( void const * buf, qint64 size );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
// Read sizeof(T) bytes and convert the read data to T
|
2009-01-28 20:55:45 +00:00
|
|
|
template< typename T >
|
2023-04-14 10:50:00 +00:00
|
|
|
T read()
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
readType( value );
|
|
|
|
return value;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
template< typename T >
|
2023-04-14 10:50:00 +00:00
|
|
|
void write( T const & value )
|
|
|
|
{
|
|
|
|
write( &value, sizeof( value ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Attempts reading at most 'count' records sized 'size'. Returns
|
|
|
|
/// the number of records it managed to read, up to 'count'.
|
2023-04-14 10:50:00 +00:00
|
|
|
size_t readRecords( void * buf, qint64 size, qint64 count );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Attempts writing at most 'count' records sized 'size'. Returns
|
|
|
|
/// the number of records it managed to write, up to 'count'.
|
|
|
|
/// This function does not employ buffering, but flushes the buffer if it
|
|
|
|
/// was used before.
|
2023-04-14 10:50:00 +00:00
|
|
|
size_t writeRecords( void const * buf, qint64 size, qint64 count );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
/// Read a line with option to strip the trailing newline character
|
2009-01-28 20:55:45 +00:00
|
|
|
/// Returns either s or 0 if no characters were read.
|
2023-04-14 10:50:00 +00:00
|
|
|
char * gets( char * s, int size, bool stripNl = false );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
/// Like the above, but uses its own local internal buffer and strips newlines by default.
|
|
|
|
std::string gets( bool stripNl = true );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
/// export QFile::readall
|
|
|
|
QByteArray readall();
|
|
|
|
|
|
|
|
/// export QFile::seek
|
|
|
|
void seek( qint64 offset );
|
|
|
|
|
|
|
|
/// Seeks to the end of file.
|
|
|
|
void seekEnd();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Seeks to the beginning of file
|
2023-04-14 10:50:00 +00:00
|
|
|
void rewind();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Tells the current position within the file, relative to its beginning.
|
2023-04-14 10:50:00 +00:00
|
|
|
qint64 tell();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
/// QFile::atEnd() const
|
|
|
|
bool eof() const;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
uchar * map( qint64 offset, qint64 size );
|
|
|
|
bool unmap( uchar * address );
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the underlying QFile* , so other operations can be
|
2009-01-28 20:55:45 +00:00
|
|
|
/// performed on it.
|
2023-04-14 10:50:00 +00:00
|
|
|
QFile & file();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Closes the file. No further operations are valid.
|
2023-04-14 10:50:00 +00:00
|
|
|
void close();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
~Class() noexcept;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
private:
|
2023-04-14 10:50:00 +00:00
|
|
|
// QFile::open but with fopen-like mode settings.
|
|
|
|
void open( char const * mode );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
template< typename T >
|
|
|
|
void readType( T & value )
|
|
|
|
{
|
|
|
|
read( &value, sizeof( value ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
2023-04-14 10:50:00 +00:00
|
|
|
} // namespace File
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
#endif
|