home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
lisp
/
interpre
/
xlispdos
/
source
/
xlstr.c
< prev
next >
Wrap
Text File
|
1986-04-13
|
3KB
|
116 lines
/* xlstr - xlisp string builtin functions */
/* Copyright (c) 1985, by David Michael Betz
All Rights Reserved
Permission is granted for unrestricted non-commercial use */
#include "xlisp.h"
/* external variables */
extern char buf[];
/* external procedures */
extern char *strcat();
/* xstrcat - concatenate a bunch of strings */
NODE *xstrcat(args)
NODE *args;
{
NODE *val,*p;
char *str;
int len;
/* find the length of the new string */
for (p = args, len = 0; p; )
len += strlen(getstring(xlmatch(STR,&p)));
/* create the result string */
val = newstring(len);
str = getstring(val);
*str = 0;
/* combine the strings */
while (args)
strcat(str,getstring(xlmatch(STR,&args)));
/* return the new string */
return (val);
}
/* xsubstr - return a substring */
NODE *xsubstr(args)
NODE *args;
{
int start,forlen,srclen;
char *srcptr,*dstptr;
NODE *src,*val;
/* get string and its length */
src = xlmatch(STR,&args);
srcptr = getstring(src);
srclen = strlen(srcptr);
/* get starting pos -- must be present */
start = getfixnum(xlmatch(INT,&args));
/* get length -- if not present use remainder of string */
forlen = (args ? getfixnum(xlmatch(INT,&args)) : srclen);
/* make sure there aren't any more arguments */
xllastarg(args);
/* don't take more than exists */
if (start + forlen > srclen)
forlen = srclen - start + 1;
/* if start beyond string -- return null string */
if (start > srclen) {
start = 1;
forlen = 0;
}
/* create return node */
val = newstring(forlen);
dstptr = getstring(val);
/* move string */
for (srcptr += start-1; forlen--; *dstptr++ = *srcptr++)
;
*dstptr = 0;
/* return the substring */
return (val);
}
/* xstring - return a string consisting of a single character */
NODE *xstring(args)
NODE *args;
{
/* get the character (integer) */
buf[0] = getfixnum(xlmatch(INT,&args));
xllastarg(args);
/* make a one character string */
buf[1] = 0;
return (cvstring(buf));
}
/* xchar - extract a character from a string */
NODE *xchar(args)
NODE *args;
{
char *str;
int n;
/* get the string and the index */
str = getstring(xlmatch(STR,&args));
n = getfixnum(xlmatch(INT,&args));
xllastarg(args);
/* range check the index */
if (n < 0 || n >= strlen(str))
xlerror("index out of range",cvfixnum((FIXNUM)n));
/* return the character */
return (cvfixnum((FIXNUM)str[n]));
}