home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / COMPOUND.ZIP / Core.cpp next >
Encoding:
C/C++ Source or Header  |  1998-11-09  |  6.6 KB  |  301 lines

  1. //
  2. // Core.cpp
  3. //+
  4. // Core input/output routines for OLE compound documents - Structured storage
  5. //-
  6. //    rev 11/01/98 gls
  7. //
  8.  
  9. #include "stdafx.h"
  10. #include "gstg.h"
  11. #include "Core.h"
  12.  
  13.  
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CCore
  23.  
  24. IMPLEMENT_DYNCREATE(CCore, CCmdTarget)
  25.  
  26. CCore::CCore()
  27. {
  28.     EnableAutomation();
  29.     
  30.     // To keep the application running as long as an OLE automation 
  31.     //    object is active, the constructor calls AfxOleLockApp.
  32.     
  33.     AfxOleLockApp();
  34.  
  35.  
  36. }
  37.  
  38. CCore::~CCore()
  39. {
  40.     // To terminate the application when all objects created with
  41.     //     with OLE automation, the destructor calls AfxOleUnlockApp.
  42.     
  43.     AfxOleUnlockApp();
  44.  
  45.  
  46.     m_StgFile.CloseStg();
  47. }
  48.  
  49.  
  50. void CCore::OnFinalRelease()
  51. {
  52.     // When the last reference for an automation object is released
  53.     // OnFinalRelease is called.  The base class will automatically
  54.     // deletes the object.  Add additional cleanup required for your
  55.     // object before calling the base class.
  56.  
  57.     CCmdTarget::OnFinalRelease();
  58. }
  59.  
  60.  
  61. BEGIN_MESSAGE_MAP(CCore, CCmdTarget)
  62.     //{{AFX_MSG_MAP(CCore)
  63.         // NOTE - the ClassWizard will add and remove mapping macros here.
  64.     //}}AFX_MSG_MAP
  65. END_MESSAGE_MAP()
  66.  
  67. BEGIN_DISPATCH_MAP(CCore, CCmdTarget)
  68.     //{{AFX_DISPATCH_MAP(CCore)
  69.     DISP_FUNCTION(CCore, "Create", Create, VT_I4, VTS_BSTR)
  70.     DISP_FUNCTION(CCore, "Open", Open, VT_I4, VTS_BSTR)
  71.     DISP_FUNCTION(CCore, "Close", Close, VT_I4, VTS_NONE)
  72.     DISP_FUNCTION(CCore, "MkDir", MkDir, VT_I4, VTS_BSTR)
  73.     DISP_FUNCTION(CCore, "CopyTo", CopyTo, VT_I4, VTS_BSTR VTS_BSTR)
  74.     DISP_FUNCTION(CCore, "CopyFrom", CopyFrom, VT_I4, VTS_BSTR VTS_BSTR)
  75.     //}}AFX_DISPATCH_MAP
  76. END_DISPATCH_MAP()
  77.  
  78. // Note: we add support for IID_ICore to support typesafe binding
  79. //  from VBA.  This IID must match the GUID that is attached to the 
  80. //  dispinterface in the .ODL file.
  81.  
  82. // {C33397D4-71C5-11D2-A233-968A023E954F}
  83. static const IID IID_ICore =
  84. { 0xc33397d4, 0x71c5, 0x11d2, { 0xa2, 0x33, 0x96, 0x8a, 0x2, 0x3e, 0x95, 0x4f } };
  85.  
  86. BEGIN_INTERFACE_MAP(CCore, CCmdTarget)
  87.     INTERFACE_PART(CCore, IID_ICore, Dispatch)
  88. END_INTERFACE_MAP()
  89.  
  90. // {C33397D5-71C5-11D2-A233-968A023E954F}
  91. IMPLEMENT_OLECREATE(CCore, "gstg.Core", 0xc33397d5, 0x71c5, 0x11d2, 0xa2, 0x33, 0x96, 0x8a, 0x2, 0x3e, 0x95, 0x4f)
  92.  
  93. // ////////////////////////////////////////////////////////////
  94. // CCore message handlers
  95.  
  96. // ////////////////////////////////////////////////////////////
  97. //
  98. //    Create the specified file
  99. //
  100. long CCore::Create(LPCTSTR pszFileStg) 
  101. {
  102.     USES_CONVERSION;                // needed for OLE2CT
  103.  
  104.     printf( "CCore::Create( %s )\n", pszFileStg );
  105.  
  106.     long    CC = S_FALSE;
  107.  
  108.     if( m_StgFile.CreateStg( pszFileStg ) )
  109.         {
  110.         CC = S_OK;
  111.         }
  112.  
  113.     return( CC );
  114. }
  115. // ////////////////////////////////////////////////////////////
  116. //
  117. //    Open the specified storage for subsequent operations
  118. //
  119. long CCore::Open(LPCTSTR pszFileStg) 
  120. {
  121.     USES_CONVERSION;                // needed for OLE2CT
  122.  
  123.     printf( "CCore::Open( %s )\n", pszFileStg );
  124.  
  125.     m_StgFile.CloseStg();
  126.  
  127.     long CC = S_FALSE;
  128.  
  129.     if( m_StgFile.OpenStg( pszFileStg ) )
  130.         {
  131.         CC = S_OK;
  132.         }
  133.  
  134.     return( CC );
  135. }
  136. // ////////////////////////////////////////////////////////////
  137. //
  138. //    Close and commit any changes
  139. //
  140. long CCore::Close() 
  141. {
  142.     long CC = S_FALSE;
  143.  
  144.     if( m_StgFile.CloseStg() )
  145.         {
  146.         CC = S_OK;
  147.         }
  148.  
  149.     return( CC );
  150. }
  151. // ////////////////////////////////////////////////////////////
  152. //
  153. //    Make the specified directory (sub-storage)
  154. //
  155. long CCore::MkDir(LPCTSTR pszDir) 
  156. {
  157.     USES_CONVERSION;                // needed for OLE2CT
  158.  
  159.     printf( "CCore::MkDir( %s )\n", pszDir );
  160.  
  161.     long CC = S_FALSE;
  162.  
  163.     if( m_StgFile.MkStg( pszDir ) )
  164.         {
  165.         CC = S_OK;
  166.         }
  167.  
  168.     return( CC );
  169. }
  170. // ////////////////////////////////////////////////////////////
  171. //
  172. //    Copy the external file to the storage stream
  173. //
  174. long CCore::CopyTo(LPCTSTR pszFileExternal, LPCTSTR pszFileStg) 
  175. {
  176.     USES_CONVERSION;                // needed for OLE2CT
  177.  
  178.     printf( "CCore::CopyTo( %s, %s )\n", pszFileExternal, pszFileStg );
  179.  
  180.     long CC = S_FALSE;
  181.  
  182.     if( !m_StgFile.isOpen() )
  183.         {
  184.         return( S_FALSE );                // not open
  185.         }
  186.  
  187.  
  188.     //
  189.     //    Determine if only a sub-storage was specified.
  190.     //    If so, add the filename to it.
  191.     //
  192.     CString sNameStream = pszFileStg;
  193.     if( *pszFileStg == '\\' )
  194.         {
  195.         int    iSlash = sNameStream.Find( '\\' );
  196.         int iSlash2 = sNameStream.ReverseFind( '\\' );
  197.         if( iSlash == iSlash2 )
  198.             {
  199.             char    szFilename[_MAX_PATH], szExt[_MAX_PATH];
  200.             _splitpath( pszFileExternal, NULL, NULL, szFilename, szExt );
  201.  
  202.             sNameStream.Format( "%s\\%s%s", pszFileStg, szFilename, szExt );
  203.             }
  204.         }
  205.  
  206.  
  207.     //
  208.     //    Open the file and copy external file to stream
  209.     //
  210.     if( m_StgFile.Open( sNameStream, CFile::modeCreate | CFile::modeWrite ) )
  211.         {
  212.         //
  213.         //    Now, the actual copy to the output stream
  214.         //
  215.         CFile FileSrc;
  216.         if( FileSrc.Open( pszFileExternal, CFile::modeRead ) )
  217.             {
  218.             UINT    cB = 0;
  219.             BYTE    rgB[512*8];
  220.             while( (cB = FileSrc.Read( rgB, sizeof(rgB) )) > 0 )
  221.                 {
  222.                 m_StgFile.Write( rgB, cB );
  223.                 }
  224.  
  225.             CC = S_OK;
  226.             }
  227.         else
  228.             {
  229.             ASSERT( 0 );
  230.             }
  231.  
  232.         m_StgFile.Close();            // must cleanup
  233.         }
  234.  
  235.  
  236.  
  237.     return( CC );
  238. }
  239. // ////////////////////////////////////////////////////////////
  240. //
  241. //    Copy from the structured storage to the external filename
  242. //
  243. long CCore::CopyFrom(LPCTSTR pszFileStg, LPCTSTR pszFileExternal) 
  244. {
  245.     USES_CONVERSION;                // needed for OLE2CT
  246.  
  247.     printf( "CCore::CopyFrom( %s, %s )\n", pszFileStg, pszFileExternal );
  248.  
  249.     long CC = S_FALSE;
  250.  
  251.     if( !m_StgFile.isOpen() )
  252.         {
  253.         return( S_FALSE );                // not open
  254.         }
  255.  
  256.  
  257.     //
  258.     //    Form a valid name for the class to open
  259.     //
  260.     CString sNameStream = pszFileStg;
  261.  
  262.  
  263.     //
  264.     //    Now, the actual copy to the output stream
  265.     //
  266.     CFile FileDes;
  267.     if( FileDes.Open( pszFileExternal, CFile::modeWrite | CFile::modeCreate ) )
  268.         {
  269. //        printf( "Open stream %s\n", sNameStream );
  270.  
  271.         //
  272.         //    Create a stream from the specified storage filename
  273.         //
  274.         VERIFY( m_StgFile.Open( sNameStream, CFile::modeRead ) );
  275.  
  276.         //
  277.         //    Copy the data to the output stream
  278.         //
  279.         UINT    cB = 0;
  280.         BYTE    rgB[512*8];
  281.         while( (cB = m_StgFile.Read( rgB, sizeof(rgB) )) > 0 )
  282.             {
  283.             FileDes.Write( rgB, cB );
  284.             }
  285.  
  286.         CC = S_OK;
  287.  
  288.         m_StgFile.Close();            // must cleanup
  289.         }
  290.     else
  291.         {
  292.         ASSERT( 0 );
  293.         }
  294.  
  295.  
  296.     return( CC );
  297. }
  298. // ////////////////////////////////////////////////////////////
  299. // ////////////////////////////////////////////////////////////
  300. // ////////////////////////////////////////////////////////////
  301.