home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 January / Pcwk0198.iso / Zadarmo / HEXVIEW / SRC / HEXVIEWD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-04  |  4.4 KB  |  181 lines

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