home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_300
/
357_01
/
cstar1.exe
/
STR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-18
|
5KB
|
281 lines
/*
C* -- string handling routines.
source: str.c
started: October 6, 1985
version:
December 6, 1986
March 7, 1989
PUBLIC DOMAIN SOFTWARE
The CSTAR program was placed in the public domain on June 15, 1991,
by its author and sole owner,
Edward K. Ream
1617 Monroe Street
Madison, WI 53711
(608) 257-0802
CSTAR may be used for any commercial or non-commercial purpose.
See cstar.h or cstar.c for a DISCLAIMER OF WARRANTIES.
*/
#include "cstar.h"
/*
All routines in this file are externally visible:
*/
void str_cat (register char *s1, register char *s2);
int str_cmp (register char *s1, register char *s2);
void str_cpy (register char *s1, register char *s2);
bool str_eq (register char *s1, register char *s2);
char * str_galloc (register char *s);
char * str_gcat (register char *s1, register char *s2);
char * str_lalloc (register char *s);
char * str_lcat (register char *s1, register char *s2);
int str_len (register char *s);
char * str_name (void);
char * str_salloc (register char *s);
char * chr_salloc (register char *s, int n);
/*
Concatenate s2 to the end of s1.
*/
void
str_cat(register char *s1, register char *s2)
{
TRACEP("str_cat", printf("(%s, %s)\n", s1, s2));
while(*s1) {
s1++;
}
do {
*s1++ = *s2;
}
while (*s2++);
return;
}
/*
Compare s1 and s2.
Return <0 ==0 >0
*/
bool
str_cmp(register char *s1, register char *s2)
{
register char c;
TRACEPB("str_cmp", printf("(%s, %s)\n", s1, s2));
while (*s1 == *s2) {
if (*s1 == '\0') {
RETURN_BOOL("str_cmp", 0);
}
else {
s1++;
s2++;
}
}
RETURN_BOOL("str_cmp", ((int)*s1) - ((int)*s2));
}
/*
Copy s2 to s1.
s1 must be large enough to hold s2.
*/
void
str_cpy(register char *s1, register char *s2)
{
TRACEP("str_cpy", printf("(%p, %s)\n", s1, s2));
while (*s2) {
*s1++ = *s2++;
}
}
/*
Return TRUE if s1 == s2
*/
bool
str_eq(register char *s1, register char *s2)
{
TRACEPB("str_eq", printf("(%s, %s)\n", s1, s2));
while(*s1) {
if (*s1++ != *s2++) {
RETURN_BOOL("str_eq", FALSE);
}
}
RETURN_BOOL("str_eq", !*s2);
}
/*
Allocate global (static) memory big enough to hold the string,
then copy the string to the allocated memory.
NOTE: if scope is set up properly, this is not really required
as a separate routine.
*/
char *
str_galloc(register char *s)
{
register char * p;
register int n;
TRACEPB("str_galloc", printf("(%s)\n", s));
n = str_len(s);
p = mg_alloc(n + 1);
str_cpy(p, s);
RETURN_STRING("str_galloc", p);
}
/*
Allocate global memory for two strings
*/
char *
str_gcat(register char *s1, register char *s2)
{
register char * p;
register int n1, n2;
TRACEPB("str_gcat", printf("(%s, %s)\n", s1, s2));
n1 = str_len(s1);
n2 = str_len(s2);
p = mg_alloc(n1 + n2 + 1);
str_cpy(p, s1);
str_cpy(p + n1, s2);
RETURN_STRING("str_gcat", p);
}
/*
Same as str_galloc() except local scope.
*/
char *
str_lalloc(register char *s)
{
register char * p;
register int n;
TRACEPB("str_lalloc", printf("(%s)\n", s));
n = str_len(s);
p = ml_alloc(n + 1);
str_cpy(p, s);
RETURN_STRING("str_lalloc", p);
}
/*
Allocate local memory for two strings
*/
char *
str_lcat(register char *s1, register char *s2)
{
register char * p;
register int n1, n2;
TRACEPB("str_lcat", printf("(%s, %s)\n", s1, s2));
n1 = str_len(s1);
n2 = str_len(s2);
p = ml_alloc(n1 + n2 + 1);
str_cpy(p, s1);
str_cpy(p + n1, s2);
RETURN_STRING("str_lcat", p);
}
/*
Return the length of a string.
*/
int
str_len(register char *s)
{
register int len;
TRACEPB("str_len", printf("(%s)\n", s));
len=0;
while (*s++) {
len++;
}
RETURN_INT("str_len", len);
}
/*
Return a unique symbolic name for a string.
*/
char *
str_name(void)
{
static char buf[LONG_DIGITS+5];
static unsigned long ssn = 1;
TICKB("str_name");
buf[0] = 'S';
conul2sc(ssn, buf+1, 3);
ssn++;
RETURN_STRING("str_name", &buf[0]);
}
/*
Same as str_galloc() but for appropriate scope.
*/
char *
str_salloc(register char *s)
{
register char * p;
register int n;
TRACEPB("str_salloc", printf("(%s)\n", s));
n = str_len(s);
switch(scope.s_scope) {
case PROTO_SCOPE:
case BLOCK_SCOPE:
case FNDEF_SCOPE:
p = ml_alloc(n + 1);
break;
case FILE_SCOPE:
default:
p = mg_alloc(n + 1);
}
str_cpy(p, s);
RETURN_STRING("str_salloc", p);
}
char *
chr_salloc(register char *s, int n)
{
register char *p, *d;
TRACEPB("chr_salloc", printf("(%s, %d)\n", s, n));
switch(scope.s_scope) {
case PROTO_SCOPE:
case BLOCK_SCOPE:
case FNDEF_SCOPE:
d = p = ml_alloc(n + 1);
break;
case FILE_SCOPE:
default:
d = p = mg_alloc(n + 1);
}
while (n--) {
*p++ = *s++;
}
RETURN_STRING("chr_salloc", d);
}