/*******************************************************
 *
 * file name - strutil.c
 *
 * author - rtgregory
 *
 * date - 7-mar-90
 *
 * purpose - simple string handling routines - lower, upper, trim, skip_white,
 *	     parse_line, space, tabspace
 *
 * $Id: ccs:[rich.work]strutil.c_v 2.0 91/01/09 13:20:24 RICH Exp $
 * $Author: RICH $
 *
 * 
 *
 *******************************************************/

#ifdef TESTING_CHARS
#define INT int
#define INTX int
#define NEWLINE " "
#endif /* test read */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef VMS
#  include <stdlib.h>
#endif

#if defined( APOLLO) || defined (MICROSOFT_C)
#  include <malloc.h>
#endif

#ifdef TURBOC
#  include <alloc.h>
#endif

#include "ccs.h"


char *lower( char *outstr, char *instr)
{
char *tmp;
INT i;

	i = strlen( instr) + 1;
	tmp = (char *) malloc( (INTX) i * sizeof( char));
	i = 0;
	/* lowercase all lines */
	while( instr[ i ] != NULL)
	{
	  tmp[ i ] = tolower( (INT) instr[ i ]);
	  i++;
	}
	tmp[ i ] = '\0';
	strcpy( outstr, tmp);
	free( tmp);
	return( outstr);
} /* lower */


char *upper( char *outstr, char *instr)
{
char *tmp;
INT i;

	i = strlen( instr) + 1;
	tmp = (char *) malloc( (INTX) i * sizeof( char));
	i = 0;
	while( instr[ i ] != NULL)
	{
	  tmp[ i ] = toupper( (INT) instr[ i ]);
	  i++;
	}
	tmp[ i ] = '\0';
	strcpy( outstr, tmp);
	free( tmp);
	return( outstr);
} /* upper */

char *trim_string( char *outstr, char *instr)
{
char *tmp;
INT i;

	i = strlen( instr) + 1;
	tmp = (char *) malloc( (INTX) i * sizeof( char));
	strcpy( tmp, instr);
	i--;
	while(
	     ( strchr( WHITE_SPACE, (INT) tmp[ i ]) != NULL) &&
	     ( i > 0)
	     )
	{
	  tmp[ i ] = '\0';
	  i--;
	}
	strcpy( outstr, tmp);
	free( tmp);
	return( outstr);
}

char *skip_white( char *outstr, char *instr)
{
char *tmp;
INT i;

	i = strlen( instr) + 1;
	tmp = (char *) malloc( (INTX) i * sizeof( char));
	i = 0;
	while(
	  ( strchr( WHITE_SPACE, instr[ i ]) != NULL) &&
	  ( instr[ i ] != '\0')
	  )
	{
	  i++;
	}
	strcpy( tmp, &instr[ i ]);
	strcpy( outstr, tmp);
	free( tmp);
	return( outstr);
} /* skip white */


void parse_line( char *instr, char *token, char *rest, char *delim)
{
char *copy, *start, *str;
INT i;

	i = strlen( instr);
	copy = (char *) malloc( (INTX) i * sizeof( char));
	strcpy( copy, instr);
	str = copy;
	/** skip over all delimiters at the beginning of instr **/
	while(
	     ( strchr( delim, (INT) *copy) != NULL) &&
	     ( *copy != NULL)
	     )
	{
	  copy++;
	}
	if( ISNULLSTRING( copy))
	{
	  SETNULLSTRING( rest);
	  SETNULLSTRING( token);
	}
	else
	{
	  start = strpbrk( copy, delim);
	  if( start == NULL)
	  {
            SETNULLSTRING( rest);
	    if( strlen( copy) > 0)
	    {
	      strcpy( token, copy);
	    }
	    else
	    {
	      SETNULLSTRING( token);
	    }
	  }
	  else
	  {
	    *start = '\0';
	    strcpy( token, copy);
	    start++;
	    strcpy( rest, start);
	  }
	}
	free( str);

} /* parse line */

/*******************************************************
 * space()
 * num - number of characters to fill with spaces
 * str - pointer to first character to fill
 *******************************************************/

char *space( int num, char *str)
{
int i;

	for( i = 0; i < num; i++)
	{
	  str[ i ] = ' ';
	}
	str[ num ] = '\0';
	return( str);
}


char *tabspace( int num, char *str, int size)
{
int i;
char tmp[ 25 ];

	for( i = 0; i < num; i = i+size)
	{
	  if( i < size)
	  {
	    strcpy( str, "\t");
	  }
	  else
	  {
	    strcat( str, space( size, tmp));
	  }
	}
	return( str);
}

#ifdef TESTING_CHARS

main()

{
char tmp[ 100 ], str[ 100 ], tok[100], rest[100];

#ifdef DONE
	strcpy( str, "asdfasdf");
	upper( str, str);
	printf( "%s\n", str);

	strcpy( str, "   asdfasdf");
	skip_white( str, str);
	printf( "%s\n", str);

	SETNULLSTRING( str);
	skip_white( str, str);
	printf( "%s\n", str);
#endif /* done */

	strcpy( str, "asdf34 333-+.12,,,,4312 4124-=++++qq qqqqqqq+++");
	printf( "%s\n", str);
	parse_line( str, tok, rest, "-=+.");
	printf( "%s\n", tok);
	while( ISNOTNULLSTRING( tok))
	{
	  parse_line( rest, tok, rest, "-=+,.");
	  printf( "%s\n", tok);
	}
	SETNULLSTRING( str);
	parse_line( str, tok, rest, "-=+.");
	strcpy( str, "=-=...sdf");
	parse_line( str, tok, rest, "-=+.");

} /* main  */ 

#endif  /* TESTING_CHARS */

