Click here to Skip to main content
15,891,473 members
Articles / Desktop Programming / MFC

The Ultimate Toolbox Home Page

Rate me:
Please Sign up or sign in to vote.
4.97/5 (141 votes)
25 Aug 2007CPOL13 min read 3.2M   91.4K   476  
The Ultimate Toolbox is now Open Source
// ==========================================================================
// 						Class Implementation : COXSerializer
// ==========================================================================

// Source file : oxsrlzr.cpp

// This software along with its related components, documentation and files ("The Libraries")
// is � 1994-2007 The Code Project (1612916 Ontario Limited) and use of The Libraries is
// governed by a software license agreement ("Agreement").  Copies of the Agreement are
// available at The Code Project (www.codeproject.com), as part of the package you downloaded
// to obtain this file, or directly from our office.  For a copy of the license governing
// this software, you may contact us at legalaffairs@codeproject.com, or by calling 416-849-8900.    
                  
// //////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "OXSRLZR.H"

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

static TCHAR BASED_CODE MsgOutOfMemory[]= _T("Out of Memory!");
static TCHAR BASED_CODE no_resource_string_available[] = _T("RC error: No RESSERLZR.RC file found");

COXSerializer::COXSerializer() :
	m_bInitialized(FALSE),
    m_pObject(NULL)
{
}

COXSerializer::~COXSerializer()
{
}

BOOL COXSerializer::Initialize(CString sFileName, CObject* pObject)
{
    if (pObject == NULL)
	{
        TRACE0("COXSerializer::Initialize: pObject argument can't be NULL");
		ASSERT(FALSE);
        return FALSE;
    }        
    ASSERT_VALID(pObject);
    if (pObject->IsSerializable() == FALSE)
        return FALSE;

    m_fileException.m_cause = CFileException::none;    
    m_fileException.m_lOsError = 0L;    
    m_archiveException.m_cause = CArchiveException::none;
    m_bMemoryException = FALSE;

    m_bInitialized = TRUE;
    m_sFileName = sFileName;
    m_pObject = pObject;
    return TRUE;
}

BOOL COXSerializer::Load(BOOL bDisplayException)
{
	CFile		file;
	CArchive*	pArchive=NULL;

    if (m_bInitialized==FALSE)
        return FALSE;

    m_fileException.m_cause = CFileException::none;    
    m_fileException.m_lOsError = 0L;    
    m_archiveException.m_cause = CArchiveException::none;
    m_bMemoryException = FALSE;

    if (!file.Open(m_sFileName, CFile::modeRead | CFile::typeBinary |   
                               CFile::shareExclusive, &m_fileException))
	{
        if (bDisplayException)
            m_fileException.ReportError();
        return FALSE;
    }
    TRY
	{
        pArchive = new CArchive(&file, CArchive::load);
        m_pObject->Serialize(*pArchive);
        delete pArchive;
        file.Close();
    }
	CATCH (CMemoryException, e)
	{
        m_bMemoryException = TRUE;
		delete pArchive;
        if (bDisplayException) 
            AfxMessageBox(MsgOutOfMemory);
        return FALSE;
	}
    AND_CATCH (CArchiveException, e)
	{
        m_archiveException.m_cause = e->m_cause;
        if (bDisplayException) 
           e->ReportError();
        delete pArchive;
        return FALSE;
    }
    AND_CATCH (CFileException, e)
	{
        m_fileException.m_cause = e->m_cause;
        m_fileException.m_lOsError = e->m_lOsError;
        if (bDisplayException) 
           e->ReportError();
        delete pArchive;
        return FALSE;
    }
	END_CATCH

    return TRUE;
}

BOOL COXSerializer::Save(BOOL bDisplayException)
{
	CFile		file;
	CArchive*	pArchive=NULL;

    if (m_bInitialized==FALSE)
        return FALSE;
    m_fileException.m_cause = CFileException::none;    
    m_fileException.m_lOsError = 0L;    
    m_archiveException.m_cause = CArchiveException::none;
    m_bMemoryException = FALSE;

    if (!file.Open(m_sFileName, CFile::modeCreate | CFile::modeWrite |
                               CFile::typeBinary | CFile::shareExclusive, 
                               &m_fileException))
	{
        if (bDisplayException)
            m_fileException.ReportError();
        return FALSE;
    }
    TRY
	{
        pArchive = new CArchive(&file, CArchive::store);
        m_pObject->Serialize(*pArchive);
        delete pArchive;
        file.Close();
    }
	CATCH (CMemoryException, e)
	{
        m_bMemoryException = TRUE;
		delete pArchive;
        if (bDisplayException) 
            AfxMessageBox(MsgOutOfMemory);
        return FALSE;
	}
    AND_CATCH (CArchiveException, e)
	{
        m_archiveException.m_cause = e->m_cause;
        if (bDisplayException) 
           e->ReportError();
        delete pArchive;
        return FALSE;
    }
    AND_CATCH (CFileException, e)
	{
        m_fileException.m_cause = e->m_cause;
        m_fileException.m_lOsError = e->m_lOsError;
        if (bDisplayException) 
            e->ReportError();
        delete pArchive;
        return FALSE;
    }
	END_CATCH

    return(TRUE);
}

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
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
This is a Organisation

476 members

Comments and Discussions