home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / luschsrc.sit / error.c < prev    next >
Text File  |  1990-05-23  |  4KB  |  169 lines

  1. /********************************************************************************
  2.  *    error.c
  3.  *
  4.  *    Error Handling Package
  5.  *
  6.  *    Written by Paco Xander Nathan
  7.  *    ⌐1990, Motorola Inc.  Public domain source code.
  8.  ********************************************************************************/
  9.  
  10. #include "applic.h"
  11. #include "dialog.h"
  12. #include "error.h"
  13. #include "string.h"
  14. #include <MemoryMgr.h>
  15.  
  16.  
  17. /* External Data Structures
  18.  */
  19. Handle
  20.     errAvatarHdl = NULL;
  21.  
  22.  
  23. /* Present a warning error dialog to the user...  Can ask the user whether or not
  24.  * to exit gracefully and then call ApplQuit.
  25.  */
  26. short
  27. ErrWarning (nButtons, isFatal, p1, p2, p3, p4)
  28.     register short nButtons;
  29.     register Boolean isFatal;
  30.     register StringPtr p1, p2, p3, p4;
  31. {
  32.     register short result = 0;
  33.  
  34.     /* Stuff the specified strings into the alert and force the cursor to be an arrow
  35.      */
  36.     ParamText(p1, p2, p3, p4);
  37.     InitCursor();
  38.  
  39.     /* Call the appropriate alert template, based on nButtons
  40.      */
  41.     switch (nButtons) {
  42.     case warnYes:
  43.         result = Alert(continueAlertID, (Ptr) DlogModalEvent);
  44.         break;
  45.  
  46.     case warnNo:
  47.         result = Alert(yesnoAlertID, (Ptr) DlogModalEvent);
  48.         break;
  49.  
  50.     case warnCancel:
  51.         result = Alert(cancelAlertID, (Ptr) DlogModalEvent);
  52.         break;
  53.  
  54.     default:
  55.         break;
  56.     }
  57.  
  58.     /* If we need to die, go mercifully...  Otherwise return the user's choice
  59.      */
  60.     if (isFatal && (result == warnYes))
  61.         ApplQuit();
  62.  
  63.     return result;
  64. }
  65.  
  66.  
  67. /* In case our application cannot get enough memory, please squeek about it.
  68.  */
  69. pascal long
  70. ErrGrowZone (needed)
  71.     register long needed;
  72. {
  73.     register Size theSize = 0L;
  74.     static Str255 param1, param2, param3;
  75.  
  76.     /* Going to first get rid of the avatar handle...  Invoke the 
  77.      * goddess of the MemoryMgr to intervene on behalf of this 
  78.      * application
  79.      */
  80.     if (errAvatarHdl) {
  81.         theSize = GetHandleSize(errAvatarHdl);
  82.         DisposHandle(errAvatarHdl);
  83.         errAvatarHdl = NULL;
  84.     }
  85.  
  86.     /* Pull in the needed strings
  87.      */
  88.     GetIndString(param1, strsConverse, strApplName);
  89.     GetIndString(param2, strsConverse, strNoMem);
  90.     GetIndString(param3, strsConverse, strExitGrace);
  91.     
  92.     /* Now, ask whether the user wishes to die mercifully or by tempting
  93.      * Fate and continuing with a mortally wounded application.  If the
  94.      * has industrial strength computing balls and continues, then we
  95.      * don't need to be careful anymore, so free the locked last resort
  96.      * warning alert DITL.
  97.      */
  98.     (void) ErrWarning(warnNo, TRUE, param1, param2, param3, NullStr);
  99.     FreeAlert(yesnoAlertID);
  100.     
  101.     return theSize;
  102. }
  103.  
  104.  
  105. /* Attempt to allocate memory, error check the result and notify the user in the
  106.  * event of a failure, otherwise return a pointer to the requested memory block.
  107.  */
  108. Ptr
  109. ErrNewPtr (theSize)
  110.     register Size theSize;
  111. {
  112.     register Ptr result = NULL;
  113.     Str255 param1, param2, param3;
  114.     
  115.     if (!(result = NewPtr(theSize))) {
  116.         /* Pull in the needed strings
  117.          */
  118.         GetIndString(param1, strsConverse, strMemFail);
  119.         GetIndString(param2, strsConverse, strDontPanic);
  120.         GetIndString(param3, strsConverse, strExitGrace);
  121.         
  122.         (void) ErrWarning(warnNo, TRUE, param1, param2, param3, NullStr);
  123.     }
  124.  
  125.     return result;
  126. }
  127.  
  128.  
  129. /* Attempt to allocate a new handle, error check the result and notify the user in
  130.  * the event of a failure, otherwise return a handle to the requested memory block.
  131.  */
  132. Handle
  133. ErrNewHandle (theSize)
  134.     register Size theSize;
  135. {
  136.     register Handle result = NULL;
  137.     Str255 param1, param2, param3;
  138.     
  139.     if (!(result = NewHandle(theSize)) || !(*result)) {
  140.         /* Pull in the needed strings
  141.          */
  142.         GetIndString(param1, strsConverse, strMemFail);
  143.         GetIndString(param2, strsConverse, strDontPanic);
  144.         GetIndString(param3, strsConverse, strExitGrace);
  145.         
  146.         (void) ErrWarning(warnNo, TRUE, param1, param2, param3, NullStr);
  147.     }
  148.  
  149.     return result;
  150. }
  151.  
  152.  
  153. /* Signal a file error to the user.
  154.  */
  155. void
  156. ErrFileMgr (errNum, fileName)
  157.     register OSErr errNum;
  158.     register StringPtr fileName;
  159. {
  160.     Str255 param1, errStr;
  161.     
  162.     /* Pull in the needed strings
  163.      */
  164.     GetIndString(param1, strsConverse, strFileErr);
  165.     NumToString(errNum, errStr);
  166.  
  167.     (void) ErrWarning(warnYes, FALSE, param1, fileName, errStr, NullStr);
  168. }
  169.