home *** CD-ROM | disk | FTP | other *** search
- //
- // Core.cpp
- //+
- // Core input/output routines for OLE compound documents - Structured storage
- //-
- // rev 11/01/98 gls
- //
-
- #include "stdafx.h"
- #include "gstg.h"
- #include "Core.h"
-
-
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CCore
-
- IMPLEMENT_DYNCREATE(CCore, CCmdTarget)
-
- CCore::CCore()
- {
- EnableAutomation();
-
- // To keep the application running as long as an OLE automation
- // object is active, the constructor calls AfxOleLockApp.
-
- AfxOleLockApp();
-
-
- }
-
- CCore::~CCore()
- {
- // To terminate the application when all objects created with
- // with OLE automation, the destructor calls AfxOleUnlockApp.
-
- AfxOleUnlockApp();
-
-
- m_StgFile.CloseStg();
- }
-
-
- void CCore::OnFinalRelease()
- {
- // When the last reference for an automation object is released
- // OnFinalRelease is called. The base class will automatically
- // deletes the object. Add additional cleanup required for your
- // object before calling the base class.
-
- CCmdTarget::OnFinalRelease();
- }
-
-
- BEGIN_MESSAGE_MAP(CCore, CCmdTarget)
- //{{AFX_MSG_MAP(CCore)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- BEGIN_DISPATCH_MAP(CCore, CCmdTarget)
- //{{AFX_DISPATCH_MAP(CCore)
- DISP_FUNCTION(CCore, "Create", Create, VT_I4, VTS_BSTR)
- DISP_FUNCTION(CCore, "Open", Open, VT_I4, VTS_BSTR)
- DISP_FUNCTION(CCore, "Close", Close, VT_I4, VTS_NONE)
- DISP_FUNCTION(CCore, "MkDir", MkDir, VT_I4, VTS_BSTR)
- DISP_FUNCTION(CCore, "CopyTo", CopyTo, VT_I4, VTS_BSTR VTS_BSTR)
- DISP_FUNCTION(CCore, "CopyFrom", CopyFrom, VT_I4, VTS_BSTR VTS_BSTR)
- //}}AFX_DISPATCH_MAP
- END_DISPATCH_MAP()
-
- // Note: we add support for IID_ICore to support typesafe binding
- // from VBA. This IID must match the GUID that is attached to the
- // dispinterface in the .ODL file.
-
- // {C33397D4-71C5-11D2-A233-968A023E954F}
- static const IID IID_ICore =
- { 0xc33397d4, 0x71c5, 0x11d2, { 0xa2, 0x33, 0x96, 0x8a, 0x2, 0x3e, 0x95, 0x4f } };
-
- BEGIN_INTERFACE_MAP(CCore, CCmdTarget)
- INTERFACE_PART(CCore, IID_ICore, Dispatch)
- END_INTERFACE_MAP()
-
- // {C33397D5-71C5-11D2-A233-968A023E954F}
- IMPLEMENT_OLECREATE(CCore, "gstg.Core", 0xc33397d5, 0x71c5, 0x11d2, 0xa2, 0x33, 0x96, 0x8a, 0x2, 0x3e, 0x95, 0x4f)
-
- // ////////////////////////////////////////////////////////////
- // CCore message handlers
-
- // ////////////////////////////////////////////////////////////
- //
- // Create the specified file
- //
- long CCore::Create(LPCTSTR pszFileStg)
- {
- USES_CONVERSION; // needed for OLE2CT
-
- printf( "CCore::Create( %s )\n", pszFileStg );
-
- long CC = S_FALSE;
-
- if( m_StgFile.CreateStg( pszFileStg ) )
- {
- CC = S_OK;
- }
-
- return( CC );
- }
- // ////////////////////////////////////////////////////////////
- //
- // Open the specified storage for subsequent operations
- //
- long CCore::Open(LPCTSTR pszFileStg)
- {
- USES_CONVERSION; // needed for OLE2CT
-
- printf( "CCore::Open( %s )\n", pszFileStg );
-
- m_StgFile.CloseStg();
-
- long CC = S_FALSE;
-
- if( m_StgFile.OpenStg( pszFileStg ) )
- {
- CC = S_OK;
- }
-
- return( CC );
- }
- // ////////////////////////////////////////////////////////////
- //
- // Close and commit any changes
- //
- long CCore::Close()
- {
- long CC = S_FALSE;
-
- if( m_StgFile.CloseStg() )
- {
- CC = S_OK;
- }
-
- return( CC );
- }
- // ////////////////////////////////////////////////////////////
- //
- // Make the specified directory (sub-storage)
- //
- long CCore::MkDir(LPCTSTR pszDir)
- {
- USES_CONVERSION; // needed for OLE2CT
-
- printf( "CCore::MkDir( %s )\n", pszDir );
-
- long CC = S_FALSE;
-
- if( m_StgFile.MkStg( pszDir ) )
- {
- CC = S_OK;
- }
-
- return( CC );
- }
- // ////////////////////////////////////////////////////////////
- //
- // Copy the external file to the storage stream
- //
- long CCore::CopyTo(LPCTSTR pszFileExternal, LPCTSTR pszFileStg)
- {
- USES_CONVERSION; // needed for OLE2CT
-
- printf( "CCore::CopyTo( %s, %s )\n", pszFileExternal, pszFileStg );
-
- long CC = S_FALSE;
-
- if( !m_StgFile.isOpen() )
- {
- return( S_FALSE ); // not open
- }
-
-
- //
- // Determine if only a sub-storage was specified.
- // If so, add the filename to it.
- //
- CString sNameStream = pszFileStg;
- if( *pszFileStg == '\\' )
- {
- int iSlash = sNameStream.Find( '\\' );
- int iSlash2 = sNameStream.ReverseFind( '\\' );
- if( iSlash == iSlash2 )
- {
- char szFilename[_MAX_PATH], szExt[_MAX_PATH];
- _splitpath( pszFileExternal, NULL, NULL, szFilename, szExt );
-
- sNameStream.Format( "%s\\%s%s", pszFileStg, szFilename, szExt );
- }
- }
-
-
- //
- // Open the file and copy external file to stream
- //
- if( m_StgFile.Open( sNameStream, CFile::modeCreate | CFile::modeWrite ) )
- {
- //
- // Now, the actual copy to the output stream
- //
- CFile FileSrc;
- if( FileSrc.Open( pszFileExternal, CFile::modeRead ) )
- {
- UINT cB = 0;
- BYTE rgB[512*8];
- while( (cB = FileSrc.Read( rgB, sizeof(rgB) )) > 0 )
- {
- m_StgFile.Write( rgB, cB );
- }
-
- CC = S_OK;
- }
- else
- {
- ASSERT( 0 );
- }
-
- m_StgFile.Close(); // must cleanup
- }
-
-
-
- return( CC );
- }
- // ////////////////////////////////////////////////////////////
- //
- // Copy from the structured storage to the external filename
- //
- long CCore::CopyFrom(LPCTSTR pszFileStg, LPCTSTR pszFileExternal)
- {
- USES_CONVERSION; // needed for OLE2CT
-
- printf( "CCore::CopyFrom( %s, %s )\n", pszFileStg, pszFileExternal );
-
- long CC = S_FALSE;
-
- if( !m_StgFile.isOpen() )
- {
- return( S_FALSE ); // not open
- }
-
-
- //
- // Form a valid name for the class to open
- //
- CString sNameStream = pszFileStg;
-
-
- //
- // Now, the actual copy to the output stream
- //
- CFile FileDes;
- if( FileDes.Open( pszFileExternal, CFile::modeWrite | CFile::modeCreate ) )
- {
- // printf( "Open stream %s\n", sNameStream );
-
- //
- // Create a stream from the specified storage filename
- //
- VERIFY( m_StgFile.Open( sNameStream, CFile::modeRead ) );
-
- //
- // Copy the data to the output stream
- //
- UINT cB = 0;
- BYTE rgB[512*8];
- while( (cB = m_StgFile.Read( rgB, sizeof(rgB) )) > 0 )
- {
- FileDes.Write( rgB, cB );
- }
-
- CC = S_OK;
-
- m_StgFile.Close(); // must cleanup
- }
- else
- {
- ASSERT( 0 );
- }
-
-
- return( CC );
- }
- // ////////////////////////////////////////////////////////////
- // ////////////////////////////////////////////////////////////
- // ////////////////////////////////////////////////////////////
-