calisto-20000323/
calisto-20000323/lib/
calisto-20000323/lib/etc/
calisto-20000323/lib/players/
calisto-20000323/lib/text/
calisto-20000323/log/
/*
 Calisto (c) 1998-1999 Peter Howkins, Matthew Howkins, Simon Howkins

 $Id: strplus.c,v 1.3 2000/01/14 19:52:59 peter Exp $

 $Log: strplus.c,v $
 Revision 1.3  2000/01/14 19:52:59  peter
 Added strchomp() function, reminiscent of Perl's chomp

 Revision 1.2  1999/12/11 23:07:35  peter
 added strip_trailing_white_space

 Revision 1.1  1999/11/29 22:07:50  peter
 Initial revision


 */
static char rcsid[] = "$Id: strplus.c,v 1.3 2000/01/14 19:52:59 peter Exp $";

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

#include "strplus.h"

void strmunch(char *source, char **first, char **rest)
{
  char *term;
  char *f, *r;

  /* find initial non white space char in source */
  while(*source && (isspace(*source)))
    source++;
  f = source;
  r  = source;
  /* move 'rest' to the first white-space char after 'first */
  while (*r && (!isspace(*r)))
    r ++;
  /* make a note of where the terminator will go */
  term = r;
  /* Now move 'rest' on to next non white-space char */
  while (*r && (isspace(*r)))
    r ++;
  /* insert the terminator after the first word */
  *term = '\0';
  
  *first = f;
  *rest  = r;
}

int strchomp(char *s)
{
  if (s) {
    int len = strlen(s);

    if (s[len - 1] == '\n') {
      s[len - 1] = '\0';
      return 1;
    }
  }
  return 0;
}

void strlower(char *s)
{
  while (*s) {
    *s = tolower(*s);
    s++;
  }
}

void strupper(char *s)
{
  while (*s) {
    *s = toupper(*s);
    s++;
  }
}

int stricmp(const char *s1, const char *s2)
{
  char i, j;

  do {
    i = *s1++;
    j = *s2++;

    i = tolower(i);
    j = tolower(j);
  } while (i && i == j);

  return i - j;
}

int strnicmp(const char *s1, const char *s2, size_t n)
{
  char i, j;

  if (!n)
    return 0;
 
  do {
    i = *s1++;
    j = *s2++;
    i = tolower(i);
    j = tolower(j);
  } while (i && i==j && --n);

  return i - j;
}


/*
  String versions of ctype functions
*/

bool strisalnum(const char *s)
{
  while (*s) {
    if (!isalnum(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisalpha(const char *s)
{
  while (*s) {
    if (!isalpha(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool striscntrl(const char *s)
{
  while (*s) {
    if (!iscntrl(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisdigit(const char *s)
{
  while (*s) {
    if (!isdigit(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisgraph(const char *s)
{
  while (*s) {
    if (!isgraph(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strislower(const char *s)
{
  while (*s) {
    if (!islower(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisprint(const char *s)
{
  while (*s) {
    if (!isprint(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strispunct(const char *s)
{
  while (*s) {
    if (!ispunct(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisspace(const char *s)
{
  while (*s) {
    if (!isspace(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisupper(const char *s)
{
  while (*s) {
    if (!isupper(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

bool strisxdigit(const char *s)
{
  while (*s) {
    if (!isxdigit(*s))
      return FALSE;
    s++;
  }
  return TRUE;
}

char *strip_leading_white_space(const char *s)
{
  while (isspace(*s))
    s++;
  return (char *) s;
}

void strip_trailing_white_space(char *s)
{
  char *pos;
  int len = strlen(s); 

  if (len > 0) {
    pos = s + len - 1; /* pointing to last char in string proper (not \0) */
    while(pos >= s && isspace(*pos)) {
      pos--;
    }
    *(pos+1) = '\0';
  }
}