/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelHeader.h  -  description
                             -------------------
    begin                : Mon Jan 19 2004
    copyright            : (C) 2004 by Alexey S.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 __CEXCELHEADER_H__
#define __CEXCELHEADER_H__

#include <sptk3/excel/CExcelBAT.h>

namespace sptk {

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

#define BIG_BLOCK_SIZE    0x0200         ///< The size of the big block in Excel header
#define SMALL_BLOCK_SIZE  0x040          ///< The size of the small block in Excel header
#define END_OF_CHAIN      (unsigned)-2   ///< End of chain mark
#define UNUSED_BLOCK      (unsigned)-1   ///< Unused block mark

#define _SIGNATURE               0xE11AB1A1E011CFD0L  ///< Excel file signature
#define _BAT_ARRAY_OFFSET        0x4c                 ///< BAT array data offset in the header
#define _MAX_BATS_IN_HEADER      (BIG_BLOCK_SIZE - _BAT_ARRAY_OFFSET) / 4  ///< Max BAT blocks in the header

// useful offsets
#define _SIGNATURE_OFFSET        0     ///< Signature offset in the file
#define _BAT_COUNT_OFFSET        0x2C  ///< BAT count offset in the file
#define _PROPERTY_START_OFFSET   0x30  ///< Property list offset in the file
#define _SBAT_START_OFFSET       0x3C  ///< Small BAT data offset in the file
#define _SBAT_COUNT_OFFSET       0x40  ///< Small BAT count offset in the file
#define _XBAT_START_OFFSET       0x44  ///< Large BAT data offset in the file
#define _XBAT_COUNT_OFFSET       0x48  ///< Large BAT count offset in the file

   /// Represents the Excel file header
	class CExcelHeader {
		char m_data[BIG_BLOCK_SIZE];  ///< Binary copy of the header
	public:

      /// Constructor
      /// @param data const char *, external header data to copy into header
		CExcelHeader(const char *data=0L); 

      /// Defines the BAT table - for creating an Excel file
      /// @param batTable CExcelBAT&, the BAT table
		void setBatTable(CExcelBAT& batTable);

      /// Defines the position and data of SBAT table - for creating an Excel file
      /// @param locationBlock unsigned, the block where SBAT table is located
      /// @param batTable CExcelBAT&, the SBAT table
		void setSBatTable(unsigned locationBlock,CExcelBAT& batTable);

      /// Defines the Property table start - for creating an Excel file
      /// @param locationBlock unsigned, the block where property table is located
		void setPropertyTableStart(unsigned locationBlock);

      /// Returns the pointer to header data
		const char *data() const { return m_data; }

      /// Sets the external header data 
      /// @param data const char *, external header data
		void data(const char *data);

      /// Reads header from file
      /// @param f FILE *, the input file
		void read(FILE *f);

      /// Saves header to file
      /// @param f FILE *, the output file
		void save(FILE *f);

      /// Returns the total BAT block count
		unsigned batBlockCount() const;

      /// Sets the start block of XBAT
      /// @param startBlock unsigned, XBAT start block
		void xbatStartBlock(unsigned startBlock);

      /// Sets the start block of XBAT
      /// @param blockCount unsigned, XBAT block count
		void xbatBlockCount(unsigned blockCount);

      /// Returns the start block of XBAT
		unsigned xbatStartBlock() const;

      /// Returns the XBAT block count
		unsigned xbatBlockCount() const;

      /// Resets header to default state
		void clear();
	};
   /// @}
}
#endif
