ack/lang/basic/lib/string.c

185 lines
3.1 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <string.h>
#include "bc_string.h"
1984-11-29 14:22:02 +00:00
1994-06-24 11:31:16 +00:00
/* $Id$ */
1984-11-29 14:22:02 +00:00
#define ok(X) if( X ==0) return;
#define okr(X) if( X ==0) return(0);
extern char *salloc() ;
_length(str)
1984-11-29 14:22:02 +00:00
String *str;
{
okr(str);
return(str->strlength);
}
String *_newstr(str)
char *str;
{
String *s;
okr(str);
s= (String *) salloc(sizeof(String));
s->strcount=1;
s->strlength= strlen(str);
s->strval= salloc(s->strlength+1);
1984-11-29 14:22:02 +00:00
strcpy(s->strval,str);
return(s);
}
_incstr(src)
String *src;
{
/* one more variable uses the string */
ok(src);
src->strcount++;
}
_decstr(str)
String *str;
{
ok(str);
/* Strings in ROM are initialized with this count */
if ( str->strcount==9999 ) return ;
1984-11-29 14:22:02 +00:00
str->strcount--;
if(str->strcount<=0) _delstr(str);
}
_strcpy(dst,src)
String *src,*dst;
{
ok(src);
ok(dst);
_decstr(dst);
*dst = *src;
_incstr(src);
}
_delstr(src)
String *src;
{
ok(src);
sfree(src->strval);
sfree((char *)src);
1984-11-29 14:22:02 +00:00
}
String *_concat(s1,s2)
String *s1,*s2;
{
String *s;
int length;
okr(s1); okr(s2);
s= (String *) salloc(sizeof(String));
s->strlength= _length(s1)+_length(s2);
1988-07-01 17:30:06 +00:00
s->strval= salloc(s->strlength+1);
s->strcount = 1;
1984-11-29 14:22:02 +00:00
strcpy(s->strval,s2->strval);
strcat(s->strval,s1->strval);
return(s);
}
_strcomp(s1,s2)
1984-11-29 14:22:02 +00:00
String *s1,*s2;
{
okr(s1);okr(s2);
return(strcmp(s2->strval,s1->strval));
}
String *_left(size,s)
String *s;
int size;
{
String *ns;
int i;
okr(s);
if( size <0 || size >s->strlength) error(3);
ns= (String *) salloc(sizeof(String));
ns->strval= salloc(size+1);
1984-11-29 14:22:02 +00:00
ns->strcount=1;
for(i=0; i<size && s->strval[i];i++)
ns->strval[i]= s->strval[i];
ns->strval[i]=0;
ns->strlength= i;
return(ns);
}
String *_space(d)
int d;
{
String *s;
int i,len;
len= d;
s= (String *) salloc(sizeof(String));
s->strlength= len;
s->strcount=1;
s->strval= salloc(len+1);
1984-11-29 14:22:02 +00:00
for(i=0;i<len;i++)
s->strval[i]= ' ';
s->strval[i]=0;
return(s);
}
String *_strascii()
{
}
1988-07-01 17:30:06 +00:00
String *_string(f, d)
1984-11-29 14:22:02 +00:00
double d,f;
{
int i,j;
String *s;
i=d;j=f;
if( i<0 || i>MAXSTRING) error(3);
s= (String *) salloc(sizeof(String));
s->strlength= i;
s->strcount=1;
s->strval= salloc(i+1);
1988-07-04 11:44:03 +00:00
s->strval[i--]=0;
1984-11-29 14:22:02 +00:00
for(; i>=0;i--)
s->strval[i]= j;
return(s);
}
_midstmt(s2,i1,i2,s)
int i1,i2;
String *s, *s2;
{
int l;
1988-07-01 17:30:06 +00:00
/*printf("mid called %d %d %s %s\n",i1,i2,s->strval, s2->strval);*/
if (i2 < 0 || i1 < -1) error(3);
1988-07-04 11:44:03 +00:00
if( s->strlength<i2 || s2->strlength < i1) error(3); /* source string too short */
1984-11-29 14:22:02 +00:00
if( i1== -1) i1= s2->strlength;
l= s->strlength - i2+1;
if( i1>l ) i1=l;
strncpy(s->strval+i2-1,s2->strval,i1);
}
String *_mid(i1,i2,s)
int i1,i2;
String *s;
{
int l;
String *s2;
/* printf("mid fcn called %d %d %s\n",i1,i2,s->strval);*/
1988-07-01 17:30:06 +00:00
if (i2 < 0 || i1 < -1) return(s2); /* or error? */
1984-11-29 14:22:02 +00:00
if( i1 == -1) i1= s->strlength;
s2= _newstr(s->strval);
s2->strval[0]=0;
if( s->strlength<i2) return(s2); /* source string too short */
l= s->strlength - i2+1;
if( i1>l ) i1=l;
strncpy(s2->strval,s->strval+i2-1,i1);
s2->strval[i1]=0;
return(s2);
}
String *_right(length,str)
String *str;
int length;
{
String *s;
int i;
i= _length(str)-length;
1984-11-29 14:22:02 +00:00
if(i<0) i=0;
s= _newstr(str->strval+i);
return(s);
}