home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
HyperCard
/
reg_exp.XFCN
/
XCmdUtil.c
< prev
next >
Wrap
Text File
|
1989-09-18
|
5KB
|
213 lines
/*
| | XCmdUtil.c:
| |
| | Utility routines for HyperCard XCMDs and XFCNs.
| |
| | Greg Anderson
| | 29 Kerr Hall
| | Social Sciences Computing
| | University of California at Santa Cruz
| | sirkm@ssyx.ucsc.edu
| |
*/
#include <MacTypes.h>
#include "HyperXCmd.h"
#include "XCmdUtil.h"
/*-----------------------------------------------------------------
| Convert a pascal string to a C string. Note that it is legal to
| call with two pointers to the same string if desired.
-----------------------------------------------------------------*/
char *Pas2C(cstr,pstr)
char *cstr,*pstr;
{
int len,i;
len = pstr[0];
for(i=0;i<len;++i)
cstr[i] = pstr[i+1];
cstr[len] = 0;
return(cstr);
}
/*-----------------------------------------------------------------
| Convert a C string to a pascal string.
-----------------------------------------------------------------*/
char *C2Pas(pstr,cstr)
char *pstr,*cstr;
{
int len,i;
len = strlen(cstr);
for(i=len;i;--i)
pstr[i] = cstr[i-1];
pstr[0] = len;
return(pstr);
}
/*-----------------------------------------------------------------
| Convert lower case characters in a string to upper case
-----------------------------------------------------------------*/
void MakeUpper(str)
char *str;
{
while(*str)
{
*str = toupper(*str);
++str;
}
}
/*-----------------------------------------------------------------
| Copy a string into a new handle
-----------------------------------------------------------------*/
Handle CopyStrToHand(string)
char *string;
{
Handle newHndl;
long hsize;
hsize = strlen(string) + 1;
newHndl = NewHandle( hsize );
strcpy( (char *)(*newHndl), string );
return(newHndl);
}
/*-----------------------------------------------------------------
| Return a message to Hypercard
-----------------------------------------------------------------*/
void ReturnMsg(paramPtr,Message)
XCmdBlockPtr paramPtr;
char *Message;
{
paramPtr->returnValue = CopyStrToHand(Message);
}
/*-----------------------------------------------------------------
| Convert an ascii string to an integer
-----------------------------------------------------------------*/
int atoi(str)
char *str;
{
int result = 0,neg=1;
char c;
if( *str == '-' )
{
++str;
neg = -1;
}
while( (c = *str) )
{
if( (c >= '0') && (c <= '9') )
result = (result*10) + c - '0';
++str;
}
return(result*neg);
}
/*-----------------------------------------------------------------
| Convert an integer to an ASCII string
-----------------------------------------------------------------*/
void itoa(str,var)
char *str;
int var;
{
int i,m = 1;
if(var < 0)
{
var = -var;
*(str++) = '-';
}
*str = 0;
do
{
for(i=m;i>0;--i)
str[i] = str[i-1];
str[0] = '0' + (var % 10);
++m;
var /= 10;
} while(var);
}
/*-----------------------------------------------------------------
| Convert a long to an ASCII string
-----------------------------------------------------------------*/
void ltoa(str,var)
char *str;
long var;
{
int i,m = 1;
if(var < 0)
{
var = -var;
*(str++) = '-';
}
*str = 0;
do
{
for(i=m;i>0;--i)
str[i] = str[i-1];
str[0] = '0' + (var % 10);
++m;
var /= 10L;
} while(var);
}
/*=================================================================
|| The following glue routines are similar to those given in
|| the file 'XCmdGlue.c; however, these routines take C strings
|| rather than pascal strings as arguements.
=================================================================*/
pascal void cSendHCMessage(paramPtr,msg)
XCmdBlockPtr paramPtr; char *msg;
/* Send a HyperCard message (a command with arguments) to HyperCard.
msg is a pointer to a C format string. */
{
Str255 Pstr;
C2Pas(Pstr,msg);
SendHCMessage(paramPtr,Pstr);
}
pascal void cSendCardMessage(paramPtr,msg)
XCmdBlockPtr paramPtr; char *msg;
/* Send a HyperCard message (a command with arguments) to the current card.
msg is a pointer to a C format string. */
{
Str255 Pstr;
C2Pas(Pstr,msg);
SendCardMessage(paramPtr,Pstr);
}
pascal Handle cEvalExpr(paramPtr,expr)
XCmdBlockPtr paramPtr; char *expr;
/* Evaluate a HyperCard expression and return the answer. The answer is
a handle to a zero-terminated string. */
{
Str255 Pstr;
C2Pas(Pstr,expr);
return(EvalExpr(paramPtr,Pstr));
}
pascal void cSetGlobal(paramPtr,globalVar,msg)
XCmdBlockPtr paramPtr;
char *globalVar,
*msg;
{
Handle tempHandle;
Str255 globName;
tempHandle = CopyStrToHand(msg);
C2Pas(globName,globalVar);
SetGlobal(paramPtr,globName,tempHandle);
DisposHandle(tempHandle);
}