#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "configfile.h"

Vector<Option*> *readConfigFile(const char *path)
{
  FILE *f=fopen(path,"r");
  if (f==NULL)
    {
      perror(path);
      return NULL;
    }

  char buf[1024];
  int line=0;

  Vector<Option*> *v=new Vector<Option*>();
  while (fgets(buf, 1023, f)!=NULL)
    {
      line++;
      int len=strlen(buf);

      // strip the newline
      if (len>0)
	buf[--len]=0;

      // ignore comment lines
      if (len<1 || buf[0]=='#')
        continue;

      // look for first equal sign
      char *equals=strchr(buf,'=');
      if (equals==NULL)
        {
          printf("parse error in line %i\n",line);
          continue;
        }
      equals[0]=0;

      // chop the input string.
      char *key=buf;
      char *value=&equals[1];

      // get rid of any leading white space.
      /*
      while (isspace(*value))
	value++;
      len=strlen(value);
      while (len>0 && isspace(value[len-1]))
	len--;
      value[len]=0;
      */

      Option *opt=new Option(key, value, 0);
      //      printf("%s=>%s. \n", key, value);
      v->add(opt);
    }

  return v;
}
