home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / prctclxf.sit / Sources / Error / Sources / ErrorString.c < prev    next >
C/C++ Source or Header  |  1990-08-27  |  4KB  |  148 lines

  1. /*
  2. Copyright ⌐ 1990 Ari I. Halberstadt
  3.  
  4. This file is part of a free program. For precise terms of distribution
  5. and use please see the files in the folder ╥Legal╙ accompanying this
  6. software. If you make any modifications to this file you must add the
  7. date, your name, and a description of what you have done to the
  8. revision history below. This notice and the copyright notice must
  9. remain intact in all copies of this file. The files in the folder
  10. ╥Legal╙ must be available to anyone receiving copies of this file.
  11.  
  12. Add initials and name here:
  13. Initials    Name
  14. AIH        Ari I. Halberstadt
  15.  
  16. Revision History (date format is mm-dd-yy):
  17. Date      Name  Comments
  18. 07-11-90     AIH   Version 0.9
  19. */
  20.  
  21. /*
  22.     This is the ErrorString XFCN. ErrorString takes an error code
  23.     (an integer), or an error name as returned by the ErrorName
  24.     XFCN, and converts it into a string describing the error.
  25.     The string can then be displayed in an alert box to notify
  26.     the user.
  27.     
  28.     A typical way to use ErrorString is:
  29.     
  30.         if theErrorCode is not empty then
  31.             answer "Error: " & ErrorString(theErrorCode)
  32.             exit to HyperCard
  33.         end if
  34.         
  35.     ErrorString accepts either an error code (an integer)
  36.     or an error name (a string), and converts them into
  37.     a descriptive string. For instance, the following lines
  38.     yield the same string (the actual strings returned may
  39.     vary slightly from one version to the next, but these
  40.     commands, if executed at the same time, will return the
  41.     same string):
  42.     
  43.         -- These lines return "Unknown error"
  44.         ErrorString(1)
  45.         ErrorString(ERR_BASIC_UNKNOWN)
  46.         ErrorString(ErrorName(1))
  47.         ErrorString(ErrorName(ERR_BASIC_UNKNOWN))
  48.     
  49.     ErrorString could easily be adapted to hold descriptions
  50.     of the Macintosh toolbox errors. The "MakeRsrcs"
  51.     project contains data files for the resources.
  52.     By modifying (or adding to) this data, you can
  53.     use ErrorString for any other program.
  54. */
  55.         
  56. #include <MyHeaders>
  57. #include <SetUpA4.h>
  58. #include "hyperlib.h"
  59. #include "strlib.h"
  60. #include "rsrclib.h"
  61.  
  62. /* Resource IDs are stored internally as unique integers.
  63.     The IDs are passed to a function that maps these internal
  64.     numbers to the IDs of the actual resources in the resource file. */
  65.  
  66. /* resources for the program */
  67. enum {
  68.     RSRC_INFO_ID,        /* info strings */
  69.     RSRC_STRINGS_ID,    /* table of error strings */
  70.     RSRC_NAMES_ID,        /* table of error names */
  71.     RSRC_COUNT            /* number of resources */
  72. };
  73.  
  74. /* the info strings */
  75. enum {
  76.     RSRC_VERSION_INDEX=1,    /* version string */
  77.     RSRC_USAGE_INDEX            /* usage string */
  78. };
  79.  
  80. static Boolean isnum(char *s);
  81. static void arg2str(char *arg, char *str, ResID *map);
  82. static ResID GetRsrcID(ResID *map, ResID internal_id);
  83.  
  84. pascal void main(XCmdBlockPtr param)
  85. {
  86.     CStr255 str;    /* generally useful string */
  87.     char *arg;        /* the argument to the program */
  88.     ResID map[RSRC_COUNT];/*map from internal resource IDs to actual resource IDs*/
  89.     OSErr err = noErr;
  90.     
  91.     RememberA0();
  92.     SetUpA4();
  93.     err = rsrc_build_named_map(ERRORSTRING_XFCN_NAME ":ResourceMap", map);
  94.     if (param->paramCount == 1) {
  95.         HLock(param->params[0]);
  96.         arg = *param->params[0];
  97.         switch (*arg) {
  98.         case '!':
  99.             GetVerString(str, GetRsrcID(map, RSRC_INFO_ID),
  100.                                         RSRC_VERSION_INDEX, __DATE__, __TIME__);
  101.             break;
  102.         case '?':
  103.             GetIndString(str, GetRsrcID(map, RSRC_INFO_ID),
  104.                                         RSRC_USAGE_INDEX);
  105.             PtoCstr(str);
  106.             break;
  107.         default:
  108.             arg2str(arg, str, map);
  109.         }
  110.         HUnlock(param->params[0]);
  111.         PtrToHand(str, ¶m->returnValue, strlen(str)+1);
  112.     }        
  113.     RestoreA4();
  114. }
  115.  
  116. /* convert argument into an error string */
  117. static void arg2str(char *arg, char *str, ResID *map)
  118. {
  119.     if (isnum(arg))
  120.         ErrNumToStr(str, GetRsrcID(map, RSRC_STRINGS_ID), atol(arg));
  121.     else {
  122.         int num = ErrStrToNum(arg, GetRsrcID(map, RSRC_NAMES_ID));
  123.         ErrNumToStr(str, GetRsrcID(map, RSRC_STRINGS_ID), num);
  124.     }
  125. }
  126.  
  127. /* true if string is a number */
  128. static Boolean isnum(char *s)
  129. {
  130.     while (_isspace(*s)) ++s;
  131.     return(_isdigit(*s) || *s == '-' || *s == '+');
  132. }
  133.  
  134. /* build a version string */
  135. static void GetVerString(char *str, ResID id, short strnum,
  136.                                     char *date, char *time)
  137. {
  138.     GetIndString(str, id, strnum);
  139.     PtoCstr(str);
  140.     strcat(strcat(strcat(strcat(str, " "), date), " "), time);
  141. }
  142.  
  143. /* return the actual ID of the resource with the given internal ID */
  144. static ResID GetRsrcID(ResID *map, ResID internal_id)
  145. {
  146.     return(map[internal_id]);
  147. }
  148.