static char stringUtils_id[] = "$Header: /usr/tiburon/xPlatform/utils/RCS/stringUtils.cc,v 1.4 1997/12/29 23:41:00 oreilly Exp pean $";


/*
$Log: stringUtils.cc,v $
Revision 1.4  1997/12/29 23:41:00  oreilly
Added substr() function

Revision 1.3  1997/09/10 09:25:34  oreilly
Fixed problem in trim() when removing leading blanks

Revision 1.2  97/04/09  16:36:59  16:36:59  oreilly (Thomas C. O'Reilly)
Fixed old-style function defintions

Revision 1.1  97/03/20  12:26:50  12:26:50  oreilly (Thomas C. O'Reilly)
Initial revision

Revision 1.2  96/07/22  10:42:33  10:42:33  oreilly (Thomas C. O'Reilly)
First external release

*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef UNIX
# include <malloc.h>
#endif

#include "stringUtils.h"

/*
Function: trim()
Description: Trim leading and trailing whitespace from string
Return values: none
*/
void trim(char *string)
{
  char *ptr;

  // Trim trailing whitespace
  for (ptr = string + strlen(string) - 1; ptr >= string; ptr--)
  {
    if (isspace(*ptr))
      *ptr = '\0';
    else
      break;
  }

  // Trim leading whitespace
  for (ptr = string; *ptr != '\0'; ptr++)
  {   
    if (!isspace(*ptr))
      break;
  }
  if (ptr == string)
    // No leading whitespace
    return;

  if (*ptr == '\0')
    // string was all whitespace
    return;
  
  
  for (char *destPtr = string; ; ptr++, destPtr++)
  {
    *destPtr = *ptr;
    if (*ptr == '\0')
      break;
  }
  
}



/*
Function: allBlanks()
Description: Check whether string contains only SPACE, TAB, RETURN,
NEWLINE, FORMFEED, or vertical tab characters
Return values: TRUE if string is composed only of the above characters,
else FALSE.
*/
MBool allBlanks(const char *ptr)
{
  int i;
  
  /* Check for all blanks */
  for (i = 0; i < strlen(ptr); i++)
  {
    if (!isspace((int )ptr[i]))
    {
      return FALSE;
    }
  }
  return TRUE;
}  



/* Convert string to upper case (in place). */
/* This function does more checking than SUN's tolower function */
void upperCase(char *string)
{
  int	i, length;
  
  length = strlen( string );
  for ( i = 0; i < length; i++ ) {
    if ( islower(string[i]) ) {
      string[i] = toupper(string[i]);
    }
  }
}


/* Convert string to lower case (in place). */
/* This function does more checking than SUN's tolower function */

void lowerCase(char *string)
{
  int i;
  int len;
  
  len = strlen(string);
  for (i = 0; i < len; i++)
    if (isupper(string[i]))
      string[i] = tolower(string[i]);
}



/*
Function: substr()
Description: Find address of substring in string
Return values: Address of substring, or NULL if substring not in string
*/
char *
substr(char *string, char *substring)
{
  char *start_addr;
  char *str_ptr = string;
  int sublen = strlen(substring);


  /* Search the string for the first character of the substring; if found,
     compare string starting at that address with substring */
  while ((start_addr = strchr(str_ptr, *substring)) != (char *)NULL)
  {
    if (!strncmp(start_addr, substring, sublen))
      return(start_addr);    

    /* Advance pointer for next search */
    str_ptr = start_addr + 1;
  }
  return((char *)NULL);
}


