📄 hexviewerdoc.cpp
字号:
// HexViewerDoc.cpp : implementation of the CHexViewerDoc class
//
#include "stdafx.h"
#include "HexViewer.h"
#include "HexViewerDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CHexViewerDoc
IMPLEMENT_DYNCREATE(CHexViewerDoc, CDocument)
BEGIN_MESSAGE_MAP(CHexViewerDoc, CDocument)
END_MESSAGE_MAP()
// CHexViewerDoc construction/destruction
CHexViewerDoc::CHexViewerDoc()
{
m_pFile = NULL;
m_lFileSize = 0L;
}
CHexViewerDoc::~CHexViewerDoc()
{
if (m_pFile != NULL)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
m_lFileSize = 0L;
}
}
BOOL CHexViewerDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
// CHexViewerDoc serialization
void CHexViewerDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
// CHexViewerDoc diagnostics
#ifdef _DEBUG
void CHexViewerDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CHexViewerDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CHexViewerDoc commands
BOOL CHexViewerDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// Clean up if a file is already open
if (m_pFile != NULL)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
m_lFileSize = 0L;
}
try
{
// Open specified file
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;
}
// Set the file size variable - used in determining the
// scroll size of the view
m_lFileSize = m_pFile->GetLength();
return TRUE;
}
BOOL CHexViewerDoc::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 + -