// Change History:
// 01/08/04 AV Development: Write [MISC] section in macro file on stop
// 01/08/04 AV Development: Add find_proc to fix bug #55
// 01/08/04 AV Development: Created.

// DART Macro Def

#include "ddp/ddp_macrodef.h"

#include <time.h>
#include <stdio.h>
#include <windows.h>


void DDP_MacroDef::write_misc()
{
    byte buf[MAX_PATH];
    uint32 sz = sizeof(buf);

    const byte * section = GetSectionName();
	if(::GetUserNameA(PORT_char(buf), (LPDWORD)&sz))
        WritePrivateProfileStringA(PORT_cchar(section), "UserName", PORT_cchar(buf), FileName.c_str());
    // Time
    time_t tt;
    time(&tt);
    struct tm * t = localtime(&tt);
    if(!t)
        {
        WritePrivateProfileStringA(PORT_cchar(section), "RecordingDate", "ERROR", FileName.c_str());
        return;
        }
    PORT_snprintf_6(buf, sizeof(buf), "%2.2u/%2.2u/%2.2u-%2.2u:%2.2u:%2.2u",
        t->tm_mon+1, t->tm_mday, t->tm_year%100,
        t->tm_hour, t->tm_min, t->tm_sec);
    WritePrivateProfileStringA(PORT_cchar(section), "RecordingDate", PORT_cchar(buf), FileName.c_str());    
}

void DDP_MacroDef::Stop()
{
    if(Status == MS_RECORDING)
        write_misc();

    Status = MS_NONE;
    FileName = "";
    Processes.clear();
}

const DDP_MacroDef::ProcessDef * DDP_MacroDef::find_proc(const byte * n) const
{
    int32 i, cnt = Processes.size();
    for(i = cnt - 1; i >= 0; --i)
        {
        const ProcessDef * p = &Processes[i];
        if(p->Name == (char *)n)
            return p;
        }
return NULL;
}
 