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

📄 frmesdb.cpp

📁 symbian 3nd 实现短信息收发
💻 CPP
字号:
#include "FrMesDB.h"
#include "FrMes.rsg"

#include <s32file.h>
#include <eikenv.h>


CFrMesDB* CFrMesDB::NewL()
{
	CFrMesDB* self = NewLC();
    CleanupStack::Pop(self);
    return self;
}

CFrMesDB* CFrMesDB::NewLC()
{
	CFrMesDB* self = new (ELeave) CFrMesDB;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

void CFrMesDB::ConstructL()
{
	iArray = new(ELeave) CArrayFixFlat<TInt32>(10); //构造数组,粒度为10
	m_CurrentIndex = 0;  //初始化当前索引值为0
}

CFrMesDB::CFrMesDB()
{
}

CFrMesDB::~CFrMesDB()
{
	TInt num = iArray->Count();
	if(num!=0)
	{ 
		for(TInt i=0;i<num;i++)
		{
			delete (TFrMesDBStruct*)iArray->At(i);
		}
	}
	delete iArray;
	iArray = NULL;
}

void CFrMesDB::SaveDBL()
{
	RFs& fs = CEikonEnv::Static()->FsSession();//声明一个RFs类型的句柄以访问File服务器
	RFileWriteStream writer;    //声明一个输出流

	TInt err = writer.Replace(fs, KFrMesFileName, EFileWrite);//打开文件
	if (err != KErrNone) 
	{
		return;
	}
	writer.PushL();       //进栈
	TInt nFileLen = iArray->Count(); //得到数组中的元素个数
// 	if(nFileLen <= 0)                //如果数组中没有元素直接退出函数
// 	{
// 		writer.Pop();     //出栈
// 		writer.Release(); //释放	
// 		return;
// 	}
	writer.WriteInt32L(nFileLen);//把数组中的元素个数写入文件
	TFrMesDBStruct* tmp;
	for(TInt i=0;i<nFileLen;i++) //把数组中的元素依次写入文件
	{
		tmp = (TFrMesDBStruct*)iArray->At(i);
		writer << tmp->iName;
		writer << tmp->iCode;
	}
    writer.CommitL(); //提交
	writer.Pop();     //出栈
	writer.Release(); //释放
//	fs.Close();       //关闭
}

void CFrMesDB::ReadDBL()
{
	RFileReadStream reader;    //声明一个输入流
    User::LeaveIfError(reader.Open(CEikonEnv::Static()->FsSession(), KFrMesFileName, EFileRead));  //打开一个文件
	reader.PushL();
	
	TInt32 numberOfelem;
	numberOfelem = reader.ReadInt32L();//读取元素个数
	iArray->Reset();                   //数组清空
	//BrowseList();
	for(TInt i=0;i<numberOfelem;i++)   //依次读出元素到iArray数组
	{
		TFrMesDBStruct * mes = new(ELeave) TFrMesDBStruct;
		reader >> mes->iName;
		reader >> mes->iCode;
		iArray->AppendL((TInt32)mes);
	}
	reader.Pop();
	reader.Release();
}

void CFrMesDB::AddInfoL(TDes& aName,TDes& aCode) //add a new info to the array.
{
	TFrMesDBStruct * mes = new(ELeave) TFrMesDBStruct;//在堆上创建一个存储TFrMesDBStruct类型对象的空间
	mes->iName = aName;
	mes->iCode = aCode;
	iArray->AppendL((TInt32)mes);      //加入数组
}

void CFrMesDB::DelInfo(TInt aIndex) //delete a info from the array.
{
	TFrMesDBStruct* tmp = (TFrMesDBStruct*)iArray->At(aIndex);//记录要删除的对象指针
	iArray->Delete(aIndex);//从数组中删除
	delete tmp; //释放空间
}

void CFrMesDB::GetItem(TInt aIndex,TDes& aName, TDes & aCode)
{
	TFrMesDBStruct* tmp = (TFrMesDBStruct*)iArray->At(aIndex);//得到数组中第aIndex个元素
	aName = tmp->iName;           //给引用赋值
	aCode = tmp->iCode;
}

⌨️ 快捷键说明

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