/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelBAT.h  -  description
                             -------------------
    begin                : 19 March 2004
    copyright            : (C) 2004 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

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

   This library 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 Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#ifndef __CEXCELBAT_H__
#define __CEXCELBAT_H__

#include <sptk3/CBuffer.h>
#include <stdio.h>

class CExcelHeader;
class CExcelProperties;

/// @addtogroup excel Excel Classes
/// @{

/// The collection of the BAT blocks for the Excel file header
class CExcelBAT {
   unsigned m_batStartBlock;        ///< The location of the first BAT block in the file
   unsigned m_batBlockCount;        ///< Total number of BAT blocks in the file
   unsigned m_xbatStartBlock;       ///< The location of the first extended BAT block in the file
   unsigned m_xbatBlockCount;       ///< Total number of extended BAT blocks in the file
   unsigned m_streamStartBlock;     ///< The first stream block in the file
   unsigned m_streamBlockCount;     ///< Total number of blocks in stream
   CBuffer  m_batIndex;             ///< The binary representation of BAT index (for the file header part)
   CBuffer  m_batBuffer;            ///< The binary representation of BAT itself including XBAT if presented
   CBuffer  m_xbatIndex;            ///< The binary representation of XBAT index
public:

   /// Constructor
   CExcelBAT(); 

   /// Reads BAT from the Excel file 
   /// @param fileHeader CExcelHeader *, the Excel file header 
   /// @param f FILE *, the input file
   void extract(CExcelHeader *fileHeader,FILE *f);

   /// Builds BAT table and index from m_streamStartBlock, m_streamBlockCount etc
   /// @param properties CExcelProperties *, the list of Excel properties
   /// @param batStartBlock unsigned, defines the start block of the XBAT and BAT
   void build(CExcelProperties *properties,unsigned batStartBlock);

   /// Returns binary representation of the BAT index
   const CBuffer& batIndex() const;

   /// Returns binary representation of the XBAT index
   const CBuffer& xbatIndex() const;

   /// Returns binary representation of the BAT table
   const CBuffer& batData() const;

   /// Resets the data buffers and sets the pointers to default values
   void clear();

   /// Defines the start block of the stream
   void streamStartBlock(unsigned ssb);

   /// Returns the start block of the stream
   unsigned streamStartBlock() const { return m_streamStartBlock; }

   /// Defines the block count of the stream
   void streamBlockCount(unsigned sbc);

   /// Returns the block count of the stream
   unsigned streamBlockCount() const { return m_streamBlockCount; }

   /// Returns the XBAT start block number
   unsigned xbatStartBlock() const { return m_xbatStartBlock; }

   /// Returns the required XBAT block count
   unsigned xbatBlockCount() const { return m_xbatBlockCount; }

   /// Returns the start block of the BAT
   unsigned batStartBlock() const { return m_batStartBlock; }

   /// Returns total number of blocks in BAT
   unsigned blockCount() const { return m_batBlockCount; }

   /// Returns total size occupied by BAT
   unsigned bytes() const { return m_batBuffer.bytes(); }
};

/// @}

#endif
