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

#include "../include/aftersteplib.h"
#include "../include/parse.h"

/****************************************************************************
 * 
 * Some usefull parsing functions
 *
 ****************************************************************************/
char *
ReadIntValue (char *restofline, int *value)
{
  sscanf (restofline, "%d", value);

  while (isspace ((unsigned char) *restofline))
    restofline++;
  while ((!isspace ((unsigned char) *restofline)) && (*restofline != 0) &&
	 (*restofline != ',') && (*restofline != '\n'))
    restofline++;
  while (isspace ((unsigned char) *restofline))
    restofline++;

  return restofline;
}

char *
ReadColorValue (char *restofline, char **color, int *len)
{
  char *tmp;
  *len = 0;

  while (isspace ((unsigned char) *restofline))
    restofline++;
  for (tmp = restofline; (tmp != NULL) && (*tmp != 0) && (*tmp != ',') &&
       (*tmp != '\n') && (*tmp != '/') && (!isspace ((unsigned char) *tmp));
       tmp++)
    (*len)++;

  if (len > 0)
    {
      if (*color)
	free (*color);
      *color = safemalloc (*len + 1);
      strncpy (*color, restofline, *len);
      (*color)[*len] = 0;
    }

  return tmp;
}

char *
ReadFileName (char *restofline, char **fname, int *len)
{
  char *tmp;
  *len = 0;

  while (isspace ((unsigned char) *restofline))
    restofline++;
  for (tmp = restofline;
       (tmp != NULL) && (*tmp != 0) && (*tmp != ',') && (*tmp != '\n');
       tmp++)
    (*len)++;

  if (*len > 0)
    {
      if (*fname)
	free (*fname);
      *fname = safemalloc (*len + 1);
      strncpy (*fname, restofline, *len);
      (*fname)[*len] = 0;
    }

  return tmp;
}

/****************************************************************************
 * 
 * Copies a string into a new, malloc'ed string
 * Strips leading and trailing whitespace
 *
 ****************************************************************************/

char *
stripcpy (const char *source)
{
  const char *ptr;
  for (; isspace (*source); source++);
  for (ptr = source + strlen (source); ptr > source && isspace (*(ptr - 1)); ptr--);
  return mystrndup (source, ptr - source);
}


/****************************************************************************
 * 
 * Copies a string into a new, malloc'ed string
 * Strips all data before the first quote and after the second
 *
 ****************************************************************************/

char *
stripcpy2 (const char *source, int tab_sensitive)
{
  const char *ptr;
  for (; *source != '"' && *source != '\0'; source++);
  if (*source == '\0')
    return NULL;
  source++;
  ptr = source;
  if (!tab_sensitive)
    for (; *ptr != '"' && *ptr != '\0'; ptr++);
  else if (tab_sensitive == 1)
    for (; *ptr != '"' && *ptr != '\0' && *ptr != '\t'; ptr++);
  else if (tab_sensitive == 2)
    {
      for (; *source != '"' && *source != '\0' && *source != '\t'; source++);
      for (; *source != '"' && *source != '\0'; source++);
      for (ptr = source; *ptr != '"' && *ptr != '\0'; ptr++);
    }
  return mystrndup (source, ptr - source);
}

/****************************************************************************
 *
 * Matches text from config to a table of strings.
 *
 ****************************************************************************/

struct config *
find_config (struct config *table, const char *text)
{
  for (; strlen (table->keyword); table++)
    if (!mystrncasecmp (text, table->keyword, strlen (table->keyword)))
      return table;

  return NULL;
}
