#include <configfile.h>

/* definition */
REGISTRY_BEGIN_NAMESPACE

using namespace std;

ConfigFile config;
string sConfigName=".avirc";

template<class T> inline T mymin(T a, T b)
{
    return (a < b) ? a : b;
}

int ReadData(string appname, string valname, void* data, int size)
{
    config.Open(sConfigName);
    ConfigEntry* e=config.Find(appname, valname);

    if (e && e->type == ConfigEntry::Binary)
    {
	memcpy(data, e->b, mymin(size, e->length));
	return 0;
    }

    return -1;
}

int WriteInt(string appname, string valname, int value)
{
    config.Open(sConfigName);
    ConfigEntry* e=config.Find(appname, valname);
    if (e)
    {
	if (e->type==ConfigEntry::Binary)
	    delete e->b;
	e->type=ConfigEntry::Int;
	e->length=sizeof(int);
	e->i=value;
	config.dirty=true;
    }
    else
    {
	ConfigEntry en;
	en.appname=appname;
	en.valname=valname;
	en.length=sizeof(int);
	en.i=value;
	en.type=ConfigEntry::Int;
	config.push_back(en);
    }
    return 0;
}

int ReadInt(string appname, string valname, int def_value)
{
    config.Open(sConfigName);
    ConfigEntry* e=config.Find(appname, valname);
    if (e)
    {
	if (e->type != ConfigEntry::Int)
	    return -1;
	return e->i;
    }
    WriteInt(appname, valname, def_value);
    return def_value;
}

int WriteFloat(string appname, string valname, float value)
{
    config.Open(sConfigName);
    ConfigEntry* e=config.Find(appname, valname);
    if(e)
    {
	if(e->type==ConfigEntry::Binary)delete e->b;
	e->type=ConfigEntry::Float;
	e->length=sizeof(float);
	e->f=value;
	config.dirty=true;
	return 0;
    }
    ConfigEntry en;
    en.appname=appname;
    en.valname=valname;
    en.length=sizeof(float);
    en.f=value;
    en.type=ConfigEntry::Float;
    config.push_back(en);
    return 0;
}

float ReadFloat(string appname, string valname, float def_value)
{
    config.Open(sConfigName);
    ConfigEntry* e=config.Find(appname, valname);
    if (e)
    {
	if (e->type!=ConfigEntry::Float)
	    return -1;
	return e->f;
    }
    WriteFloat(appname, valname, def_value);
    return def_value;
}

int WriteString(string appname, string valname, string value)
{
    config.Open(sConfigName);
    ConfigEntry* e=config.Find(appname, valname);
    if (e)
    {
	if (e->type==ConfigEntry::Binary)
	    delete e->b;
	e->type=ConfigEntry::Binary;
	e->length=value.size()+1;
	e->b = new char[value.size()+1];
	strcpy(e->b, value.c_str());
	config.dirty=true;
	return 0;
    }
    ConfigEntry en;
    en.appname=appname;
    en.valname=valname;
    en.type=ConfigEntry::Binary;
    en.length=value.size()+1;
    en.b=new char[value.size()+1];
    strcpy(en.b, value.c_str());
    config.push_back(en);
    return 0;
}

string ReadString(string appname, string valname, string def_value)
{
    config.Open(sConfigName);
    ConfigEntry* e = config.Find(appname, valname);

    if (e)
    {
	if (e->type!=ConfigEntry::Binary)
	    return "";
	return string(e->b);
    }

    WriteString(appname, valname, def_value);
    return def_value;
}

REGISTRY_END_NAMESPACE
