home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / mac / macerrs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-25  |  4.2 KB  |  171 lines

  1. /*    SCCS Id: @(#)macerrs.c    3.1    93/01/24          */
  2. /* Copyright (c) Michael Hamel, 1991 */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #ifdef applec    /* This needs to be resident always */
  6. #pragma segment Main
  7. #endif
  8.  
  9. #include "hack.h"
  10.  
  11. #include <OSUtils.h>
  12. #include <files.h>
  13. #include <Types.h>
  14. #ifdef MAC_MPW32
  15. #include <String.h>
  16. #include <Strings.h>
  17. #endif
  18. #ifdef MAC_THINKC5
  19. #include <pascal.h>
  20. #endif
  21. #include <Dialogs.h>
  22. #include <Packages.h>
  23. #include <ToolUtils.h>
  24. #include <Resources.h>
  25.  
  26. #define stackDepth  4
  27. #define errAlertID 129
  28. #define stdIOErrID 1999
  29.  
  30. void FDECL(comment,(char *, long));
  31. void FDECL(showerror,(char *, const char *));
  32. Boolean FDECL(itworked,(short));
  33. void FDECL(mustwork,(short));
  34. static void VDECL(vprogerror,(const char *line, va_list the_args));
  35. void FDECL(attemptingto,( char * activity ));
  36. void FDECL(pushattemptingto,( char * activity ));
  37. void NDECL(popattempt);
  38.  
  39. static Str255 gActivities[stackDepth] = {"","","",""};
  40. static short gTopactivity = 1;
  41.  
  42. void  comment( char *s, long n )
  43. {
  44.   Str255 paserr;
  45.   short itemHit;
  46.   
  47.   sprintf((char *)paserr, "%s - %d",s,n);
  48.   ParamText(c2pstr((char *)paserr),(StringPtr)"",(StringPtr)"",(StringPtr)"");
  49.   itemHit = Alert(128, (ModalFilterProcPtr)nil);
  50. }
  51.  
  52. void showerror( char * errdesc, const char * errcomment )
  53. {
  54.        short        itemHit;
  55.     Str255        paserr,
  56.                 pascomment;
  57.                 
  58.     SetCursor(&qd.arrow);
  59.     if (errcomment == nil)  pascomment[0] = '\0';
  60.       else strcpy((char *)pascomment,(char *)errcomment);
  61.     strcpy((char *)paserr,(char *)errdesc);
  62.     ParamText(c2pstr((char *)paserr),c2pstr((char *)pascomment),gActivities[gTopactivity],(StringPtr)"");
  63.     itemHit = Alert(errAlertID, (ModalFilterProcPtr)nil);
  64. }
  65.  
  66.  
  67. Boolean itworked( short errcode )
  68. /* Return TRUE if it worked, do an error message and return false if it didn't. Error
  69.    strings for native C errors are in STR#1999, Mac errs in STR 2000-errcode, e.g
  70.    2108 for not enough memory */
  71.  
  72. {
  73.   if (errcode != 0) {
  74.     short         itemHit;
  75.     Str255          errdesc;
  76.     StringHandle strh;
  77.     Ptr             junk;
  78.     
  79.     errdesc[0] = '\0';
  80.     if (errcode > 0) GetIndString(errdesc,stdIOErrID,errcode);  /* STDIO file rres, etc */
  81.     else {
  82.        strh = GetString(2000-errcode);
  83.        if (strh != (StringHandle) nil) {
  84.           memcpy(errdesc,*strh,256);
  85.           ReleaseResource((Handle)strh);
  86.        }
  87.     }
  88.     if (errdesc[0] == '\0') {  /* No description found, just give the number */
  89.        sprintf((char *)errdesc,"a %d error occurred",errcode);
  90.        (void)c2pstr((char *)errdesc);
  91.     }
  92.     SetCursor(&qd.arrow);
  93.     ParamText(errdesc,(StringPtr)"",gActivities[gTopactivity],(StringPtr)"");
  94.     itemHit = Alert(errAlertID, (ModalFilterProcPtr)nil);
  95.  
  96.   }
  97.   return(errcode==0);
  98. }
  99.  
  100. void mustwork( short errcode )
  101. /* For cases where we can't recover from the error by any means */
  102. {
  103.   if (itworked(errcode)) ;
  104.        else ExitToShell();
  105. }
  106.  
  107.  
  108. #if defined(USE_STDARG) || defined(USE_VARARGS)
  109. # ifdef USE_STDARG
  110. static void vprogerror(const char *line, va_list the_args);
  111. # else
  112. static void vprogerror();
  113. # endif
  114.  
  115. /* Macro substitute for error() */
  116. void progerror VA_DECL(const char *, line)
  117.     VA_START(line);
  118.     VA_INIT(line, char *);
  119.     vprogerror(line, VA_ARGS);
  120.     VA_END();
  121. }
  122.  
  123. # ifdef USE_STDARG
  124. static void
  125. vprogerror(const char *line, va_list the_args) {
  126. # else
  127. static void
  128. vprogerror(line, the_args) const char *line; va_list the_args; {
  129. # endif
  130.  
  131. #else  /* USE_STDARG | USE_VARARG */
  132.  
  133. void
  134. progerror VA_DECL(const char *, line)
  135. #endif
  136. /* Do NOT use VA_START and VA_END in here... see above */
  137.  
  138.     if(!index(line, '%'))
  139.         showerror("of an internal error",line);
  140.     else {
  141.         char pbuf[BUFSZ];
  142.         Vsprintf(pbuf,line,VA_ARGS);
  143.         showerror("of an internal error",pbuf);
  144.     }
  145. }
  146.  
  147. void attemptingto( char * activity )
  148. /* Say what we are trying to do for subsequent error-handling: will appear as x in an
  149.    alert in the form "Could not x because y" */
  150. {
  151.    strcpy((char *)gActivities[gTopactivity],activity);
  152.    activity = (char *)c2pstr((char *)gActivities[gTopactivity]);
  153. }
  154.  
  155. void pushattemptingto( char * activity )
  156. /* Push a new description onto stack so we can pop later to previous state */
  157. {
  158.   if (gTopactivity < stackDepth) {
  159.     gTopactivity++;
  160.     attemptingto(activity);
  161.   }
  162.   else progerror("activity stack overflow");
  163. }
  164.  
  165. void popattempt( void )
  166. /* Pop to previous state */
  167. {
  168.   if (gTopactivity > 1) --gTopactivity;
  169.                    else progerror("activity stack underflow");
  170. }
  171.