home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 21 / macformat_21.iso / Shareware / Programación / VideoToolbox / VideoToolboxSources / PrintfExit.c < prev    next >
Text File  |  1995-09-27  |  3KB  |  90 lines

  1. /* 
  2. PrintfExit.c
  3.  
  4. PrintfExit(const char *format,...);
  5. is normally equivalent to calling printf and exit.
  6.  
  7. Lots of VideoToolbox routines, when they find a really grievous error, print out
  8. a message and exit. Having one routine that does both makes the code slightly
  9. neater, since the if statement then doesn't need braces. Furthermore, both of
  10. these functions, printf, and exit are liable to break in foreign environments,
  11. e.g. when running as a MEX resource under MATLAB. Now the problem is confined to
  12. this file, where it can be handled by conditional compilation.
  13.  
  14. Since PrintfExit is called only when we're near death, it seems prudent to make
  15. sure there's enough stack space before calling printf, which crashes if there's
  16. less than about 4500 byts of stack. Note that StackGrow() moves memory.
  17.  
  18. I've replaced all calls to exit() in the entire VideoToolbox by calls to
  19. PrintfExit().
  20.  
  21. StackGrow() is defined in VideoToolbox.h.
  22.  
  23. HISTORY:
  24. 2/20/93    dgp    Wrote it based on conversation with David Brainard.
  25. 7/9/93    dgp    Test MATLAB in #if instead of #ifdef.
  26. 9/12/93    dgp    Moved Required() to Require.c.
  27. 9/15/93    dgp    Added "const" to prototype.
  28. 6/7/95    dhb, gmb SIOUX stuff conditional on not MATLAB
  29. 9/27/95 dgp SIOUX stuff conditional on MAC_C
  30. */
  31. #include "VideoToolbox.h"
  32. #include <stdarg.h>            /* for variable-number-of-argument macros */
  33.  
  34. int PrintfExit(const char *format,...)
  35. {
  36.     va_list args;
  37.     int i;
  38.     long qD=0;
  39.   
  40.     #if MAC_C
  41.         #if __MWERKS__ && !MATLAB
  42.             SIOUXSettings.autocloseonquit=0;
  43.         #endif
  44.         /*
  45.         The main program may have changed the current device. Let's
  46.         restore the main device before doing the printf. Most Standard C libraries
  47.         on the Mac crash if you call printf and the current device is not the main
  48.         device. I've suggested to both Symantec and Metrowerks that their stdout console code
  49.         could easily take the precaution of ensuring that the MainDevice is the current device 
  50.         (to set & restore, if necessary, or at least not crash), 
  51.         but, last I checked, they took no heed.
  52.         */
  53.         Gestalt(gestaltQuickdrawVersion,&qD);
  54.         if(qD>=gestalt8BitQD)SetGDevice(GetMainDevice());
  55.         //if(StackSpace()<6000)StackGrow(6000-StackSpace());
  56.     #endif
  57.     #if !MATLAB
  58.         #if MAC_C
  59.             /* printf crashes if there's less than about 4500 bytes of stack space */
  60.             if(StackSpace()<5000){
  61.                 SysBeep(20);
  62.                 exit(EXIT_FAILURE);
  63.             }
  64.         #endif
  65.         va_start(args,format);
  66.         i=vfprintf(stdout,format,args);
  67.         va_end(args);
  68.         #if __MWERKS__
  69.             printf("Hit Command-Q to quit.\n");
  70.         #endif
  71.         exit(EXIT_FAILURE);
  72.     #else
  73.     {
  74.         char s[256];
  75.         /*
  76.             "s" remains valid in mex_error(s) 'cause we get there by making
  77.             a subroutine call, not a return, so the stack space used for s
  78.             remains allocated.
  79.         */
  80.         
  81.         va_start(args,format);
  82.         i=vsprintf(s,format,args);
  83.         va_end(args);
  84.         mex_error(s);    /* Ask MATLAB to report the error. */
  85.     }
  86.     #endif
  87.     return 0;            /* can't get here */
  88. }
  89.  
  90.