home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c07 / lab03 / ex01 / textdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  2.5 KB  |  113 lines

  1. // TextDoc.cpp : implementation of the CTextDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Text.h"
  6.  
  7. #include "TextDoc.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CTextDoc
  17.  
  18. IMPLEMENT_DYNCREATE(CTextDoc, CDocument)
  19.  
  20. BEGIN_MESSAGE_MAP(CTextDoc, CDocument)
  21.     //{{AFX_MSG_MAP(CTextDoc)
  22.         // NOTE - the ClassWizard will add and remove mapping macros here.
  23.         //    DO NOT EDIT what you see in these blocks of generated code!
  24.     //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CTextDoc construction/destruction
  29.  
  30. CTextDoc::CTextDoc()
  31. {
  32.     // TODO: add one-time construction code here
  33.  
  34. }
  35.  
  36. CTextDoc::~CTextDoc()
  37. {
  38. }
  39.  
  40. BOOL CTextDoc::OnNewDocument()
  41. {
  42.     //    Return FALSE to prevent an empty new document from
  43.     //    being opened at start up. Since this is a simple
  44.     //    text viewer all of our document will be created
  45.     //    by opening a file and processing OnOpenDocuement()
  46.     return FALSE;
  47. }
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CTextDoc serialization
  51.  
  52. void CTextDoc::Serialize(CArchive& ar)
  53. {
  54.     if (ar.IsStoring())
  55.     {
  56.         // TODO: add storing code here
  57.     }
  58.     else
  59.     {
  60.         // TODO: add loading code here
  61.     }
  62. }
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CTextDoc diagnostics
  66.  
  67. #ifdef _DEBUG
  68. void CTextDoc::AssertValid() const
  69. {
  70.     CDocument::AssertValid();
  71. }
  72.  
  73. void CTextDoc::Dump(CDumpContext& dc) const
  74. {
  75.     CDocument::Dump(dc);
  76. }
  77. #endif //_DEBUG
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CTextDoc commands
  81.  
  82. BOOL CTextDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  83. {
  84.     //    Could be a big file
  85.        BeginWaitCursor();
  86.  
  87.     //    Clear List, this will cleanup the CString objects
  88.     m_LineList.RemoveAll();
  89.  
  90.     //    Read the file and store as a list
  91.     //    of CStrings
  92.     CStdioFile    file(lpszPathName, 
  93.                     CFile::modeRead | CFile::typeText);
  94.  
  95.  
  96.     CString    strLine;
  97.     while (file.ReadString(strLine) != NULL)
  98.     {
  99.         //remove the noise characters at the end of the line            
  100.         int nLastCharIndex = strLine.GetLength()-1;
  101.         while (nLastCharIndex >= 0 && strLine[nLastCharIndex] < ' ')
  102.         {
  103.             strLine.SetAt(nLastCharIndex--, '\0');
  104.         }
  105.  
  106.         //    Add to CStringList
  107.         m_LineList.AddTail(strLine);
  108.     }
  109.  
  110.     EndWaitCursor();
  111.     return TRUE;
  112. }
  113.