/**
 * \file parameters.c
 * \brief This file provides the parameter list functionality.
 *
 * \date Apr 22, 2009
 * \author Alexander Pimenov
 */
#include "properties.h"
#include "global.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

PropertyList *createList()
{
	PropertyList *params = (PropertyList *) dv_malloc(sizeof(PropertyList));
	params->filename = "";
	params->parameterNum = 0;
	params->nextParam = NULL;
	return params;
}


PropertyList *readFromFile(char *fileName)
{
	PropertyList *params = (PropertyList *) dv_malloc(sizeof(PropertyList));
	params->filename = strdup(fileName);
	params->parameterNum = 0;
	params->nextParam = NULL;
	char buffer[200];
	FILE *F = fopen(fileName, "rt");
	if (F == NULL)
		return params;

	Property **param = &(params->nextParam);
	int count = 0;
	while (fgets(buffer, sizeof(buffer), F))
	{
		char value[200];
		char name[200];

		if (strlen(buffer) < 0 || buffer[0] == '#')
			continue;
		int result = sscanf(buffer," %s = %s ", name, value);
		if (result != 2)
			continue;
		*param = (Property *) dv_malloc(sizeof(Property));
		(*param)->name = strdup(name);
		(*param)->value = strdup(value);
		(*param)->nextParam = NULL;
		param = &((*param)->nextParam);
		count ++;
	}
	params->parameterNum = count;
	fclose(F);
	return params;
}

static void serializeParams (PropertyList *p, FILE *F)
{
	Property *param = p->nextParam;

	fprintf(F,"#There are %d parameters\n", p->parameterNum);

	while (param != NULL)
	{
		fprintf(F,"%s = %s\n", param->name, param->value);
		param = param->nextParam;
	}
}

void storeToFile(PropertyList *p, char *fileName)
{
	FILE *F = fopen(fileName, "wt");
	if (F == NULL)
		return;
	serializeParams(p, F);
	fclose(F);
}

void printParams (PropertyList *p)
{
	serializeParams(p, stdout);
}

char* getStringParameter(PropertyList *p, char *name)
{
	Property *par = p->nextParam;
	while (par != NULL)
	{
		if (strcmp(par->name, name) == 0)
		{
			return par->value;
		}
		par = par->nextParam;
	}
	return NULL;
}

int getIntParameter(PropertyList *p, char *name, int def)
{
	int val;
	char *string = getStringParameter(p,name);
	if (string == NULL)
		return def;

	if (sscanf(string, "%d", &val) == 1)
		return val;

	return def;
}

double getDoubleParameter(PropertyList *p, char *name, double def)
{
	double val;
	char *string = getStringParameter(p,name);
	if (string == NULL)
		return def;

	if (sscanf(string, "%lf", &val) == 1)
		return val;

	return def;
}

void setStringParameter(PropertyList *p, char *name, char *value)
{
	Property *par = p->nextParam;
	while (par != NULL)
	{
		if (strcmp(par->name, name) == 0)
		{
			if (par->value != NULL)
				free(par->value);
			par->value = strdup(value);
			return;
		}
		par = par->nextParam;
	}

	Property *param = (Property *)dv_malloc(sizeof(Property));
	param->nextParam = p->nextParam;
	p->nextParam = param;
	param->name = strdup(name);
	param->value = strdup(value);
	p->parameterNum++;
}



static void deleteParameter(Property *p)
{
	if (p != NULL && p->nextParam != NULL)
		deleteParameter(p->nextParam);
	if (p != NULL)
		dv_free(p);
}

void deleteParameters (PropertyList *p)
{
	deleteParameter(p->nextParam);
	p->parameterNum = 0;
	if (p != NULL)
		dv_free(p);
}
