home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / xlisp / xlisp12.ark / XLSTR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-20  |  4.6 KB  |  212 lines

  1. /* xlstr - xlisp string builtin functions */
  2.  
  3. #ifdef AZTEC
  4. #include "stdio.h"
  5. #else
  6. #include <stdio.h>
  7. #endif
  8.  
  9. #include "xlisp.h"
  10.  
  11. /* external variables */
  12. extern struct node *xlstack;
  13.  
  14. /* external procedures */
  15. extern char *strcat();
  16.  
  17. /* xstrlen - length of a string */
  18. struct node *xstrlen(args)
  19.   struct node *args;
  20. {
  21.     struct node *val;
  22.     int total;
  23.  
  24.     /* initialize */
  25.     total = 0;
  26.  
  27.     /* loop over args and total */
  28.     while (args != NULL)
  29.     total += strlen(xlmatch(STR,&args)->n_str);
  30.  
  31.     /* create the value node */
  32.     val = newnode(INT);
  33.     val->n_int = total;
  34.  
  35.     /* return the total */
  36.     return (val);
  37. }
  38.  
  39. /* xstrcat - concatenate a bunch of strings */
  40. struct node *xstrcat(args)
  41.   struct node *args;
  42. {
  43.     struct node *oldstk,val,*p;
  44.     char *str;
  45.     int len;
  46.  
  47.     /* create a new stack frame */
  48.     oldstk = xlsave(&val,NULL);
  49.  
  50.     /* find the length of the new string */
  51.     for (p = args, len = 0; p; )
  52.     len += strlen(xlmatch(STR,&p)->n_str);
  53.  
  54.     /* create the result string */
  55.     val.n_ptr = newnode(STR);
  56.     val.n_ptr->n_str = str = stralloc(len);
  57.     *str = 0;
  58.  
  59.     /* combine the strings */
  60.     while (args)
  61.     strcat(str,xlmatch(STR,&args)->n_str);
  62.  
  63.     /* restore the previous stack frame */
  64.     xlstack = oldstk;
  65.  
  66.     /* return the new string */
  67.     return (val.n_ptr);
  68. }
  69.  
  70. /* xsubstr - return a substring */
  71. struct node *xsubstr(args)
  72.   struct node *args;
  73. {
  74.     struct node *oldstk,arg,src,val;
  75.     int start,forlen,srclen;
  76.     char *srcptr,*dstptr;
  77.  
  78.     /* create a new stack frame */
  79.     oldstk = xlsave(&arg,&src,&val,NULL);
  80.  
  81.     /* initialize */
  82.     arg.n_ptr = args;
  83.     
  84.     /* get string and its length */
  85.     src.n_ptr = xlmatch(STR,&arg.n_ptr);
  86.     srcptr = src.n_ptr->n_str;
  87.     srclen = strlen(srcptr);
  88.  
  89.     /* get starting pos -- must be present */
  90.     start = xlmatch(INT,&arg.n_ptr)->n_int;
  91.  
  92.     /* get length -- if not present use remainder of string */
  93.     if (arg.n_ptr != NULL)
  94.     forlen = xlmatch(INT,&arg.n_ptr)->n_int;
  95.     else
  96.     forlen = srclen;        /* use len and fix below */
  97.  
  98.     /* make sure there aren't any more arguments */
  99.     xllastarg(arg.n_ptr);
  100.  
  101.     /* don't take more than exists */
  102.     if (start + forlen > srclen)
  103.     forlen = srclen - start + 1;
  104.  
  105.     /* if start beyond string -- return null string */
  106.     if (start > srclen) {
  107.     start = 1;
  108.     forlen = 0; }
  109.     
  110.     /* create return node */
  111.     val.n_ptr = newnode(STR);
  112.     val.n_ptr->n_str = dstptr = stralloc(forlen);
  113.  
  114.     /* move string */
  115.     for (srcptr += start-1; forlen--; *dstptr++ = *srcptr++)
  116.     ;
  117.     *dstptr = 0;
  118.  
  119.     /* restore the previous stack frame */
  120.     xlstack = oldstk;
  121.  
  122.     /* return the substring */
  123.     return (val.n_ptr);
  124. }
  125.  
  126. /* xascii - return ascii value */
  127. struct node *xascii(args)
  128.   struct node *args;
  129. {
  130.     struct node *val;
  131.  
  132.     /* build return node */
  133.     val = newnode(INT);
  134.     val->n_int = *(xlmatch(STR,&args)->n_str);
  135.  
  136.     /* make sure there aren't any more arguments */
  137.     xllastarg(args);
  138.  
  139.     /* return the character */
  140.     return (val);
  141. }
  142.  
  143. /* xchr - convert an INT into a one character ascii string */
  144. struct node *xchr(args)
  145.   struct node *args;
  146. {
  147.     struct node *oldstk,val;
  148.     char *sptr;
  149.  
  150.     /* create a new stack frame */
  151.     oldstk = xlsave(&val,NULL);
  152.  
  153.     /* build return node */
  154.     val.n_ptr = newnode(STR);
  155.     val.n_ptr->n_str = sptr = stralloc(1);
  156.     *sptr++ = xlmatch(INT,&args)->n_int;
  157.     *sptr = 0;
  158.  
  159.     /* make sure there aren't any more arguments */
  160.     xllastarg(args);
  161.  
  162.     /* restore the previous stack frame */
  163.     xlstack = oldstk;
  164.  
  165.     /* return the new string */
  166.     return (val.n_ptr);
  167. }
  168.  
  169. /* xatoi - convert an ascii string to an integer */
  170. struct node *xatoi(args)
  171.   struct node *args;
  172. {
  173.     struct node *val;
  174.     int n;
  175.  
  176.     /* get the string and convert it */
  177.     n = atoi(xlmatch(STR,&args)->n_str);
  178.  
  179.     /* make sure there aren't any more arguments */
  180.     xllastarg(args);
  181.  
  182.     /* create the value node */
  183.     val = newnode(INT);
  184.     val->n_int = n;
  185.  
  186.     /* return the number */
  187.     return (val);
  188. }
  189.  
  190. /* xitoa - convert an integer to an ascii string */
  191. struct node *xitoa(args)
  192.   struct node *args;
  193. {
  194.     struct node *val;
  195.     char buf[20];
  196.     int n;
  197.  
  198.     /* get the integer */
  199.     n = xlmatch(INT,&args)->n_int;
  200.     xllastarg(args);
  201.  
  202.     /* convert it to ascii */
  203.     sprintf(buf,"%d",n);
  204.  
  205.     /* create the value node */
  206.     val = newnode(STR);
  207.     val->n_str = strsave(buf);
  208.  
  209.     /* return the string */
  210.     return (val);
  211. }
  212.