home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK8 / MFC / SAMPLES / TESTCLNT / TCFILE.CP$ / tcfile
Encoding:
Text File  |  1992-03-19  |  6.8 KB  |  346 lines

  1. // tcfile.cpp : This file contains all code dealing with File I/O.  It
  2. //        contains the serialization routines as well as the 
  3. //            commdlg file routines, and load/save file routines.
  4. //
  5. // This is a part of the Microsoft Foundation Classes C++ library.
  6. // Copyright (C) 1992 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Foundation Classes Reference and Microsoft
  11. // QuickHelp documentation provided with the library.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Foundation Classes product.
  14.  
  15.  
  16. #include "testclnt.h"
  17. #include "defs.h"
  18. #include "resource.h"
  19.  
  20. #include "afxdlgs.h"
  21.  
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24.  
  25.  
  26. extern CStdioFile *pOleDump;
  27. static char szBuffer[80];
  28.  
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CTestClient FileDlg
  32. //
  33. BOOL CTestClient::FileDlg( BOOL bOpen, int nMaxFile, LPSTR szFile)
  34. {
  35.     char szFilter[] = "Test OLE Files (*.ole)|*.ole||";
  36.     CFileDialog dlg(bOpen, "ole", NULL, 
  37.         OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, szFilter);
  38.     
  39.     ::GetFileTitle(szFile, szFile, nMaxFile); // trim input to just filename
  40.     dlg.m_ofn.lpstrFile = szFile;
  41.     dlg.m_ofn.nMaxFile = nMaxFile;
  42.  
  43.     return dlg.DoModal() == IDOK ? TRUE : FALSE;
  44. }
  45.  
  46.  
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CTestClient OnFileOpen()
  50. //
  51. void CTestClient::OnFileOpen()
  52. {
  53.     extern CString GLOBAL_szFile;
  54.  
  55.     ASSERT(m_pClient != NULL);
  56.     ASSERT(m_pItemTitle != NULL);
  57.  
  58.  
  59.     if (!QuerySaveChanges())
  60.     {
  61.         return;     // check to save changes
  62.     }
  63.  
  64.     if (!FileDlg(TRUE, FILENAMELEN, 
  65.         GLOBAL_szFile.GetBuffer(FILENAMELEN)))
  66.     {
  67.         return;
  68.     }
  69.  
  70.     GLOBAL_szFile.ReleaseBuffer();
  71.  
  72.     if (m_pClient->m_lpObject != NULL)
  73.     {
  74.         TRY
  75.         {
  76.             m_pClient->Delete();
  77.         }
  78.         CATCH (COleException, e)
  79.         {
  80.             if (m_bLogging)
  81.             {
  82.                 LogError (THIS_FILE, e->m_status);
  83.             }
  84.             return;
  85.         }
  86.         END_CATCH
  87.     }
  88.  
  89.     if (!LoadFile(((const char *)GLOBAL_szFile)))
  90.     {
  91.         m_pItemTitle->SetWindowText("");
  92.         Invalidate(TRUE);
  93.         return;
  94.     }
  95.  
  96.     m_szFileName = GLOBAL_szFile;
  97.  
  98.     // Set up Information bar's object title
  99.     m_pItemTitle->SetWindowText(m_pClient->GetName());
  100.     m_bUntitled = FALSE;
  101.     Invalidate(TRUE);
  102. }
  103.  
  104.  
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CTestClient LoadFile(const char* pName)
  108. //
  109. BOOL CTestClient::LoadFile(const char* pName)
  110. {
  111.     CFile file;
  112.  
  113.     ASSERT(strlen(pName) != 0);
  114.     
  115.     if (!file.Open(pName, CFile::modeRead | CFile::shareDenyWrite))
  116.     {
  117.         MessageBox("Not able to open file.","Test Client",
  118.             MB_ICONEXCLAMATION | MB_OK);
  119.  
  120.         if (m_bLogging)
  121.         {
  122.             ASSERT(pOleDump != NULL);
  123.             pOleDump->WriteString("CTestClient::Unable to Open File\n");
  124.         }
  125.         return FALSE;
  126.     }
  127.  
  128.     // Set up a CArchive to serialize in the data.
  129.     //
  130.     CArchive ar(&file, CArchive::load);
  131.  
  132.     TRY
  133.     {
  134.         Serialize(ar); 
  135.     }
  136.     CATCH(CFileException, e)
  137.     {
  138.         MessageBox("Not able to read from file.", "Test Client",
  139.             MB_ICONEXCLAMATION | MB_OK);
  140.         if (m_bLogging)
  141.         {
  142.             ASSERT(pOleDump != NULL);
  143.             pOleDump->WriteString("Caught an Exception while Reading From File\n");
  144.         }
  145.         ar.Close();
  146.         file.Close();
  147.         return FALSE;
  148.     }
  149.     END_CATCH
  150.  
  151.     ar.Close();
  152.     file.Close();
  153.     m_bDirty = FALSE;
  154.     m_bRevertAvailable = TRUE;
  155.     return TRUE;
  156. }
  157.         
  158.  
  159.  
  160. /////////////////////////////////////////////////////////////////////////////
  161. // CTestClient Serialize(CArchive &ar)
  162. //
  163. void CTestClient::Serialize(CArchive &ar)
  164. {
  165.     WORD wStartFile;  // a private word format to signify start of file
  166.     wStartFile = 0xAFC0;
  167.         
  168.     if (ar.IsStoring())
  169.     {
  170.         ar << wStartFile;
  171.             // note that COleClient::Serialize is called after this
  172.             // in the CTestClient::SaveFile routine
  173.     }
  174.     else
  175.     {
  176.         WORD wNew;
  177.         ar >> wNew;
  178.  
  179.         if (wNew != wStartFile)
  180.         {
  181.             AfxThrowFileException(CFileException::invalidFile);
  182.             return;
  183.         }
  184.  
  185.         TRY
  186.         {
  187.             m_pClient->Serialize(ar);
  188.         }
  189.         CATCH (COleException, e)
  190.         {
  191.             if (m_bLogging)
  192.             {
  193.                 LogError (THIS_FILE, e->m_status);
  194.             }
  195.             return;
  196.         }
  197.         END_CATCH
  198.     }
  199. }
  200.  
  201. /////////////////////////////////////////////////////////////////////////////
  202. // CTestClient SaveFile()
  203. //
  204. void CTestClient::SaveFile (BOOL bNewFileName)
  205. {
  206.     CFile file;
  207.     WORD wStartFile;    // a private word format to signify start of file
  208.     wStartFile = 0xAFC0;
  209.     
  210.     ASSERT(m_pClient != NULL);
  211.     ASSERT(m_pDoc != NULL);
  212.  
  213.     if (bNewFileName)
  214.     {
  215.         if (m_bUntitled)
  216.         {
  217.             // set up a default name
  218.             m_szFileName = "testclnt.ole";
  219.         }
  220.  
  221.  
  222.         if (!FileDlg(FALSE, FILENAMELEN, 
  223.             m_szFileName.GetBuffer(FILENAMELEN)))
  224.         {
  225.             return;
  226.         }
  227.         m_szFileName.ReleaseBuffer();
  228.  
  229.         ASSERT(m_szFileName.GetLength() != 0);
  230.     }
  231.  
  232.     if (!file.Open(m_szFileName, CFile::modeCreate | CFile::modeWrite))
  233.     {
  234.         MessageBox("Not able to create file.", "Test Client",
  235.             MB_ICONEXCLAMATION | MB_OK);
  236.         return;
  237.     }
  238.     
  239.     // Write out the contents of the buffer to the file.
  240.     // If this fails, inform the user.
  241.     //
  242.     TRY
  243.     {
  244.         CArchive ar(&file, CArchive::store);
  245.         ar << wStartFile;
  246.         m_pClient->Serialize(ar);
  247.         ar.Close();
  248.     }
  249.     CATCH(CFileException, e)
  250.     {
  251.         file.Close();
  252.         MessageBox("Not able to write to file.", "TestClient",
  253.             MB_ICONEXCLAMATION | MB_OK);
  254.         if (m_bLogging)
  255.         {
  256.             ASSERT(pOleDump != NULL);
  257.             pOleDump->WriteString("Caught an Exception while writing file\n");
  258.         }
  259.         return;
  260.     }
  261.     END_CATCH
  262.  
  263.     TRY
  264.     {
  265.         m_pDoc->NotifySaved();
  266.     }
  267.     CATCH (COleException, e)
  268.     {
  269.         if (m_bLogging)
  270.         {
  271.             LogError (THIS_FILE, e->m_status);
  272.         }
  273.  
  274.         MessageBox("Exception thrown while reporting save to library",
  275.             "Test Client", MB_ICONEXCLAMATION);
  276.         return;
  277.     }
  278.     END_CATCH
  279.     m_bUntitled = FALSE;
  280.     m_bDirty = FALSE;
  281.     m_bRevertAvailable = TRUE;
  282. }
  283.  
  284.  
  285.  
  286. /////////////////////////////////////////////////////////////////////////////
  287. // CTestClient OnFileSave()
  288. //
  289. void CTestClient::OnFileSave()
  290. {
  291.     SaveFile(m_bUntitled);
  292. }
  293.  
  294.  
  295.  
  296. /////////////////////////////////////////////////////////////////////////////
  297. // CTestClient OnFileSave()
  298. //
  299. void CTestClient::OnFileSaveAs()
  300. {
  301.     SaveFile(TRUE);
  302. }
  303.  
  304.  
  305. /////////////////////////////////////////////////////////////////////////////
  306. // CTestClient::OnChanged()
  307. //
  308. void CTestClient::OnChanged()
  309. {
  310.     Invalidate(TRUE);
  311.     m_bDirty = TRUE;
  312. }
  313.  
  314.  
  315.  
  316. /////////////////////////////////////////////////////////////////////////////
  317. // CTestClient::OnRevert()
  318. //
  319. void CTestClient::OnRevert()
  320. {
  321.     if (!m_bRevertAvailable)
  322.     {
  323.         return;
  324.     }
  325.     
  326.     TRY
  327.     {
  328.         m_pClient->Delete();
  329.     }
  330.     CATCH (COleException, e)
  331.     {
  332.         MessageBox("Unable to Remove Current Item to Revert", "Test Client",
  333.             MB_ICONEXCLAMATION);
  334.         return;
  335.     }
  336.     END_CATCH
  337.  
  338.     LoadFile(((const char *)m_szFileName));
  339.  
  340.     m_pDoc->NotifyRevert();
  341.     m_bRevertAvailable = FALSE;
  342.     m_bDirty = FALSE;
  343.     Invalidate(TRUE);
  344. }
  345.  
  346.