/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelDBCellRecord.cpp  -  description
                             -------------------
    begin                : Mar 20 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 <sptk3/excel/CExcelDBCellRecord.h>
#include <sptk3/CException.h>

CExcelDBCellRecord::CExcelDBCellRecord()
: CExcelRecord(DBCELL,4)
{
   m_streamPosition = 0;
}

CExcelDBCellRecord::~CExcelDBCellRecord() {
   CExcelRowRecordVector::iterator itor = m_rows.begin();
   for (; itor != m_rows.end(); itor++)
      delete *itor;
}

void CExcelDBCellRecord::saveRecords(CExcelRecordsBuffer& records,const CExcelSST& sst) {
   unsigned rowBlockStarts = records.bytes();
   unsigned secondRowStarts = rowBlockStarts + 20;
   unsigned rowCount = m_rows.size();
   if (!rowCount) return;
   unsigned bufferSize = 4 + rowCount * 2;
   m_manageMemory = true;
   m_data = new char[bufferSize+1];
   char *ptr = m_data;

   // Adding ROW records
   unsigned i;
   for (i = 0; i < rowCount; i++) {
      m_rows[i]->saveRowRecord(records);
   }

   // Adding ROW records
   unsigned priorCellOffset = 0;
   for (i = 0; i < rowCount; i++) {
      m_rows[i]->saveCellRecords(records,sst,secondRowStarts);
      unsigned relativeCellOffset = m_rows[i]->firstCellPosition()-secondRowStarts;
      CExcelRecord::setshort(ptr,i*2+4,relativeCellOffset-priorCellOffset);
      priorCellOffset = relativeCellOffset;
   }
   m_bytes = bufferSize;

   unsigned rowBlockOffset = records.bytes() - rowBlockStarts;
   setint(ptr,0,rowBlockOffset);

   m_streamPosition = records.bytes();
   records.append(DBCELL,m_bytes,data());
}

void CExcelDBCellRecord::addRow(CExcelRowRecord *row) { 
   if (m_rows.size() > 31)
      throw CException("Can't have more than 32 rows in DBCELL record.");
   m_rows.push_back(row); 
}
