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

📄 hexviewdoc.cpp

📁 以十六进制显示文档内容
💻 CPP
字号:
// HexViewDoc.cpp : implementation of the CHexViewDoc class
//

#include "stdafx.h"
#include "HexView.h"

#include "HexViewDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char 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()
{
	// TODO: add one-time construction code here
	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);
}
#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 = -1L)
{
	LONG lPosition;

	if (lOffset != -1L)
		lPosition = m_pFile->Seek(lOffset, CFile::begin);
	else
		lPosition = m_pFile->GetPosition();

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

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

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

	CString strTemp;
	CString strCharsIn;

	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]))
			strCharsIn += pszBuffer[nIndex];
		else
			strCharsIn += _T('.');
		strLine += strTemp;
	}
	if (nReturned < nLength)
	{
		CString strPadding(_T(' '), 3*(nLength-nReturned));
		strLine += strPadding;
	}
	strLine += _T("  ");
	strLine += strCharsIn;

	delete pszBuffer;
	return TRUE;
}


⌨️ 快捷键说明

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