#include <stdio.h>
#include <strings.h>
#include <errno.h>

extern void CheckError();

/* Takes a string, returns all small alphabetics capitalized.
 * Original string unchanged.  Returns NULL if string is NULL.
 * Needs the file error.c, or at least the function CheckError().
 */

char *Upcase(s)
char *s;
    {
    char *upstr;
    int i;
    void CheckError();
    int tomalloc;

    if (s == NULL)
	return(NULL);

    tomalloc = strlen(s) + 1;
    upstr = (char *)malloc( tomalloc );
    if (upstr == NULL)
	CheckError("Upcase malloc(%d) failed at %d:  %E",tomalloc,__LINE__);

    for (i = 0; s[i] != '\0'; i++)
	{
	if (s[i] <= 'z' && s[i] >= 'a')
	    upstr[i] = s[i] - ('a' - 'A');
	else
	    upstr[i] = s[i];
	}
    upstr[i] = '\0';
    return(upstr);
    }

char *Downcase(s)
char *s;
    { 
    char *downstr;
    int i;
    void CheckError();
    int tomalloc;

    if (s == NULL)
	return(NULL);

    tomalloc = strlen(s) + 1;
    downstr = (char *)malloc( tomalloc );
    if (downstr == NULL)
	CheckError("Downcase malloc(%d) failed at %d:  %E",tomalloc,__LINE__);

    for (i = 0; s[i] != '\0'; i++)
	{
	if (s[i] <= 'Z' && s[i] >= 'A')
	    downstr[i] = s[i] + ('a' - 'A');
	else
	    downstr[i] = s[i];
	}
    downstr[i] = '\0';
    return(downstr);
    }

/* Takes two strings, one an arbitrary string, the other a delimiter
* to be found within it.  Finds the first occurence of the delimiter
* in the string, and returns the portion of the string after the
* delimiter.  The original string is truncated at the beginning of
* the delimiter.  Returns two empty strings if the delimiter is an
* empty string.  Returns NULL if the input string or delimiter is
* NULL.
*/

char *StripFirstWord(s, delim)
char *s, *delim;
    {
    int i, j;
    char *s1;
    int tomalloc;

    if (s == NULL || delim == NULL)
	return(NULL);

    i = 0;
    j = strlen(delim);
    while (*(s + i) != '\0')
	{
	if (strncmp(s + i, delim, j) == 0)
	    break;
	i++;
	}
    if (i == strlen(s))
	return('\0');
    *(s+i) = '\0';
    tomalloc = strlen(s+i+j) + 1;
    s1 = (char *)malloc( tomalloc );
    if (s1 == NULL)
	CheckError("StripFirstWord malloc(%d) failed at %d:  %E",
	    tomalloc,__LINE__);
    strcpy(s1, (s + i + j));
    return(s1);
    }

/* Takes two strings, one an arbitrary string, the other a delimiter
* to be found within it.  Finds the first occurence of the delimiter
* in the string, and returns the portion of the string before the
* delimiter.  The original string is now everything after the
* delimiter.  Returns two empty strings if the delimiter is an empty
* string.  Returns NULL if the input string or delimiter is NULL.
*/

char *GetFirstWord(s, delim)
char *s, *delim;
    {
    int i, j, k;
    char *s1;
    int tomalloc;

    if (s == NULL || delim == NULL)
	return(NULL);

    i = 0;
    j = strlen(delim);
    k = strlen(s);
    while (*(s + i) != '\0')
	{
	if (strncmp(s + i, delim, j) == 0)
	break;
	i++;
	}
    if (i == k)
	{
	tomalloc = k + 1;
	s1 = (char *)malloc( tomalloc );
	if (s1 == NULL)
	    CheckError("GetFirstWord malloc(%d) failed at %d:  %E",
		tomalloc,__LINE__);
	strcpy(s1, s);
	*s = '\0';
	return(s1);
	}
    *(s + i) = '\0';
    tomalloc = strlen(s) + 1;
    s1 = (char *)malloc( tomalloc );
    if (s1 == NULL)
	CheckError("GetFirstWord malloc(%d) failed at %d:  %E",
	    tomalloc,__LINE__);
    strcpy(s1, s);
    strcpy(s, (s + i + j));
    return(s1);
    }

/* Takes two strings, one the input, the second a pattern.  Returns a
* string with all occurrences of the pattern removed from the front
* of the string.  Original string is unaffected.  Returns NULL if the
* input string is NULL, or if pattern is zero length or NULL.
*/

char *StripLeadingPattern(s, patt)
char *s, *patt;
    {
    int i, j;
    char *s1;
    int tomalloc;

    if (s == NULL || patt == NULL)
	return(NULL);

    i = 0;
    j = strlen(patt);
    if (j == 0)
	return('\0');
    while (*(s + i) != '\0')
	{
	if (strncmp(s + i, patt, j) != 0)
	    break;
	i += j;
	}

    tomalloc = strlen(s+1) + 1;
    s1 = (char *)malloc( tomalloc );
    if (s1 == NULL)
	CheckError("StripLeadingPattern malloc(%d) failed at %d:  %E",
	    tomalloc, __LINE__);
    strcpy(s1, s + i);
    return(s1);
    }
