
#include "ZipGUI.h"


extern ZipGUI_Globals	g;


Boolean DoSave( WindowRef myWindow )
{
	OSErr	myErr = noErr;

	myErr = DoSaveAs( myWindow );

	if ( myErr == userCanceledErr )
		return false;
	else if ( myErr ) {
		DoAlertUser( eSaveError, false );
		return false;
	} else
		return true;
} /* DoSave */




OSErr DoSaveDoc( WindowRef myWindow )
{
	DocumentPtr	myDoc;
	long		myLength;
	OSErr		myErr = noErr;
		
	myDoc = (DocumentPtr)myWindow;
	myErr = FSpOpenDF( &myDoc->fileSpec, fsWrPerm, &myDoc->fileRefNum );
	if ( !myErr )
		myErr = SetFPos( myDoc->fileRefNum, fsFromStart, 0 );
	if ( !myErr ) {	
		myLength = (long)(**myDoc->docTxt).teLength;
		myErr = FSWrite( myDoc->fileRefNum, &myLength, *(**myDoc->docTxt).hText );
	}
	if ( !myErr )
		myErr = SetEOF( myDoc->fileRefNum, myLength );
	if ( !myErr )
		myErr = FSClose( myDoc->fileRefNum );
	if ( !myErr )
		myErr = FlushVol( nil, (myDoc->fileSpec).vRefNum );
	if ( !myErr )
		myDoc->modified = false;

	return myErr;
} /* DoSaveDoc */



OSErr DoSaveMessages(void)
{
	StandardFileReply	myReply;
	OSErr				myErr = noErr;
	Str255			myTitle;
	Str32			myApp;

	GetIndString( myTitle, rWindowTitles, MessageWindow );
	GetIndString( myApp, rCurrentAppInfo, g.CurrentApp);
	
	DoAddString( myTitle, "\p " );
	DoAddString( myTitle, myApp);
	
	StandardPutFile( myTitle, myTitle, &myReply );
	
	if ( myReply.sfGood ) {  
		g.SaveMessagesFSSpec = myReply.sfFile;
	}
	else g.SaveMessages = !g.SaveMessages;
	return myErr;	
} /* DoSaveAs */





OSErr DoSaveAs( WindowRef myWindow )
{
	Str255				myMessage, myTitle;
	StandardFileReply	myReply;
	DocumentPtr			myDoc;
	OSErr				myErr = noErr;
	
	myDoc = (DocumentPtr)myWindow;
	GetIndString( myMessage, rStandStrings, sSaveAs );
	GetWTitle( myWindow, myTitle );
	StandardPutFile( myMessage, myTitle, &myReply );
	if ( myReply.sfGood ) {
		if ( !myReply.sfReplacing ) { 
			myErr = FSpCreate( &myReply.sfFile, kCreator, kFileType, 
								myReply.sfScript );
			if ( myErr ) 
				return myErr;
		}
		myDoc->fileSpec = myReply.sfFile;
		myErr = DoSaveDoc( myWindow );
		if ( !myErr )
			SetWTitle( myWindow, myReply.sfFile.name );
	} else
		myErr = userCanceledErr;
		
	return myErr;	
} /* DoSaveAs */




void DoOpen( void )
{
	SFTypeList			myTypes = {'TEXT'};
	StandardFileReply	myReply;

	StandardGetFile( nil, 1, myTypes, &myReply );	
	if ( myReply.sfGood )
		DoOpenFile( myReply.sfFile );
} /* DoOpen */





OSErr DoOpenFile( FSSpec mySpec )
{
	WindowRef		myWindow = nil;
	DocumentPtr		myDoc;
	OSErr			myErr = noErr;

	myWindow = DoNewDocWindow();
	if ( myWindow ) {
		myDoc = (DocumentPtr)myWindow;
		g.numDocuments++;
		myErr = FSpOpenDF( &mySpec, fsRdPerm, &myDoc->fileRefNum );
		if ( !myErr )		
			myErr = DoReadFile( myDoc->fileRefNum, myDoc->docTxt );
		if ( !myErr )		
			myErr = FSClose( myDoc->fileRefNum );
		if ( !myErr ) {
			TESetSelect( 0, 0, ((DocumentPtr)myWindow)->docTxt );
			DoAdjustScrollValues( myWindow );
			SetWTitle( myWindow, mySpec.name );
			myDoc->fileSpec = mySpec;
			myDoc->modified = false;
			ShowWindow( myWindow );
		} else {
			DoCloseWindow( myWindow );
			if ( myErr != kTooBig )
				DoAlertUser( eOpenError, false );
		}
	}
	return myErr;
} /* DoOpenFile */




OSErr DoReadFile( short myRefNum, TEHandle myTxtH )
{
	Ptr 	myBuffer;
	long 	myLength;
	OSErr	myErr = noErr;
	
	GetEOF( myRefNum, &myLength );
	if ( myLength > kMaxTELength ) {
		DoAlertUser( eFileTooLarge, false );
		return kTooBig;
	}
	myBuffer = NewPtrClear( myLength );
	if ( !myBuffer )
		return MemError();
	myErr = SetFPos( myRefNum, fsFromStart, 0 );		
	if ( !myErr )
		myErr = FSRead( myRefNum, &myLength, myBuffer );
	if ( !myErr )
		TESetText( myBuffer , myLength, myTxtH );
	DisposePtr( myBuffer );
	return myErr;
} /* DoReadFile */
