Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed


#include <stdio.h>
#include <string.h>
#include "hconf.h"

static char buffer[1024];
static char name[64];
static char value[64];

static int read_h(char *filename)
{
  FILE *fin;
  struct variable *var;
  int res;

  fin=fopen(filename,"rt");
  if (fin==NULL) return -1;

  while (fgets(buffer,sizeof(buffer),fin)!=NULL) {
    res=sscanf(buffer,"#define %s %s",name,value);
    if (res==2) {
      var=findvar(name);
      if (var==NULL) continue;
      switch (var->type) {
      case RANGEVAR:
        copystring(&var->value,value);
        continue;
      case BOOLVAR:
        copystring(&var->value,"y");
        continue;
      case TRISTATEVAR:
        if (*value=='0') copystring(&var->value,"m");
        else copystring(&var->value,"y");
        continue;
      case CHOICEVAR:
        copystring(&var->value,"y");
        continue;
      }
    }
    res=sscanf(buffer,"#undef %s",name);
    if (res==1) {
      var=findvar(name);
      if (var==NULL) continue;
      copystring(&var->value,"n");
      continue;
    }
    res=sscanf(buffer,"/* var %s %s */",name,value);
    if (res==2) {
      var=findvar(name);
      if (var==NULL) continue;
      copystring(&var->value,value);
      continue;
    }
  }

  fclose(fin);
  return 0;
}

/* -- */

int readconfig(char *srcfilename)
{
  char buffer[256];
  char *ptr;

  ptr=strstr(srcfilename,".in");  
  if (ptr!=NULL) *ptr='\0';
  else {
    ptr=strchr(srcfilename,'.');
    if (ptr!=NULL) *ptr='\0';
  }

  strcpy(buffer,srcfilename);
  strcat(buffer,".h");
  read_h(buffer);

  return 0;
}