/*

$Id: CRstring,v 1.5 2002/01/05 18:42:10 joseph Exp $

*/
#include <ctype.h>
#include <stdlib.h>
#include "memleak.h"
#include <string.h>

#include "CRstring.h"

#ifndef POPNDEBUG
char *CRstrcpy(char *s1, const char *s2)
{
  int n;
  for (n=0; s2[n]>31; ++n)
    s1[n] = s2[n];
  s1[n] = 0;
  return s1;
}
#endif

int CRstrlen(const char *s)
{
  int n;
  if (!s) return 0;
  for (n=0; s[n]>31; ++n);
  return n;
}

char *CRstrncpy(char *s1, const char *s2, int m)
{
  int n;
  for (n=0; s2[n]>31 && n<m; ++n)
    s1[n] = s2[n];
  if (n < m)
    s1[n] = 0;
  return s1;
}

#ifdef NOT_USED
char *CRstrcat(char *s1, const char *s2)
{
  int n;
  for (n=0; s1[n]>31; ++n);
  for (;*s2>31; ++n, ++s2)
    s1[n] = *s2;
  s1[n] = 0;
  return s1;
}

char *CRstrterm(char *s, char t)
{
  int n;
  if (!s) return 0;
  for (n=0; s[n]>31; ++n);
    s[n] = t;
  return s;
}
#endif

char *CRstrdup(const char *s)
{
  char *s2;
  int n;
  if (!s || s[0] < 32)
    return 0;
  for (n = 0; s[n] > 31; ++n);
  s2 = malloc(n + 1);
  if (!s2)
    return 0;
  memcpy(s2, s, n);
  s2[n] = 0;
  return s2;
}

#ifdef NOT_USED
int CRstrincmp(const char *s1, const char *s2, int n)
{
  char ch1, ch2;

  for( ; n > 0; --n)
  {
    if (*s1 < 32 && *s2 < 32)
      return 0; /* s1 and s2 are equal */
    if (*s1 < 32)
      return -1;
    if (*s2 < 32)
      return 1;

    ch1 = toupper(*s1);
    ch2 = toupper(*s2);

    if (ch1 != ch2)
      return (int) (ch1 - ch2);
    s1++;
    s2++;
  }
  return 0;
}
#endif
