home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-13 | 5.9 KB | 245 lines | [TEXT/CWIE] |
- /********************************************************************************/
- //
- // DoStuff.c
- //
- // Part of a shell program, this is the file that will be changed to make it do
- // what it's suppossed to do.
- //
- /********************************************************************************/
-
-
- #include "DialogUtil.h"
- #include "DoStuff.h"
- #include "Utility.h"
-
-
- // Global variables
-
- short gLFileRef;
-
-
- // Prototypes
- void AppendNameToFile( FSSpec theFile );
- void CopyNameToPtr( Str63 name, Ptr namePtr );
-
-
- // Routines that will be called by the other parts of this program:
- // (Each of these must be present, and their names must be unchanged)
- //
- // InitializeStuff
- // DoToEachFile
- // DoToEachFolder
- // DoToSelFolder
- // DeInitializeStuff
-
-
- /********************************************************************************/
- //
- // InitializeStuff is a routine which gets called before anything else happens.
- // This allows the opportunity to set global variables, read preferences, etc.
- // that will affect how the other parts of this file behave.
- //
- // Expected to return true if initialization succeeded and we should continue
- // processing files.
- //
- /********************************************************************************/
-
- Boolean InitializeStuff( void )
- {
- // Get the name of a text file from the user and create or open it.
- // Store the reference to it in a global variable for later use.
-
- StandardFileReply reply;
- OSErr err;
- Str255 returnValue;
-
- StandardPutFile ( "\pSave results to what file?", "\pUntitled", &reply );
-
- if ( reply.sfGood ) // User did not hit cancel.
- {
- if ( !( reply.sfReplacing ) ) // User gave a new file name.
- {
- // Create a new file:
- err = FSpCreate( &reply.sfFile, 'R*ch', 'TEXT', reply.sfScript );
- if ( err != noErr )
- {
- ResolveOSErr( err, returnValue );
- DisplayAlert( returnValue );
- return ( false );
- }
- }
-
- // Open file with write access:
- err = FSpOpenDF( &reply.sfFile, fsWrPerm, &gLFileRef );
- if ( err != noErr )
- {
- ResolveOSErr( err, returnValue );
- DisplayAlert( returnValue );
- return ( false );
- }
- }
- else // User did hit cancel
- return ( false );
-
- return ( true );
- }
-
-
- /********************************************************************************/
- //
- // DoToEachFile is a routine which is called for each file the recursion routine
- // encounters.
- //
- /********************************************************************************/
-
- void DoToEachFile( FSSpec fileSpec )
- {
- AppendNameToFile( fileSpec );
- }
-
-
- /********************************************************************************/
- //
- // DoToEachFolder is a routine which is called for each file the recursion
- // routine encounters.
- //
- /********************************************************************************/
-
- void DoToEachFolder( FSSpec folderSpec )
- {
- AppendNameToFile( folderSpec );
- }
-
-
- /********************************************************************************/
- //
- // If the user dropped a file on to the icon, after all calls to DoToEachFile
- // and DoToEachFolder are completed, DoToSelFile is called with a reference to
- // the file the user dropped.
- //
- /********************************************************************************/
-
- void DoToSelFile( FSSpec fileSpec )
- {
- AppendNameToFile( fileSpec );
- }
-
-
- /********************************************************************************/
- //
- // If the user selected a folder or dropped one on to the icon, after all calls
- // to DoToEachFile and DoToEachFolder are completed, DoToSelFolder is called with
- // a reference to the folder the user selected or dropped.
- //
- /********************************************************************************/
-
- void DoToSelFolder( FSSpec folderSpec )
- {
- AppendNameToFile( folderSpec );
- }
-
-
-
- /********************************************************************************/
- //
- // DeInitializeStuff is a routine which is called after all the recursive stuff
- // is done. It allows us to close up any files opened, release memory, etc.
- //
- /********************************************************************************/
-
- void DeInitializeStuff( void )
- {
- OSErr err;
- long filePos;
- Str255 returnValue;
-
- // Truncate file where we finished writing:
- err = GetFPos( gLFileRef, &filePos );
- if ( err != noErr )
- {
- ResolveOSErr( err, returnValue );
- DisplayAlert( returnValue );
- }
- else
- {
- err = SetEOF( gLFileRef, filePos );
- if ( err != noErr )
- {
- ResolveOSErr( err, returnValue );
- DisplayAlert( returnValue );
- }
- }
-
- // Close file:
- err = FSClose( gLFileRef );
- if ( err != noErr )
- {
- ResolveOSErr( err, returnValue );
- DisplayAlert( returnValue );
- }
- }
-
-
- /********************************************************************************/
- //
- // Misc. routines to support what this app needs to do go below here
- //
- /********************************************************************************/
-
- void AppendNameToFile( FSSpec fileSpec )
- {
- Str255 returnValue, fullPathName;
- long count, origCount;
- Ptr namePtr;
- OSErr err;
-
- GetFullPath( &fileSpec, fullPathName );
-
- count = fullPathName[0] + 1; // Add one for the return character
- origCount = count;
-
- fullPathName[0] = count; // Extend string by one characer
- fullPathName[count] = '\n'; // Add the return
-
- namePtr = NewPtr( count ); // Try to allocate some memory
- if ( namePtr == nil )
- DisplayAlert( "\pERROR: Out of memory!" );
- else
- {
- CopyNameToPtr( fullPathName, namePtr );
-
- err = FSWrite( gLFileRef, &count, namePtr );
- if ( err != noErr )
- {
- ResolveOSErr( err, returnValue );
- DisplayAlert( returnValue );
- }
- else
- {
- if ( count != origCount )
- DisplayAlert( "\pWARNING: Incomplete data written to file (full disk?)" );
- } // FSWrite error check
-
- DisposePtr( namePtr );
-
- } // NewPtr error check
- }
-
-
- void CopyNameToPtr( Str255 name, Ptr namePtr )
- {
- short i;
- int length;
-
- length = *name++; // Get length of the name + move past length byte
-
- for ( i = 0; i < length; i++ )
- *namePtr++ = *name++;
-
- *namePtr++ = '\n'; // Add a return character at the end
- }
-
-
-
-
-