hexviewDoc.cpp
Upload User: ycdyang2
Upload Date: 2007-01-07
Package Size: 126k
Code Size: 4k
Category:

Editor

Development Platform:

Visual C++

  1. /* ---------------------------------------------------------------------------
  2.    This code can be used as you wish but without warranties as to performance 
  3.    of merchantability or any other warranties whether expressed or implied.
  4.    
  5.  Written by Mike Funduc, Funduc Software Inc. 8/1/96
  6.  To download the code and more useful utilities (including Search and
  7.  Replace for Windows 95/NT, 3.1x) go to: http://www.funduc.com
  8. ----------------------------------------------------------------------------*/
  9. // hexviewDoc.cpp : implementation of the CHexviewDoc class
  10. //
  11. #include "stdafx.h"
  12. #include "hexview.h"
  13. #include "hexviewDoc.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. const int HEXVIEW_BLOCK_SIZE = 16384;
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CHexviewDoc
  22. IMPLEMENT_DYNCREATE(CHexviewDoc, CDocument)
  23. BEGIN_MESSAGE_MAP(CHexviewDoc, CDocument)
  24. //{{AFX_MSG_MAP(CHexviewDoc)
  25. // NOTE - the ClassWizard will add and remove mapping macros here.
  26. //    DO NOT EDIT what you see in these blocks of generated code!
  27. //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CHexviewDoc construction/destruction
  31. CHexviewDoc::CHexviewDoc()
  32. {
  33.   m_iBlockSize = HEXVIEW_BLOCK_SIZE;
  34.   m_lStartOffset = -1;
  35.   m_lEndOffset = -1;
  36.   HexGetApp()->GetOffsets(m_lStartOffset, m_lEndOffset);
  37. }
  38. BYTE *CHexviewDoc::AdjustPointerAbsolute(int iPosition)
  39.   m_iCurrentPointer=iPosition;
  40.   return &m_lpImage[m_iCurrentPointer];
  41. }
  42. CHexviewDoc::~CHexviewDoc()
  43. {
  44. }
  45. BOOL CHexviewDoc::OnNewDocument()
  46. {
  47. // if (!CDocument::OnNewDocument())
  48. // return FALSE;
  49. // Just in case this is called
  50. return TRUE;
  51. }
  52. BOOL CHexviewDoc::OnOpenDocument(LPCTSTR lpszFileName)
  53. {
  54.   m_csFileName = (CString *)0;
  55.   if (INVALID_HANDLE_VALUE==(m_hFile=CreateFile(lpszFileName,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
  56.     OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)))
  57.   {
  58.   AfxMessageBox(IDS_ERR_OPEN);
  59.       return(FALSE);
  60.   }
  61.   m_hFileMapping = CreateFileMapping(m_hFile,NULL,PAGE_READONLY,0,0,NULL);
  62.   if (!m_hFileMapping) 
  63.   {
  64.       AfxMessageBox(IDS_ERR_MAPPING);
  65.       CloseHandle(m_hFile);
  66.       return(FALSE);
  67.   }
  68.   m_lpImage = (BYTE *)MapViewOfFile(m_hFileMapping,FILE_MAP_READ,0,0,0);
  69.   if (!m_lpImage) 
  70.   {
  71.       CloseHandle(m_hFileMapping);
  72.       CloseHandle(m_hFile);
  73.       AfxMessageBox(IDS_ERR_MAPVIEW);
  74.       return(FALSE);
  75.   }
  76.   AfxGetApp()->AddToRecentFileList(lpszFileName);
  77.   m_csFileName = new CString(lpszFileName);
  78.   SetPathName(lpszFileName);
  79.   m_iCurrentPointer = 0;
  80.   DWORD dwFileSizeHigh;
  81.   m_iFileSize = GetFileSize(m_hFile,&dwFileSizeHigh);
  82.   m_iBlockSize = min(HEXVIEW_BLOCK_SIZE, m_iFileSize);
  83.    return TRUE;
  84. }
  85. void CHexviewDoc::OnCloseDocument()
  86. {
  87.  UnmapViewOfFile(m_lpImage);
  88.  CloseHandle(m_hFileMapping);
  89.  CloseHandle(m_hFile);
  90.  strcpy(szStatusMessage,"");
  91.  CDocument::OnCloseDocument();
  92. }
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CHexviewDoc serialization
  95. void CHexviewDoc::Serialize(CArchive& ar)
  96. {
  97. if (ar.IsStoring())
  98. {
  99. // TODO: add storing code here
  100. }
  101. else
  102. {
  103. // TODO: add loading code here
  104. }
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CHexviewDoc diagnostics
  108. #ifdef _DEBUG
  109. void CHexviewDoc::AssertValid() const
  110. {
  111. CDocument::AssertValid();
  112. }
  113. void CHexviewDoc::Dump(CDumpContext& dc) const
  114. {
  115. CDocument::Dump(dc);
  116. }
  117. #endif //_DEBUG
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CHexviewDoc commands
  120. BOOL CHexviewDoc::GetNextBlock(int &iCurrentOffset)
  121. {
  122. int iTempOffset = iCurrentOffset;
  123. iTempOffset += HEXVIEW_BLOCK_SIZE;
  124. if (iTempOffset + 1 > TotalFileLength())
  125. {
  126. return FALSE;
  127. }
  128. iCurrentOffset = iTempOffset;
  129. return TRUE;
  130. }
  131. BOOL CHexviewDoc::GetPrevBlock(int &iCurrentOffset)
  132. {
  133. int iTempOffset = iCurrentOffset;
  134. iTempOffset -= HEXVIEW_BLOCK_SIZE;
  135. if (iTempOffset < 0)
  136. {
  137. return FALSE;
  138. }
  139. iCurrentOffset = iTempOffset;
  140. return TRUE;
  141. }
  142. int CHexviewDoc::BlockLength(int iCurrentOffset)
  143. {
  144. int nNewLength = min(m_iBlockSize, m_iFileSize - iCurrentOffset);
  145. if (nNewLength < 0)
  146. nNewLength = 0;
  147. return nNewLength;
  148. }