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

📄 hexvidoc.cpp

📁 主要介绍vc++6.0的编程过程
💻 CPP
字号:
// hexvidoc.cpp : implementation of the CHexViewDoc class
//

#include "stdafx.h"
#include "hexview.h"
#include <errno.h>

#include "hexvidoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHexViewDoc

IMPLEMENT_DYNCREATE(CHexViewDoc, CDocument)

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

/////////////////////////////////////////////////////////////////////////////
// CHexViewDoc construction/destruction

CHexViewDoc::CHexViewDoc()
{
	m_pFile = NULL;
	m_lFileSize = 0L;
}

CHexViewDoc::~CHexViewDoc()
{
	if (m_pFile != NULL)
	{
		m_pFile->Close();
		delete m_pFile;
		m_pFile = NULL;
	}
}

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

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CHexViewDoc serialization

void CHexViewDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CHexViewDoc diagnostics

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

void CHexViewDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
	dc << "m_lFileSize = " << m_lFileSize;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHexViewDoc commands

BOOL CHexViewDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	if (m_pFile != NULL)
	{
		m_pFile->Close();
		delete m_pFile;
	}

	try
	{
		m_pFile =
			new CFile(lpszPathName, CFile::modeRead | CFile::typeBinary);	
	}
	catch (CFileException* e)
	{
		CString strError;

		strError.Format(_T("Couldn't open file: %d"),
			_sys_errlist[e->m_lOsError]);
		AfxMessageBox(strError);
		return FALSE;
	}

	m_lFileSize = m_pFile->GetLength();
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CHexViewDoc implementation

BOOL CHexViewDoc::ReadLine(CString& strLine, int nLength,
	LONG lOffset /* = -1 */)
{
	LONG lPosition;
	if (lOffset != -1L)
		lPosition = m_pFile->Seek(lOffset, CFile::begin);
	else
		lPosition = m_pFile->GetPosition();

	if (lPosition == -1L)
	{
		TRACE2("CHexDoc::ReadLine returns FALSE Seek(%8.8lX, %8.8lX)\n",
			lOffset, lPosition);
		return FALSE;
	}

	BYTE* pszBuffer = new BYTE[nLength];
	int nReturned = m_pFile->Read(pszBuffer, nLength);

	if (nReturned <= 0)
	{
		TRACE2("CHexDoc::ReadLine returns FALSE Read(%d, %d)\n",
			nLength, nReturned);
		delete pszBuffer;
		return FALSE;
	}

	CString strTemp;
	CString strChars;

	strTemp.Format(_T("%8.8lX - "), lPosition);
	strLine = strTemp;

	for (int nIndex = 0; nIndex < nReturned; nIndex++)
	{
		if (nIndex == 0)
			strTemp.Format(_T("%2.2X"), pszBuffer[nIndex]);
		else if (nIndex % 16 == 0)
			strTemp.Format(_T("=%2.2X"), pszBuffer[nIndex]);
		else if (nIndex % 8 == 0)
			strTemp.Format(_T("-%2.2X"), pszBuffer[nIndex]);
		else
			strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);

		if (_istprint(pszBuffer[nIndex]))
			strChars += pszBuffer[nIndex];
		else
			strChars += _T('.');

		strLine += strTemp;
	}

	if (nReturned < nLength)
	{
		CString strPadding(_T(' '), 3*(nLength-nReturned));
		strLine += strPadding;
	}
	
	strLine += _T("  ");
	strLine += strChars;

	delete pszBuffer;
	return TRUE;
}

⌨️ 快捷键说明

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