home *** CD-ROM | disk | FTP | other *** search
- /*
- ************************************************************************
- * Service C++ functions
- * that support the standard environment for me
- */
-
- #pragma implementation
-
- #include "myenv.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdarg.h>
- #include <Sound.h>
-
- /*
- *-----------------------------------------------------------------------
- * Some global constant pertaining to input/output
- */
-
- const char _Minuses [] = "\
- -------------------------------------------------------------------------------";
-
- const char _Asteriscs [] = "\
- *******************************************************************************";
-
- const char _Equals [] = "\
- ===============================================================================";
-
-
- static const int System_Alert_rsc = -16411; // ID of the System ALRT
- // resource (in the System file)
- /*
- *------------------------------------------------------------------------
- * Print an error message at stderr and abort
- * Synopsis
- * volatile void _error(const char * message,... );
- * Message may contain format control sequences %x. Items to print
- * with the control sequences are to be passed as additional arguments to
- * the function call.
- */
-
- static volatile void (*abort_function)(void) = (volatile void (*)())abort;
-
- volatile void _error(const char * message,...)
- {
- va_list args;
- char buffer[1000];
- va_start(args,message); // Init 'args' to the beginning of
- // the variable length list of args
- vsprintf(buffer,message,args);
- strncat(buffer,"\rSorry man, enough is enough, I'm outta here",sizeof(buffer)-strlen(buffer)-1);
- CtoPstr(buffer); // Convert to Pascal-like string
- ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
- Alert(System_Alert_rsc,0L); // Display the Alert box and the string
- // set just before
- abort_function(); // And crash it, at last
- }
-
- void set_abort_function(volatile void (*custom_abort)(void))
- {
- if( custom_abort == nil )
- abort_function = (volatile void (*)())abort;
- else
- abort_function = custom_abort;
- }
-
- // This guy handles the situation when the memory
- // allocation through 'new' has failed
- void my_new_failed_handler(void)
- {
- _error("Flat out of memory");
- }
-
- /*
- *------------------------------------------------------------------------
- * Print a message at stderr
- * Synopsis
- * void message(const char * text,... );
- * Message may contain format control sequences %x. Items to print
- * with the control sequences are to be passed as additional arguments to
- * the function call.
- */
-
- void message(const char * text,...)
- {
- va_list args;
- char buffer[1000];
- va_start(args,text); // Init 'args' to the beginning of
- // the variable length list of args
- vsprintf(buffer,text,args);
- CtoPstr(buffer); // Convert to Pascal-like string
- ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
- Alert(System_Alert_rsc,0L); // Display the Alert box and the string
- }
-
- /*
- *------------------------------------------------------------------------
- * Some more advanced error handling
- */
-
- ErrHandler::ErrHandler(void)
- {
- raise_hell = TRUE;
- was_error = FALSE;
- }
-
- // Be lenient in case of error
- void ErrHandler::lenient(void)
- {
- if( was_error )
- _error("You've got an error you haven't handled!");
- raise_hell = FALSE;
- }
-
- // Have we failed?
- Boolean ErrHandler::was_ok(void)
- {
- raise_hell = TRUE;
- if( was_error )
- {
- was_error = FALSE;
- return FALSE;
- }
- return TRUE;
- }
-
- // Notify the user about the error and raise the hell if needed
- void ErrHandler::error(const char * message,...)
- {
- va_list args;
- char buffer[1000];
- va_start(args,message); // Init 'args' to the beginning of
- // the variable length list of args
- vsprintf(buffer,message,args);
- if( raise_hell )
- strncat(buffer,"\rRaising the hell - crashing",sizeof(buffer)-strlen(buffer)-1);
- CtoPstr(buffer); // Convert to Pascal-like string
- ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
- Alert(System_Alert_rsc,0L); // Display the Alert box and the string
- // set just before
- if( raise_hell )
- abort_function(); // And crash it, at last
- else
- {
- raise_hell = TRUE;
- was_error = FALSE;
- } // And let it go for now
- }
-
- //------------------------------------------------------------------------
- // Sound playing on errors
-
-
- static Handle error_sound_handle = nil;
-
- static pascal void error_sound_player(short sound_no)
- {
- if( error_sound_handle != nil )
- SndPlay(nil,error_sound_handle,FALSE);
- else
- SysBeep(10);
- error_sound_handle = nil;
- }
-
- void set_error_sound(Str255 sound_name)
- {
- error_sound_handle = GetNamedResource('snd ',sound_name);
- if( error_sound_handle != nil )
- ErrorSound((pascal void (*)())error_sound_player);
- }
-
-
- /*
- *------------------------------------------------------------------------
- * Suspending the application for a specified period of time
- *
- * Unlike Delay(), the present function is generous in that in relinquishes the
- * CPU control for a specified time and thus lets other (backgrounded) application
- * run.
- * The generous waiting is implemented as waiting for a null event which is to
- * be sent to the application after the specifed timed interval expired. Note
- * the application can get woken up before the time comes if other application
- * calls WakeUpProcess().
- */
-
- void sleep(const int nticks)
- {
- EventRecord event;
- const int event_mask = 0; // Wait for null events only
- WaitNextEvent(event_mask,&event,nticks,nil);
- }
-
- /*
- *------------------------------------------------------------------------
- * Initialize the Macintosh
- * Initialize all the managers & memory
- */
-
- void Initialize_MAC(void)
- {
- MaxApplZone();
-
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- extern void (*_new_handler)(void);
- _new_handler = my_new_failed_handler;
- }
-
-
-