77Fermer79
Kevin KoflerLe 16/11/2004 à 03:18
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;
}