// AddressBookDoc.cpp : implementation of the CAddressBookDoc class
//
#include "stdafx.h"
#include "AddressBook.h"
#include "AddressBookDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//#define DEFAULT_FILE ".\\default.dat"
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc
IMPLEMENT_DYNCREATE(CAddressBookDoc, CDocument)
BEGIN_MESSAGE_MAP(CAddressBookDoc, CDocument)
//{{AFX_MSG_MAP(CAddressBookDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc construction/destruction
CAddressBookDoc::CAddressBookDoc()
{
m_pCurSelect = NULL;
}
CAddressBookDoc::~CAddressBookDoc()
{
for(POSITION pos = m_lsUserInfo.GetHeadPosition();
pos != NULL; m_lsUserInfo.GetNext(pos))
delete (USERINFO*)m_lsUserInfo.GetAt(pos);
}
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc serialization
void CAddressBookDoc::Serialize(CArchive& ar)
{
DWORD dwFlag = 0;
USERINFO *pInfo = NULL;
if (ar.IsStoring())
{ // 保存数据
dwFlag = ~0;
POSITION pos = m_lsUserInfo.GetHeadPosition();
while (pos != NULL)
{
pInfo = (USERINFO*)m_lsUserInfo.GetAt(pos);
m_lsUserInfo.GetNext(pos);
ar<<dwFlag;
ar.Write(pInfo, sizeof(USERINFO));
/*
ar.Write(pInfo->szHomeCity, sizeof(pInfo->szHomeCity));
ar.Write(pInfo->szHomeFax, sizeof(pInfo->szHomeFax));
ar.Write(pInfo->szHomeMobile, sizeof(pInfo->szHomeMobile));
ar.Write(pInfo->szHomePhone, sizeof(pInfo->szHomePhone));
ar.Write(pInfo->szHomePost, sizeof(pInfo->szHomePost));
ar.Write(pInfo->szHomeProv, sizeof(pInfo->szHomeProv));
ar.Write(pInfo->szHomeStreet, sizeof(pInfo->szHomeStreet));
ar.Write(pInfo->szHomeZone, sizeof(pInfo->szHomeZone));
ar.Write(pInfo->szJobCity, sizeof(pInfo->szJobCity));
ar.Write(pInfo->szJobDept, sizeof(pInfo->szJobDept));
ar.Write(pInfo->szJobDuty, sizeof(pInfo->szJobDuty));
ar.Write(pInfo->szJobFax, sizeof(pInfo->szJobFax));
ar.Write(pInfo->szJobName, sizeof(pInfo->szJobName));
ar.Write(pInfo->szJobOffice, sizeof(pInfo->szJobOffice));
ar.Write(pInfo->szJobPhone, sizeof(pInfo->szJobPhone));
ar.Write(pInfo->szJobPost, sizeof(pInfo->szJobPost));
ar.Write(pInfo->szJobProv, sizeof(pInfo->szJobProv));
ar.Write(pInfo->szJobStreet, sizeof(pInfo->szJobStreet));
ar.Write(pInfo->szJobZone, sizeof(pInfo->szJobZone));
ar.Write(pInfo->szUserDuty, sizeof(pInfo->szUserDuty));
ar.Write(pInfo->szUserEmail[0], sizeof(pInfo->szUserEmail[0]));
ar.Write(pInfo->szUserEmail[1], sizeof(pInfo->szUserEmail[1]));
ar.Write(pInfo->szUserEmail[2], sizeof(pInfo->szUserEmail[2]));
ar.Write(pInfo->szUserEmail[3], sizeof(pInfo->szUserEmail[3]));
ar.Write(pInfo->szUserEmail[4], sizeof(pInfo->szUserEmail[4]));
ar.Write(pInfo->szUserName, sizeof(pInfo->szUserName));
ar.Write(pInfo->szUserNick, sizeof(pInfo->szUserNick));
ar.Write(pInfo->szUserSex, sizeof(pInfo->szUserSex));
*/
}
dwFlag = 0;
ar<<dwFlag;
}
else
{ // 读取数据
ar>>dwFlag;
while (dwFlag != 0)
{
pInfo = new USERINFO;
memset(pInfo, 0, sizeof(USERINFO));
ar.Read(pInfo, sizeof(USERINFO));
/*
ar.Read(pInfo->szHomeCity, sizeof(pInfo->szHomeCity));
ar.Read(pInfo->szHomeFax, sizeof(pInfo->szHomeFax));
ar.Read(pInfo->szHomeMobile, sizeof(pInfo->szHomeMobile));
ar.Read(pInfo->szHomePhone, sizeof(pInfo->szHomePhone));
ar.Read(pInfo->szHomePost, sizeof(pInfo->szHomePost));
ar.Read(pInfo->szHomeProv, sizeof(pInfo->szHomeProv));
ar.Read(pInfo->szHomeStreet, sizeof(pInfo->szHomeStreet));
ar.Read(pInfo->szHomeZone, sizeof(pInfo->szHomeZone));
ar.Read(pInfo->szJobCity, sizeof(pInfo->szJobCity));
ar.Read(pInfo->szJobDept, sizeof(pInfo->szJobDept));
ar.Read(pInfo->szJobDuty, sizeof(pInfo->szJobDuty));
ar.Read(pInfo->szJobFax, sizeof(pInfo->szJobFax));
ar.Read(pInfo->szJobName, sizeof(pInfo->szJobName));
ar.Read(pInfo->szJobOffice, sizeof(pInfo->szJobOffice));
ar.Read(pInfo->szJobPhone, sizeof(pInfo->szJobPhone));
ar.Read(pInfo->szJobPost, sizeof(pInfo->szJobPost));
ar.Read(pInfo->szJobProv, sizeof(pInfo->szJobProv));
ar.Read(pInfo->szJobStreet, sizeof(pInfo->szJobStreet));
ar.Read(pInfo->szJobZone, sizeof(pInfo->szJobZone));
ar.Read(pInfo->szUserDuty, sizeof(pInfo->szUserDuty));
ar.Read(pInfo->szUserEmail[0], sizeof(pInfo->szUserEmail[0]));
ar.Read(pInfo->szUserEmail[1], sizeof(pInfo->szUserEmail[1]));
ar.Read(pInfo->szUserEmail[2], sizeof(pInfo->szUserEmail[2]));
ar.Read(pInfo->szUserEmail[3], sizeof(pInfo->szUserEmail[3]));
ar.Read(pInfo->szUserEmail[4], sizeof(pInfo->szUserEmail[4]));
ar.Read(pInfo->szUserName, sizeof(pInfo->szUserName));
ar.Read(pInfo->szUserNick, sizeof(pInfo->szUserNick));
ar.Read(pInfo->szUserSex, sizeof(pInfo->szUserSex));
*/
m_lsUserInfo.AddTail(pInfo);
ar>>dwFlag;
}
}
// m_lsUserInfo.Serialize(ar);
UpdateAllViews(NULL, 0L, NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc diagnostics
#ifdef _DEBUG
void CAddressBookDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CAddressBookDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc commands
BOOL CAddressBookDoc::OnNewDocument()
{
return CDocument::OnNewDocument();
}
void CAddressBookDoc::OnCloseDocument()
{
CDocument::OnCloseDocument();
}
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc implements
// 判断联系人信息链表是否为空
BOOL CAddressBookDoc::IsListEmpty()
{
return m_lsUserInfo.IsEmpty(); // 链表是否为空
}
// 添加联系人信息
BOOL CAddressBookDoc::AddUserInfo(USERINFO *pInfo)
{
ASSERT(pInfo != NULL); // pInfo必须有效
if (m_lsUserInfo.Find(pInfo)) // 链表中是否已经存在该信息
return FALSE;
m_lsUserInfo.AddTail(pInfo); // 项链表中添加一个联系人信息
UpdateAllViews(NULL, 0L, NULL); // 更新所有视图
return TRUE;
}
// 修改联系人信息
BOOL CAddressBookDoc::ModUserInfo(USERINFO *pInfo)
{
ASSERT(pInfo != NULL);
POSITION pos = m_lsUserInfo.Find(pInfo); // 找出联系人在链表中的位置
if (pos == NULL) return FALSE; // 链表中不含有该联系人信息
m_lsUserInfo.SetAt(pos, pInfo); // 修改联系人信息
UpdateAllViews(NULL, 0L, NULL);
return TRUE;
}
// 删除联系人信息
BOOL CAddressBookDoc::DelUserInfo(USERINFO *pInfo)
{
ASSERT(pInfo != NULL);
POSITION pos = m_lsUserInfo.Find(pInfo); // 找出联系人在链表中的位置
if (pos == NULL) return FALSE;
m_lsUserInfo.RemoveAt(pos); // 从链表中删除联系人信息的指针
delete pInfo; // 释放内存
UpdateAllViews(NULL, 0L, NULL);
return TRUE;
}
// 取得第一个联系人信息
USERINFO* CAddressBookDoc::GetHeadInfo(USERINFO *pInfo)
{
if (pInfo == NULL