www.pudn.com > DTDaemon.rar > DTFile.cpp


/* 
============================================================================ 
 Name        : DTFile.cpp 
 Author      :  
 Version     : 
 Copyright   : Your copyright notice 
 Description : DTFile.cpp - source file 
============================================================================ 
*/ 
 
// INCLUDE FILES 
#include "DTFile.h" 
 
void test() 
{ 
	_LIT(KHello, "hello"); 
	TPtrC helloptr(KHello); 
 
	const wchar_t* a = reinterpret_cast(helloptr.Ptr()); 
} 
 
void CDTFile::ConstructL() 
{ 
	iIsOpen = false; 
	User::LeaveIfError(iRFs.Connect()); 
} 
 
CDTFile::~CDTFile() 
{ 
	delete iFileName; 
	iFileName = 0; 
	iReader.Close(); 
	iWriter.Close(); 
	iRFs.Close(); 
} 
 
CDTFile::CDTFile() 
{ 
} 
 
CDTFile* CDTFile::NewLC() 
{ 
	CDTFile* self = new(ELeave) CDTFile; 
	CleanupStack::PushL(self); 
	self->ConstructL(); 
	return self; 
} 
 
TInt CDTFile::OpenForRead(const TDesC& aFileName) 
{ 
	TInt reason = 0; 
	iFileName = aFileName.AllocL(); 
	iMode = KDTRead; 
 
	RFile file; 
	reason = file.Open( iRFs, *iFileName, EFileRead ); 
	if(reason == KErrNone) 
	{ 
		file.Size(iFileLength); 
		file.Close(); 
	} 
	else 
		return reason; 
 
	reason = iReader.Open(iRFs, *iFileName, EFileRead); 
	if(reason == KErrNone) 
	{ 
		iIsOpen = true; 
	} 
	else 
	{ 
		iIsOpen = false; 
	} 
	return reason; 
} 
 
TInt CDTFile::OpenForWrite(const TDesC& aFileName) 
{ 
	TInt reason = 0; 
	iFileName = aFileName.AllocL(); 
	iMode = KDTWrite; 
 
	reason = iWriter.Replace(iRFs, *iFileName, EFileWrite); 
	if(reason == KErrNone) 
	{ 
		iIsOpen = true; 
	} 
	else 
	{ 
		iIsOpen = false; 
	} 
	return reason; 
} 
 
TInt CDTFile::Read(TDes8& aBuffer, TInt aLength) 
{ 
	TRAPD(error, iReader.ReadL(aBuffer, aLength)); 
	if(error ==  KErrEof) 
	{ 
		aLength = iFileLength - iReadLength; 
		aLength = aLength>0?aLength:0; 
		return aLength; 
	} 
	iReadLength += aLength; 
	return aLength; 
} 
 
TInt CDTFile::Write(TDes8& aBuffer, TInt aLength) 
{ 
	TRAPD(error, iWriter.WriteL(aBuffer, aLength)); 
	if(error == KErrNone) return aLength; 
	return 0; 
} 
 
TBool CDTFile::isOpen() 
{ 
	return iIsOpen; 
} 
 
TInt CDTFile::GetLength() 
{ 
	return iFileLength; 
} 
 
HBufC* CDTFile::AnsiToUnicodeL( const TDesC8& aAnsi ) 
{ 
	return CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAnsi); 
} 
 
void CDTFile::Close() 
{ 
	if(iMode == KDTRead && isOpen() ) iReader.Close(); 
	if(iMode == KDTWrite && isOpen() ) iWriter.Close(); 
}