Click here to Skip to main content
15,896,493 members
Articles / Programming Languages / C++

Address Book

Rate me:
Please Sign up or sign in to vote.
4.48/5 (21 votes)
6 Aug 2000CPOL3 min read 287.5K   5.5K   93  
Address Book application
// AddressDoc.cpp : implementation of the CAddressDoc class
//

#include "stdafx.h"
#include "Address.h"

#include "AddressDoc.h"
#include "CntrItem.h"
#include "PersonPropSheet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAddressDoc

IMPLEMENT_DYNCREATE(CAddressDoc, CDocument)

BEGIN_MESSAGE_MAP(CAddressDoc, CDocument)
	//{{AFX_MSG_MAP(CAddressDoc)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_IMPORT, OnImport)
	//}}AFX_MSG_MAP
	// Enable default OLE container implementation
END_MESSAGE_MAP()


// Serialize the elements of the CPerson class so that the objects will be saved.
template <> void AFXAPI SerializeElements <CPerson> ( CArchive& ar, CPerson* pNewPersons, int nCount )	{
	for ( int i = 0; i < nCount; i++, pNewPersons++ )    {
		// Serialize each CPerson object
		pNewPersons->Serialize( ar );
	}
}

/////////////////////////////////////////////////////////////////////////////
// CAddressDoc construction/destruction

CAddressDoc::CAddressDoc()
{
//	m_PersonArray.SetSize(30,10);

}

CAddressDoc::~CAddressDoc()
{
}

BOOL CAddressDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CAddressDoc serialization

void CAddressDoc::Serialize(CArchive& ar)
{

	try {
		m_PersonArray.Serialize(ar);
	} 
	catch (CArchiveException* e) {
	   AfxMessageBox("Data File is corrupted or of wrong version\n"
		 "Please replace or delete Address.dat file", MB_OK );
		e->Delete();
	}
	catch (CException* e) {
		e->ReportError();
		e->Delete();
	}

}

/////////////////////////////////////////////////////////////////////////////
// CAddressDoc diagnostics

#ifdef _DEBUG
void CAddressDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CAddressDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAddressDoc commands


// New contact
void CAddressDoc::OnFileNew() 
{
	CPersonPropSheet PersonPropSheet("Properties");
	CPersonalPage personalPage;
	CHomePage     homePage;
	CBusinessPage businessPage;
	CPerson Person;

	PersonPropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
	PersonPropSheet.AddPage(&personalPage);
	PersonPropSheet.AddPage(&homePage);
	PersonPropSheet.AddPage(&businessPage);

	if (PersonPropSheet.DoModal() == IDOK) {
          Person.m_strFirstName = personalPage.m_strFirstName  ;
	      Person.m_strLastName  = personalPage.m_strLastName;
		  Person.m_strMiddleName= personalPage.m_strMiddleName;
		  Person.m_strName=       personalPage.m_strName;
		  Person.m_strNickName=   personalPage.m_strNickName;
		  Person.m_strEMail=      personalPage.m_strEMail;
		  Person.m_strNotes=      personalPage.m_strNotes;

		  Person.m_strHomeStreet=      homePage.m_strHomeStreet;
		  Person.m_strHomeCity=        homePage.m_strHomeCity;
		  Person.m_strHomePostalCode=  homePage.m_strHomePostalCode;
		  Person.m_strHomeState=       homePage.m_strHomeState;
		  Person.m_strHomeCountry=     homePage.m_strHomeCountry;
		  Person.m_strHomePhone=       homePage.m_strHomePhone;
		  Person.m_strHomeFax=         homePage.m_strHomeFax;
		  Person.m_strCarPhone=        homePage.m_strCarPhone;
		  Person.m_strPersonalWebPage= homePage.m_strPersonalWebPage;

		  Person.m_strBusinessStreet=    businessPage.m_strBusinessStreet;
		  Person.m_strBusinessCity=      businessPage.m_strBusinessCity;
		  Person.m_strBusinessPostalCode=businessPage.m_strBusinessPostalCode;
		  Person.m_strBusinessState=     businessPage.m_strBusinessState;
		  Person.m_strBusinessCountry=   businessPage.m_strBusinessCountry;
		  Person.m_strBusinessWebPage=   businessPage.m_strBusinessWebPage;
		  Person.m_strBusinessPhone=     businessPage.m_strBusinessPhone;
		  Person.m_strBusinessFax=       businessPage.m_strBusinessFax;
		  Person.m_strPager=             businessPage.m_strPager;
		  Person.m_strCompany=           businessPage.m_strCompany;
		  Person.m_strJobTitle=          businessPage.m_strJobTitle;
		  Person.m_strDepartment=        businessPage.m_strDepartment;
		  Person.m_strOfficeLocation=    businessPage.m_strOfficeLocation;
		  
          m_PersonArray.Add(Person);
		  SetModifiedFlag();
		  UpdateAllViews(NULL);
	}
}

BOOL CAddressDoc::SaveModified() 
{
	if (IsModified()) {
		OnFileSave();
	}
	return TRUE;

}


// Import Comma Separated Value file 
// exported by Outlook Express
void CAddressDoc::OnImport() 
{
	CFileDialog FileDialog(TRUE, "*.csv",NULL,OFN_HIDEREADONLY , 
		"Comma Separated Values (*.csv)|*.csv||", AfxGetMainWnd());
	CString strPathName;
	char strText[5000];
	char* pstrToken;
	char* pstrCurrPos;
	char* pstrQuote;
	char* pstrComma;
    CStdioFile *fileImport= NULL;

	short nFirstName, nLastName,nMiddleName,nName;
	short nNickName, nEMail, nHomeStreet, nHomeCity;
	short nHomePostalCode, nHomeState, nHomeCountry;
	short nHomePhone, nHomeFax, nCarPhone, nPersonalWebPage;
	short nBusinessStreet, nBusinessCity, nBusinessPostalCode;
	short nBusinessState, nBusinessCountry, nBusinessWebPage;
	short nBusinessPhone, nBusinessFax, nPager, nCompany;
	short nJobTitle, nDepartment, nOfficeLocation, nNotes;

	nFirstName= nLastName=nMiddleName=nName=
	nNickName= nEMail= nHomeStreet= nHomeCity=
	nHomePostalCode= nHomeState= nHomeCountry=
	nHomePhone= nHomeFax= nCarPhone= nPersonalWebPage=
	nBusinessStreet= nBusinessCity=nBusinessPostalCode=
	nBusinessState= nBusinessCountry= nBusinessWebPage=
	nBusinessPhone= nBusinessFax= nPager= nCompany=
	nJobTitle= nDepartment= nOfficeLocation = nNotes= 0;

	if (FileDialog.DoModal() == IDCANCEL) 
		return;
	strPathName = FileDialog.GetPathName();

	try {
		fileImport = new CStdioFile(strPathName , 
			CFile::modeRead | CFile::typeText);
	}
    catch( CFileException* e ) {
		AfxMessageBox("Import file could not be opened");
		fileImport = NULL;
		e->Delete();
		return;
	}

	if (fileImport->ReadString(strText,5000) == NULL) {
		return;
	}
	*(strText+strlen(strText)-1)=',';
	pstrToken= strtok(strText,",");
	short nIndex = 1;


	// Find out the position of each element and 
	// what exists.
	do {
		if (strcmp(pstrToken,"First Name")== 0) 
			nFirstName= nIndex;
		if (strcmp(pstrToken,"Last Name")== 0) 
			nLastName= nIndex;
		if (strcmp(pstrToken,"Middle Name")== 0) 
			nMiddleName= nIndex;
		if (strcmp(pstrToken,"Name")== 0) 
			nName= nIndex;
		if (strcmp(pstrToken,"Nickname")== 0) 
			nNickName= nIndex;
		if (strcmp(pstrToken,"E-mail Address")== 0) 
			nEMail= nIndex;
		if (strcmp(pstrToken,"Home Street")== 0) 
			nHomeStreet= nIndex;
		if (strcmp(pstrToken,"Home City")== 0) 
			nHomeCity= nIndex;
		if (strcmp(pstrToken,"Home Postal Code")== 0) 
			nHomePostalCode= nIndex;
		if (strcmp(pstrToken,"Home State")== 0) 
			nHomeState= nIndex;
		if (strcmp(pstrToken,"Home Country")== 0) 
			nHomeCountry= nIndex;
		if (strcmp(pstrToken,"Home Phone")== 0) 
			nHomePhone= nIndex;
		if (strcmp(pstrToken,"Home Fax")== 0) 
			nHomeFax= nIndex;
		if (strcmp(pstrToken,"Car Phone")== 0) 
			nCarPhone= nIndex;
		if (strcmp(pstrToken,"Personal Web Page")== 0) 
			nPersonalWebPage= nIndex;
		if (strcmp(pstrToken,"Business Street")== 0) 
			nBusinessStreet= nIndex;
		if (strcmp(pstrToken,"Business City")== 0) 
			nBusinessCity= nIndex;
		if (strcmp(pstrToken,"Business Postal Code") == 0)
			nBusinessPostalCode= nIndex;
		if (strcmp(pstrToken,"Business State") == 0)
			nBusinessState= nIndex;
		if (strcmp(pstrToken,"Business Country") == 0)
			nBusinessCountry= nIndex;
		if (strcmp(pstrToken,"Business Web Page") == 0)
			nBusinessWebPage = nIndex;
		if (strcmp(pstrToken,"Business Phone") == 0)
			nBusinessPhone = nIndex;
		if (strcmp(pstrToken,"Business Fax") == 0)
			nBusinessFax= nIndex;
		if (strcmp(pstrToken,"Pager") == 0)
			nPager= nIndex;
		if (strcmp(pstrToken,"Company") == 0)
			nCompany= nIndex;
		if (strcmp(pstrToken,"Job Title") == 0)
			nJobTitle= nIndex;
		if (strcmp(pstrToken,"Department") == 0)
			nDepartment= nIndex;
		if (strcmp(pstrToken,"Office Location") == 0)
			nOfficeLocation= nIndex;
		if (strcmp(pstrToken, "Notes") == 0)
			nNotes = nIndex;
		nIndex++;
	} while ((pstrToken = strtok(NULL,",")) != NULL);


	// Read the data 
	while (fileImport->ReadString(strText,5000) != NULL) {
		nIndex = 1;
		pstrCurrPos = strText;
		*(strText+ strlen(strText) - 1) = ',';
		CPerson Person;
		Person.Empty();
		while (pstrCurrPos) {
			pstrToken = pstrCurrPos;
			pstrQuote = strstr(pstrCurrPos,"\"");
			pstrComma=  strstr(pstrCurrPos,",");
		
			if ((pstrQuote && (pstrQuote  < pstrComma)) || (pstrQuote && (pstrComma==NULL))) {
				pstrToken=pstrCurrPos = pstrQuote + 1;
				while ((pstrQuote = strstr(pstrCurrPos,"\"")) == NULL) {
					strcpy(pstrCurrPos + strlen(pstrCurrPos) - 1, "\r\n");
					fileImport->ReadString(pstrCurrPos + strlen(pstrCurrPos)  , 3000);
				}
                pstrCurrPos = pstrQuote + 1;
				*pstrQuote = 0;
			} else {
				pstrCurrPos = pstrComma;
			}
			
			if (pstrCurrPos) {
				*pstrCurrPos=0;
				pstrCurrPos++;
			}
			if (nFirstName== nIndex)
				Person.m_strFirstName=  (LPCTSTR)pstrToken;
			if (nLastName== nIndex )
				Person.m_strLastName=  (LPCTSTR)pstrToken;
			if (nMiddleName== nIndex)
				Person.m_strMiddleName=(LPCTSTR)pstrToken;
			if (nName== nIndex)
				Person.m_strName=(LPCTSTR)pstrToken;
			if (nNickName== nIndex)
				Person.m_strNickName=(LPCTSTR)pstrToken;
			if (nEMail== nIndex)
				Person.m_strEMail =(LPCTSTR) pstrToken;
			if (nHomeStreet== nIndex)
				Person.m_strHomeStreet= (LPCTSTR)pstrToken;
			if (nHomeCity== nIndex)
				Person.m_strHomeCity= (LPCTSTR)pstrToken;
			if (nHomePostalCode== nIndex)
				Person.m_strHomePostalCode= (LPCTSTR)pstrToken;
			if (nHomeState== nIndex)
				Person.m_strHomeState= (LPCTSTR)pstrToken;
			if (nHomeCountry== nIndex)
				Person.m_strHomeCountry = (LPCTSTR)pstrToken;
			if (nHomePhone== nIndex)
				Person.m_strHomePhone =(LPCTSTR)pstrToken;
			if (nHomeFax== nIndex)
				Person.m_strHomeFax =(LPCTSTR)pstrToken;
			if (nCarPhone== nIndex)
				Person.m_strCarPhone =(LPCTSTR)pstrToken;
			if (nPersonalWebPage== nIndex)
				Person.m_strPersonalWebPage = (LPCTSTR)pstrToken;
	        if (nBusinessStreet== nIndex)
				Person.m_strBusinessStreet =(LPCTSTR)pstrToken;
			if (nBusinessCity== nIndex)
				Person.m_strBusinessCity =(LPCTSTR)pstrToken;
			if (nBusinessPostalCode== nIndex)
				Person.m_strBusinessPostalCode =(LPCTSTR)pstrToken;
	        if (nBusinessState== nIndex)
				Person.m_strBusinessState=(LPCTSTR)pstrToken;
			if (nBusinessCountry== nIndex)
				Person.m_strBusinessCountry =(LPCTSTR)pstrToken;
			if (nBusinessWebPage== nIndex)
				Person.m_strBusinessWebPage =(LPCTSTR)pstrToken;
			if (nBusinessPhone== nIndex)
				Person.m_strBusinessPhone=(LPCTSTR)pstrToken;
			if (nBusinessFax== nIndex)
				Person.m_strBusinessFax=(LPCTSTR)pstrToken;
			if (nPager== nIndex)
				Person.m_strPager =(LPCTSTR)pstrToken;
			if (nCompany== nIndex)
				Person.m_strCompany =(LPCTSTR)pstrToken;
			if (nJobTitle == nIndex)
				Person.m_strJobTitle =(LPCTSTR)pstrToken;
			if (nDepartment == nIndex)
				Person.m_strDepartment =(LPCTSTR)pstrToken;
			if (nOfficeLocation == nIndex)
				Person.m_strOfficeLocation =(LPCTSTR)pstrToken;
			if (nNotes == nIndex) {
				Person.m_strNotes =(LPCTSTR)pstrToken;
			}
			nIndex++;
		}
		m_PersonArray.Add(Person);
	}
	SetModifiedFlag();
	UpdateAllViews(NULL);
	delete fileImport;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions