⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readandwriteappui.cpp

📁 在symbian下读和写文件源代码
💻 CPP
字号:
/*
============================================================================
 Name        : ReadAndWriteAppUi.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Main application UI class (controller)
============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <ReadAndWrite.rsg>
#include <f32file.h>
#include <s32file.h>

#include "ReadAndWrite.pan"
#include "ReadAndWriteAppUi.h"
#include "ReadAndWriteAppView.h"
#include "ReadAndWrite.hrh"

#include <CHARCONV.H>

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CReadAndWriteAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CReadAndWriteAppUi::ConstructL()
    {
    // Initialise app UI with standard value.
    BaseConstructL();

    // Create view object
    iAppView = CReadAndWriteAppView::NewL( ClientRect() );
    }
// -----------------------------------------------------------------------------
// CReadAndWriteAppUi::CReadAndWriteAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CReadAndWriteAppUi::CReadAndWriteAppUi()
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CReadAndWriteAppUi::~CReadAndWriteAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CReadAndWriteAppUi::~CReadAndWriteAppUi()
    {
    if ( iAppView )
        {
        delete iAppView;
        iAppView = NULL;
        }

    }

// -----------------------------------------------------------------------------
// CReadAndWriteAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CReadAndWriteAppUi::HandleCommandL( TInt aCommand )
    {
    switch( aCommand )
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            Exit();
            break;
        case EReadAndWriteCommand1:
            {
            // Load a string from the resource file and display it
            HBufC* textResource = StringLoader::LoadLC( R_HEWB_COMMAND1_TEXT );
            
            // Show the information Note with
            // textResource loaded with StringLoader.
            CAknInformationNote* informationNote = new ( ELeave ) CAknInformationNote;
            informationNote->ExecuteLD( *textResource );

            // Pop HBuf from CleanUpStack and Destroy it.
            CleanupStack::PopAndDestroy( textResource );
            }
            break;
		case EReadCommand:
			{
				
				this->Read(buf);
				this->GetType(buf,_L("sip"),buf1);
				this->GetType(buf,_L("Port"),buf2);
				this->GetType(buf,_L("Port2"),buf3);
				this->GetType(buf,_L("name"),buf4);
				this->fullbuf.Append(buf1);
				this->fullbuf.Append(_L(" "));
				this->fullbuf.Append(buf2);
				this->fullbuf.Append(_L("\n"));
				this->fullbuf.Append(buf3);
				this->fullbuf.Append(_L(" "));
				this->fullbuf.Append(buf4);

				
				CAknInformationNote* informationNote1 = new ( ELeave ) CAknInformationNote;
				informationNote1->ExecuteLD(this->fullbuf );
				
			}
			break;
		case EWriteCommand:
			{
				this->Write(_L("sip"),_L("Tel"));
				this->Write(_L("Port"),90000);
				this->Write(_L("Port"),100000);
				this->Write(_L("Port"),3300000);
				this->Write(_L("Port2"),222);
				this->Write(_L("name"),_L("Sunyuanjie"));
			}
			break;
        default:
            Panic( EReadAndWriteUi );
            break;
        }
    }
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CReadAndWriteAppUi::HandleStatusPaneSizeChange()
{
	iAppView->SetRect( ClientRect() );
	
} 

void CReadAndWriteAppUi::Read(TDes16 &readbuf)
{
	TBuf8<120> buf;
	buf.Zero ();
	RFile iFile;
	RFs rfs;
	TInt aErr;
	TInt iRead;
	rfs.Connect();
	_LIT( KLOG_FILE, "C:\\data\\text.txt" );
	aErr = iFile.Open(rfs, KLOG_FILE, EFileRead);
	if ( KErrNotFound == aErr )
	{
		return ;
	}
	User::LeaveIfError(iRead = iFile.Read(buf));
	readbuf.Copy (buf);
	iFile.Close();
	rfs.Close();
}

void CReadAndWriteAppUi::Write(const TDesC &keystring,const TDesC &writestringbuf)
{
	TBuf<100> stringbuf;
	TBuf<100> tempbuf;
	TBuf<10>  tempbuf1;
	this->Read(stringbuf);
	TInt flag;
	flag = stringbuf.Find(keystring);
	if(flag == -1)
	{
		stringbuf.Append(keystring);
		stringbuf.Append(_L(":"));
		stringbuf.Append(writestringbuf);
		stringbuf.Append(_L("end"));
		this->Save(stringbuf);
	}
	else
	{
		tempbuf.Copy(stringbuf.Mid(0,flag ));
		stringbuf.Delete (0,flag);
		flag = stringbuf.Find (_L("end"));
		stringbuf.Delete(0,flag);
		stringbuf.Insert(0,writestringbuf);
		stringbuf.Insert (0,_L(":"));
		stringbuf.Insert(0,keystring);
		stringbuf.Insert(0,tempbuf);
		this->Save(stringbuf);
	}
}

void CReadAndWriteAppUi::Write(const TDesC &keystring,TInt writeint)
{
	TBuf<100> stringbuf;
	TBuf<100> tempbuf;
	TBuf<10>  tempbuf1;
	this->Read(stringbuf);
	TInt flag;
	flag = stringbuf.Find(keystring);
	if(flag == -1)
	{
		stringbuf.Append(keystring);
		stringbuf.Append(_L(":"));
		stringbuf.AppendNum(writeint);
		stringbuf.Append(_L("end"));
		this->Save(stringbuf);
	}
	else
	{
		tempbuf.Copy(stringbuf.Mid(0,flag));
		stringbuf.Delete (0,flag);
		flag = stringbuf.Find (_L("end"));
		stringbuf.Delete(0,flag );
		tempbuf1.AppendNum(writeint);
		stringbuf.Insert(0,tempbuf1);
		stringbuf.Insert (0,_L(":"));
		stringbuf.Insert(0,keystring);
		stringbuf.Insert(0,tempbuf);
		this->Save(stringbuf);
	}
}

void CReadAndWriteAppUi::Save(const TDesC &savestringbuf)
{
	TBuf8<100> tempbuf;
	tempbuf.Copy(savestringbuf);
	RFile iFile;
	TInt aErr;
	RFs rfs;
	rfs.Connect();
	aErr = iFile.Open(rfs, _L("c:\\data\\text.txt"), EFileWrite);	
	if ( KErrNotFound == aErr )
	{
		aErr = iFile.Create(rfs, _L("c:\\data\\text.txt"), EFileWrite);
	}
	TInt pos1=0;
	User::LeaveIfError(iFile.Seek(ESeekStart,pos1));
	User::LeaveIfError(aErr);
	User::LeaveIfError(iFile.Write(tempbuf));
	iFile.Close();
	rfs.Close();
}


void CReadAndWriteAppUi::GetType(const TDesC& mybuf,const TDesC& identifier,TDes& type)
{
	
	TBuf<120> buf1;
	TBuf<20> buf2;

	TInt pos = mybuf.Find (identifier);
	if (pos == -1)
	{
		return ;
	}
	buf1.Copy(mybuf.Mid (pos,mybuf.Length() - pos));
	pos = buf1.Find(_L(":"));
	buf1.Delete (0,pos+1);
	pos = buf1.Find(_L("end"));
	buf2.Copy (buf1.Mid (0,pos));
	type = buf2;
	
}


void CReadAndWriteAppUi::ConvertToUnicode(TDes16 &aUnicode, const TDesC8 &aForeign)
{
	CCnvCharacterSetConverter* convert = CCnvCharacterSetConverter::NewL();
	CleanupStack::PushL(convert);

	RFs rfs;
	User::LeaveIfError(rfs.Connect());
	if( convert->PrepareToConvertToOrFromL(	KCharacterSetIdentifierGb2312,rfs )
		!= CCnvCharacterSetConverter::EAvailable )
	{
		User::Leave(KErrNotSupported);
		return;
	}
	CleanupStack::Pop();

	TInt state = CCnvCharacterSetConverter::KStateDefault;
	convert->ConvertToUnicode(aUnicode,aForeign,state);

	rfs.Close();
	delete convert;
}

void CReadAndWriteAppUi::ConvertFromUnicode(TDes8 &aForeign, const TDesC16 &aUnicode)
{
	
	CCnvCharacterSetConverter* convert = CCnvCharacterSetConverter::NewL();
	CleanupStack::PushL(convert);

	RFs rfs;
	User::LeaveIfError(rfs.Connect());
	if( convert->PrepareToConvertToOrFromL(	KCharacterSetIdentifierGb2312,rfs )
		!= CCnvCharacterSetConverter::EAvailable )
	{
		User::Leave(KErrNotSupported);
		return;
	}
	CleanupStack::Pop();
	convert->ConvertFromUnicode(aForeign,aUnicode);

	rfs.Close();
	delete convert;
}


// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -