home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / ParamRAM guard / myenv.cc next >
Encoding:
C/C++ Source or Header  |  1993-10-03  |  6.0 KB  |  218 lines  |  [TEXT/KAHL]

  1. /*
  2.  ************************************************************************
  3.  *            Service C++ functions 
  4.  *         that support the standard environment for me
  5.  */
  6.  
  7. #pragma implementation
  8.  
  9. #include "myenv.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <Sound.h>
  15.  
  16. /*
  17.  *-----------------------------------------------------------------------
  18.  *        Some global constant pertaining to input/output
  19.  */
  20.  
  21. const char _Minuses [] = "\
  22. -------------------------------------------------------------------------------";
  23.  
  24. const char _Asteriscs [] = "\
  25. *******************************************************************************";
  26.  
  27. const char _Equals [] = "\
  28. ===============================================================================";
  29.  
  30.  
  31. static const int System_Alert_rsc = -16411;            // ID of the System ALRT 
  32.                                                     // resource (in the System file)
  33. /*
  34.  *------------------------------------------------------------------------
  35.  *            Print an error message at stderr and abort
  36.  * Synopsis
  37.  *    volatile void _error(const char * message,... );
  38.  *    Message may contain format control sequences %x. Items to print 
  39.  *    with the control sequences are to be passed as additional arguments to
  40.  *    the function call.
  41.  */
  42.  
  43. static volatile void (*abort_function)(void) = (volatile void (*)())abort;
  44.  
  45. volatile void _error(const char * message,...)
  46. {
  47.   va_list args;
  48.   char buffer[1000];
  49.   va_start(args,message);            // Init 'args' to the beginning of
  50.                                     // the variable length list of args
  51.   vsprintf(buffer,message,args);
  52.   strncat(buffer,"\rSorry man, enough is enough, I'm outta here",sizeof(buffer)-strlen(buffer)-1);
  53.   CtoPstr(buffer);                            // Convert to Pascal-like string
  54.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  55.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  56.                                               // set just before
  57.   abort_function();                            // And crash it, at last
  58. }
  59.  
  60. void set_abort_function(volatile void (*custom_abort)(void))
  61. {
  62.     if( custom_abort == nil )
  63.       abort_function = (volatile void (*)())abort;
  64.     else
  65.       abort_function = custom_abort;
  66. }
  67.  
  68.                                     // This guy handles the situation when the memory
  69.                                     // allocation through 'new' has failed
  70. void my_new_failed_handler(void)
  71. {
  72.     _error("Flat out of memory");
  73. }
  74.  
  75. /*
  76.  *------------------------------------------------------------------------
  77.  *                    Print a message at stderr
  78.  * Synopsis
  79.  *    void message(const char * text,... );
  80.  *    Message may contain format control sequences %x. Items to print 
  81.  *    with the control sequences are to be passed as additional arguments to
  82.  *    the function call.
  83.  */
  84.  
  85. void message(const char * text,...)
  86. {
  87.   va_list args;
  88.   char buffer[1000];
  89.   va_start(args,text);                        // Init 'args' to the beginning of
  90.                                             // the variable length list of args
  91.   vsprintf(buffer,text,args);
  92.   CtoPstr(buffer);                            // Convert to Pascal-like string
  93.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  94.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  95. }
  96.  
  97. /*
  98.  *------------------------------------------------------------------------
  99.  *                            Some more advanced error handling
  100.  */
  101.  
  102. ErrHandler::ErrHandler(void)
  103. {
  104.   raise_hell = TRUE;
  105.   was_error = FALSE;
  106. }
  107.  
  108.                         // Be lenient in case of error
  109. void ErrHandler::lenient(void)
  110. {
  111.   if( was_error )
  112.     _error("You've got an error you haven't handled!");
  113.   raise_hell = FALSE;
  114. }
  115.  
  116.                         // Have we failed?
  117. Boolean ErrHandler::was_ok(void)
  118. {
  119.   raise_hell = TRUE;
  120.   if( was_error )
  121.   {
  122.     was_error = FALSE;
  123.     return FALSE;
  124.   }
  125.   return TRUE;
  126. }
  127.  
  128.                         // Notify the user about the error and raise the hell if needed
  129. void ErrHandler::error(const char * message,...)
  130. {
  131.   va_list args;
  132.   char buffer[1000];
  133.   va_start(args,message);            // Init 'args' to the beginning of
  134.                                     // the variable length list of args
  135.   vsprintf(buffer,message,args);
  136.   if( raise_hell )
  137.       strncat(buffer,"\rRaising the hell - crashing",sizeof(buffer)-strlen(buffer)-1);
  138.   CtoPstr(buffer);                            // Convert to Pascal-like string
  139.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  140.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  141.                                               // set just before
  142.   if( raise_hell )
  143.     abort_function();                        // And crash it, at last
  144.   else
  145.   {
  146.     raise_hell = TRUE;
  147.     was_error = FALSE;
  148.   }                                            // And let it go for now
  149. }
  150.  
  151. //------------------------------------------------------------------------
  152. //                            Sound playing on errors
  153.  
  154.  
  155. static Handle error_sound_handle = nil;
  156.  
  157. static pascal void error_sound_player(short sound_no)
  158. {
  159.   if( error_sound_handle != nil )
  160.     SndPlay(nil,error_sound_handle,FALSE);
  161.   else
  162.     SysBeep(10);
  163.   error_sound_handle = nil;
  164. }
  165.  
  166. void set_error_sound(Str255 sound_name)
  167. {
  168.   error_sound_handle = GetNamedResource('snd ',sound_name);
  169.   if( error_sound_handle != nil )
  170.     ErrorSound((pascal void (*)())error_sound_player);
  171. }
  172.  
  173.  
  174. /*
  175.  *------------------------------------------------------------------------
  176.  *               Suspending the application for a specified period of time
  177.  *
  178.  * Unlike Delay(), the present function is generous in that in relinquishes the
  179.  * CPU control for a specified time and thus lets other (backgrounded) application
  180.  * run.
  181.  * The generous waiting is implemented as waiting for a null event which is to
  182.  * be sent to the application after the specifed timed interval expired. Note
  183.  * the application can get woken up before the time comes if other application
  184.  * calls WakeUpProcess().
  185.  */
  186.  
  187. void sleep(const int nticks)
  188. {
  189.     EventRecord event;
  190.     const int event_mask = 0;                // Wait for null events only
  191.     WaitNextEvent(event_mask,&event,nticks,nil);
  192. }
  193.  
  194. /*
  195.  *------------------------------------------------------------------------
  196.  *                            Initialize the Macintosh
  197.  *                 Initialize all the managers & memory
  198.  */
  199.  
  200. void Initialize_MAC(void)
  201. {
  202.     MaxApplZone();
  203.     
  204.     InitGraf(&thePort);
  205.     InitFonts();
  206.     FlushEvents(everyEvent, 0);
  207.     InitWindows();
  208.     InitMenus();
  209.     TEInit();
  210.     InitDialogs(0L);
  211.     InitCursor();
  212.     
  213.     extern void (*_new_handler)(void);
  214.     _new_handler = my_new_failed_handler;
  215. }
  216.  
  217.  
  218.