/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelSST.h  -  description
                             -------------------
    begin                : Tue Apr 6 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 __CEXCELSST_H__
#define __CEXCELSST_H__

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

#include <map>

namespace sptk {

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

	class CExcelBook;

   /// Excel Shared String Table (SST)
	class CExcelSST {
		typedef std::map<std::string,unsigned> CSstStringMap;
		unsigned              m_totalBookStrings; ///< Total number of strings in the workbook
		unsigned              m_sstSize;          ///< Total number of unique strings in the workbook
		CStrings           m_stringList;          ///< List of the strings in the SST
		CSstStringMap         m_stringMap;        ///< Map of the strings in the SST. The (unsigned) in the map is the index of the string in SST
		const CExcelBook     *m_parentBook;       ///< Parent workbook

      /// Appnds a long string (8221+ bytes) to Excel records buffer
      /// @param records CExcelRecordsBuffer&, the output records buffer
      /// @param recordType short&, current record type (SST or CONTINUE)
      /// @param sstData char *, raw SST data buffer
      /// @param recordStart unsigned&, the position of the current record in the record buffer
      /// @param int& pos,unsigned len, position of the string in the raw SST data buffer
      /// @param len unsigned, the length of the string
		void saveLongString(CExcelRecordsBuffer& records,short& recordType,char *sstData,unsigned& recordStart,unsigned& pos,unsigned len);
	public:
      /// Default constructor
		CExcelSST() { m_parentBook = 0L; clear(); }

      /// Fast insert of the string into SST. Used when we know that the string isn't in SST yet.
		unsigned forceInsert(const char *str);

      /// Inserts string into the SST. The string is auto-checked for the uniqueness.
		unsigned insert(const char *str);

      /// Returns index of the given string in SST, or -1 if not found
		int      index(const char *str) const;

      /// Clears the content of the SST
		void clear();

      /// Encodes SST into internal Excel format
		void saveRecords(CExcelRecordsBuffer& records);

      /// Returns string by it's index in SST
		const std::string& operator [] (unsigned i) const { return m_stringList[i]; }

      /// Returns count of the unique strings in SST
		unsigned size() const { return m_stringList.size(); }

      /// Returns parent workbook
		const CExcelBook *parentBook() const { return m_parentBook; }

      /// Sets parent workbook
      /// @param book const CExcelBook *, the pointer to the parent workbook
		void parentBook(const CExcelBook *book) { m_parentBook = book; }
	};
   /// @}
}
#endif
