将链表中的数据保存到二进制文件和读出数据
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;
}
本文介绍了一个用于会议逻辑的类CCallMeetingLogic中的两个关键函数:V4SaveConfTemplateListToFile和V4LoadConfTemplateListFromFile。这两个函数负责将链表中的会议配置数据保存到二进制文件中以及从文件中读取这些数据。文章详细描述了如何进行文件的创建、打开、读取和写入操作,并检查文件的版本和用户标识确保数据的一致性和正确性。

1986

被折叠的 条评论
为什么被折叠?



