/* fileheader.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2020, 2021 Askar
    Almatov

    This file is part of Trafficmeter.

    Trafficmeter is free software: you can redistribute it and/or modify it under the terms of the GNU General
    Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    Trafficmeter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
    implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
    License for more details.

    You should have received a copy of the GNU General Public License along with Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#ifndef __FILEHEADER_H
#define __FILEHEADER_H 1

#include <cstdint>
#include <ctime>

#include "tmdefs.h"

namespace trafficmeter
{
    /**********************************************************************************************************/
    class FileHeader
    {
    public:
        enum class Type
        {
                                        FLOW = 1,
                                        STAT,
                                        FLOW2
        };

                                        FileHeader();
                                        FileHeader( Type type, int nIntervals, time_t group );
                                        ~FileHeader();

        void                            update( time_t group, long offset, long nBytes );
        bool                            isCompatible( Type type ) const;
        bool                            isEmpty() const;
        bool                            hasGroup( time_t group ) const;
        time_t                          oldestGroup() const;
        time_t                          newestGroup() const;
        long                            groupOffset( time_t group ) const;
        long                            fileSize() const;
        bool                            read( int fd );
        bool                            write( int fd ) const;

    protected:
        struct BeHeader_;

        long                            size_;
        BeHeader_*                      beHeader_;
    };

    /**********************************************************************************************************/
    struct FileHeader::BeHeader_
    {
        uint32_t                        magic_;
        uint32_t                        version_;
        uint32_t                        type_;
        uint16_t                        nIntervals_;
        uint16_t                        groupFactor_;
        uint64_t                        fileSize_;
        uint32_t                        oldestGroup_;
        uint32_t                        newestGroup_;
    };
}

#endif  // __FILEHEADER_H
