home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK11 / MFC / SAMPLES / TESTSERV / TSFILE.CP$ / tsfile
Encoding:
Text File  |  1992-03-19  |  6.4 KB  |  310 lines

  1. // tsfile.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 "testserv.h"
  17. #include "resource.h"
  18. #include "defs.h"
  19.  
  20. #include <afxdlgs.h>
  21.  
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24.  
  25. extern void OutputLog(const char* pText);
  26. static char szBuffer[80];
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CTestServer FileDlg
  31. //
  32. BOOL CTestServer::FileDlg( BOOL bOpen, int nMaxFile, LPSTR szFile)
  33. {
  34.     char szFilter[] = "Test OLE Files (*.ols)|*.ols||";
  35.  
  36.     CFileDialog dlg(bOpen, "ols", NULL, 
  37.         OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, szFilter);
  38.     
  39.     ::GetFileTitle(szFile, szFile, nMaxFile);
  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. // CTestServer OnOpen()
  50. //
  51. void CTestServer::OnOpen()
  52. {
  53.     extern CString GLOBAL_szFileName;
  54.  
  55.     ASSERT_VALID (this);
  56.  
  57.     if (!FileDlg(TRUE, FILENAMELEN, 
  58.         GLOBAL_szFileName.GetBuffer(FILENAMELEN)))
  59.     {
  60.         if (m_bLogging)
  61.         {
  62.             OutputLog("CTestServer::OnOpen : Cancelling due to invalid filename\n");
  63.         }
  64.         return;
  65.     }
  66.  
  67.     GLOBAL_szFileName.ReleaseBuffer();
  68.  
  69.  
  70.     if (!LoadFile(GLOBAL_szFileName))
  71.     {
  72.         return;
  73.     }
  74.  
  75.     m_szFileName = GLOBAL_szFileName;
  76.     m_bUntitled = FALSE;
  77.  
  78.     m_pList->ResetContent();
  79.     m_pList->AddString(m_pItem->m_Text);
  80. }
  81.  
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CTestServer LoadFile(const char* pName)
  86. //
  87. BOOL CTestServer::LoadFile(const char* pName)
  88. {
  89.     CFile file;
  90.  
  91.     ASSERT_VALID(this);
  92.     ASSERT(strlen(pName) != 0);
  93.     
  94.     if (!file.Open(pName, CFile::modeRead | CFile::shareDenyWrite))
  95.     {
  96.         MessageBox("Not able to open file.","Test Server",
  97.             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
  98.         
  99.         if (m_bLogging)
  100.         {
  101.             wsprintf(szBuffer, 
  102.                 "CTestServer::Load File : Couldn't Open File %s\n",
  103.                 (LPCSTR)pName);
  104.                 OutputLog(szBuffer);
  105.         }
  106.         return FALSE;
  107.     }
  108.  
  109.     // Set up a CArchive to serialize in the data.
  110.     //
  111.     CArchive ar(&file, CArchive::load);
  112.  
  113.     TRY
  114.     {
  115.         Serialize(ar); // a way to serialize in data
  116.     }
  117.     CATCH(CFileException, e)
  118.     {
  119.         MessageBox("Not able to read from file.", "Test Server",
  120.             MB_ICONEXCLAMATION | MB_OK);
  121.  
  122.         ar.Close();
  123.         file.Close();
  124.  
  125.         if (m_bLogging)
  126.         {
  127.             OutputLog("Have caught a CFileException during loading\n");
  128.         }
  129.         return FALSE;
  130.     }
  131.     END_CATCH
  132.     m_bDirty = FALSE;
  133.     m_pItem->m_pOwner = this;
  134.     m_pDoc->m_pItem = m_pItem;
  135.  
  136.     extern BOOL GLOBAL_bEmbedded;
  137.     if (!GLOBAL_bEmbedded)
  138.     {
  139.         m_pDoc->NotifyRename(pName);
  140.     }
  141.     m_pItem->SetDocName(pName);
  142.                
  143.     ar.Close();
  144.     file.Close();
  145.  
  146.     char szBuf[32], szCaption[80];  
  147.     ::GetFileTitle(pName, szBuf, sizeof(szBuf));
  148.     wsprintf(szCaption,"%s - %s", (LPCSTR)AfxGetAppName(), (LPCSTR)szBuf);
  149.     SetWindowText(szCaption);
  150.  
  151.     return TRUE;
  152. }
  153.         
  154.  
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157. // CTestServer Serialize(CArchive &ar)
  158. //
  159. void CTestServer::Serialize(CArchive &ar)
  160. {
  161.     ASSERT_VALID(this);
  162.     WORD wStartFile = 0xAFC1;  // Private Start-of-File marker
  163.  
  164.     if (ar.IsStoring())
  165.     {
  166.         ar << wStartFile;
  167.             // note that COleClient::Serialize should be called after this
  168.         ar << m_pItem;
  169.     }
  170.     else
  171.     {
  172.         WORD wNew;
  173.         ar >> wNew;
  174.         if (wNew != wStartFile)
  175.         {
  176.             AfxThrowFileException(CFileException::invalidFile);
  177.             return;     // Not our file, don't read it
  178.         }
  179.  
  180.         ar >> m_pItem;
  181.     }
  182. }
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CTestServer SaveFile()
  186. //
  187. void CTestServer::SaveFile (BOOL bNewFileName)
  188. {
  189.     CFile file;
  190.     WORD wStartFile;
  191.     wStartFile = 0xAFC1;  // Private Start-Of-File marker
  192.     
  193.     if (bNewFileName)
  194.     {
  195.         if (m_bUntitled)
  196.         {
  197.             // set up a default name
  198.             m_szFileName = "testserv.ols";
  199.         }
  200.  
  201.  
  202.         if (!FileDlg(FALSE, FILENAMELEN, 
  203.             m_szFileName.GetBuffer(FILENAMELEN)))
  204.         {
  205.             if (m_bLogging)
  206.             {
  207.                 OutputLog("CTestServer::SaveFile : Cancelling due to invalid filename\n");
  208.             }
  209.             return;
  210.         }
  211.         m_szFileName.ReleaseBuffer();
  212.  
  213.         ASSERT(m_szFileName.GetLength() != 0);
  214.     }
  215.  
  216.     if (!file.Open(m_szFileName, CFile::modeCreate | CFile::modeWrite))
  217.     {
  218.         MessageBox("Not able to create file.", "Test Server",
  219.             MB_ICONEXCLAMATION | MB_OK);
  220.  
  221.         if (m_bLogging)
  222.         {
  223.             wsprintf(szBuffer, 
  224.                 "CTestServer::SaveFile : Couldn't Open File %s\n",
  225.                     (LPCSTR)m_szFileName);
  226.             OutputLog(szBuffer);
  227.         }
  228.         return;
  229.     }
  230.     
  231.     // Write out the contents of the buffer to the file.
  232.     // If this fails, inform the user.
  233.     //
  234.     TRY
  235.     {
  236.         CArchive ar(&file,CArchive::store);
  237.         ar << wStartFile;
  238.         ar << m_pItem;
  239.         ar.Close();
  240.     }
  241.     CATCH(CFileException, e)
  242.     {
  243.         file.Close();
  244.         MessageBox("Not able to write to file.", "Test Server",
  245.             MB_ICONEXCLAMATION | MB_OK);
  246.  
  247.         if (m_bLogging)
  248.         {
  249.             OutputLog("CTestServer::SaveFile : Caught archive exception while archiving file\n");
  250.         }
  251.         return;
  252.     }
  253.     END_CATCH
  254.     
  255.     file.Close();
  256.  
  257.     TRY
  258.     {
  259.         m_pDoc->NotifySaved();
  260.         if (bNewFileName)
  261.         {
  262.             m_pDoc->NotifyRename(m_szFileName);
  263.             m_pItem->SetDocName(m_szFileName);
  264.         }
  265.     }
  266.     CATCH (COleException, e)
  267.     {
  268.         if (m_bLogging)
  269.         {
  270.             LogError (THIS_FILE, e->m_status);
  271.         }
  272.  
  273.         MessageBox("Exception Thrown Will Reporting Save to Library",
  274.             "Test Server", MB_ICONEXCLAMATION);
  275.         return;
  276.     }
  277.     END_CATCH
  278.  
  279.     m_bUntitled = FALSE;
  280.     m_bDirty = FALSE;
  281.  
  282.     char szBuf[80];
  283.     wsprintf(szBuf,"%s - %s", (LPCSTR)AfxGetAppName(), (LPCSTR)m_szFileName);
  284.     SetWindowText(szBuf);
  285. }
  286.  
  287.  
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CTestServer OnSave()
  291. //
  292. void CTestServer::OnSave()
  293. {
  294.     ASSERT_VALID (this);
  295.     SaveFile(m_bUntitled);
  296. }
  297.  
  298.  
  299.  
  300. /////////////////////////////////////////////////////////////////////////////
  301. // CTestServer OnSaveAs()
  302. //
  303. void CTestServer::OnSaveAs()
  304. {
  305.     ASSERT_VALID(this);
  306.     SaveFile(TRUE);
  307. }
  308.  
  309.  
  310.