/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelRecordsBuffer.cpp  -  description
                             -------------------
    begin                : Mon Jan 19 2004
    copyright            : (C) 2000-2006 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"
 ***************************************************************************/

#include <string.h>
#include <sptk3/excel/CExcelRecordsBuffer.h>

using namespace sptk;

void CExcelRecordsBuffer::reset() {
	CBuffer::reset();
	m_currentPosition = 0;
	m_sheetPosition.clear();
}

CExcelRecord *CExcelRecordsBuffer::first() {
	m_currentPosition = 0;
	unsigned rectype = CExcelRecord::getshort(m_buffer,0);
	unsigned reclen  = CExcelRecord::getshort(m_buffer,2);
	return new CExcelRecord(rectype,reclen,m_buffer+4); 
}

CExcelRecord *CExcelRecordsBuffer::next() {
	unsigned rectype = CExcelRecord::getshort(m_buffer,m_currentPosition);
	unsigned reclen  = CExcelRecord::getshort(m_buffer,m_currentPosition+2);

	m_currentPosition += 4;
	if (m_currentPosition >= m_bytes) return 0L;

	char *data = m_buffer + m_currentPosition;
	m_currentPosition += reclen;

	return new CExcelRecord(rectype,reclen,data); 
}

void CExcelRecordsBuffer::append(const CExcelRecord *rec) {
	append(rec->type(),rec->bytes(),rec->data());
}

void CExcelRecordsBuffer::append(unsigned recordType,unsigned dataSize,const char *data) {
	unsigned newSize = m_bytes + dataSize + 4;
	checkSize(newSize);
	CExcelRecord::setshort(m_buffer,m_bytes,recordType);
	CExcelRecord::setshort(m_buffer,m_bytes+2,dataSize);
	memcpy(m_buffer+m_bytes+4,data,dataSize);
	bytes(newSize);
}
