#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];
extern long keycount;

char ConfRet[1020];

char *GetConfigStr( char *key, int space )
{
long i=0;
//char report[1024];

while ( i < keycount )
   {
   if ( strcmp(ConfigDB[i].keypath,key) == 0 )
      {
//      sprintf(report,"Got %s",ConfigDB[i].keydata1);
//      Error(0,report);
      switch(space)
         { 
         case 0:
            strcpy(ConfRet,ConfigDB[i].keydata1);
            break;
         case 1:
            strcpy(ConfRet,ConfigDB[i].keydata2);
            break;
         case 2:
            strcpy(ConfRet,ConfigDB[i].keydata3);
            break;
         case 3:
            strcpy(ConfRet,ConfigDB[i].keydata4);
            break;
         }
      return ConfRet;
      }
   i++;
   }

/* Wasn't found */
strcpy(ConfRet,"");
return ConfRet;
}

int GetConfigInt( char *key, int space )
{
long i=0;
//char report[1024];

while ( i < keycount )
   {
   if ( strcmp(ConfigDB[i].keypath,key) == 0 )
      {
//      sprintf(report,"Got %s",ConfigDB[i].keydata1);
//      Error(0,report);
      switch(space)
         {
         case 0:
            return atoi(ConfigDB[i].keydata1);
         case 1:
            return atoi(ConfigDB[i].keydata2);
         case 2:
            return atoi(ConfigDB[i].keydata3);
         case 3:
            return atoi(ConfigDB[i].keydata4);
         }
      }
   i++;
   }

/* Wasn't found */ 
return -2000;
}

char *GetConfigList(char *key, int slot, char *lastvalue)
{
long i=0;   
//char report[1024];
char	dummy[2048];

if ( lastvalue[0] == 0 )
   {
   /* Blank lastvalue, start from begining */
   sprintf(dummy,"%s.",key);
   while ( i < keycount )
      {
      if ( ConfigDB[i].keypath[0] != 0 )
         {
         if ( strstr(ConfigDB[i].keypath,dummy) != 0 )
            {
            if ( slot == 0 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keypath));
            if ( slot == 1 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata1));
            if ( slot == 2 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata2));
            if ( slot == 3 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata3));
            if ( slot == 4 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata4));
            if ( slot == 5 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata5));
            return ConfRet;
            }
         }
      i++;
      }
   return "";
   }

/* otherwise, find the next entry */
sprintf(dummy,"%s.%s",key, lastvalue);

while ( i < keycount )
   {
   if ( strcmp(ConfigDB[i].keypath,dummy) == 0 )
      {
//      sprintf(report,"Got %s",ConfigDB[i].keypath);
//      Error(0,report);
      i++;
      if ( ConfigDB[i].keypath[0] != 0 )
         {
         if ( strstr(ConfigDB[i].keypath,key) != 0 )
            {
            /* Make sure the next entry after the lastvalue is still apart of the header */
            sprintf(dummy,"%s.",key);
            if ( slot == 0 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keypath));
            if ( slot == 1 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata1));
            if ( slot == 2 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata2));
            if ( slot == 3 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata3));
            if ( slot == 4 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata4));
            if ( slot == 5 )
               strcpy(ConfRet,StripHeading(dummy,ConfigDB[i].keydata5));
            return ConfRet;
            }
         }
      return "";
      }
   i++;
   }

/* Wasn't found */
return "";
}
