#include "stdafx.h"
#include "PlugInObject.h"

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
CPlugIn::CPlugIn() 
{
    m_szPlugInName="ProtocalAnalyser";

    Tracer::inst()->open(this,m_szPlugInName.GetBuffer());

    TRACE0_("DllLoaded\n");
}

CPlugIn::~CPlugIn() 
{
    Release();
}
    
char* CPlugIn::GetPlugName(long& version) 
{ 
    version=1;
    
    return m_szPlugInName.GetBuffer(); 
}
    
void CPlugIn::Release()
{
    TRACE0_("\nDllFreed");

    Tracer::inst()->close();
}

void* CPlugIn::GetFunctionList()
{
    return &Tbl_FuncList;
}

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// Add your function implement codes here !

DWORD CPlugIn::TimestampToDateStream(void* pParent, unsigned int params_num, DWORD params[])
{
    if (params_num<4) return 0;
    
    ExecuteEnv&      Env            =*((ExecuteEnv*)params[1]);  // current environment
    Procedure*       pProcedure     =  (Procedure* )params[2];   // current procedure
    BinStream*       pstream        =  (BinStream* )params[3];   // current binary stream 
    RunTimeStk&      curStk         =*(Env.GetTopStk());         // current stack in environment
    
    ////////////////////////////////////////////////
    bool  bRT=true;
    
    //////////////////////////////////////
    // GET PROCEDURE PARAMETER
    
    long  nTimestamp;
    
    bRT=bRT & curStk.get("timestamp", nTimestamp);
    if (!bRT)
    {
        Env.SetLastError("%s() get param fail",pProcedure->GetProcName().GetBuffer());
        return 0;   // error
    }
    
    //////////////////////////////////////
    // EXECUTE PROCEDURE
    
    if (nTimestamp<0)
    {
        Env.SetLastError("%s() value of timestamp error",pProcedure->GetProcName().GetBuffer());
        return 0;
    }
    
    OTSTR  str;
    time_t sec  = nTimestamp;
    tm* cur_time= localtime(&sec);
    
    str.Format("%04d-%02d-%02d %02d:%02d:%02d", 		
                (cur_time->tm_year+1900), cur_time->tm_mon+1, cur_time->tm_mday,
                cur_time->tm_hour, cur_time->tm_min, cur_time->tm_sec);
    
    //////////////////////////////////////
    // SET RETURN
    
    CVarient Varient;
    Varient.set(str);
    if (!pProcedure->setReturn(Env, Varient))
    {
        Env.SetLastError("%s() set return fail",pProcedure->GetProcName().GetBuffer());
        return 0;
    }
    
    return 1;
}


