//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   Disk.h
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    CDisk class defintion.
//
//  Notes:
//

#if !defined(AFX_DISK_H__17BA3A51_8415_11D4_988D_00500465A0A8__INCLUDED_)
#define AFX_DISK_H__17BA3A51_8415_11D4_988D_00500465A0A8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

const unsigned long g_ulDefaultSwitchFilesSize = 200UL * 1024UL * 1024UL;           // 200 MBytes for now.

class C7kFile;                                                                      // Forward definition.

class CDisk
{
public:

    ///////////////
    // Definitions.

#pragma pack( push, DISK_H_PACK, 1 )

    typedef struct tagSUBSYSTEMENTRY                                                // Sensor entry relevant to 7k File Header (record number 7200).
    {
        unsigned long   m_ulDeviceId;                                               // 7k Device Identifier.
        unsigned short  m_unSubsystemEnumerator;                                    // 7k System enumerator.

        static size_t Size( void )
        {
            return sizeof( struct tagSUBSYSTEMENTRY );
        }
    }
    SUBSYSTEMENTRY, *PSUBSYSTEMENTRY;

#pragma pack( pop, DISK_H_PACK )

    ///////////////
    // Services.

                    CDisk               (   LPCTSTR                 lpszPath       = NULL,
                                            LPCTSTR                 lpszFileName   = NULL,
                                            const bool             &rbEnable       = false,     // Default behaviour is not to log.
                                            const bool             &rbAutoSwitch   = true,
                                            const unsigned long    &rulMaxFileSize = g_ulDefaultSwitchFilesSize );

    virtual        ~CDisk               (   void );

    void            SetAutoSwitch       (   const bool             &rbEnable );

    bool            UsingDefaultFolder  (   void )  const;

    bool            MaxFileSize         (   const unsigned long    &rulMaxFileSizeInMBytes );
    unsigned long   MaxFileSize         (   void )  const;

    bool            RecordOverlap       (   const unsigned long    &rulRecordOverlap );
    unsigned long   RecordOverlap       (   void )  const;

    bool            Path                (   LPCTSTR                 lpszPath );
    CString         Path                (   void )  const;

    bool            Set7kFileHeaderInfo (   const unsigned long    &rulNumberOfSensors,
                                            const PSUBSYSTEMENTRY   psSensorTable,
                                            const unsigned long    &rulSessionId            = 0UL,
                                            LPCTSTR                 lpszProgramVersion      = NULL,
                                            LPCTSTR                 lpszUserName            = NULL,
                                            LPCTSTR                 lpszUserNotes           = NULL );

    bool            Switch              (   LPCTSTR                 lpszPath       = NULL,
                                            LPCTSTR                 lpszFileName   = NULL );

    bool            Close               (   void );

    bool            Write               (   BYTE                   *pbyRecord,
                                            const unsigned long    &rulBytes );

    double          FileSize            (   void )  const;

    CString         FullFileName        (   void );

    bool            IsRecording         (   void )  const;

    float           PercentDiskSpaceFree(   void )  const;

private:

    ///////////////
    // Definitions.

    enum EFILEMODE
    {
        fileModeNormal,
        fileModeOverlapped
    };

    enum EFILETYPE
    {
        fileTypePrimary,
        fileTypeSecondary
    };

    ///////////////
    // Attributes.


    const unsigned long             m_ulMaxRecordsBeforeFlush;                              // Wrap point before forcing a logging file flush to disk.
    const unsigned long             m_ulMaxPossibleOverlap;                                 // Maximum overlap in records possible.
    const unsigned long             m_ulMinFileSizeInBytes;                                 // Minimum file size in bytes allowed.
    const unsigned short            m_unFileHeaderVersion;                                  // 7k file header version number.

    bool                            m_bAutoSwitch;
    bool                            m_bUsingDefaultFolder;                                  // Indicates the default folder is in use either because the specified folder does not exist and could not be created.

    unsigned long                   m_ulMaxRecordOverlapCount;                              // Number of records to hold off during switched file record overlap.
    unsigned long                   m_ulOverlapCount;                                       // Duplicate record count in files during switch operation.
    unsigned long                   m_ulFlushCount;                                         // Records counted before next flush.
    unsigned long                   m_ulMaxFileSizeInBytes;                                 // Max size of file before auto switch if enabled.
    unsigned long                   m_ulBytesLogged;                                        // Bytes logged since switch.

    C7kFile                        *m_pPrimaryFile;                                         // Main 7k logging file.
    C7kFile                        *m_pSecondaryFile;                                       // Secondary 7k loggiing file used for overlapped record switch.

    CString                         m_Path;                                                 // File path.
    CString                         m_FileName;                                             // File name.
    CString                         m_FullFileName;                                         // Fully qualified file name.

    // 7k File header parameters.

    SUBSYSTEMENTRY                 *m_pSensorIndexTable;                                    // Subsystem table to support 7k file header generation (record #7200).
    unsigned long                   m_ulSensorsInTable;                                     // Number of sensors in table.

    unsigned long                   m_ulSessionId;                                          // User defined session id.
    TCHAR                           m_szProgramVersion  [ 16  ];                            // This program's version number.
    TCHAR                           m_szUserName        [ 64  ];                            // User name.
    TCHAR                           m_szUserNotes       [ 128 ];                            // User specified notes string.

    ///////////////
    // Services.

    bool            ReadFileSizeFromRegistry        (   void );
    bool            ReadPathFromRegistry            (   void );
    bool            ReadRecordOverlapFromRegistry   (  void );

    bool            WritePathToRegistry             (   void );
    bool            WriteRecordOverlapToRegistry    (   void );
    bool            WriteFileSizeToRegistry         (   void );

    bool            IsAutoSwitchMode                (   void )  const;

    bool            SetFileName                     (   LPCTSTR                 lpszPath,
                                                        LPCTSTR                 lpszFileName,
                                                        const bool             &rbUseDefault );

    bool            OpenFile                        (   const EFILETYPE        &reFileType );

    bool            WritePrimaryFile                (   const BYTE             *pbyRecord,
                                                        const unsigned long    &rulBytes );

    bool            WriteSecondaryFile              (   const BYTE             *pbyRecord,
                                                        const unsigned long    &rulBytes );

    EFILEMODE       MaintainOverlap                 (   const bool              &rbNewRecord = true );

                    CDisk                           (   const CDisk            &rRhs );             // Not implemented.
    CDisk &         operator =                      (   const CDisk            &rRhs );             // Not implemented.

};

#endif // !defined(AFX_DISK_H__17BA3A51_8415_11D4_988D_00500465A0A8__INCLUDED_)
