# include <ohana.h>

/* Strip WHITESPACE from the start and end of STRING. */
int stripwhite (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);

}

/* compare two strings either as strings, or as numbers if both are
   pure numeric strings (base 10) */
int strnumcmp (char *str1, char *str2) {

  char *end1;
  char *end2;
  int num1, num2;
  int value;
 
  value = FALSE;
  num1 = strtol (str1, &end1, 10);
  num2 = strtol (str2, &end2, 10);

  if (!end1[0] && !end2[0] && (num1 == num2)) {
    value = TRUE;
  }
  if (!strcmp (str1, str2)) {
    value = TRUE;
  }
  
  return (value);
}

/* create a new string from this string */
char *strcreate (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 (char *string, int n) {

  char *line;

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

// replace a single entry of 'match' in the string with 'with'
// (quick-and-dirty regex for a common case...)
char *strsubs (char *string, char *match, char *with) {

  int N1, N2, N3;
  char *root;
  char *out;
  char *ext;

  if (string == NULL) return NULL;
  if (match == NULL) return NULL;
  if (with == NULL) return NULL;

  root = strstr (string, match);
  if (root == NULL) {
    return (strcreate (string));
  }
  N1 = root - string;

  N2 = strlen (with);

  ext = root + strlen(match);
  N3 = strlen (ext);

  ALLOCATE (out, char, N1 + N2 + N3 + 1);

  strncpy (out, string, N1);
  strncpy (&out[N1], with, N2);
  strncpy (&out[N1+N2], ext, N3);
  out[N1+N2+N3] = 0;

  return out;
}

# if 0
// replace a single entry of 'match' in the string with 'with'
// (quick-and-dirty regex for a common case...)
char *strrsubs (char *string, char *match, char *with) {

  char *root;
  char *out;

  if (string == NULL) return NULL;
  if (match == NULL) return NULL;
  if (with == NULL) return NULL;

  root = strstr (string, match);
  if (root == NULL) return NULL;

  ext = string + strlen (match);
  ALLOCATE (out, char *, strlen(with) + strlen(ext) + 1);
  strcpy (out, with);
  strcat (out, ext);

  return out;
}
# endif

int scan_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 (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 (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);
  if (word[0] == '-') return (-1);
  return (1);
}

int fparse (float *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);
  if (word[0] == '-') return (-1);
  return (1);
}

int get_argument (int argc, char **argv, char *arg) {

  int i;

  for (i = 0; i < argc; i++) {
    if (!strcmp(argv[i], arg))
      return (i);
  }
  
  return ((int)NULL);
}

int remove_argument (int N, int *argc, char **argv) {

  int i;

  if ((N != (int)NULL) && (N != 0)) {
    (*argc)--;
    for (i = N; i < *argc; i++) {
      argv[i] = argv[i+1];
    }
  }

  return (N);
}

void uppercase (char *string) {

  int i;
    
  for (i = 0; i < strlen (string); i++) string[i] = toupper (string[i]);

}

/* expect a line of the form "$Name: not supported by cvs2svn $", strip out contents */
char *strip_version (char *input) {

  char *p, *q;

  p = strstr (input, "$Name:");
  if (p == NULL) return (strcreate ("NONE"));

  q = strcreate (input + 6);
  p = strrchr (q, '$');
  if (p != NULL) *p = 0;
  stripwhite (q);
  if (*q == 0) {
    free (q);
    q = strcreate ("NONE");
  }

  return (q);
}
