7779Close
Kevin KoflerOn the 2004-11-16 at 03:18am
short conflib__get_string_key(const char *file, const char *section, const char *key, const char *value, long max_length, const char *defaut)
{
  FILE *f=fopen(file,"r");
  if (!f) return FAIL;
  char sechead[strlen(section)+3];
  sprintf(sechead,"[%s]",section);
  int r;
  do {
    r=fscanf(f,sechead);
    if (r>0) goto secfound; else if (!r) r=fscanf(f,"%*s");
  } while (r!=EOF);
 notfound:
  strncpy(value,defaut,max_length);
  value[max_length+1]=0;
  goto valfound;
 secfound:;
  char valhead[strlen(value)+11];
  sprintf(valhead,"%s = %%%ds",section,max_length);
  do {
    if (fscanf(f,"[%*s]")) goto notfound;
    r=fscanf(f,valhead);
    if (r>0) goto valfound; else if (!r) r=fscanf(f,"%*s");
  } while (r!=EOF);
  goto notfound;
 valfound:
  fclose(f);
  return SUCCESS;
}