home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-17 | 5.7 KB | 240 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
-
- long gFolderCount, gFileCount;
-
-
- // Prototypes
-
- void reverse( char s[] );
- void itoa( long n, char s[] );
- long strlen(char *str);
- void CtoP( char* cString, Str255 pString );
- void PtoC( Str255 pString, char* cString );
-
-
- // 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.
-
- gFolderCount = 0;
- gFileCount = 0;
- return ( true );
- }
-
-
- /********************************************************************************/
- //
- // DoToEachFile is a routine which is called for each file the recursion routine
- // encounters.
- //
- /********************************************************************************/
-
- void DoToEachFile( FSSpec fileSpec )
- {
- gFileCount++;
- }
-
-
- /********************************************************************************/
- //
- // DoToEachFolder is a routine which is called for each file the recursion
- // routine encounters.
- //
- /********************************************************************************/
-
- void DoToEachFolder( FSSpec folderSpec )
- {
- gFolderCount++;
- }
-
-
- /********************************************************************************/
- //
- // 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 )
- {
- gFileCount++;
- }
-
-
- /********************************************************************************/
- //
- // 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 )
- {
- gFolderCount++;
- }
-
-
-
- /********************************************************************************/
- //
- // 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 )
- {
- Str255 resultString;
- unsigned long totalCount;
- Str255 fileCountString, folderCountString, totalCountString;
- char *tempStr;
-
- itoa( gFileCount, tempStr );
- CtoP( tempStr, fileCountString );
-
- itoa( gFolderCount, tempStr );
- CtoP( tempStr, folderCountString );
-
- totalCount = ( gFolderCount + gFileCount );
- itoa( totalCount, tempStr );
- CtoP( tempStr, totalCountString );
-
- ConcatPStrings( totalCountString, "\p items: ", resultString );
- ConcatPStrings( resultString, fileCountString, resultString );
- ConcatPStrings( resultString, "\p files and ", resultString );
- ConcatPStrings( resultString, folderCountString, resultString );
- ConcatPStrings( resultString, "\p folders", resultString );
-
- DisplayAlert( resultString );
- }
-
-
- /********************************************************************************/
- //
- // Misc. routines to support what this app needs to do go below here
- //
- /********************************************************************************/
-
-
- //
- // Simplistic routine which reverses the order of characters in a string.
- // Taken from K&R C book.
- //
- void reverse( char s[] )
- {
- long c, i, j;
- for ( i = 0, j = strlen(s) - 1; i < j; i++, j-- )
- {
- c = s[i];
- s[i] = s[j];
- s[j] = c;
- }
- }
-
-
- //
- // Simplistic routine which converts a number into a zero-terminated string.
- // Taken from K&R C book.
- //
- void itoa( long n, char s[] )
- {
- long i, sign;
- if ( ( sign = n ) < 0 )
- n = -n;
- i = 0;
- do
- {
- s[i++] = n % 10 + '0';
- } while ( ( n /= 10 ) > 0 );
- if ( sign < 0 )
- s[i++] = '-';
- s[i] = '\0';
- reverse(s);
- }
-
-
- //
- // Quickie version of strlen (because that's all I need from the string.h library)
- // Taken from K&R C book.
- //
- long strlen(char *str)
- {
- char *ptr = str;
-
- while (*ptr != '\0')
- ptr++;
- return ptr - str;
- }
-
-
- //
- // Extremely rudimentary version of a routine which converts zero-terminated
- // strings to Pascal strings.
- //
- void CtoP( char* cString, Str255 pString )
- {
- short length, i, j;
-
- length = strlen( cString );
-
- for ( i = 0, j = 1; i <= length; i++, j++ )
- pString[j] = cString[i];
-
- pString[0] = length;
- }
-
-
- //
- // Extremely rudimentary version of a routine which converts Pascal Strings
- // to zero-terminated strings.
- //
- void PtoC( Str255 pString, char* cString )
- {
- short length, i, j;
-
- length = pString[0];
-
- for ( i = 1, j = 0; i <= length; i++, j++ )
- cString[j] = pString[i];
-
- cString[length] = '\0';
- }