#include "dfeed.h"

static char *i_val[1000];
static int i_true[1000];

int ini_getd(int x, int dflt)
{
	if (strlen(ini_get(x))==0) return dflt;

	return atoi(ini_get(x));
}
void ini_load(void)
{
	FILE *f;
	int i;
	char bf[BFSZ];
	char var[BFSZ];
	char *s;
	ini_free();
	f = fopen(file_dnews(),"r");
	if (f==NULL) {
		printf("Couldn't find %s %s\n",file_dnews(),strerror(errno));
		return;
	}
	for (;!feof(f);) {
		if (fgets(bf,BFSZ-1,f)==NULL)  break;
		s = strtok(bf," \r\n\t"); if (s==NULL) continue;
		ncpy(var,s,100);
		s = strtok(NULL," \r\n\t");
		for (i=0; ini_names[i]!=NULL; i++) {
			if (strcmp(ini_names[i],var)==0) {
				if (i_val[i]!=NULL) {
					imsg("Symbol defined twice %s\n",var);
				} else 	i_val[i] = strdup(s);
				printf("Symbol [%d] (%s) = (%s)\n",i,ini_names[i],i_val[i]);
				goto xxx;
			}
		}
		/* imsg("Unknown setting in vchat.ini (%s) \n",var);*/
xxx:		;
	}

	for (i=0; ini_names[i]!=NULL; i++) {
		i_true[i] = FALSE;
		if (i_val[i]!=NULL) {
			if (strcmp(i_val[i],"true")==0) i_true[i] = TRUE;
		}
	}
	fclose(f);

}
void ini_free(void)
{
	int i;
	for (i=0; ini_names[i]!=NULL; i++) {
		if (i_val[i]!=NULL) free(i_val[i]);
		i_val[i] = NULL;
	}
}
char *ini_get(int i)
{
	if (i_val[i]==NULL) {
		return "";
	}
	return i_val[i];
}
int ini_true(int x)
{
	return i_true[x];
}