[net.sources] string.c extension sources

jantypas@wolf.SNS (John Antypas) (05/03/86)

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	string.doc
#	string2.c
#	string2.h
# This archive created: Fri May  2 14:12:14 1986
export PATH; PATH=/bin:$PATH
if test -f 'string.doc'
then
	echo shar: will not over-write existing file "'string.doc'"
else
cat << \SHAR_EOF > 'string.doc'
			Strings2 - John Antypas

		An Addition to the Strings(3) Package

	The Unix environment provides most functions in standard libraries,
but more often not, a programmer will need a function NOT provided.
Such is the case with string(3).  I find myself wanting an index() which
works with strings and not characters alone.  I don't know why this was
left out, but it was.  Also, a function to replace a pattern in a string
with another would be nice.  So...

strindex(s,t) - Returns the point at which string t is located in string s.
                If t is not in s, -1 is returned.

strreplace(s,p,r) - Replace first pattern (p) in string (s) with the pattern
                    (r).  This can also be used as strdel() to delete patterns
                    bhaving r = NULL hence replace with nothing.

Pointers are not checked and disaster will result if bad pointers are passed.
SHAR_EOF
fi # end of overwriting check
if test -f 'string2.c'
then
	echo shar: will not over-write existing file "'string2.c'"
else
cat << \SHAR_EOF > 'string2.c'
/*
      Strings2 - Provides even more string functions for C.
      I don't know why these weren't included in strings(3).

      John Antypas -- ...!sdcsvax!jantypas
      
     strword1(r,s,c,i) -- Returns 1st word in s to r.  c is the character
                          which separates words.
     strindex(s,t) -- returns first occurrence of t in s or -1.
     strreplace(s,p,r) -- replace first pattern p with pattern r.
          This can be used to strinsert and delete functions.
          It returns a pointer to the new string s.

     All parameters are character pointers.

*/

#define		NULL	0
#include	<string.h>	/* Need strcat, strcpy	*/


int	strindex(s,t)
char *s, *t;
{
	int loop, n;		/* Loop counter		*/

	n = strlen(t);
	for (loop=0;s+loop != '\0'; loop++)
	if (strncmp(s+loop, t, n) == 0) { return(loop); }
	return -1;
}

char	*strreplace(s,p,r)
char	*s, *p, *r;
{
	int	f, n;
	char	*m;		/* Used for temporary	*/

	m = malloc( strlen(s) + strlen(r) - strlen(p) );
	/* Get enough memory for the new string		*/
	/* If it doesn't work, die here			*/
	if (m == NULL) return(m);

	f = strindex(s,p);
	strncpy(m,s,f);	/* Copy up to p.	*/
	strcat(m,r);		/* Add in r.		*/
	
        /* Now add the part after the replacement	*/
	strcat(m,s+f+strlen(p));
	/* Copy it back to s				*/
	strcpy(s,m);
	free(m);
	return(s);
}

int	strword(r,s,c,i)
char	*s, *r, c;
int	i;
/*
	Function: Returns number of words/specific word in string s.
                  Returns in string r the word requested.  If i<0;
                  returns # words in s, else reurns ith word in r.
		  c is the character used to mark separate words.
		  Usually is a space (32).
*/
{
	int l;

	l = strlen(s);
	if (i<0)		/* Count words		*/
	{
		int nw,j;	/* # words and loop var	*/

		for (j=0,nw=0; j<l; j++)
		{
			if (*(s+j) == c) nw++;
		}
		if (nw != 0) nw++;
		return(nw);
	}
	else
	{
		char *sp,t[2];
		int nw,m;		/* Loops and length(s)	*/

		t[0] = c;  t[1] = 0;
		/* Find begining of ith word		*/
		nw = 0; sp = s;
		while ((nw<i) && (*sp != NULL))
		{
			if ((*sp) == c) nw++;
			sp++;
		}
		/* Now find end of that word		*/
		m = strindex(sp,t);
		if (m>0) { strncpy(r,sp,m); } else { strcpy(r,sp); }
		return(0);
	}
}
SHAR_EOF
fi # end of overwriting check
if test -f 'string2.h'
then
	echo shar: will not over-write existing file "'string2.h'"
else
cat << \SHAR_EOF > 'string2.h'
/*
      String2 -- String2 header file

      Make sure to link with string2.o

      strindex(s,t) -- Return index to pattern t in string s or -1
      strreplace(s,p,r) -- Replace pattern p by pattern r in string s.
                           Return char pointer to s or NULL if error.
*/
extern	int	strword();
extern	int	strindex();
extern	char	*strreplace();
#define	strdelete(s,p)	strreplace(s,p,"");
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0