📄 kwqfile.cpp
字号:
/*
* Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
* Portions Copyright (c) 2005 Nokia Corporation, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "KWQFile.h"
#include "KWQGlobalServices.h"
#include <f32file.h>
#include <bautils.h>
// QFilePrivate
class QFilePrivate
OOM_MODIFIED
{
public:
QFilePrivate();
~QFilePrivate();
TInt Open( const TDesC& aName, TInt aMode );
void Close();
TInt Read( TInt aPos, TDes8& aDes ) const;
TBool isOpened() const { return iOpened; }
TInt Size();
private:
RFile iFile;
TBool iOpened;
// quick access for QFile
friend class QFile;
};
QFilePrivate::QFilePrivate() : iOpened( EFalse )
{
}
QFilePrivate::~QFilePrivate()
{
if( iOpened )
iFile.Close();
}
TInt QFilePrivate::Open( const TDesC& aName, TInt aMode )
{
TInt err = iFile.Open( KWQGlobalServices::InstanceL()->FileSession(), aName, aMode );
iOpened = ( err == KErrNone );
return err;
}
void QFilePrivate::Close()
{
if( iOpened ) iFile.Close();
}
TInt QFilePrivate::Read( TInt aPos, TDes8& aDes ) const
{
if( iOpened )
{
iFile.Lock( aPos, aDes.MaxLength() );
TInt err = iFile.Read( aPos, aDes );
iFile.UnLock( aPos, aDes.MaxLength() );
return err == KErrNone ? aDes.Length() : -1 ;
}
else
return -1;
}
// QFile
QFile::QFile(const QString &n) : iName( n ), iPrivate(0)
{
iPrivate = new QFilePrivate;
}
QFile::~QFile()
{
delete iPrivate;
}
bool QFile::exists() const
{
return BaflUtils::FileExists( KWQGlobalServices::InstanceL()->FileSession(), iName.Des() );
}
bool QFile::open(int mode)
{
if( mode == IO_ReadOnly )
{
if( !iPrivate->isOpened() )
iPrivate->Open( iName.Des(), EFileShareReadersOnly );
return iPrivate->isOpened();
}
return false;
}
void QFile::close()
{
iPrivate->Close();
}
int QFile::readBlock(char *data, uint bytesToRead)
{
// Apple's code only assumes reading file from the begining
TPtr8 ptr( (unsigned char*)data, 0, bytesToRead );
return iPrivate->Read( 0, ptr );
}
uint QFile::size() const
{
if( !iPrivate->isOpened() )
iPrivate->Open( iName.Des(), EFileShareReadersOnly );
if( iPrivate->isOpened() )
{
TInt sz;
iPrivate->iFile.Size( sz );
return sz;
}
else
return 0;
}
bool QFile::exists(const QString &path)
{
return QFile(path).exists();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -