home *** CD-ROM | disk | FTP | other *** search
- // -------------------------------------------------------------------------
- // Copyright @ 1997 TCK Software, Incorporated
- // All Rights Reserved
- // -------------------------------------------------------------------------
-
- // VfHexDoc.cpp : implementation of the CVfHexDoc class
- //
-
- #include "stdafx.h"
- #include "VfHex.h"
-
- #include "VfHexDoc.h"
- #include "MainFrm.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CVfHexDoc
-
- IMPLEMENT_DYNCREATE(CVfHexDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CVfHexDoc, CDocument)
- //{{AFX_MSG_MAP(CVfHexDoc)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- ON_BN_CLICKED(IDC_TB_SELECT, OnTbSelect)
- ON_EN_CHANGE(IDC_TB_RECSIZE, OnTbRecsizeChanged)
- ON_EN_CHANGE(IDC_TB_OFFSET, OnTbOffsetChanged)
-
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CVfHexDoc construction/destruction
-
- CVfHexDoc::CVfHexDoc()
- {
- // TODO: add one-time construction code here
- m_nRecSize = 80;
- m_nOffset = 0;
-
- m_nFileSize = 0;
- m_nRecId = -1;
- m_fdFile = 0;
- m_fileName = "-- No File Open --";
- }
-
- CVfHexDoc::~CVfHexDoc()
- {
- if(m_fdFile)
- {
- fclose(m_fdFile);
- m_fdFile = 0;
- }
- }
-
- void CVfHexDoc::InitTB()
- {
- CString s1;
- CWnd *pWnd = GetTbCtl(IDC_TB_RECSIZE);
- s1.Format("%d", m_nRecSize);
- pWnd->SetWindowText(s1);
-
- pWnd = GetTbCtl(IDC_TB_OFFSET);
- s1.Format("%d", m_nOffset);
- pWnd->SetWindowText(s1);
-
- pWnd = GetTbCtl(IDC_TB_FILENAME);
- pWnd->SetWindowText(m_fileName);
- }
-
- BOOL CVfHexDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- // if (!CDocument::OnOpenDocument(lpszPathName))
- // return FALSE;
-
- UseNewFile(lpszPathName);
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CVfHexDoc diagnostics
-
- #ifdef _DEBUG
- void CVfHexDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void CVfHexDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CVfHexDoc commands
-
-
- // -------------------------------------------------------------------------
- // Calculate how many records we have according to offset/recSize/FileSize
- // -------------------------------------------------------------------------
- long CVfHexDoc::RecCount()
- {
- long nCnt = (m_nFileSize - m_nOffset) / m_nRecSize;
- return nCnt;
- }
-
- long CVfHexDoc::RecCountHigh()
- {
- long nBytes = m_nFileSize - m_nOffset;
- long nCnt = nBytes / m_nRecSize;
- if((nBytes % m_nRecSize) > 0)
- nCnt++; // Add one for partial record
-
- return nCnt;
- }
-
- // -------------------------------------------------------------------------
- // ReadRecord
- // Note: nRecId is zero based
- // -------------------------------------------------------------------------
- BOOL CVfHexDoc::ReadRecord(long nRecId)
- {
- long nRecOffset = m_nOffset + (m_nRecSize * nRecId);
- int nRead;
-
- if(nRecId < 0 || nRecId > RecCountHigh())
- {
- TRACE("CVfHexDoc::ReadRecord *** Invalid rec: %ld of %ld\n",
- nRecId, RecCountHigh());
- return FALSE;
- }
-
- m_nRecId = nRecId;
-
- // Read the row from the File
- // Note: Wrap everying in one big exception catcher
- fseek(m_fdFile, nRecOffset, SEEK_SET);
- memset(m_recData, '\0', sizeof(m_recData));
- nRead = fread(m_recData, 1, m_nRecSize, m_fdFile);
- if(nRead < m_nRecSize && ferror(m_fdFile))
- {
- AfxMessageBox("Error reading file");
- m_nRecId = -1;
- return FALSE;
- }
-
- return TRUE;
- }
-
- // -------------------------------------------------------------------------
- // Utility function to get a Toolbar controls window pointer
- // -------------------------------------------------------------------------
- CWnd* CVfHexDoc::GetTbCtl(UINT nCtlId)
- {
- CVfHexApp *pApp = (CVfHexApp*)AfxGetApp();
- CMainFrame *pMainFrame = (CMainFrame*) pApp->GetMainFrame();
- CDialogBar *pTB = pMainFrame->GetOptionsTB();
- CWnd *pWnd = pTB->GetDlgItem(nCtlId);
-
- return pWnd;
- }
-
-
- // -------------------------------------------------------------------------
- // Allow user to select a file - uses common file dialog
- // -------------------------------------------------------------------------
- void CVfHexDoc::OnTbSelect()
- {
- // By using OnFileOpen, we will utilize the recent files menu
- CVfHexApp *pApp = (CVfHexApp*)AfxGetApp();
- pApp->DoFileOpen();
- }
-
- // -------------------------------------------------------------------------
- // Opens and utilizes new file
- // -------------------------------------------------------------------------
- void CVfHexDoc::UseNewFile(LPCSTR szNewFile)
- {
- CWnd *pWnd;
- m_fileName = szNewFile;
- pWnd = GetTbCtl(IDC_TB_FILENAME);
- pWnd->SetWindowText(m_fileName);
-
- // CString title;
- // title.Format("%s", (LPCSTR)m_fileName);
- // SetTitle(title);
-
- m_fdFile = fopen(m_fileName, "rb");
- if(!m_fdFile)
- {
- AfxMessageBox("Error opening file");
- m_nFileSize = 0; // Act as if file is empty
- }
- else
- {
- CFileStatus status;
- // -------------- Get General File Info - without actually reading ----
- CFile::GetStatus(m_fileName, status);
- m_nFileSize = status.m_size;
- }
-
- if(m_nFileSize <= m_nOffset)
- {
- CString s1;
- m_nOffset = 0;
- s1.Format("%d", m_nOffset);
- pWnd = GetTbCtl(IDC_TB_OFFSET);
- pWnd->SetWindowText(s1);
- }
-
- UpdateAllViews(0);
- }
-
- // -------------------------------------------------------------------------
- // The Record Size Changed
- // -------------------------------------------------------------------------
- void CVfHexDoc::OnTbRecsizeChanged()
- {
- CString s1;
-
- CWnd *pWnd = GetTbCtl(IDC_TB_RECSIZE);
- pWnd->GetWindowText(s1);
- int i = atoi(s1);
-
- if(i < 1 || i > MAX_RECSIZE)
- {
- s1.Format("Record Size must be between 1 and %d", MAX_RECSIZE);
- AfxMessageBox(s1);
- s1.Format("%d", m_nRecSize);
- pWnd->SetWindowText(s1);
- return; // No change accepted
- }
-
- m_nRecSize = i; // Value is good - so change it
- UpdateAllViews(0);
- pWnd->SetFocus(); // Keep focus in control
- }
-
- // -------------------------------------------------------------------------
- // The Offset Changed
- // -------------------------------------------------------------------------
- void CVfHexDoc::OnTbOffsetChanged()
- {
- CString s1;
-
- CWnd *pWnd = GetTbCtl(IDC_TB_OFFSET);
- pWnd->GetWindowText(s1);
- long i = atol(s1);
-
- if(i < 0 || (i >= m_nFileSize-1 && i != 0))
- {
- if(m_nFileSize < 1)
- {
- s1 = "File size is 0 - offset must be 0";
- m_nOffset = 0;
- }
- else
- {
- s1.Format("Offset Size must be between 0 and %ld", m_nFileSize-1);
- }
- AfxMessageBox(s1,MB_OK | MB_ICONEXCLAMATION);
- s1.Format("%ld", m_nOffset);
- pWnd->SetWindowText(s1);
- return; // No change accepted
- }
- m_nOffset = i; // Value is good - so change it
-
- UpdateAllViews(0);
- pWnd->SetFocus(); // Keep focus in control
- }
-
-
-
-