/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelIndex.cpp  -  description
                             -------------------
    begin                : Mar 17 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"
 ***************************************************************************/

#include <stdio.h>
#include <string.h>
#include <sptk3/excel/CExcelIndex.h>

using namespace std;

CExcelIndex::CExcelIndex(CExcelRows *rows)
: CExcelRecord(INDEX,0) {
   m_rows = rows;

   unsigned dbCellRecordsCount = m_rows->size() / 32;
   if (dbCellRecordsCount * 32 < m_rows->size()) dbCellRecordsCount++;
   // BIFF7:12, BIFF8:16?
   unsigned bufferSize = 16 + dbCellRecordsCount * 4;
   if (!dbCellRecordsCount) bufferSize += 4;

   //printf("Index block size = %i\n",bufferSize);

   m_data = new char[bufferSize+1];
   m_manageMemory = true;
   m_bytes = bufferSize;

   memset(m_data,0,bufferSize);
   setint(m_data,4,0);                 // First used row
   setint(m_data,8,m_rows->size());   // Last used row, +1

   unsigned rowCount = m_rows->size();
   unsigned recordsInBlock = 32;
   CExcelDBCellRecord *dbcellRecord = 0L;
   for (unsigned r = 0; r < rowCount; r++) {
      if (recordsInBlock == 32) {
         recordsInBlock = 0;
         dbcellRecord = new CExcelDBCellRecord();
         m_dbcellRecords.push_back(dbcellRecord);
      }
      dbcellRecord->addRow(new CExcelRowRecord(r,&(*m_rows)[r]));
      recordsInBlock++;
   }
}

CExcelIndex::~CExcelIndex() {
   vector<CExcelDBCellRecord *>::iterator itor = m_dbcellRecords.begin();
   for (; itor != m_dbcellRecords.end(); itor++)
      delete *itor;
   m_dbcellRecords.clear();
}

void CExcelIndex::saveRecords(CExcelRecordsBuffer& records,const CExcelSST& sst,unsigned sheetStreamPosition,unsigned defColWidthPosition) {
   unsigned dbcellRecordCount = m_dbcellRecords.size();
   char *dbcellIndex = m_data + 16;
   setint(m_data + 12,0,defColWidthPosition);
   if (dbcellRecordCount) {
      for (unsigned i = 0; i < dbcellRecordCount; i++) {
         CExcelDBCellRecord *dbcellRecord = m_dbcellRecords[i];
         dbcellRecord->saveRecords(records,sst);
         setint(dbcellIndex,i*4,sheetStreamPosition+dbcellRecord->streamPosition());
         //printf("Index position = %i\n",i*4+16);
      }
   }
}
