📄 fileio.cpp
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Jabber
* Copyright (C) 2004 Xie Tian Lu http://sabber.jabberstudio.org/
*/
#include "fileio.h"
void CFileIO::ReadData( RFile& file, TAny* data, TInt bytes )
{
TPtr8 buf( NULL, 0, 0 );
buf.Set( (TUint8*)data, bytes, bytes );
file.Read( buf, bytes );
}
void CFileIO::WriteData( RFile& file, TAny* data, TInt bytes )
{
TPtr8 buf( NULL, 0, 0 );
buf.Set( (TUint8*)data, bytes, bytes );
file.Write( buf, bytes );
}
void CFileIO::Write( RFile& file, TInt32 i )
{
WriteInteger( file, 4, &i );
}
void CFileIO::Write( RFile& file, TInt16 i )
{
WriteInteger( file, 2, &i );
}
void CFileIO::Write( RFile& file, TInt8 i )
{
WriteInteger( file, 1, &i );
}
void CFileIO::Write( RFile& file, TInt i )
{
WriteInteger( file, sizeof( TInt ), &i );
}
void CFileIO::Write( RFile& file, TUint32 i )
{
WriteInteger( file, sizeof( TUint32 ), &i );
}
void CFileIO::Read( RFile& file, TInt32& i )
{
i = ReadInteger( file, 4 );
}
void CFileIO::Read( RFile& file, TInt16& i )
{
i = ReadInteger( file, 2 );
}
void CFileIO::Read( RFile& file, TInt8& i )
{
i = ReadInteger( file, 1 );
}
void CFileIO::Read( RFile& file, TInt& i )
{
i = ReadInteger( file, sizeof( TInt ) );
}
void CFileIO::Read( RFile& file, TUint32& i )
{
i = ReadInteger( file, sizeof( TUint32 ) );
}
TInt CFileIO::ReadInteger( RFile& file, TInt bytes )
{
TBuf8< 8 > buf;
TUint8* res;
file.Read( buf, bytes );
res = ( TUint8* )buf.PtrZ();
switch ( bytes ) {
case 1:
return *( ( TInt8* )res );
case 2:
return *( ( TInt16* )res );
case 4:
return *( ( TInt32* )res );
}
return 0;
}
void CFileIO::WriteInteger( RFile& file, TInt bytes, void* des )
{
TPtr8 buf( NULL, 0, 0 );
buf.Set( (TUint8*)des, bytes, bytes );
file.Write( buf, bytes );
}
TBool CFileIO::ReadBool( RFile& file)
{
TBuf8< 8 > buf;
TUint8* res;
file.Read( buf, 1 );
res = ( TUint8* )buf.PtrZ();
return *( ( TInt8* )res );
}
void CFileIO::WriteBool( RFile& file, void* des )
{
TPtr8 buf( NULL, 0, 0 );
buf.Set( (TUint8*)des, 1, 1 );
file.Write( buf,1 );
}
TInt CFileIO::DelFile( TDesC& name )
{
RFs fs;
User::LeaveIfError( fs.Connect() );
TInt err = fs.Delete( name );
fs.Close();
return err;
}
TInt CFileIO::GetFileSize( TDesC& name )
{
RFs fs;
User::LeaveIfError( fs.Connect() );
RFile file;
User::LeaveIfError( file.Open( fs, name, EFileRead ) );
TInt size;
file.Size( size );
file.Close();
fs.Close();
return size;
}
TBool CFileIO::OpenForRead( RFs& rfs, RFile& file, const TDesC& name )
{
if ( KErrNone != rfs.Connect() )
return EFalse;
if ( KErrNone != file.Open( rfs, name, EFileRead ) ) {
rfs.Close();
return EFalse;
}
return ETrue;
}
TBool CFileIO::OpenForWrite( RFs& fs, RFile& file, const TDesC& name )
{
if ( KErrNone != fs.Connect() )
return EFalse;
if ( KErrNone != file.Open( fs, name, EFileWrite ) ) {
if ( KErrNone != file.Create( fs, name, EFileWrite ) ) {
fs.Close();
return EFalse;
}
}
return ETrue;
}
void CFileIO::Close( RFs& fs, RFile& file )
{
file.Close();
fs.Close();
}
TBool CFileIO::OpenAndGetAppendPos(RFs& fs,const TDesC& aName,RFile& file)
{
fs.Connect();
if ( KErrNone != file.Open( fs, aName, EFileWrite ) ) {
if ( KErrNone != file.Create( fs,aName, EFileWrite ) ) {
fs.Close();
return EFalse;
}
}
TInt Pos=0;
file.Seek(ESeekEnd,Pos);
return ETrue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -