⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doc.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
// doc.cpp : implementation of the DDoc class
//

#include "stdafx.h"
#include "HASVIEWS.h"

#include "doc.h"

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

/////////////////////////////////////////////////////////////////////////////
// DDoc

IMPLEMENT_DYNCREATE(DDoc, CDocument)

BEGIN_MESSAGE_MAP(DDoc, CDocument)
    //{{AFX_MSG_MAP(DDoc)
        // 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()

/////////////////////////////////////////////////////////////////////////////
// DDoc construction/destruction

DDoc::DDoc()
{
    // Init all data members to a known value.
    d_lpTextBuffer = 0;
    d_dwFileLength = 0;
}

DDoc::~DDoc()
{
    // Free allocated buffer.
    if (d_lpTextBuffer)
    {
        free(d_lpTextBuffer);
    }
}

BOOL DDoc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
        return FALSE;

    // Free allocated buffer.
    if (d_lpTextBuffer)
    {
        free(d_lpTextBuffer);
        d_lpTextBuffer = 0;
    }

    // Reset file line length counter.
    d_dwFileLength = 0;

    // Reset count of lines.
    d_cLines       = 0;

    // Reset string array size.
    d_saTextInfo.RemoveAll();

    return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
// DDoc helper functions.

//-----------------------------------------------------------------------------
// BuildStringArray -- Parses text file to create a CString array.
void DDoc::BuildStringArray()
{
    // Scan buffer to calculate line count.
    LPTSTR lpNext = d_lpTextBuffer;
    LPTSTR lpEnd  = d_lpTextBuffer + d_dwFileLength - 1;
    *lpEnd = '\n';
    for (d_cLines = 0; lpNext < lpEnd; d_cLines++, lpNext++)
    {
        // Search for next <CR> character.
        lpNext = strchr(lpNext, '\n');

        // Discontinue if NULL encountered.
        if (lpNext == NULL) break;
    }

    // Set array size.
    d_saTextInfo.SetSize(d_cLines);

    // Scan buffer to build array of pointers & sizes.
    STRING string;
    lpNext = d_lpTextBuffer;
    for (int iLine = 0; iLine < d_cLines; iLine++)
    {
        // Char count for current line.
        string.ccLen = 0;
        string.pText = lpNext;

        // Loop to end-of-line.
        while ((*lpNext != '\n') && (*lpNext != '\r'))
        {
            lpNext++;          // Check next char.
            string.ccLen++;    // Increment length counter.
        }

        // Enter value in array.
        d_saTextInfo[iLine] = string;

       // Skip over <CR> <LF>
       lpNext += (2 * sizeof(TCHAR));
    }
}

/////////////////////////////////////////////////////////////////////////////
// DDoc serialization

void DDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        // Should never get here because document files are read-only.
        ASSERT(FALSE);
    }
    else
    {
        // Free previous buffer contents
        if (d_lpTextBuffer)
        {
            free(d_lpTextBuffer);
            d_lpTextBuffer=0;
        }

        // Fetch CFile for accessing data.
        CFile * pfileData = ar.GetFile();

        // Calculate file size & allocate buffer.
        // Pad to avoid bad address reference.
        d_dwFileLength = pfileData->GetLength() + sizeof(char);
        d_lpTextBuffer = (LPTSTR)malloc (d_dwFileLength);
        if (!d_lpTextBuffer)
        {
            AfxMessageBox(_T("Cannot Allocate Memory For File"));
            return;
        }

        // Read Buffer.
        pfileData->Read((LPVOID)d_lpTextBuffer, d_dwFileLength);

        // Empty buffer array.
        d_saTextInfo.RemoveAll();

        // Scan buffer for text line info.
        BuildStringArray();

    }   /* [if (ar.IsStoring()) ] */
}

/////////////////////////////////////////////////////////////////////////////
// DDoc diagnostics

#ifdef _DEBUG
void DDoc::AssertValid() const
{
    CDocument::AssertValid();
}

void DDoc::Dump(CDumpContext& dc) const
{
    CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// DDoc commands

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -