# include <ohana.h>

/* Strip WHITESPACE from the start and end of STRING. */
int stripwhite (string)
char *string;
{
  int i;

  if (string == (char *) NULL)
    return (FALSE);

  for (i = 0; OHANA_WHITESPACE (string[i]); i++);
  if (i) memmove (string, string + i, strlen(string+i)+1);
  for (i = strlen (string) - 1; (i > 0) && OHANA_WHITESPACE (string[i]); i--);
  string[++i] = 0;
  return (i);

}

/* create a new string from this string */
char *strcreate (string)
char *string;
{

  char *line;

  if (string == (char *) NULL) return ((char *) NULL);
  
  ALLOCATE (line, char, MAX (1, strlen(string)) + 1);
  line = strcpy (line, string);

  return (line);

}

/* create a new string of length n from this string */
char *strncreate (string, n)
char *string;
int   n;
{

  char *line;

  if (string == (char *) NULL) return ((char *) NULL);
  
  ALLOCATE (line, char, n + 1);
  memmove (line, string, n);
  line[n] = 0;
  return (line);

}

int scan_line (f, line) 
FILE *f;
char line[];
{

  int i, status;
  char c;
  
  status = EOF + 1;
  
  for (i = 0, c = 0; (c != '\n') && (status != EOF); i++) {
    status = fscanf (f, "%c", &c);
    line[i] = c;
  }
  line[i - 1] = 0;  /* this could make things crash! */

  if (i > 1) {
    status = EOF + 1;
  }

  return (status);

}

char *_parse_nextword(string)
char *string;
{
  if (string == (char *) NULL) return ((char *) NULL);

  for (; isspace (*string); string++);
  for (; (*string != 0) && !isspace (*string); string++);
  for (; isspace (*string); string++);
  return (string);
}

int dparse (X, NX, line)
double *X;
int NX;
char *line;
{

  int i;
  char *word;
  char *ptr;

  word = line;
  for (i = 0; i < NX - 1; i++)
    word = _parse_nextword (word);

  *X = strtod (word, &ptr);
  if (ptr == word)
    return (FALSE);
  else {
    if (word[0] == '-') 
      return (-1);
    else
      return (1);
  }
}


