#ifndef AVIFILE_CONFIGFILE_H
#define AVIFILE_CONFIGFILE_H

#include <default.h>

#ifndef WIN32
#include <pwd.h>
#endif
#include <sys/types.h>

#include <string>
#include <vector>

struct ConfigEntry
{
    enum Types
    {
	Int,
	Float,
	Binary
    };
    std::string appname;
    std::string valname;
    int length;
    Types type;
    union
    {
	int i;
	float f;
	char *b;
    };

    ConfigEntry();
    ~ConfigEntry();
    ConfigEntry(const ConfigEntry& e);
    ConfigEntry& operator= (const ConfigEntry& e);
};

struct ConfigFile {
    std::string filename;
    std::vector<ConfigEntry> entries;
    bool dirty;
    bool opened;

    ConfigFile();
    ~ConfigFile();
    void Open(const std::string& fn);
    void Close();
    ConfigEntry *Find(const std::string& appname, const std::string& valname);
    void push_back(const ConfigEntry& e);
};

/**
 * This namespace provides means to conveniently store and retrieve app-specific
 * data from the registry ( file ~/.registry ).
 *
 * Function names are self-explanatory. Do not include spaces in first two
 * arguments to avoid collisions. 'def_value' arguments correspond to
 * values which will be returned if needed record is not found in the
 * registry or if registry access fails for some reason.
 *
 * You can use this namespace without avifile library, but you should
 * define Registry::sConfigName & config in some place where linker will
 * find it.
 */

#define REGISTRY_BEGIN_NAMESPACE    namespace Registry {
#define REGISTRY_END_NAMESPACE      };

/* declaration */

REGISTRY_BEGIN_NAMESPACE

extern std::string sConfigName;
extern ConfigFile config;

int WriteInt(std::string appname, std::string valname, int value);
int ReadInt(std::string appname, std::string valname, int def_value);
int WriteFloat(std::string appname, std::string valname, float value);
float ReadFloat(std::string appname, std::string valname, float def_value);
int WriteString(std::string appname, std::string valname, std::string value);
std::string ReadString(std::string appname, std::string valname, std::string def_value);
int WriteData(std::string appname, std::string valname, const void *data, int size);
int ReadData(std::string appname, std::string valname, void *data, int size);

REGISTRY_END_NAMESPACE

#endif
