/*
 * $Log: my_string,v $
 * Revision 1.4  1998/09/26 19:57:37  joseph
 * Fix to my_strncasecmp
 *
 * Revision 1.3  1998/09/17 15:19:13  joseph
 * New versions of my_str[n]casecmp
 *
 * Revision 1.2  1997/12/29 18:40:48  jogu
 * Fixed header file inclusion for NFS compile
 * Fixed <a name="xxx" href="tttt"> not getting picked up
 *   Added my_strcasestr, a case insensitive version of strstr
 *
 * Revision 1.1.1.1  1997/12/29 14:37:55  jogu
 * WebGet Initial CMS Ver
 *
 */


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

#include "my_string.h"

/* Next two are a nasty combination of code from me, K&R and gerph: */

int my_strncasecmp(const char *s, const char *t, int n)
{
  int a,b;

  if (n==0)
    return 0;

  for ( ; (a=tolower(*s)) == (b=tolower(*t)); s++,t++ )
    if ( (*s==0) || (--n==0) )
      return 0;

  return a - b;
}

int my_strcasecmp(const char *s, const char *t)
{
  int a,b,c;
  for ( ; (a=tolower(*s)) == (b=tolower(c=*t)); t++,s++ )
    if (c==0)
      break;
  return a - b;
}

int my_memcasecmp(const char * s, const char * t, size_t n )
{
  const char *end = s + n - 1;
  for ( ; tolower(*s)==tolower(*t) && s<end; s++, t++ );

  return tolower(*s)-tolower(*t);
}

char *my_strcasestr( const char * s1, const char * s2 )
{
  int l1, l2;

  l2 = strlen(s2);
  if (!l2)
    return (char *)s1; /* remove the const */

  l1 = strlen(s1);

  while (l1 >= l2)
  {
    l1--;
    if ( ! my_memcasecmp( s1, s2, l2 ) )
      return (char *)s1;
    s1++;
  }
  return NULL;
}


int my_hexatoi(const char *s)
{
  int n=0;

  for (; isxdigit(*s); s++)
    if (isdigit(*s))
      n = n * 16 + *s - '0';
    else
      n = n * 16 + toupper(*s) - 'A' + 10;

  return n;
}

char *my_strstrip(char *buf)
{
  char *ptr;
  while (isspace(*buf)) buf++;

  ptr=buf+strlen(buf)-1;
  while (isspace(*ptr) && ptr>buf) *ptr--=0;
  return buf;
}
