将链表中的数据保存到二进制文件和读出数据

将链表中的数据保存到二进制文件和读出数据

std::vector<TMtSetupCreateConfParam_Api> m_v4tConfTemplateList; 


BOOL CCallMeetingLogic::V4LoadConfTemplateListFromFile()
{
	// 载入链表
	CString strMsg;
	strMsg.Format( _T("[CCallMeetingLogic::V4LoadConfTemplateListFromFile] m_bV4ConfTemplateListLoaded = %d."), m_bV4ConfTemplateListLoaded );
	//	WriteUnreadMsgLog( strMsg );

	// 已经加载过了,就不再加载
	if( m_bV4ConfTemplateListLoaded )
	{
		return FALSE;
	}

	m_v4tConfTemplateList.clear();

	CString strPath = GetSelfFilePath();

	CFile cFileContactInfo;
	CString strFileName = _T("V4ConfTemplateList.htr");
	if( FALSE == cFileContactInfo.Open( strPath + strFileName, CFile::modeRead/*, &ex*/ ) )
	{
		m_bV4ConfTemplateListLoaded = TRUE;
// 		if ( m_pV4ContactPageInterface != NULL )
// 		{
// 			m_pV4ContactPageInterface->V4CheckShowContactPage();
// 		}
		return FALSE;
	}

	// 读取记录文件版本号
	s8 achVerBuf[V4_CONF_TEMPLATELIST_VERSION_STR_LEN] = "\0";
	u32 dwNumByteRead = 0;

	dwNumByteRead = cFileContactInfo.Read( achVerBuf, V4_CONF_TEMPLATELIST_VERSION_STR_LEN );
	if( dwNumByteRead != V4_CONF_TEMPLATELIST_VERSION_STR_LEN )
	{
		cFileContactInfo.Close(); 
		DeleteFile( strPath + strFileName ); 
		m_bV4ConfTemplateListLoaded = TRUE;
// 		if ( m_pV4ContactPageInterface != NULL )
// 		{
// 			m_pV4ContactPageInterface->V4CheckShowContactPage();
// 		}
		return FALSE;
	}

	// 比对记录文件版本号,若是老版本的记录文件,则直接删除
	CString strVer;
	CopyUtf8ToCStringT( strVer, achVerBuf );

	if( strVer != V4_CONF_TEMPLATELIST_VERSION )
	{
		cFileContactInfo.Close();
		DeleteFile( strPath + strFileName );
		m_bV4ConfTemplateListLoaded = TRUE;
// 		if ( m_pV4ContactPageInterface != NULL )
// 		{
// 			m_pV4ContactPageInterface->V4CheckShowContactPage();
// 		}
		return FALSE;
	}

	// 读取记录用户jid
	s8 achJidBuf[V4_CONTACT_JID_STR_LEN] = "\0";
	dwNumByteRead = cFileContactInfo.Read( achJidBuf, V4_CONTACT_JID_STR_LEN );
	if( dwNumByteRead != V4_CONTACT_JID_STR_LEN )
	{
		cFileContactInfo.Close(); 
		DeleteFile( strPath + strFileName ); 
		m_bV4ConfTemplateListLoaded = TRUE;
// 		if ( m_pV4ContactPageInterface != NULL )
// 		{
// 			m_pV4ContactPageInterface->V4CheckShowContactPage();
// 		}
		return FALSE;
	}

	// 比对记录文件Jid,若不是当前用户,则直接删除

	if( strcmp( achJidBuf, GetConfigPtr()->GetLoginInfo().szNum ) )
	{
		cFileContactInfo.Close();
		DeleteFile( strPath + strFileName );
		m_bV4ConfTemplateListLoaded = TRUE;
// 		if ( m_pV4ContactPageInterface != NULL )
// 		{
// 			m_pV4ContactPageInterface->V4CheckShowContactPage();
// 		}
		return FALSE;
	}

	TMtSetupCreateConfParam_Api tContactInfo;
	while( TRUE )
	{ 
		u32 uReadBytes = cFileContactInfo.Read( &tContactInfo, sizeof(tContactInfo) );
		if( uReadBytes != sizeof(tContactInfo) )
		{
			break;
		}
		else
		{
			m_v4tConfTemplateList.push_back( tContactInfo );
		}
	}

	cFileContactInfo.Close();
	m_bV4ConfTemplateListLoaded = TRUE;
	//UpdateContactList();

	V4SaveConfTemplateListToFile();
	return TRUE;
}

BOOL CCallMeetingLogic::V4SaveConfTemplateListToFile()
{
	// 保存链表
	CString szFileName;
	szFileName.Format( _T("%sV4ConfTemplateList.htr"), GetSelfFilePath() );

	CFile cFileContactInfo;
	if( FALSE == cFileContactInfo.Open( szFileName, CFile::modeCreate | CFile::modeWrite/*, &ex*/) )
	{
		int n = GetLastError();
		return FALSE;
	}

	if( 0 == m_v4tConfTemplateList.size() )
	{
		cFileContactInfo.Close();
		return FALSE;
	}

	// 写联系人信息版本信息 
	s8 achVerBuf[V4_CONF_TEMPLATELIST_VERSION_STR_LEN] = "\0";
	CString strVer = V4_CONF_TEMPLATELIST_VERSION;
	CopyCStringTToUtf8( achVerBuf, strVer, sizeof(achVerBuf) );
	cFileContactInfo.Write( achVerBuf, sizeof(achVerBuf) );

	// 写用户jid号
	s8 achNameBuf[V4_CONTACT_JID_STR_LEN] = "\0";

	SafeCharBufCopy( achNameBuf, GetConfigPtr()->GetLoginInfo().szNum, V4_CONTACT_JID_STR_LEN );
	cFileContactInfo.Write( achNameBuf, V4_CONTACT_JID_STR_LEN );


	// 只保存最近的MAX_RECENT_CONTACT_COUNT条记录
	u32 dwOffset = 0;
	// 	if( m_vctContactInfoList.size() > MAX_RECENT_CONTACT_COUNT )
	// 	{
	// 		dwOffset = m_vctRecentContactInfoList.size() - MAX_RECENT_CONTACT_COUNT;
	// 	}
	for( u32 dwIndex = dwOffset; dwIndex < (u32)m_v4tConfTemplateList.size(); dwIndex++ )
	{
		TMtSetupCreateConfParam_Api info = m_v4tConfTemplateList[dwIndex];
		cFileContactInfo.Write( &info, sizeof(info) );
	}

	cFileContactInfo.Close();
	return TRUE;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值