#if !defined(_MSGTRANLATER_H)
#define _MSGTRANLATER_H

#include "../Include/MsgUtility.h"
#include "CPlusAnalyser.h"


/////////////////////////////////////////////////
//////////////////////////////////////
class TStruct;

#ifdef _DLL_PROJECT
class CLASS_EXPORT TVariableItem: public CVariableItem
#else
class TVariableItem: public CVariableItem
#endif
{
public:
    long m_beginLocat,m_len;
    bool m_bErr;

    TObArray<TVariableItem> m_IntegrateStructList;

public:
    TVariableItem();
    virtual ~TVariableItem();
    
    void DumpLocationMask(OTSTR& str);
    bool DumpBitsMask(OTSTR& str);
    bool DumpErrorFlag() { return m_bErr; }
    bool ValueValid();                  // 是否解析完整
    
    virtual bool isValueCommentValid();
    virtual int  GetValueComment(TObject* pTranslater,CVariableItem* pParentObject,int nArrIndex,OTSTR& strComment,char* szDefault=SHOW_WHEN_COMMENT_NOT_FOUND); // false表示未取得匹配的Comment ( or或者equal不匹配 , strComment可返回不匹配描述)
                                // return:   0 fail    1 succ   2 default(not found)
    
    bool ProcessDumpProc(int nArrIndex,ExecuteEnv* pRunEnv,Procedure* pDumpProc,bool isVarDump, CVariableItem* pThisObject,OTSTR& strRet);
        
    virtual bool isTypeOfDataValid(WORD nDisplayAttr);
    virtual CVariableItem* GetTypeOfData(int nIndex,WORD nDisplayAttr);
        
protected:
    friend class MsgTranslater;
    
    CStruct* NewStruct();
};

//////////////////////////////////////
class TStruct: public CStruct
{
public:
    long m_beginLocat,m_len;
    
public:
    TStruct();
    virtual ~TStruct();
    
protected:
    friend class MsgTranslater;
    
    CVariableItem* NewVariableItem();
};

//////////////////////////////////////////////////
#ifdef _DLL_PROJECT
class CLASS_EXPORT MsgTranslater: public TObject
#else
class MsgTranslater: public TObject
#endif
{
public:
    OTSTR m_strProtocolName;

    OTSTR m_strLastError;
    
    void ClearLastError();

public:
    MsgTranslater();	
   ~MsgTranslater();	

    void SetPlugInCallback(void* handler, cCallback::tFunction pFunctionPointer);

    bool LoadScript(char* szFilename);
    bool SetTranslateAttr(char* szAccessStruct,Waiter* pWaiter);
        
    bool Translate(bool bPreserveIntegrateStructObject, BYTE* szRowData, long& len); // len return translated byte

    TVariableItem& GetResult() { return m_VariableTran; }
        
    ExecuteEnv* GetEnv()     { return (m_pRootProgram)? (&m_Env):NULL; }
    Program*    GetProgram() { return m_pRootProgram; }
    bool        GetConstDefinition(OTSTR strName, OTSTR& strContent); // Get the Defininition string of "strName"
        
protected:
    void Clear(bool bClearError=true);

    bool TranslateStruct(TVariableItem& VariableTran, long nIndex, BinStream& stream);
    bool TranslateVariableItem          (char* szVarIdent, TVariableItem* pThisObject, TVariableItem& VariableTran, BinStream& stream, DWORD* lpParam=NULL);
    bool TranslateIntegratedVariableItem(char* szVarIdent, TVariableItem* pThisObject, TVariableItem& VariableTran, BinStream& stream, StructDefinition* pTypeOfStructDefinition);

    ///////////////////////////////////////////////////////////
    // Default TypeOf struct process function ( when relate function not found, try this default proc )
    // return: 0 fail    1 succ   2 default(not found)   // 暂不支持Parent.Ident, this.Ident

    static int Process_IntegrateProc(MsgTranslater& Translater,char* szVarIdent, StructDefinition* pTypeOfStructDefinition, BinStream& stream, CVarient& result);    // #1
    static int Process_IntegrateDumpProc(TObject* Translater, StructDefinition* pTypeOfStructDefinition, bool bJustValidCheck, CVarient* pItemValue, OTSTR* pStrComment);    // #1
    
    //////////////////////////////////////////
    static DWORD SysFunction(void* pParent, unsigned int params_num, DWORD params[]);
        
    //////////////////////////////////////////
    cCallback         m_PlugIn_Callback;

    Program*          m_pRootProgram;
    StructDefinition* m_pAccessDefinition;
    bool              m_bPreserveIntegrateStructObject;

    ExecuteEnv        m_Env;
    TVariableItem     m_VariableTran;

    //////////////////////////////////////////
    friend class TVariableItem;
};


#endif















