home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / VFORM.ZIP / Samples / vfHex / VfHexDoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-10  |  7.0 KB  |  280 lines

  1. // -------------------------------------------------------------------------
  2. // Copyright @ 1997 TCK Software, Incorporated
  3. // All Rights Reserved
  4. // -------------------------------------------------------------------------
  5.  
  6. // VfHexDoc.cpp : implementation of the CVfHexDoc class
  7. //
  8.  
  9. #include "stdafx.h"
  10. #include "VfHex.h"
  11.  
  12. #include "VfHexDoc.h"
  13. #include "MainFrm.h"
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CVfHexDoc
  23.  
  24. IMPLEMENT_DYNCREATE(CVfHexDoc, CDocument)
  25.  
  26. BEGIN_MESSAGE_MAP(CVfHexDoc, CDocument)
  27.     //{{AFX_MSG_MAP(CVfHexDoc)
  28.         // NOTE - the ClassWizard will add and remove mapping macros here.
  29.         //    DO NOT EDIT what you see in these blocks of generated code!
  30.     ON_BN_CLICKED(IDC_TB_SELECT, OnTbSelect)
  31.     ON_EN_CHANGE(IDC_TB_RECSIZE, OnTbRecsizeChanged)
  32.     ON_EN_CHANGE(IDC_TB_OFFSET, OnTbOffsetChanged)
  33.  
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CVfHexDoc construction/destruction
  39.  
  40. CVfHexDoc::CVfHexDoc()
  41. {
  42.     // TODO: add one-time construction code here
  43.     m_nRecSize = 80;
  44.     m_nOffset = 0;
  45.  
  46.     m_nFileSize = 0;
  47.     m_nRecId = -1;
  48.     m_fdFile = 0;
  49.     m_fileName = "-- No File Open --";
  50. }
  51.  
  52. CVfHexDoc::~CVfHexDoc()
  53. {
  54.     if(m_fdFile)
  55.     {
  56.         fclose(m_fdFile);
  57.         m_fdFile = 0;
  58.     }
  59. }
  60.  
  61. void CVfHexDoc::InitTB()
  62. {
  63.     CString s1;
  64.     CWnd *pWnd = GetTbCtl(IDC_TB_RECSIZE);
  65.     s1.Format("%d", m_nRecSize);
  66.     pWnd->SetWindowText(s1);
  67.  
  68.     pWnd = GetTbCtl(IDC_TB_OFFSET);
  69.     s1.Format("%d", m_nOffset);
  70.     pWnd->SetWindowText(s1);
  71.  
  72.     pWnd = GetTbCtl(IDC_TB_FILENAME);
  73.     pWnd->SetWindowText(m_fileName);
  74. }
  75.  
  76. BOOL CVfHexDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  77. {
  78.     // if (!CDocument::OnOpenDocument(lpszPathName))
  79.     //    return FALSE;
  80.  
  81.     UseNewFile(lpszPathName);    
  82.     return TRUE;
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CVfHexDoc diagnostics
  87.  
  88. #ifdef _DEBUG
  89. void CVfHexDoc::AssertValid() const
  90. {
  91.     CDocument::AssertValid();
  92. }
  93.  
  94. void CVfHexDoc::Dump(CDumpContext& dc) const
  95. {
  96.     CDocument::Dump(dc);
  97. }
  98. #endif //_DEBUG
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CVfHexDoc commands
  102.  
  103.  
  104. // -------------------------------------------------------------------------
  105. // Calculate how many records we have according to offset/recSize/FileSize
  106. // -------------------------------------------------------------------------
  107. long CVfHexDoc::RecCount() 
  108. {
  109.     long nCnt = (m_nFileSize - m_nOffset) / m_nRecSize;
  110.     return nCnt;
  111. }
  112.  
  113. long CVfHexDoc::RecCountHigh() 
  114. {
  115.     long nBytes = m_nFileSize - m_nOffset;
  116.     long nCnt = nBytes / m_nRecSize;
  117.     if((nBytes % m_nRecSize) > 0)
  118.         nCnt++;                            // Add one for partial record
  119.  
  120.     return nCnt;
  121. }
  122.  
  123. // -------------------------------------------------------------------------
  124. // ReadRecord
  125. // Note: nRecId is zero based
  126. // -------------------------------------------------------------------------
  127. BOOL CVfHexDoc::ReadRecord(long nRecId)
  128. {
  129.     long nRecOffset = m_nOffset + (m_nRecSize * nRecId);
  130.     int  nRead;
  131.  
  132.     if(nRecId < 0 || nRecId > RecCountHigh())
  133.     {
  134.         TRACE("CVfHexDoc::ReadRecord *** Invalid rec: %ld of %ld\n",
  135.             nRecId, RecCountHigh());
  136.         return FALSE;
  137.     }
  138.  
  139.     m_nRecId = nRecId;
  140.  
  141.     // Read the row from the File
  142.     // Note: Wrap everying in one big exception catcher
  143.     fseek(m_fdFile, nRecOffset, SEEK_SET);
  144.     memset(m_recData, '\0', sizeof(m_recData));
  145.     nRead = fread(m_recData, 1, m_nRecSize, m_fdFile); 
  146.     if(nRead < m_nRecSize && ferror(m_fdFile))
  147.     {
  148.         AfxMessageBox("Error reading file");
  149.         m_nRecId = -1;
  150.         return FALSE;
  151.     }
  152.  
  153.     return TRUE;
  154. }
  155.  
  156. // -------------------------------------------------------------------------
  157. // Utility function to get a Toolbar controls window pointer
  158. // -------------------------------------------------------------------------
  159. CWnd* CVfHexDoc::GetTbCtl(UINT nCtlId) 
  160. {
  161.     CVfHexApp *pApp = (CVfHexApp*)AfxGetApp();
  162.     CMainFrame *pMainFrame = (CMainFrame*) pApp->GetMainFrame();
  163.     CDialogBar *pTB = pMainFrame->GetOptionsTB();
  164.     CWnd *pWnd = pTB->GetDlgItem(nCtlId);
  165.  
  166.     return pWnd;
  167. }
  168.  
  169.  
  170. // -------------------------------------------------------------------------
  171. // Allow user to select a file - uses common file dialog
  172. // -------------------------------------------------------------------------
  173. void CVfHexDoc::OnTbSelect() 
  174. {
  175.     // By using OnFileOpen, we will utilize the recent files menu
  176.     CVfHexApp *pApp = (CVfHexApp*)AfxGetApp();
  177.     pApp->DoFileOpen();
  178. }
  179.  
  180. // -------------------------------------------------------------------------
  181. // Opens and utilizes new file
  182. // -------------------------------------------------------------------------
  183. void CVfHexDoc::UseNewFile(LPCSTR szNewFile)
  184. {
  185.     CWnd *pWnd;
  186.     m_fileName = szNewFile;
  187.     pWnd = GetTbCtl(IDC_TB_FILENAME);
  188.     pWnd->SetWindowText(m_fileName);
  189.  
  190.     // CString title;
  191.     // title.Format("%s", (LPCSTR)m_fileName);
  192.     // SetTitle(title);
  193.  
  194.     m_fdFile = fopen(m_fileName, "rb");
  195.     if(!m_fdFile)
  196.     {
  197.         AfxMessageBox("Error opening file");
  198.         m_nFileSize = 0;            // Act as if file is empty
  199.     }
  200.     else
  201.     {
  202.         CFileStatus status;
  203.         // -------------- Get General File Info - without actually reading ----
  204.         CFile::GetStatus(m_fileName, status);
  205.         m_nFileSize = status.m_size;
  206.     }
  207.  
  208.     if(m_nFileSize <= m_nOffset)
  209.     {
  210.         CString s1;
  211.         m_nOffset = 0;
  212.         s1.Format("%d", m_nOffset);
  213.         pWnd = GetTbCtl(IDC_TB_OFFSET);
  214.         pWnd->SetWindowText(s1);
  215.     }
  216.  
  217.     UpdateAllViews(0);
  218. }
  219.  
  220. // -------------------------------------------------------------------------
  221. // The Record Size Changed
  222. // -------------------------------------------------------------------------
  223. void CVfHexDoc::OnTbRecsizeChanged() 
  224. {
  225.     CString s1;
  226.  
  227.     CWnd *pWnd = GetTbCtl(IDC_TB_RECSIZE);
  228.     pWnd->GetWindowText(s1);
  229.     int i = atoi(s1);
  230.  
  231.     if(i < 1 || i > MAX_RECSIZE)
  232.     {
  233.         s1.Format("Record Size must be between 1 and %d", MAX_RECSIZE);
  234.         AfxMessageBox(s1);
  235.         s1.Format("%d", m_nRecSize);
  236.         pWnd->SetWindowText(s1);
  237.         return;                            // No change accepted
  238.     }
  239.  
  240.     m_nRecSize = i;                        // Value is good - so change it
  241.     UpdateAllViews(0);
  242.     pWnd->SetFocus();                    // Keep focus in control
  243. }
  244.  
  245. // -------------------------------------------------------------------------
  246. // The Offset Changed
  247. // -------------------------------------------------------------------------
  248. void CVfHexDoc::OnTbOffsetChanged() 
  249. {
  250.     CString s1;
  251.  
  252.     CWnd *pWnd = GetTbCtl(IDC_TB_OFFSET);
  253.     pWnd->GetWindowText(s1);
  254.     long i = atol(s1);
  255.  
  256.     if(i < 0 || (i >= m_nFileSize-1 && i != 0))
  257.     {
  258.         if(m_nFileSize < 1)
  259.         {
  260.             s1 = "File size is 0 - offset must be 0";
  261.             m_nOffset = 0;
  262.         }
  263.         else
  264.         {
  265.             s1.Format("Offset Size must be between 0 and %ld", m_nFileSize-1);
  266.         }
  267.         AfxMessageBox(s1,MB_OK | MB_ICONEXCLAMATION);
  268.         s1.Format("%ld", m_nOffset);
  269.         pWnd->SetWindowText(s1);
  270.         return;                            // No change accepted
  271.     }
  272.     m_nOffset = i;                        // Value is good - so change it
  273.  
  274.     UpdateAllViews(0);
  275.     pWnd->SetFocus();                    // Keep focus in control
  276. }
  277.  
  278.  
  279.  
  280.