#include <Threading.h>

extern struct ConfigArray{
        char keypath[1024];
        char keydata1[512];
	char keydata2[512];
	char keydata3[512];
	char keydata4[512];
	char keydata5[512];
        int count;
} ConfigDB[512];

void ParseLine(char *line);

extern long keycount;

char path[512];
char value1[512];
char value2[512];  
char value3[512];
char value4[512];
char value5[512];

int ConfigLoad(char *configfile)
{
int c=0;
char heading[255];
int headingc=0;
char value[512];
int valuec=0;
char	report[2048];

FILE *in;

if ( ( in = fopen(configfile,"r")) == NULL )
   {
   /* Problem opening the config file */
   return -1;
   }

c = fgetc(in);
while ( 1 )
   {
   if ( c == '[' )
      {
      headingc=0;
      /* Get heading */
      while ( 1 )
	{
	c = fgetc(in);
	if ( c != ']' )
	   {
	   heading[headingc] = c;
	   headingc++;
	   }
        if ( c == ']' )
           {
           heading[headingc] = NULL;
           break;
           }
	}
      /* heading got, now read the vars */
      while ( 1 )
            {
	    c = fgetc(in);
            if (( c != '[' ) && ( c != 0x0a ) && ( c != EOF ) && ( c != '#' ) && ( c != ' '))
               {
 	       valuec=0;
               /* Line is a config line under a heading */
               while( 1 )
                  {
		  value[valuec] = c;
		  valuec++;
		  c = fgetc(in);
		  if (( c == 0x0a ) || ( c == EOF ))
		     {
		     value[valuec] = NULL;
		     break;
		     }
		  }
	       ParseLine(value);
               /* Add to config Database */
               sprintf(report,"%s.%s",heading,path);
               strcpy(ConfigDB[keycount].keypath,report);
               strcpy(ConfigDB[keycount].keydata1,value1);
               strcpy(ConfigDB[keycount].keydata2,value2);
               strcpy(ConfigDB[keycount].keydata3,value3);
               strcpy(ConfigDB[keycount].keydata4,value4);
//               sprintf(report,"%s.%s = [%s] [%s] [%s] [%s]",heading,path,value1,value2,value3,value4);
//               Error(0,report);
               keycount++;
               }
            if ( c == '[' )
               {
 	       break;
               }
	    if ( c == '#' )
               {
 	       /* Comment line, ignore it */
               while ( 1 )
                  {
		  c = fgetc(in);
		  if (( c == 0x0a ) || ( c == EOF ))
		     break;
 		  }
               }
            if ( c == EOF )
               break;
	    }
      }

   /* 
      Check to see if the end of the file has been reached
      if it has, the config file has been read in, end.
   */
   if ( feof(in) != 0 )
      break;
   }

fclose(in);
sprintf(report,"Config File locked and cocked.  %ld keys loaded.",keycount);
Error(10,report);
return 0;
}

void ParseLine(char *line)
{
int pathc=0,valuec=0,pos=0;	/* Counters for the string possitions */
int breaks=0; 		/* Amount of whitespace (breaks) hit */
int quote=0;		/* Weather or not we're in the middle of a quoted block */

/* Reset everything */
value1[0] = NULL;
value2[0] = NULL; 
value3[0] = NULL; 
value4[0] = NULL; 
path[0] = NULL; 

pos = 0;
while ( 1 )
   {
   if ( line[pos] == NULL )
      {
      if ( breaks == 0 )
         path[pathc] = NULL;
      if ( breaks == 1 )
         value1[valuec] = NULL;
      if ( breaks == 2 )
         value2[valuec] = NULL;
      if ( breaks == 3 )
         value3[valuec] = NULL;
      if ( breaks == 4 ) 
         value4[valuec] = NULL;
      break;
      }
   if (( line[pos] == '#' ) && ( quote == 0 ))
      {
      /* Rest if line is a comment */
      break;
      }
   if ( line[pos] == '"' )
      {
      /* Start or end "" encased text */
      while ( 1 )
         {
         if ( quote == 0 )
            {
            quote=1;
	    //printf("%c[%c%c",line[pos-1],line[pos+1],line[pos+2]);
            break;
            }
         if ( quote == 1 )
            {
            //printf("%c%c]%c",line[pos-2],line[pos-1],line[pos+1]);
            quote=0;
            break;
            }
         Error(0,"Config Error, Quotes Value Fuct line 148 configload.c");
         break;
         }
      //pos++;
      }
   if ( (( line[pos] == ' ' ) || ( line[pos] == 0x09 )) && ( quote == 0 ))
      {
      /* hit dead space, inc the break count, and zing to the next text */
      if ( breaks == 0 )
         path[pathc] = NULL;
      if ( breaks == 1 )
         value1[valuec] = NULL;
      if ( breaks == 2 )
         value2[valuec] = NULL;
      if ( breaks == 3 )
         value3[valuec] = NULL;
      if ( breaks == 4 )
         value4[valuec] = NULL;
      while ( 1 )
         {
         pos++;
         if (( line[pos] != ' ' ) && ( line[pos] != 0x09 ))
            break;
         }
      valuec=0;
      breaks++;
      }
   else
      {
      if ( breaks == 0 )
         {
         /* still reading in the variable name */
         if ( quote == 0 )
            {  
            if (( line[pos] != 0x09 ) && ( line[pos] != ' ' ))
               {
               path[pathc] = line[pos];
               pathc++;
               }
            }
         if ( quote == 1 )
            {
            path[pathc] = line[pos];
            pathc++;
            }         }
      if ( breaks == 1 )
         {
         /* reading in options and the values */
         if ( quote == 0 )
            {  
            if (( line[pos] != 0x09 ) && ( line[pos] != ' ' ))
               {
               value1[valuec] = line[pos];
               valuec++;
               }
            }
         if ( quote == 1 )
            {
            value1[valuec] = line[pos];
            valuec++;
            }         }
      if ( breaks == 2 )
         {
         /* reading in options and the values */
         if ( quote == 0 )
            {  
            if (( line[pos] != 0x09 ) && ( line[pos] != ' ' ))
               {
               value2[valuec] = line[pos];
               valuec++;
               }
            }
         if ( quote == 1 )
            {
            value2[valuec] = line[pos];
            valuec++;
            }
         }
      if ( breaks == 3 )   
         {   
         /* reading in options and the values */
         if ( quote == 0 )
            {
            if (( line[pos] != 0x09 ) && ( line[pos] != ' ' ))
               {
               value3[valuec] = line[pos];
               valuec++;
               }
            }
         if ( quote == 1 )
            {
            value3[valuec] = line[pos];
            valuec++;
            }
         }
      if ( breaks == 4 )
         {
         /* reading in options and the values */
         value4[valuec] = line[pos];
         valuec++;
         }
      pos++;
      }
   }

}
