home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
hypercrd
/
xcmd
/
prctclxf.sit
/
Sources
/
Error
/
Sources
/
ErrorString.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-27
|
4KB
|
148 lines
/*
Copyright ⌐ 1990 Ari I. Halberstadt
This file is part of a free program. For precise terms of distribution
and use please see the files in the folder ╥Legal╙ accompanying this
software. If you make any modifications to this file you must add the
date, your name, and a description of what you have done to the
revision history below. This notice and the copyright notice must
remain intact in all copies of this file. The files in the folder
╥Legal╙ must be available to anyone receiving copies of this file.
Add initials and name here:
Initials Name
AIH Ari I. Halberstadt
Revision History (date format is mm-dd-yy):
Date Name Comments
07-11-90 AIH Version 0.9
*/
/*
This is the ErrorString XFCN. ErrorString takes an error code
(an integer), or an error name as returned by the ErrorName
XFCN, and converts it into a string describing the error.
The string can then be displayed in an alert box to notify
the user.
A typical way to use ErrorString is:
if theErrorCode is not empty then
answer "Error: " & ErrorString(theErrorCode)
exit to HyperCard
end if
ErrorString accepts either an error code (an integer)
or an error name (a string), and converts them into
a descriptive string. For instance, the following lines
yield the same string (the actual strings returned may
vary slightly from one version to the next, but these
commands, if executed at the same time, will return the
same string):
-- These lines return "Unknown error"
ErrorString(1)
ErrorString(ERR_BASIC_UNKNOWN)
ErrorString(ErrorName(1))
ErrorString(ErrorName(ERR_BASIC_UNKNOWN))
ErrorString could easily be adapted to hold descriptions
of the Macintosh toolbox errors. The "MakeRsrcs"
project contains data files for the resources.
By modifying (or adding to) this data, you can
use ErrorString for any other program.
*/
#include <MyHeaders>
#include <SetUpA4.h>
#include "hyperlib.h"
#include "strlib.h"
#include "rsrclib.h"
/* Resource IDs are stored internally as unique integers.
The IDs are passed to a function that maps these internal
numbers to the IDs of the actual resources in the resource file. */
/* resources for the program */
enum {
RSRC_INFO_ID, /* info strings */
RSRC_STRINGS_ID, /* table of error strings */
RSRC_NAMES_ID, /* table of error names */
RSRC_COUNT /* number of resources */
};
/* the info strings */
enum {
RSRC_VERSION_INDEX=1, /* version string */
RSRC_USAGE_INDEX /* usage string */
};
static Boolean isnum(char *s);
static void arg2str(char *arg, char *str, ResID *map);
static ResID GetRsrcID(ResID *map, ResID internal_id);
pascal void main(XCmdBlockPtr param)
{
CStr255 str; /* generally useful string */
char *arg; /* the argument to the program */
ResID map[RSRC_COUNT];/*map from internal resource IDs to actual resource IDs*/
OSErr err = noErr;
RememberA0();
SetUpA4();
err = rsrc_build_named_map(ERRORSTRING_XFCN_NAME ":ResourceMap", map);
if (param->paramCount == 1) {
HLock(param->params[0]);
arg = *param->params[0];
switch (*arg) {
case '!':
GetVerString(str, GetRsrcID(map, RSRC_INFO_ID),
RSRC_VERSION_INDEX, __DATE__, __TIME__);
break;
case '?':
GetIndString(str, GetRsrcID(map, RSRC_INFO_ID),
RSRC_USAGE_INDEX);
PtoCstr(str);
break;
default:
arg2str(arg, str, map);
}
HUnlock(param->params[0]);
PtrToHand(str, ¶m->returnValue, strlen(str)+1);
}
RestoreA4();
}
/* convert argument into an error string */
static void arg2str(char *arg, char *str, ResID *map)
{
if (isnum(arg))
ErrNumToStr(str, GetRsrcID(map, RSRC_STRINGS_ID), atol(arg));
else {
int num = ErrStrToNum(arg, GetRsrcID(map, RSRC_NAMES_ID));
ErrNumToStr(str, GetRsrcID(map, RSRC_STRINGS_ID), num);
}
}
/* true if string is a number */
static Boolean isnum(char *s)
{
while (_isspace(*s)) ++s;
return(_isdigit(*s) || *s == '-' || *s == '+');
}
/* build a version string */
static void GetVerString(char *str, ResID id, short strnum,
char *date, char *time)
{
GetIndString(str, id, strnum);
PtoCstr(str);
strcat(strcat(strcat(strcat(str, " "), date), " "), time);
}
/* return the actual ID of the resource with the given internal ID */
static ResID GetRsrcID(ResID *map, ResID internal_id)
{
return(map[internal_id]);
}