#include "StdAfx.h"
#include "FileHeader7k.h"

#include "..\..\Include\Utils.h"

#include "..\..\Components\NetUtils\7kProtocol.h"
#include "..\..\Components\NetUtils\7kFile.h"
#include "..\..\Components\NetUtils\7kFileHeader.h"


#ifdef _DEBUG
#pragma comment( lib, "..\\..\\Components\\NetUtils\\Debug\\NetUtils.lib " )
#pragma comment( lib, "..\\..\\Remote6046\\Debug\\Remote6046.lib" )
#else
#pragma comment( lib, "..\\..\\Components\\NetUtils\\Release\\NetUtils.lib" )
#pragma comment( lib, "..\\..\\Remote6046\\Release\\Remote6046.lib" )
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    if ( ! AfxWinInit( ::GetModuleHandle( NULL ), NULL, ::GetCommandLine(), 0 ) )
    {
        printf( "Can't initalize MFC\n" );
        return -1;
    }

    BYTE *pbyRecord = NULL;

    try
    {
        CString  FileName( _T( "C:\\DataStore6046\\20021025_111402.s7k" ) );
        C7kFile  File;

        if ( ! File.Open( FileName, C7kFile::accessModeRead ) )
        {
            throw "Can't open the specified file";
        }

        const unsigned long ulMaxBufferSize = 1UL * 1024UL;     // Use 1k buffer for now.
        unsigned long       ulBytesRead     = 0UL;

        pbyRecord = new BYTE[ ulMaxBufferSize ];
        ASSERT( pbyRecord != NULL );

        if ( ! File.Read( pbyRecord, ulBytesRead, ulMaxBufferSize, false ) )
        {
            throw "Can't read from file";
        }

        TRACE( "Record of %lu bytes read\n", ulBytesRead );

        PRECORDHEADER7K psRecordHeader = reinterpret_cast<PRECORDHEADER7K>( pbyRecord );

        if ( psRecordHeader->m_ulRecordType == 7200UL )
        {
            C7kFileHeader                       FileHeader;

            C7kFileHeader::PRECORDTYPEHEADER    psRTH              = NULL;
            C7kFileHeader::PRECORDDATAHEADER    psRDH              = NULL;
            C7kFileHeader::PSUBSYSTEMENTRY      psSystemEntryTable = NULL;

            if ( ! FileHeader.Decode( pbyRecord, ulBytesRead, false, psRTH, psRDH, psSystemEntryTable ) )
            {
                throw "Can't decode file header";
            }

            const GUID sExpectedId = {
                                        0xf3302f43,
                                        0xcfb0,
                                        0x4d6f,
                                        { 0xa9, 0x3e, 0x2a, 0xec, 0x33, 0xdf, 0x57, 0x7d }
                                    };

            if (  *((GUID *) &(psRTH->m_byRecordId[ 0 ])) != sExpectedId )
            {
                printf( "File id mismatch !!!\n\n" );
            }

            //psRTH->m_bySessionId[ 16 ];     // Session identifier; a user defined session identifier. Used to associate multiple files for a given session.

            printf( "File format version  : %u\n\n", psRTH->m_unFormatVersion );

            printf( "Program name         : %s\n"
                    "Program version      : %s\n"
                    "User name            : %s\n"
                    "Notes                : %s\n\n", psRDH->m_szRecordingProgramName,
                                                     psRDH->m_szRecordingProgramVersion,
                                                     psRDH->m_szUserDefinedName,
                                                     psRDH->m_szNotes );

            printf( "Subsystems           : %lu\n", psRTH->m_ulNumberOfSubsystem );

            for ( unsigned long ulSubsystem = 0UL; ulSubsystem < psRTH->m_ulNumberOfSubsystem; ulSubsystem++ )
            {
                printf( "\n"
                        "Device id            : %ul\n"
                        "Subsystem id         : %u\n" 
                        "Subsystem enumerator : %u\n",  psSystemEntryTable[ ulSubsystem ].m_ulDeviceId,
                                                        psSystemEntryTable[ ulSubsystem ].m_unSubsystemId,
                                                        psSystemEntryTable[ ulSubsystem ].m_unSubsystemEnumerator );
            }
        }
    }
    catch ( const char *pszMessage )
    {
        printf( "%s.\n", pszMessage );
    }
    catch ( ... )
    {
        printf( "Unspecified exception caught\n" );
    }

    if ( pbyRecord != NULL )
    {
        delete [] pbyRecord;
        pbyRecord = NULL;
    }


    return 0;
}


