/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540.3430
*	Web: http://www.rollthunder.com
*
*	File Name: STGUTIL.CPP
*
*	Purpose: Utility functions for structured storage app
*
*********************************************************************
*/


#include <windows.h>
#include "main.h"



extern CLSID MyCLSID ;
extern HWND hEdit ;

BOOL WriteNewStorageStructure (LPSTORAGE pNewStg) 
{
	DWORD nActual ;
	HRESULT hr	;

/*
write our class ID into the storage so we can identify it
*/

	pNewStg->SetClass (MyCLSID) ;

/*
Create a stream for data in DisplayText[], then release it because we don't need it 
here any more.
*/
	LPSTREAM pDisplayText ;

	hr = pNewStg->CreateStream (L"DisplayText",
			STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 
			0, 0, &pDisplayText) ;

	pDisplayText->Release( ) ;

	
/*
Create new substorage for summary info.
*/
	LPSTORAGE pSubStg ;

	hr = pNewStg->CreateStorage (L"Summary Info", STGM_CREATE | STGM_DIRECT |
		STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pSubStg) ;

/*
Create new streams in the Summary Info substorage, write default
data into them, then release both streams and substorage.
*/

	LPSTREAM pAuthor, pTitle ;

	pSubStg->CreateStream (L"Author", 
		STGM_CREATE | STGM_DIRECT |	STGM_WRITE | STGM_SHARE_EXCLUSIVE,
		0, 0, &pAuthor)	;

	pAuthor->Write ("Some Bozo", 10, &nActual) ;
	pAuthor->Release ( ) ;

	pSubStg->CreateStream (L"Title", 
		STGM_CREATE | STGM_DIRECT |	STGM_WRITE | STGM_SHARE_EXCLUSIVE,
		0, 0, &pTitle) ;

	pTitle->Write ("Pointless Memo", 15, &nActual) ;
	pTitle->Release ( ) ;

	hr = pSubStg->Release ( ) ;
	return TRUE ;
}	



BOOL OpenFileByName (OLECHAR *pName, LPSTORAGE *ppStgOut)
{

	LPSTORAGE pNewStg ;
	LPSTREAM pStream ;


/*
Check to see if this is a compound storage file.  If not, complain and quit.
*/
	if (StgIsStorageFile (pName) != S_OK)
	{
		MessageBox (NULL, "File is not a structured storage file",
			"Error", MB_OK | MB_ICONSTOP) ;
		return FALSE ;
	}
	

/*
It is a structured storage file; open it. If unable to, 
the complain and quit.
*/

	StgOpenStorage (pName, NULL, 
		STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_TRANSACTED, 
		NULL, 0, &pNewStg) ;


	char AnsiName [256] ;
	WideCharToMultiByte (CP_ACP, 0, pName, -1, AnsiName, sizeof (AnsiName), NULL, NULL) ;

	if (!pNewStg)
	{
		MessageBox (NULL, AnsiName, "Couldn't open file", MB_OK | MB_ICONSTOP) ;
		return FALSE ;
	}

/*
Check to see if the root storage's class ID is the one we gave it, i.e. it's one 
of our files.  If not, complain and quit.
*/

	STATSTG stat ;

	pNewStg->Stat (&stat, STATFLAG_DEFAULT) ;

	if (MyCLSID != stat.clsid)
	{
		MessageBox (NULL, "File's class ID is different from ours",
			"Error", MB_OK | MB_ICONSTOP) ;

		pNewStg->Release ( ) ;
		return FALSE ;
	}


/*
It's our file, all right.  Open stream that contains text data, read it in,
and release the stream
*/

	char buf [4096] ;
	
	pNewStg->OpenStream (L"DisplayText",  NULL, 
		STGM_READ | STGM_SHARE_EXCLUSIVE, NULL,
		&pStream) ;

	pStream->Read (buf, sizeof(buf), NULL) ;
	pStream->Release( ) ;

	SetWindowText (hEdit, buf) ;

	*ppStgOut = pNewStg ;

	return TRUE ;
}
