ack/lang/basic/lib/string.c

183 lines
3.2 KiB
C
Raw Permalink Normal View History

#include <stdlib.h>
#include <string.h>
#include "lib.h"
1984-11-29 14:22:02 +00:00
#define ok(X) \
if (X == 0) \
return;
#define okr(X) \
if (X == 0) \
return (0);
1984-11-29 14:22:02 +00:00
2016-12-12 20:15:25 +00:00
int _length(String* str)
1984-11-29 14:22:02 +00:00
{
okr(str);
return (str->strlength);
1984-11-29 14:22:02 +00:00
}
String* _newstr(char* str)
1984-11-29 14:22:02 +00:00
{
String* s;
1984-11-29 14:22:02 +00:00
okr(str);
s = (String*)salloc(sizeof(String));
s->strcount = 1;
s->strlength = strlen(str);
s->strval = salloc(s->strlength + 1);
strcpy(s->strval, str);
return (s);
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _incstr(String* src)
1984-11-29 14:22:02 +00:00
{
/* one more variable uses the string */
ok(src);
src->strcount++;
}
2016-12-12 20:15:25 +00:00
void _decstr(String* str)
1984-11-29 14:22:02 +00:00
{
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);
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _strcpy(String* dst, String* src)
1984-11-29 14:22:02 +00:00
{
ok(src);
ok(dst);
_decstr(dst);
*dst = *src;
_incstr(src);
}
2016-12-12 20:15:25 +00:00
void _delstr(String* src)
1984-11-29 14:22:02 +00:00
{
ok(src);
sfree(src->strval);
sfree((char*)src);
1984-11-29 14:22:02 +00:00
}
String* _concat(String* s1, String* s2)
1984-11-29 14:22:02 +00:00
{
String* s;
1984-11-29 14:22:02 +00:00
int length;
okr(s1);
okr(s2);
s = (String*)salloc(sizeof(String));
s->strlength = _length(s1) + _length(s2);
s->strval = salloc(s->strlength + 1);
1988-07-01 17:30:06 +00:00
s->strcount = 1;
strcpy(s->strval, s2->strval);
strcat(s->strval, s1->strval);
return (s);
1984-11-29 14:22:02 +00:00
}
int _strcomp(String* s1, String* s2)
1984-11-29 14:22:02 +00:00
{
okr(s1);
okr(s2);
return (strcmp(s2->strval, s1->strval));
1984-11-29 14:22:02 +00:00
}
String* _left(int size, String* s)
1984-11-29 14:22:02 +00:00
{
String* ns;
1984-11-29 14:22:02 +00:00
int i;
okr(s);
if (size < 0 || size > s->strlength)
error(3);
ns = (String*)salloc(sizeof(String));
ns->strval = salloc(size + 1);
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);
1984-11-29 14:22:02 +00:00
}
String* _space(int d)
1984-11-29 14:22:02 +00:00
{
String* s;
int i, len;
1984-11-29 14:22:02 +00:00
len = d;
s = (String*)salloc(sizeof(String));
s->strlength = len;
s->strcount = 1;
s->strval = salloc(len + 1);
for (i = 0; i < len; i++)
s->strval[i] = ' ';
s->strval[i] = 0;
return (s);
1984-11-29 14:22:02 +00:00
}
String* _strascii(void)
1984-11-29 14:22:02 +00:00
{
}
String* _string(double f, double d)
1984-11-29 14:22:02 +00:00
{
int i, j;
String* s;
1984-11-29 14:22:02 +00:00
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);
s->strval[i--] = 0;
for (; i >= 0; i--)
s->strval[i] = j;
return (s);
1984-11-29 14:22:02 +00:00
}
2016-12-12 20:15:25 +00:00
void _midstmt(String* s2, int i1, int i2, String* s)
1984-11-29 14:22:02 +00:00
{
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);
if (s->strlength < i2 || s2->strlength < i1)
error(3); /* source string too short */
if (i1 == -1)
i1 = s2->strlength;
l = s->strlength - i2 + 1;
if (i1 > l)
i1 = l;
strncpy(s->strval + i2 - 1, s2->strval, i1);
1984-11-29 14:22:02 +00:00
}
String* _mid(int i1, int i2, String* s)
1984-11-29 14:22:02 +00:00
{
int l;
String* s2;
1984-11-29 14:22:02 +00:00
/* printf("mid fcn called %d %d %s\n",i1,i2,s->strval);*/
if (i2 < 0 || i1 < -1)
error(3);
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);
1984-11-29 14:22:02 +00:00
}
String* _right(int length, String* str)
1984-11-29 14:22:02 +00:00
{
String* s;
1984-11-29 14:22:02 +00:00
int i;
i = _length(str) - length;
if (i < 0)
i = 0;
s = _newstr(str->strval + i);
return (s);
1984-11-29 14:22:02 +00:00
}