//*****************************************************************************
//
// "$RCSfile: Config.h,v $"
//
// (c) 1999 - 2013 IMAGO Technologies GmbH
//*****************************************************************************
// Information by CVS or VSS:
//     Last check in on: $Date: 2013-07-09 08:23:53 $
//     by                $Author: schluckebier $
//     Revision        : $Revision: 1.21 $
//
// CVS: $Source: /Daten/Projekte/CVS/Lib/VIB_Lib/include/Config.h,v $
// VSS: $Archive:$
//
// $NoKeywords: $ expanding by VSS -->
//
// CVS: $Revision: 1.21 $
//
//*****************************************************************************
//! \file Config.h
//! \brief Header for \ref CONFIG
//! \author (c) 1999 - 2013 IMAGO Technologies GmbH
//!
//! $Revision: 1.21 $
//! $Date: 2013-07-09 08:23:53 $
//! \addtogroup CONFIG Configuration Class
//! \{
//*****************************************************************************

#ifndef _CONFIG_H_
#define _CONFIG_H_

#include <limits.h>
#include <float.h>

#ifdef __cplusplus

//! Configuration value type
enum eTYPE
{
	TypeInvalid,		//!< Invalid type
	TypeInteger,		//!< Integer type
	TypeString,			//!< String type
	TypeDouble,			//!< Double type
	TypeCommentLine		//!< Line including only comment
};

//! Configuration parameter class
class ConfigParam
{
public:
	//! Configuration value
	union ConfigValue
	{
		int    Int;		//!< Integer value
		double Double;	//!< Double value
		char  *String;	//!< String value
	};

	ConfigParam(const char *pName);
	~ConfigParam();
	ConfigParam &operator=(const ConfigParam &Source);

	eTYPE type;				//!< Type of configuration parameter
	ConfigValue value;		//!< Value of configuration parameter
	ConfigValue min;		//!< Maximum allowed value of configuration parameter
	ConfigValue max;		//!< Minimum allowed value of configuration parameter
	unsigned int maxS;		//!< Maximum string length of string value
	bool hasLimits;			//!< Flag indicating whether parameter has limitations (minimum/maximum)
	char *name;				//!< Name of configuration parameter
	char *comment;			//!< Comment
	ConfigParam *next;		//!< Next configuration parameter
};

//! Configuration class
class Config
{
public:
	Config(const char *IniKeyWord);
	~Config();
	Config& operator+=(const Config& Source);
	Config& operator=(const Config& Source);

	ConfigParam *getFirstParam() const;

	int    getValue(const char *pName, int         DefaultValue, int    Min=INT_MIN,  int    Max=INT_MAX);
	float  getValue(const char *pName, float       DefaultValue, float  Min=-FLT_MAX, float  Max=FLT_MAX);
	double getValue(const char *pName, double      DefaultValue, double Min=-DBL_MAX, double Max=DBL_MAX);
	char   *getValue(const char *pName, const char *DefaultValue, unsigned int MaxLength=0xffffffff);

	int    *getPointer(const char *pName, int    DefaultValue, int    Min=INT_MIN,  int    Max=INT_MAX);
	double *getPointer(const char *pName, double DefaultValue, double Min=-DBL_MAX, double Max=DBL_MAX);

	bool getLimits(const char *pName, int    *pMin, int    *pMax);
	bool getLimits(const char *pName, float  *pMin, float  *pMax);
	bool getLimits(const char *pName, double *pMin, double *pMax );
	bool getLimits(const char *pName, int    *pMaxLength);

	int    setValue(const char *pName, int         Value);
	float  setValue(const char *pName, float       Value);
	double setValue(const char *pName, double      Value);
	char  *setValue(const char *pName, const char *Value);
	char  *setComment(const char *pName, const char *Value);

	int    serialize(char **ppData, int *pSize);
	static Config *deserialize(const char *pData, int Size, const char *pIniKeyWord);
	int    saveToIni(int Device, const char *pFilename=NULL);
	static Config *loadFromIni(int Device, const char *pName, const char *pIniKeyWord);

	const char *getKeyWord() const;

protected:
	ConfigParam *getParam(const char *pName);
	char *findComment(char *pText);

	ConfigParam *m_pFirstParam;	//!< First configuration parameter
	const char *m_pIniKeyWord;	//!< 'Magic word'
	char *m_pFilename;			//!< Name of .INI file
	int m_CommentCount;			//!< Number of comments
};

#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif

void  *newConfig(const char *pIniKeyWord);
void   deleteConfig(void *pConfig);
int    getIntValue(void *pConfig, const char *pName, int DefaultValue);
float  getFloatValue(void *pConfig, const char *pName, float DefaultValue);
double getDoubleValue(void *pConfig, const char *pName, double DefaultValue);
char  *getStringValue(void *pConfig, const char *pName, const char *pDefaultValue);
int    setIntValue(void *pConfig, const char *pName, int Value);
float  setFloatValue(void *pConfig, const char *pName, float Value);
double setDoubleValue(void *pConfig, const char *pName, double Value);
char  *setStringValue(void *pConfig, const char *pName, const char *pValue);
char  *setComment(void *pConfig, const char *pName, const char *Value);
int    serializeConfig(void *pConfig, char **ppData, int *pSize);
void  *deserializeConfig(const char *pData, int Size, const char *pIniKeyWord);
void  *loadConfigFromIni(int Device, const char *pFilename, const char *pIniKeyWord);
int    saveConfigToIni(int Device, void *pConfig, const char *pFilename);

#ifdef __cplusplus
}
#endif

#endif // _CONFIG_H_
//! \}
/*****************************************************************************/
