/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#ifndef DDCONFIG_H
#define DDCONFIG_H

#include <ddbaseobject.h>
#include <ddstring.h>
#include <set>
#include <sstream>
#include <ddstack.h>
#include <ddstringrow.h>
#include <ddtoken.h>
#include <ddmaprow.h>


class ddconfig;
class ddconfigoptions;
class ddconfigsection;


/// ddconfigoptions: Verwaltet kommanozeilenparameter
/**

*/

class ddconfigoptions : public ddstringmap
{

public:

	ddconfigoptions();
	virtual ~ddconfigoptions();

	void create( int argc, char** argv );
	void sync( ddconfig& config, const ddstring& sectionname );
	void add_option( const ddstring& key, const ddstring& skey, const ddstring& lkey, const ddstring& defval="" );
  void set_option( const ddstring& optionkey, const ddstring& value );

  void dump() const;

protected:

	ddstringmap	_aliases;   

};


/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/// ddparseentry is a simple container for parsed script atoms and (optional)
/// parameters.
/**
	Entries are compiled into ddopcodes by ddcompiler.

	@see ddtoken
	@see ddopcode
	@see ddparser
	@see ddcompiler
 */

class ddparseentry
{

public:

	/**
		Default constructor.
	*/

	ddparseentry()
	:	token(0), param(0)
	{

	}

	/**
		Default constructor.
	*/

	ddparseentry( size_t atoken, size_t aline )
	:	token(atoken), line(aline), param(0)
	{
	}

	/**
		Default constructor.
	*/

	ddparseentry( size_t atoken, size_t aline, const ddstring& avalue, size_t aparam=0 )
	:	token(atoken), line(aline), value(avalue), param(aparam)
	{
	}

	/**
		Dump this entry for debugging purpose.
	*/

	void dump() const
	{
	}

	size_t 		token;
	size_t 		line;
	ddstring	value;
	size_t 		param;

};


/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/// ddconfigparser : etwas missglückter parser
/**

*/

class ddconfigparser : public ddbaseobject
{

public: 

	ddconfigparser();
	virtual ~ddconfigparser();

	virtual bool parse_config( ddconfig* target, const ddstring& filename, const ddstring& base="" );

protected:

	bool parse_section();
	void parse_entry();

	void create_error( const ddstring& msg, size_t line );
	void create_error( const ddstring& msg );
	void read_file( ddstring& target, const ddstring& filename );
	void include( ddstring& target, size_t from, size_t count, const ddstring& filename );

	void scan_includes( ddstring& target, const ddstring& filename );

	char get_char();
	size_t get_token();
	size_t peek_token();

	size_t scan_string_literal();
	size_t scan_numeric_literal();
	size_t scan_boolean_literal();
	size_t scan_operator();
	size_t scan_identifier();

	bool scan_token( size_t token, bool create_error=true );
	void scan_blank();

	void create_entry( size_t type );

	size_t						   	_curtok;
	bool							   	_peek;

	size_t						    _lineno;
	
	ddstring						  _txt, _tmp;
	ddstack<ddparseentry>	_entrystack;

	set<string>					 	_sources;
	ddstringrow					 	_src;
	ddmapindex					 	_tokenmap;

	istringstream				 	_input;
  char							   	_currentchar;
    
private:

	ddconfigparser( const ddconfigparser& src );
	ddconfigparser& operator=( const ddconfigparser& src );
	
	ddconfig*	         _target;
	ddconfigsection*   _current;
	
};








/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/// ddconfigparser_neu : etwas missglückter parser
/**

*/

class ddconfigparser_neu : public ddbaseobject
{

public: 

	ddconfigparser_neu();
	virtual ~ddconfigparser_neu();

	virtual bool parse_config( ddconfig* target, const ddstring& filename, const ddstring& base="" );

protected:

	bool parse_section();
	void parse_entry();

	void create_error( const ddstring& msg, size_t line );
	void create_error( const ddstring& msg );
	void read_file( ddstring& target, const ddstring& filename );
	void include( ddstring& target, size_t from, size_t count, const ddstring& filename );

	void scan_includes( ddstring& target, const ddstring& filename );

	char get_char();
	size_t get_token();
	size_t peek_token();

	size_t scan_string_literal();
	size_t scan_numeric_literal();
	size_t scan_boolean_literal();
	size_t scan_operator();
	size_t scan_identifier();

	bool scan_token( size_t token, bool create_error=true );
	void scan_blank();

	void create_entry( size_t type );

	size_t						   	_curtok;
	bool							   	_peek;

	size_t						    _lineno;
	
	ddstring						  _txt, _tmp;
	ddstack<ddparseentry>	_entrystack;

	set<string>					 	_sources;
	ddstringrow					 	_src;
	ddmapindex					 	_tokenmap;

	istringstream				 	_input;
  char							   	_currentchar;
    
private:

	ddconfigparser_neu( const ddconfigparser_neu& src );
	ddconfigparser_neu& operator=( const ddconfigparser_neu& src );
	
	ddconfig*	         _target;
	ddconfigsection*   _current;
	
};






/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/// ddconfigsection: Eine benannte ddmaprow welche Config-Daten enthält.
/**
	
	ddconfigsection: Eine benannte ddmaprow welche
	Config-Daten enthält: 
	
	Wie in der guten alten Zeit:
	
	section:"main"
	{
		max_connections=1200;
		port=8088;
		default_domain="sn_computer";
		default_templates="sn_templates";
		daemon=false;
		watchdog=false;
		timeout=60;
		connectstring="users;localhost;dope;moomoo";
		user="user";
		chroot=".";
	}

	FIX! Der Parser stinkt!
	FIX! Sections sollten erben können!
	
*/

class ddconfigsection : public ddmaprow<ddstring>, public ddbasename
{

public:

	ddconfigsection();
	ddconfigsection( const ddstring& type, const ddstring& name );
	ddconfigsection( const ddconfigsection& src );
	ddconfigsection& operator=( const ddconfigsection& src );
	virtual ~ddconfigsection();
	
	virtual ddstring& get_entry( const ddstring& key, const ddstring& defaultvalue="" );
	virtual bool 			get_bool( const ddstring& key, bool defaultvalue );
	virtual int 			get_int( const ddstring& key, int defaultvalue );
	virtual double 		get_double( const ddstring& key, double defaultvalue );

	virtual void dump() const;

	const ddstring& get_type();
	
protected:

	ddstring _type;
	
};


/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/// A ddconfig represents the content of a configuration file.
/**

*/

class ddconfig : public ddmaprow<ddconfigsection>, public ddbasename
{

public:

	ddconfig();
	virtual ~ddconfig();

	virtual bool load( const ddstring& filename, const ddstring& base="" );
	virtual ddconfigsection* add_section( const ddstring& type, const ddstring& name );
	virtual void dump() const;
	
protected:

	ddconfigparser _configparser;
	
};


#endif
