// CONFIG.C - Handling of text-config-files
// Written by Stefan Pfeiffer, DL1ELY, 940727
// Last modification: 940913
// (C) 1994 by Stefan Pfeiffer, DL1ELY

#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "secmem.h"
#include "config.h"


char *cfg_line[256];
unsigned char cfg_count;

unsigned char read_config(char *name)
{
  FILE *cfg;
  char s[256];
  char s2[256];
  char *cp, *cp2;
  char is_string;
  
  cfg = fopen(name,"rt");
  if(!cfg) return(0);
  
  cfg_count = 0;
  while(!feof(cfg))
  {
    is_string = 0;
    fgets(s,256,cfg);
    cp = s;
    
    while(*cp == ' ') cp++;   // Spaces am Anfang überspringen
    if((*cp != ';')&&(*cp!='\n')) // Keine Leerzeile und kein Kommentar?
    {
      cp2 = s2;
      while(*cp!='\n')
      {
        switch(*cp)
        {
          case ' ':           // Space filtern, falls nicht in einem String
            switch(is_string)
            {
              case 3:
                is_string = 0;
              case 0:
                break;
              case 2:
                is_string = 1;
              case 1:
                *cp2++ = ' ';
                break;
            };        
            break;
          case '"':          // " extra behandeln: "" -> "
            switch(is_string)
            {
              case 0:
                is_string = 2;
                break;
              case 1:
                is_string = 3;
                break;
              case 2:
                is_string = 0;
                *cp2++ = '"';
                break;
              case 3:
                is_string = 1;
                *cp2++ = '"';
                break;
            };
            break;
          default:
            switch(is_string)
            {
              case 0:
              case 1:
                *cp2++ = *cp;
                break;
              case 2:
                is_string = 1;
                *cp2++ = *cp;
                break;
              case 3:
                is_string = 0;
                *cp2++ = *cp;
                break;
            };
        };
        cp++;                        
      };
      *cp2 = 0;
      cp = s2;
      while(*cp!='=' && *cp)   // Schlüsselwort groß machen
      {
        *cp = toupper(*cp);
        cp++;
      };
      cfg_line[cfg_count] = sec_alloc(strlen(s2)+1);
      strcpy(cfg_line[cfg_count],s2);
      cfg_count++;
    };
  };
  fclose(cfg);
  return(cfg_count);
};

void free_config()
{
  while(cfg_count)
  {
    sec_free(cfg_line[cfg_count-1]);
    cfg_count--;
  };
};

char *get_str(char *s)
{
  int i;
  int slen;
  char *cp;
  char buf[256];

  strcpy(buf,s);
  strupr(buf);
  strcat(buf,"=");
  slen = strlen(buf);
  
  for(i=0;i<cfg_count;i++)
  {
    if(!strncmp(buf,cfg_line[i],slen))
    {
      strcpy(buf,cfg_line[i]);
      cp = strchr(buf,'=');
      cp++;
      return(cp);
    };
  };
  return(NULL);
};    

int get_bool(char *s, int def)
{
  s = get_str(s);
  
  if(!s) return(def);
  
  if(!strcmp(s,"0")) return(0);
  if(!stricmp(s,"AUS")) return(0);
  if(!stricmp(s,"OFF")) return(0);
  if(!stricmp(s,"NO")) return(0);
  if(!stricmp(s,"NEIN")) return(0);
  
  if(!strcmp(s,"1")) return(1);
  if(!stricmp(s,"AN")) return(1);
  if(!stricmp(s,"ON")) return(1);
  if(!stricmp(s,"YES")) return(1);
  if(!stricmp(s,"JA")) return(1);
  
  return(def);
};

unsigned int get_int(char *s, unsigned int def)
{
  char *cp;
  unsigned int val;
  unsigned char str[80];
  
  s = get_str(s);
  strcpy(str,s);
  
  if(!str) return(def);
  
  cp = str;
  while(*cp&&isdigit(*cp)) cp++;
  if(!*cp) return(atoi(str));
  
  strupr(str);
  cp = str;
  if(*cp == '$') cp++;
  else if((*cp == '0') && (*(cp+1) == 'X')) cp+=2;
       else return(def);
  val = 0;
  
  while(*cp&&isxdigit(*cp))
  {
    val<<=4;
    if(*cp >= '0' && *cp <= '9') val+=(*cp)-'0';
    else val+=(*cp)-'A'+10;
    cp++;
  }
  return(val);  
};    

unsigned char get_color(char *s, unsigned char def)
{
  unsigned char result;
  unsigned char i;
  char *cp;
  
  char *colnames[16] =
  {
    "BLACK","BLUE","GREEN","CYAN","RED","MAGENTA","BROWN","LIGHTGRAY",
    "DARKGRAY","LIGHTBLUE","LIGHTGREEN","LIGHTCYAN","LIGHTRED","LIGHTMAGENTA","YELLOW","WHITE"
  };
  
  s = get_str(s);
  
  result = atoi(s);
  if(result) return(result);
  
  cp = s;
  while(*cp!=','&&*cp) cp++;
  *cp = 0;
  cp++;
  
  for(i=0;i<16;i++) if(!stricmp(colnames[i],s)) result = i;
  for(i=0;i<16;i++) if(!stricmp(colnames[i],cp)) result |= i<<4;
  if(result) return(result);
  
  return(def);
};  
              
