Skip to content

Commit

Permalink
Use QFile instead of FILE * functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Abs62 committed Feb 26, 2014
1 parent 95a9685 commit becc74c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 51 deletions.
87 changes: 51 additions & 36 deletions file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#endif

#include "ufile.hh"
#include "fsencoding.hh"

namespace File {

Expand Down Expand Up @@ -69,9 +70,31 @@ bool exists( char const * filename ) throw()

void Class::open( char const * filename, char const * mode ) throw( exCantOpen )
{
f = gd_fopen( filename, mode );
QFile::OpenMode openMode = QIODevice::Text;
const char * pch = mode;
while( *pch )
{
switch( *pch )
{
case 'r': openMode |= QIODevice::ReadOnly;
break;
case 'w': openMode |= QIODevice::WriteOnly;
break;
case '+': openMode &= ~( QIODevice::ReadOnly | QIODevice::WriteOnly );
openMode |= QIODevice::ReadWrite;
break;
case 'a': openMode |= QIODevice::Append;
break;
case 'b': openMode &= ~QIODevice::Text;
break;
default: break;
}
++pch;
}

if ( !f )
f.setFileName( FsEncoding::decode( filename ) );

if ( !f.open( openMode ) )
throw exCantOpen( std::string( filename ) + ": " + strerror( errno ) );
}

Expand All @@ -87,29 +110,30 @@ Class::Class( std::string const & filename, char const * mode )
open( filename.c_str(), mode );
}

void Class::read( void * buf, size_t size ) throw( exReadError, exWriteError )
void Class::read( void * buf, qint64 size ) throw( exReadError, exWriteError )
{
if ( !size )
return;

if ( writeBuffer )
flushWriteBuffer();

size_t result = fread( buf, size, 1, f );
qint64 result = f.read( reinterpret_cast<char *>( buf ), size );

if ( result != 1 )
if ( result != size )
throw exReadError();
}

size_t Class::readRecords( void * buf, size_t size, size_t count ) throw( exWriteError )
size_t Class::readRecords( void * buf, qint64 size, size_t count ) throw( exWriteError )
{
if ( writeBuffer )
flushWriteBuffer();

return fread( buf, size, count, f );
qint64 result = f.read( reinterpret_cast<char *>( buf ), size * count );
return result < 0 ? result : result / size;
}

void Class::write( void const * buf, size_t size ) throw( exWriteError )
void Class::write( void const * buf, qint64 size ) throw( exWriteError )
{
if ( !size )
return;
Expand All @@ -119,9 +143,9 @@ void Class::write( void const * buf, size_t size ) throw( exWriteError )
// If the write is large, there's not much point in buffering
flushWriteBuffer();

size_t result = fwrite( buf, size, 1, f );
size_t result = f.write( reinterpret_cast<char const *>( buf ), size );

if ( result != 1 )
if ( result != size )
throw exWriteError();

return;
Expand Down Expand Up @@ -154,12 +178,13 @@ void Class::write( void const * buf, size_t size ) throw( exWriteError )
}
}

size_t Class::writeRecords( void const * buf, size_t size, size_t count )
size_t Class::writeRecords( void const * buf, qint64 size, size_t count )
throw( exWriteError )
{
flushWriteBuffer();

return fwrite( buf, size, count, f );
qint64 result = f.write( reinterpret_cast<const char *>( buf ), size * count );
return result < 0 ? result : result / size;
}

char * Class::gets( char * s, int size, bool stripNl )
Expand All @@ -168,11 +193,11 @@ char * Class::gets( char * s, int size, bool stripNl )
if ( writeBuffer )
flushWriteBuffer();

char * result = fgets( s, size, f );
qint64 len = f.readLine( s, size );
char * result = len > 0 ? s : NULL;

if ( result && stripNl )
{
size_t len = strlen( result );

char * last = result + len;

Expand Down Expand Up @@ -205,7 +230,7 @@ void Class::seek( long offset ) throw( exSeekError, exWriteError )
if ( writeBuffer )
flushWriteBuffer();

if ( fseek( f, offset, SEEK_SET ) != 0 )
if ( !f.seek( offset ) )
throw exSeekError();
}

Expand All @@ -214,7 +239,7 @@ void Class::seekCur( long offset ) throw( exSeekError, exWriteError )
if ( writeBuffer )
flushWriteBuffer();

if ( fseek( f, offset, SEEK_CUR ) != 0 )
if( !f.seek( f.pos() + offset ) )
throw exSeekError();
}

Expand All @@ -223,7 +248,7 @@ void Class::seekEnd( long offset ) throw( exSeekError, exWriteError )
if ( writeBuffer )
flushWriteBuffer();

if ( fseek( f, offset, SEEK_END ) != 0 )
if( !f.seek( f.size() + offset ) )
throw exSeekError();
}

Expand All @@ -234,7 +259,7 @@ void Class::rewind() throw( exSeekError, exWriteError )

size_t Class::tell() throw( exSeekError )
{
long result = ftell( f );
qint64 result = f.pos();

if ( result == -1 )
throw exSeekError();
Expand All @@ -250,35 +275,25 @@ bool Class::eof() throw( exWriteError )
if ( writeBuffer )
flushWriteBuffer();

return feof( f ) != 0;
return f.atEnd();
}

FILE * Class::file() throw( exWriteError )
QFile & Class::file() throw( exWriteError )
{
flushWriteBuffer();

return f;
}

FILE * Class::release() throw( exWriteError )
{
releaseWriteBuffer();

FILE * c = f;

f = 0;

return c;
}

void Class::close() throw( exWriteError )
{
fclose( release() );
releaseWriteBuffer();
f.close();
}

Class::~Class() throw()
{
if ( f )
if ( f.isOpen() )
{
try
{
Expand All @@ -287,17 +302,17 @@ Class::~Class() throw()
catch( exWriteError & )
{
}
fclose( f );
f.close();
}
}

void Class::flushWriteBuffer() throw( exWriteError )
{
if ( writeBuffer && writeBufferLeft != WriteBufferSize )
{
size_t result = fwrite( writeBuffer, WriteBufferSize - writeBufferLeft, 1, f );
size_t result = f.write( writeBuffer, WriteBufferSize - writeBufferLeft );

if ( result != 1 )
if ( result != WriteBufferSize - writeBufferLeft )
throw exWriteError();

writeBufferLeft = WriteBufferSize;
Expand Down
17 changes: 7 additions & 10 deletions file.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdio>
#include <string>
#include <vector>
#include <QFile>
#include "ex.hh"

/// A simple wrapper over FILE * operations with added write-buffering,
Expand All @@ -33,7 +34,7 @@ inline bool exists( std::string const & filename ) throw()

class Class
{
FILE * f;
QFile f;
char * writeBuffer;
size_t writeBufferLeft;

Expand All @@ -47,7 +48,7 @@ public:

/// Reads the number of bytes to the buffer, throws an error if it
/// failed to fill the whole buffer (short read, i/o error etc).
void read( void * buf, size_t size ) throw( exReadError, exWriteError );
void read( void * buf, qint64 size ) throw( exReadError, exWriteError );

template< typename T >
void read( T & value ) throw( exReadError, exWriteError )
Expand All @@ -59,15 +60,15 @@ public:

/// Attempts reading at most 'count' records sized 'size'. Returns
/// the number of records it managed to read, up to 'count'.
size_t readRecords( void * buf, size_t size, size_t count ) throw( exWriteError );
size_t readRecords( void * buf, qint64 size, size_t count ) throw( exWriteError );

/// Writes the number of bytes from the buffer, throws an error if it
/// failed to write the whole buffer (short write, i/o error etc).
/// This function employs write buffering, and as such, writes may not
/// end up on disk immediately, or a short write may occur later
/// than it really did. If you don't want write buffering, use
/// writeRecords() function instead.
void write( void const * buf, size_t size ) throw( exWriteError );
void write( void const * buf, qint64 size ) throw( exWriteError );

template< typename T >
void write( T const & value ) throw( exWriteError )
Expand All @@ -77,7 +78,7 @@ public:
/// 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.
size_t writeRecords( void const * buf, size_t size, size_t count )
size_t writeRecords( void const * buf, qint64 size, size_t count )
throw( exWriteError );

/// Reads a string from the file. Unlike the normal fgets(), this one
Expand Down Expand Up @@ -107,11 +108,7 @@ public:

/// Returns the underlying FILE * record, so other operations can be
/// performed on it.
FILE * file() throw( exWriteError );

/// Releases the file handle out of the control of the class. No further
/// operations are valid. The file will not be closed on destruction.
FILE * release() throw( exWriteError );
QFile & file() throw( exWriteError );

/// Closes the file. No further operations are valid.
void close() throw( exWriteError );
Expand Down
17 changes: 12 additions & 5 deletions lsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QUrl>
#include <QDir>
#include <QDebug>
#include <QFile>

namespace Lsa {

Expand Down Expand Up @@ -316,10 +317,10 @@ sptr< Dictionary::DataRequest > LsaDictionary::getArticle( wstring const & word,
/// This wraps around file operations
struct ShiftedVorbis
{
FILE * f;
QFile & f;
size_t shift;

ShiftedVorbis( FILE * f_, size_t shift_ ): f( f_ ), shift( shift_ )
ShiftedVorbis( QFile & f_, size_t shift_ ): f( f_ ), shift( shift_ )
{}

static size_t read( void * ptr, size_t size, size_t nmemb, void * datasource );
Expand All @@ -334,7 +335,7 @@ size_t ShiftedVorbis::read( void * ptr, size_t size, size_t nmemb,
{
ShiftedVorbis * sv = ( ShiftedVorbis * ) datasource;

return fread( ptr, size, nmemb, sv->f );
return sv->f.read( reinterpret_cast<char *>( ptr ), size * nmemb );
}

int ShiftedVorbis::seek( void * datasource, ogg_int64_t offset, int whence )
Expand All @@ -344,13 +345,19 @@ int ShiftedVorbis::seek( void * datasource, ogg_int64_t offset, int whence )
if ( whence == SEEK_SET )
offset += sv->shift;

return fseek( sv->f, offset, whence );
if( whence == SEEK_CUR )
offset += sv->f.pos();

if( whence == SEEK_END )
offset += sv->f.size();

return sv->f.seek( offset );
}

long ShiftedVorbis::tell( void * datasource )
{
ShiftedVorbis * sv = ( ShiftedVorbis * ) datasource;
long result = ftell( sv->f );
long result = sv->f.pos();

if ( result != -1 )
result -= sv->shift;
Expand Down

0 comments on commit becc74c

Please sign in to comment.