home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch06 / filevdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-30  |  3.0 KB  |  148 lines

  1. // FileVDoc.cpp : implementation of the CFileViewDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "FileView.h"
  6.  
  7. #include "FileVDoc.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. // CFileViewDoc
  17.  
  18. IMPLEMENT_DYNCREATE(CFileViewDoc, CDocument)
  19.  
  20. BEGIN_MESSAGE_MAP(CFileViewDoc, CDocument)
  21.     //{{AFX_MSG_MAP(CFileViewDoc)
  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. // CFileViewDoc construction/destruction
  29.  
  30. CFileViewDoc::CFileViewDoc()
  31. {
  32.     lines = NULL;
  33.     maxLineLength = 0;
  34.  
  35.     try {
  36.         lines = new CPtrArray();
  37.     }
  38.     catch (CMemoryException *e)
  39.     {
  40.         AfxMessageBox("Can't allocate memory for reading file contents");
  41.         TRACE0("Unable to allocate memory for file contents\n");
  42.         e->Delete();
  43.     }
  44.     TRACE0("Allocated lines array\n");
  45. }
  46.  
  47. CFileViewDoc::~CFileViewDoc()
  48. {
  49.     int i = 0;
  50.  
  51.     if (lines != NULL)
  52.     {
  53.         while (i < lines->GetSize())
  54.             delete (CString *) lines->GetAt(i++);
  55.         lines->RemoveAll();
  56.         delete lines;
  57.         lines = NULL;
  58.     }
  59.     TRACE0("Deleted lines array\n");
  60. }
  61.  
  62. BOOL CFileViewDoc::OnNewDocument()
  63. {
  64.     if (!CDocument::OnNewDocument())
  65.         return FALSE;
  66.  
  67.     // TODO: add reinitialization code here
  68.     // (SDI documents will reuse this document)
  69.  
  70.     return TRUE;
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CFileViewDoc serialization
  75.  
  76. void CFileViewDoc::Serialize(CArchive& ar)
  77. {
  78.     BYTE buf;
  79.     CString s;
  80.  
  81.     if (ar.IsStoring())
  82.     {
  83.         // TODO: add storing code here
  84.     }
  85.     else
  86.     {
  87.         // TODO: add loading code here
  88.         while (1)
  89.         {
  90.             try {
  91.                 ar >> buf;
  92.             }
  93.             catch (CArchiveException *e)
  94.             {
  95.                 if (e->m_cause != CArchiveException::endOfFile)
  96.                 {
  97.                     TRACE0("Unknown exception loading file!\n");
  98.                     throw;
  99.                 } else
  100.                 {
  101.                     TRACE0("End of file reached...\n");
  102.                     e->Delete();
  103.                 }
  104.                 break;
  105.             }
  106.             s += buf;
  107.             if (buf == '\n')
  108.             {
  109.                 try {
  110.                     lines->Add(new CString(s));
  111.                 }
  112.                 catch (CMemoryException *e)
  113.                 {
  114.                     AfxMessageBox("Not enough memory to load entire file");
  115.                     TRACE1("Not enough memory to load entire file; only %d lines loaded\n", lines->GetSize());
  116.                     e->Delete();
  117.                     break;
  118.                 }
  119.                 if (maxLineLength < s.GetLength())
  120.                     maxLineLength = s.GetLength();
  121.                 s.Empty();
  122.             }
  123.         }
  124. #ifdef _DEBUG
  125.         afxDump.SetDepth(1);
  126.         afxDump << lines;
  127. #endif
  128.     }
  129. }
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CFileViewDoc diagnostics
  133.  
  134. #ifdef _DEBUG
  135. void CFileViewDoc::AssertValid() const
  136. {
  137.     CDocument::AssertValid();
  138. }
  139.  
  140. void CFileViewDoc::Dump(CDumpContext& dc) const
  141. {
  142.     CDocument::Dump(dc);
  143. }
  144. #endif //_DEBUG
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147. // CFileViewDoc commands
  148.