|
#include "stdafx.h"
#include "ess_gui.h"
#include "doc.h"
#include ".\doc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CAppDoc, CDocument)
BEGIN_MESSAGE_MAP(CAppDoc, CDocument)
ON_UPDATE_COMMAND_UI(ID_SYSTEM_STATUS, OnUpdateSystemStatus)
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
CAppDoc::CAppDoc()
{
// register!
ess::registry_manager registry;
registry
<< ESS_REGISTER(Contact,Contact)
<< ESS_REGISTER(Address,Address)
<< ESS_REGISTER(Birthday,IEvent)
<< ESS_REGISTER(Meeting,IEvent)
<< ESS_REGISTER(Gig,IEvent);
}
//-----------------------------------------------------------------------------
CAppDoc::~CAppDoc()
{
//
m_contacts.clear();
}
//-----------------------------------------------------------------------------
BOOL CAppDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
UpdateAllViews(0,ClearView);
return TRUE;
}
//-----------------------------------------------------------------------------
BOOL CAppDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
//
m_contacts.clear();
//
BOOL ret = FALSE;
// *always* wrap ess operations in a try/catch block
try
{
int version = 1;
std::string xml_root = "root";
// buffer
std::string storage;
// load XML file into buffer
if (LoadXML(lpszPathName,storage))
{
// set up the XML source
Chordia::xml_source xmls(&storage[0],storage.size());
// and the adapter
ess::xml_loading_adapter adapter(xmls,xml_root,version);
// stream into map of contacts
ess::stream(adapter,m_contacts,"contacts");
//
UpdateAllViews(0,LoadDoc);
//
ret = TRUE;
}
else
{
CString cs;
cs.Format("Cannot open %s",lpszPathName);
AfxMessageBox(cs);
}
}
catch (std::exception ex)
{
AfxMessageBox(ex.what());
}
catch (...)
{
AfxMessageBox("Unknown exception!");
}
return ret;
}
//-----------------------------------------------------------------------------
bool CAppDoc::LoadXML(const char* lpszPathName,std::string& buffer)
{
bool ret = false;
// read XML directly
std::ifstream ifs(lpszPathName,std::ios::binary);
if (ifs.is_open())
{
ifs.seekg(0,std::ios::end);
std::streamsize bytes = ifs.tellg();
buffer.assign(bytes,0);
ifs.seekg(0,std::ios::beg);
ifs.read(&buffer[0],bytes);
ifs.close();
ret = true;
}
return ret;
}
//-----------------------------------------------------------------------------
BOOL CAppDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
//
BOOL ret = FALSE;
// *always* wrap ess operations in a try/catch block
try
{
int version = 1;
std::string xml_root = "root";
// where data is stored ...
ess::xml_medium storage;
{
// instance to serialize
// this version hides an XML parser...
ess::xml_storing_adapter adapter(storage,xml_root,version);
// stream the set of contacts
ess::stream(adapter,m_contacts,"contacts");
}
// write to file
std::ofstream ofs(lpszPathName);
if (ofs.good() == true)
{
ofs << storage.c_str();
}
// saved
SetModifiedFlag(FALSE);
ret = TRUE;
}
catch (std::exception ex)
{
AfxMessageBox(ex.what());
}
catch (...)
{
AfxMessageBox("Unknown exception!");
}
return ret;
}
//-----------------------------------------------------------------------------
// track current tree selection
void CAppDoc::SetSelection(CString cs,DWORD dw)
{
m_status.Format("%s [%d]",cs,dw);
}
//-----------------------------------------------------------------------------
// Update status bar selection indicator
void CAppDoc::OnUpdateSystemStatus(CCmdUI *pCmdUI)
{
pCmdUI->SetText(m_status);
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.