Webocreation

Tuesday, November 17, 2009

The CDocument Derived Class


  1. The CDocument Derived Class


The class CReaderDoc, derived from CDocument, is the document class for the Reader application.

CReaderDoc contains a protected member variable, m_LineList, which is of the type CStringList. This member variable is used to store lines of text read from the file. The accessor function for m_LineList is defined as:

CStringList* GetLineList() { return &m_LineList; }



The member function DeleteContents calls the RemoveAll function to remove all the elements from the CStringList as shown in the following code:

virtual void DeleteContents()

{

m_LineList.RemoveAll();

}



The OnOpenDocument uses the CStdioFile to open the file passed in the pointer, lpszPathName, by constructing a CStdioFile object as follows:

CStdioFile file(lpszPathName,CFile::modeRead | CFile::typeText);



The following statement will cause data to be read from the file as long as there is anything to read:

while (file.ReadString(strLine) != NULL)



This statement returns TRUE if anything was read and FALSE if the end of the file was encountered prior to reading any data. After the line is read, the contents of strLine are added to the CStringList as follows:

m_LineList.AddTail(strLine);



To see the source code for the files associated with the CReaderDoc class, click this icon. For easy reference, the segments of code discussed in this section are highlighted in bold.


// ReaderDoc.h : interface of the CReaderDoc class

//

/////////////////////////////////////////////////////////////////////////////


#if !defined(AFX_READERDOC_H__7EC9070D_F96E_11D0_B9C5_00AA00688598__INCLUDED_)

#define AFX_READERDOC_H__7EC9070D_F96E_11D0_B9C5_00AA00688598__INCLUDED_


#if _MSC_VER >= 1000

#pragma once

#endif // _MSC_VER >= 1000



class CReaderDoc : public CDocument

{

protected: // create from serialization only

CReaderDoc();

DECLARE_DYNCREATE(CReaderDoc)


// Attributes

public:

CStringList* GetLineList() { return &m_LineList; }

// Operations

public:


// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CReaderDoc)

public:

virtual BOOL OnNewDocument();

virtual void Serialize(CArchive& ar);

virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);

//}}AFX_VIRTUAL


// Implementation

public:

virtual ~CReaderDoc();

#ifdef _DEBUG

virtual void AssertValid() const;

virtual void Dump(CDumpContext& dc) const;

#endif


protected:

CStringList m_LineList;

virtual void DeleteContents() {m_LineList.RemoveAll();}

// Generated message map functions

protected:


//{{AFX_MSG(CReaderDoc)

// NOTE - the ClassWizard will add and remove member functions here.

// DO NOT EDIT what you see in these blocks of generated code !

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};


/////////////////////////////////////////////////////////////////////////////


//{{AFX_INSERT_LOCATION}}

// Microsoft Developer Studio will insert additional declarations immediately before the previous line.


#endif // !defined(AFX_READERDOC_H__7EC9070D_F96E_11D0_B9C5_00AA00688598__INCLUDED_)



// ReaderDoc.cpp : implementation of the CReaderDoc class

//


#include "stdafx.h"

#include "Reader.h"


#include "ReaderDoc.h"


#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif


/////////////////////////////////////////////////////////////////////////////

// CReaderDoc


IMPLEMENT_DYNCREATE(CReaderDoc, CDocument)


BEGIN_MESSAGE_MAP(CReaderDoc, CDocument)

//{{AFX_MSG_MAP(CReaderDoc)

// NOTE - the ClassWizard will add and remove mapping macros here.

// DO NOT EDIT what you see in these blocks of generated code!

//}}AFX_MSG_MAP

END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////

// CReaderDoc construction/destruction


CReaderDoc::CReaderDoc()

{

// TODO: add one-time construction code here


}


CReaderDoc::~CReaderDoc()

{

}


BOOL CReaderDoc::OnNewDocument()

{

if (!CDocument::OnNewDocument())

return FALSE;


// TODO: add reinitialization code here

// (SDI documents will reuse this document)


return TRUE;

}



/////////////////////////////////////////////////////////////////////////////

// CReaderDoc serialization


void CReaderDoc::Serialize(CArchive& ar)

{

if (ar.IsStoring())

{

// TODO: add storing code here

}

else

{

// TODO: add loading code here

}

}


/////////////////////////////////////////////////////////////////////////////

// CReaderDoc diagnostics


#ifdef _DEBUG

void CReaderDoc::AssertValid() const

{

CDocument::AssertValid();

}


void CReaderDoc::Dump(CDumpContext& dc) const

{

CDocument::Dump(dc);

}

#endif //_DEBUG


/////////////////////////////////////////////////////////////////////////////

// CReaderDoc commands



BOOL CReaderDoc::OnOpenDocument(LPCTSTR lpszPathName)

{

// Could be a big file

BeginWaitCursor();


// Clear List, this will cleanup the CString objects



DeleteContents();

// Read the file and store as a list

// of CStrings

CStdioFile file(lpszPathName,

CFile::modeRead | CFile::typeText);



CString strLine;

while (file.ReadString(strLine) != NULL)

{

m_LineList.AddTail(strLine);

}


EndWaitCursor();

return TRUE;

}

No comments:

Post a Comment