Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
Validation.c
/************************************************************************************ |
P R O J E C T I N F O |
************************************************************************************* |
Project Name: Simon |
File Name: Validation.c |
Description: Contains C code specific to the validation code resource |
of a simple file transfer tool. |
Copyright © 1992 Apple Computer, Inc. |
All rights reserved. |
************************************************************************************* |
A U T H O R I D E N T I T Y |
************************************************************************************* |
Initials Name |
-------- ----------------------------------------------- |
CH Craig Hotchkiss |
************************************************************************************* |
R E V I S I O N H I S T O R Y |
************************************************************************************* |
Change History (most recent first): |
<0> 5/8/92 CH Creation date |
************************************************************************************/ |
/************************************************************************************ |
STANDARD Mac HEADERS |
************************************************************************************/ |
#ifdef DUMPFILENAME |
#pragma load DUMPFILENAME |
#else |
#define DoNotDump 1 |
#include Load.c |
#endif |
/************************************************************************************ |
Other dependent HEADERs |
************************************************************************************/ |
#ifndef __VALIDATION__ |
#include "Validation.h" |
#endif |
#ifndef __CONSTANTS__ |
#include "Constants.h" |
#endif |
#ifndef __GLOBALS__ |
#include "Globals.h" |
#endif |
/************************************************************************************ |
************************************************************************************/ |
/***************************************************************************** |
* Procedure: MakeTempResourceFile |
* Purpose: Creates temporary files |
* Passed: theReply: a ptr to a sfreply record |
* theFileSpec: a ptr to the FSSpec record |
* Returns: An OSErr indicating nature of the failure |
******************************************************************************/ |
OSErr MakeTempResourceFile( FSSpec* spec) |
{ |
OSErr retValue = noErr; |
short tempVRefNum; |
long tempDirID; |
unsigned long seconds; |
Str255 tempFileName; |
#if Debugging == 1 |
#if OSErrDebug == 1 |
Str255 tempString; |
#endif |
#endif |
GetDateTime( &seconds ); |
NumToString( (long) seconds, tempFileName ); |
if ( noErr == ( retValue = FindFolder( |
kOnSystemDisk, |
kTemporaryFolderType, |
kCreateFolder, |
&tempVRefNum, |
&tempDirID ) ) ) { |
if ( fnfErr == ( retValue = FSMakeFSSpec( |
tempVRefNum, |
tempDirID, |
tempFileName, |
spec ) ) ) { |
FSpCreateResFile( spec, '????', 'pref', smSystemScript ); |
if ( noErr == ( retValue = ResError() ) ) { |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at MakeTempFile/FSpCreateResFile" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
if ( noErr != retValue ) { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at MakeTempFile/FSMakeFSSpec" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at MakeTempFile/FindFolder" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
return ( retValue ); |
} /*MakeTempResourceFile*/ |
/************************************************************************************ |
Procedure: PrefsExists |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
Boolean PrefsExists( FSSpec* spec, Str32 fileName ) |
{ |
Boolean retValue = true; |
OSErr osResult = noErr; |
short fileVRefNum; |
long fileDirID; |
#if Debugging == 1 |
#if OSErrDebug == 1 |
Str255 tempString; |
#endif |
#endif |
if ( noErr == ( osResult = FindFolder( |
kOnSystemDisk, |
kPreferencesFolderType, |
kCreateFolder, |
&fileVRefNum, |
&fileDirID ) ) ) { |
if ( fnfErr == ( osResult = FSMakeFSSpec( |
fileVRefNum, |
fileDirID, |
fileName, |
spec ) ) ) { |
/* we now know the file exists */ |
retValue = false; |
} else { |
if ( noErr != osResult ) { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at PrefsExists/FSMakeFSSpec" ); |
NumToString( (long) osResult, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at PrefsExists/FindFolder" ); |
NumToString( (long) osResult, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
return ( retValue ); |
} /*PrefsExists*/ |
/************************************************************************************ |
Procedure: MakeTempPrefsFile |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr MakeTempPrefsFile( FSSpec* tempSpec, short toolRefNum ) |
{ |
OSErr retValue = noErr; |
short savedResFile = CurResFile(), |
tempRefNum, |
i; |
Handle tempHandle; |
OSType typeArray[] = { |
kDefaultType, |
'TMPL', |
'TMPL', |
'flst' |
}; |
short idArray[] = { |
rDefaultConfigID, |
rDefaultTemplateID, |
rFlstTemplateID, |
rFlstID |
}; |
Str255 resNameArray[] = { |
"\pDefault configuration data", |
kDefaultResName, |
kFlstResName, |
"\pWindow Font Information" |
}; |
#if Debugging == 1 |
#if OSErrDebug == 1 |
Str255 tempString; |
#endif |
#endif |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p Entering MakeTempPrefsFile.;g" ); |
#endif |
#endif |
if ( noErr == ( retValue = MakeTempResourceFile( tempSpec ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p Made it through MakeTempResourceFile.;g" ); |
#endif |
#endif |
if ( ( -1 ) != ( tempRefNum = FSpOpenResFile( tempSpec, fsCurPerm ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p FSpOpenResFile on tempSpec.;g" ); |
#endif |
#endif |
for ( i = 0; i < ( sizeof( typeArray ) / 4 ); ++i ) { |
/* set the current resource file to be the tool */ |
UseResFile( toolRefNum ); |
if ( nil != ( tempHandle = Get1Resource( typeArray[ i ], idArray[ i ] ) ) ) { |
HLock( tempHandle ); |
HNoPurge( tempHandle ); |
DetachResource( tempHandle ); |
if ( noErr == ( retValue = ResError() ) ) { |
UseResFile( tempRefNum ); |
AddResource( tempHandle, typeArray[ i ], idArray[ i ], resNameArray[ i ] ); |
if ( noErr == ( retValue = ResError() ) ) { |
HPurge( tempHandle ); |
HUnlock( tempHandle ); |
UpdateResFile( tempRefNum ); |
if ( noErr == ( retValue = ResError() ) ) { |
ReleaseResource( tempHandle ); |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p UpdateResFile on tempfile.;g" ); |
#endif |
#endif |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p ResError at MakeTempPrefsFile/UpdateResFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
break; |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p ResError at MakeTempPrefsFile/AddResource.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
break; |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p ResError at MakeTempPrefsFile/DetachResource.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
break; |
} |
} else { |
retValue = ResError(); |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p ResError at MakeTempPrefsFile/Get1Resource.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
break; |
} |
} /* for each resource */ |
if ( noErr == ( retValue = FSClose( tempRefNum ) ) ) { |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at MakeTempPrefsFile/FSClose.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
retValue = ResError(); |
DebugStr( "\p ResError at MakeTempPrefsFile/FSpOpenResFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at MakeTempPrefsFile/MakeTempFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
/* create tempfile |
copy defaults to tempfile |
create prefs |
FSpExchangeFiles |
delete tempfile */ |
/* restore original resource file */ |
UseResFile( savedResFile ); |
return ( retValue ); |
} /*MakeTempPrefsFile*/ |
/************************************************************************************ |
Procedure: GetPrefsSpec |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr GetPrefsSpec( FSSpec* spec, short toolRefNum ) |
{ |
OSErr retValue = noErr; |
FSSpec tempSpec; |
#if Debugging == 1 |
#if OSErrDebug == 1 |
Str255 tempString; |
#endif |
#endif |
if ( PrefsExists( spec, kPrefsFileName ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p Prefs file DOES exist.;g" ); |
#endif |
#endif |
} else { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p Prefs file does NOT exist.;g" ); |
#endif |
#endif |
if ( noErr == ( retValue = MakeTempPrefsFile( &tempSpec, toolRefNum ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p MakeTempPrefsFile on the tempfile.;g" ); |
#endif |
#endif |
FSpCreateResFile( spec, '????', 'pref', smSystemScript ); |
if ( noErr == ( retValue = ResError() ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p FSpCreateResFile on prefs.;g" ); |
#endif |
#endif |
if ( noErr == ( retValue = FSpExchangeFiles( &tempSpec, |
spec ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p FSpExchangeFiles on prefs.;g" ); |
#endif |
#endif |
if ( noErr == ( retValue = FSpDelete( &tempSpec ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p FSpDelete on the tempfile.;g" ); |
#endif |
#endif |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at GetPrefsSpec/DeleteTempFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at GetPrefsSpec/FSpExchangeFiles.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at GetPrefsSpec/FSpCreateResFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at GetPrefsSpec/MakeTempPrefsFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} |
return ( retValue ); |
} /*GetPrefsSpec*/ |
/************************************************************************************ |
Procedure: FillFromDefaults |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr FillFromDefaults( ConfigGlobalRec* pConfig, short toolRefNum ) |
{ |
OSErr retValue = noErr; |
short savedResFile = CurResFile(), |
toolResFile = toolRefNum, |
tempRefNum; |
Handle tempHandle; |
FSSpec prefsSpec; |
#if Debugging == 1 |
#if OSErrDebug == 1 |
Str255 tempString; |
#endif |
#endif |
if ( noErr == ( retValue = GetPrefsSpec( &prefsSpec, toolRefNum ) ) ) { |
if ( ( -1 ) != ( tempRefNum = FSpOpenResFile( &prefsSpec, fsCurPerm ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p FSpOpenResFile on prefs.;g" ); |
#endif |
#endif |
if ( nil != ( tempHandle = Get1Resource( kDefaultType, rDefaultConfigID ) ) ) { |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p tempHandle from prefs is valid:;g" ); |
#endif |
#endif |
DetachResource( tempHandle ); |
if ( noErr == ( retValue = ResError() ) ) { |
if ( sizeof( ConfigGlobalRec ) == GetHandleSize( tempHandle ) ) { |
/* Move the resource to our config pointer */ |
BlockMove( (*tempHandle), pConfig, sizeof( ConfigGlobalRec ) ); |
#if Debugging == 1 |
#if PrefsDebug == 1 |
DebugStr( "\p We've BlockMove'ed the config.;g" ); |
#endif |
#endif |
#if Debugging == 1 |
#if ShowDefaults == 1 |
DebugStr( "\p pConfig->fToolVersion is:;g" ); |
NumToString( (long) pConfig->fToolVersion, tempString ); |
DebugStr( tempString ); |
DebugStr( "\p pConfig->fPersistentPackets is:;g" ); |
NumToString( (long) pConfig->fPersistentPackets, tempString ); |
DebugStr( tempString ); |
DebugStr( "\p pConfig->fOKToContinue is:;g" ); |
NumToString( (long) pConfig->fOKToContinue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p Incorrect handle size FillFromDefaults/ConfigGlobalRec." ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p ResError at FillFromDefaults/DetachResource.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
retValue = ResError(); |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p ResError at FillFromDefaults/Get1Resource.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
if ( noErr == ( retValue = FSClose( tempRefNum ) ) ) { |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at FillFromDefaults/FSClose.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
retValue = ResError(); |
DebugStr( "\p ResError at FillFromDefaults/FSpOpenResFile.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
} else { |
#if Debugging == 1 |
#if OSErrDebug == 1 |
DebugStr( "\p OSErr at FillFromDefaults/GetPrefsSpec.;g" ); |
NumToString( (long) retValue, tempString ); |
DebugStr( tempString ); |
#endif |
#endif |
} |
return ( retValue ); |
} /*FillFromDefaults*/ |
/************************************************************************************ |
Procedure: RangeCheckConfig |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr RangeCheckConfig( ConfigGlobalRec* pConfig, short toolRefNum ) |
{ |
OSErr retValue = noErr; |
short savedResFile = CurResFile(), |
toolResFile = toolRefNum; |
/* check out our version number */ |
if ( kThisToolVersion != ( pConfig->fToolVersion ) ) { |
pConfig->fToolVersion = kThisToolVersion; |
} |
if ( ( 0 != ( pConfig->fPersistentPackets ) ) || |
( 1 != ( pConfig->fPersistentPackets ) ) ) { |
pConfig->fPersistentPackets = true; |
} |
if ( ( 0 != ( pConfig->fOKToContinue ) ) || |
( 1 != ( pConfig->fOKToContinue ) ) ) { |
pConfig->fOKToContinue = true; |
} |
return ( retValue ); |
} /*RangeCheckConfig*/ |
/************************************************************************************ |
Procedure: VerifyConfig |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr VerifyConfig( ConfigGlobalRec* pConfig, Boolean pullDefaults, short toolRefNum ) |
{ |
OSErr retValue = noErr; |
if ( sizeof( ConfigGlobalRec ) == GetPtrSize( (Ptr) pConfig ) ) { |
if ( pullDefaults ) { |
if ( noErr == ( retValue = FillFromDefaults( pConfig, toolRefNum ) ) ) { |
} else { |
} |
} else { |
/* ftGenericError tells us something's wrong */ |
if ( ftGenericError == ( retValue = RangeCheckConfig( pConfig, toolRefNum ) ) ) { |
} else { |
} |
} |
} else { |
retValue = ftGenericError; |
} |
return ( retValue ); |
} /*VerifyConfig*/ |
/************************************************************************************ |
Procedure: DoToolDefault |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr DoToolDefault( Ptr* configPtr, Boolean allocateIt, short toolRefNum ) |
{ |
OSErr retValue = noErr; |
if ( allocateIt ) { |
if ( nil != ( *configPtr = NewPtrClear( sizeof( ConfigGlobalRec ) ) ) ) { |
} else { |
retValue = MemError(); |
} |
} |
if ( ( noErr == retValue ) && |
( noErr == ( retValue = VerifyConfig( (ConfigGlobalRec*) *configPtr, |
true, |
toolRefNum ) ) ) ) { |
} else { |
retValue = ftGenericError; |
} |
return ( retValue ); |
} /*DoToolDefault*/ |
/************************************************************************************ |
Procedure: DoToolValidate |
Purpose: |
Inputs: |
Outputs: |
************************************************************************************/ |
OSErr DoToolValidate( FTHandle toolHandle ) |
{ |
OSErr retValue = noErr; |
ToolGlobalRec* pGlobals; |
ConfigGlobalRec* pConfig; |
if ( ( nil != toolHandle ) && |
( nil != ( pConfig = (ConfigGlobalRec*) ( (**toolHandle).config ) ) ) && |
( nil != ( pGlobals = (ToolGlobalRec*) ( (**toolHandle).ftPrivate ) ) ) ) { |
if ( noErr == ( retValue = VerifyConfig( pConfig, false, (**toolHandle).procID ) ) ) { |
} |
} else { |
retValue = ftGenericError; |
} |
return ( retValue ); |
} /*DoToolValidate*/ |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14