/////////////////////////////////////////////////////////////////////////////
// Name:        variable substitution in script-templates
// Purpose:     wxBugTracker
// Author:      Aleksandras Gluchovas (mailto:alex@soften.ktu.lt)
// Modified by:
// Created:     20/10/1999
// RCS-ID:      $Id: scripttempl.h,v 1.5 2001/03/16 04:37:09 aleksandras Exp $
// Copyright:   (c) Aleksandras Gluchovas
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifndef __SCRIPTTEMPL_G__
#define __SCRIPTTEMPL_G__

#include "wx/hash.h"
#include "wx/dynarray.h"
#include "wx/string.h"
#include "myutils.h"

/* 
 * performs fast substitution of variables in
 * within given script
 */

class ScriptTemplate : public wxObject
{
public:
    wxString        mTempl;
	wxString		mName;

	static char		mVarPrefixChar; // default: '$'

	ScriptTemplate* mpParent;

    wxHashTable mHash; // name<->value

    wxList      mOfsList;

    void DeleteStrList( wxList& lst );

    void PreparseScript( const wxString& scriptTempl );

    enum EXPR_OPERATOR_TYPES
    {
        OP_EQUAL,
        OP_NOT_EQUAL,
		OP_INCLUDES,
		OP_EXCLUDES,
        OP_NOT_NULL
    };

    bool IsIfExpr( char* cur );
    bool ParseIfExpr( char*& cur, wxString& var, 
					  wxString& value, int& exprType, 
					  bool& isMultiValue, bool& isOrExpr );
    bool GetIfExprResult( char* cur );
    bool IsEndIf( char* cur );

public:
    ScriptTemplate( const wxString& scriptTempl );
    ScriptTemplate();

	void SetName( const wxString& name );
	const wxString& GetName();

    ~ScriptTemplate();

    void SetScript( const wxString& scriptTempl );

	const wxString& GetScript() { return mTempl; }

	void SetParentTemplate( ScriptTemplate* pParent );
	ScriptTemplate* GetTopLevelTemplate();

	char* FindValue( const char* key );
	ScriptTemplate* FindTemplateForValue( const char* key );

    // prepare for the next substitution pass

    void Reset();

    void SetVar( const wxString& name, const wxString& value );

    wxString SubstitueVariables();

	////////////////////////////////////////////////////////////////////////////////
	// hook for the derived classes												  //
	//																			  //
	// NOTE:: special substitution occure when $(variable) is found				  //
	//        which starts with '@' character, e.g. $(@my_special_tag[dfsdfs...]) //
	////////////////////////////////////////////////////////////////////////////////

	virtual wxString HandleSpecialSubstitution( char* at, ScriptTemplate& templ  );

	// static methods

	static void SetVarPrefixChar( char ch = '$' );
	static char GetVarPrefixChar();
};

WX_DEFINE_ARRAY( ScriptTemplate*, ScriptTemplateListT );

#endif	// __SCRIPTTEMPL_G__
