/*===========================================================================
 *
 * File:	Gencfg.H
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	Thursday, December 07, 2000
 *
 * Defines the CConfigFile class which allows simple operations on text
 * config files.
 *
 *=========================================================================*/
#ifndef __GENCFG_H
#define __GENCFG_H
 
	/* Required Includes */
#include "genutil.h"
#include "variant.h"


/*===========================================================================
 *
 * Begin Defines
 *
 *=========================================================================*/

	/* Maximum number of values allowed per variable */
  #define MAX_CFG_VALUES 10

	/* Number of variables allowed to be defined in a config file */
  #define MAX_CFG_VARS 100

	/* Used as a return value to indicate an error */
  #define NULL_VAR_INDEX (-1)

	/* Maximum length of config lines */
  #define CFG_LINE_SIZE 1025

/*===========================================================================
 *		End of Defines
 *=========================================================================*/


/*===========================================================================
 *
 * Begin Type Definitions
 *
 *=========================================================================*/

/*===========================================================================
 *		End of Type Definitions
 *=========================================================================*/


/*===========================================================================
 *
 * Class CConfigRecord Definition
 *
 * Defines one object record in the config file.
 *
 *=========================================================================*/
class CConfigRecord {

  /*---------- Begin Protected Class Members -----------------------*/
protected:
  char*     pName;			/* The parameter name */
  CVariant* pValues[MAX_CFG_VALUES];	/* The parameter data */
  int	    NumValues;


  /*---------- Begin Public Class Methods ---------------------------*/
public:

	/* Class Constructor/Destructor */
  CConfigRecord (void);
  virtual ~CConfigRecord (void) { Destroy(); }
  virtual void Destroy (void);

	/* Adds a new parameter to the variable record */
  boolean AddValue (const char* pString);

	/* Get a specific value */
  CVariant* GetValue (const int Index) { if (IsValidIndex(Index)) return (pValues[Index]); return (NULL); }
  char*     GetName      (void) { return (pName); }
  int	    GetNumValues (void) { return (NumValues); }

	/* Check for a valid record index */
  IMPLEMENT_ISVALIDINDEX(NumValues);

	/* Compares the given string and the record name */
  boolean MatchName (const char* pString, const boolean CaseSensitive = FALSE);

	/* Change the variable name */
  IMPLEMENT_SETSTRING1(Name);

	/* Sets the given parameter to the appropiate value */
  boolean SetValue (const int Index, const char* pString);

 };
/*===========================================================================
 *		End of Class CConfigRecord
 *=========================================================================*/


/*===========================================================================
 *
 * Class CConfigFile Definition
 *
 * Main class for handling general config files.
 *
 *=========================================================================*/
class CConfigFile {

  /*---------- Begin Protected Class Members -----------------------*/
protected:
  char*		 pFilename;			/* The current config filename */

  CConfigRecord* pVariables[MAX_CFG_VARS];	/* The parameter data */
  int		 NumVariables;

  long		 LineCount;			/* Used when parsing the config file */

  char		 VariableSepChar;		/* Variable value seperator character */
  char		 ValueSepChar;			/* Value-Value seperator char */
  char		 CommentSepChar;		/* Comment character */


  /*---------- Begin Protected Class Methods -----------------------*/
protected:

	/* Input helper routines */
  boolean ParseLine     (char*   pString);
  boolean ParseVariable (char** ppString);
  boolean ParseValue    (char** ppString);

	/* Parse a record from the config file */
  boolean ParseCFGRecord (CConfigRecord* pCFGRecord);


  /*---------- Begin Public Class Methods --------------------------*/
public:
	/* Class Constructor/Destructor */
  CConfigFile (void);
  virtual ~CConfigFile (void) { DestroyPointerArray(pFilename); Destroy(); }
  virtual void Destroy (void);

	/* Adds a variable definition to array */
  int AddVariable (const char* pName);

	/* Looks for the specified variable name */
  int FindVariable (const char* pString);

	/* Get class members */
  int  GetNumVariables    (void) { return (NumVariables); }
  char GetCommentSepChar  (void) { return (CommentSepChar); }
  char GetVariableSepChar (void) { return (VariableSepChar); }
  char GetValueSepChar    (void) { return (ValueSepChar); }

	/* Access a variable record */
  CConfigRecord* GetVariable (const int Index) { if (IsValidIndex(Index)) return (pVariables[Index]); return (NULL); }

	/* Check the validity of a variable index */
  IMPLEMENT_ISVALIDINDEX(NumVariables);

	/* Attempts to load and parse the specified config file */
  boolean Load (const char* pFilename = NULL);

	/* Set class members */
  void SetCommentSepChar  (const char Ch) { CommentSepChar = Ch; }
  void SetVariableSepChar (const char Ch) { VariableSepChar = Ch; }
  void SetValueSepChar    (const char Ch) { ValueSepChar = Ch; }
  IMPLEMENT_SETSTRING1(Filename);

 };
/*===========================================================================
 *		End of Class CConfigFile Definition
 *=========================================================================*/


#endif
/*===========================================================================
 *		End of File GenCFG.H
 *=========================================================================*/