/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelNumericFormat.cpp  -  description
                             -------------------
    begin                : Fri May 6 2004
    copyright            : (C) 2000-2005 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 <sptk3/CStrings.h>
#include <sptk3/excel/CExcelNumericFormat.h>

using namespace std;

CExcelNumericFormat::CExcelNumericFormat(CVariantType kind,unsigned index,string format) {
   m_kind = kind;
   m_index = index;
   m_format = format;
}

CExcelNumericFormat::CExcelNumericFormat(unsigned index,string format) {
   m_kind = VAR_STRING;
   m_index = index;
   m_format = format;

   string lcFormat(lowerCase(format));
   if ( lcFormat.find("$") != string::npos ) {
      m_kind = VAR_MONEY;
   } else {
      if ( lcFormat.find("mm") != string::npos
         || lcFormat.find("dd") != string::npos
         || lcFormat.find("yy") != string::npos
         || lcFormat.find("ss") != string::npos
         || lcFormat.find("hh") != string::npos ) 
      {
         m_kind = VAR_STRING;
         if ( lcFormat.find("d") != string::npos || m_format.find("y") != string::npos )
            m_kind = VAR_DATE;
         if ( lcFormat.find("s") != string::npos || m_format.find("h") != string::npos )
            m_kind = VAR_DATE_TIME;
      } else {
         if ( lcFormat.find(".0") != string::npos )
            m_kind = VAR_FLOAT;
         else
            m_kind = VAR_STRING;
      }
   }
}

CExcelNumericFormat::CExcelNumericFormat(const CExcelNumericFormat& nf) {
   m_kind = nf.m_kind;
   m_index = nf.m_index;
   m_format = nf.m_format;
}
// --------------------------------------------------------------------------------------------------
void CExcelNumericFormats::clear() {
   for (iterator itor = begin(); itor != end(); itor++) 
      delete itor->second;
   map<int,CExcelNumericFormat *>::clear();
}

void CExcelNumericFormats::saveRecords(CExcelRecordsBuffer& records) {
   for (iterator itor = begin(); itor != end(); itor++) {
      CExcelNumericFormat *nf = itor->second;
      int formatID = nf->index();
      int formatLength = nf->format().length();
      char *buffer = new char[ formatLength + 5 ];
      CExcelRecord::setshort(buffer,0,formatID);
      buffer[2] = char(formatLength);
      CExcelRecord::setshort(buffer,3,0);
      memcpy(buffer + 5,nf->format().c_str(),formatLength);
      records.append(FORMAT,formatLength + 5,buffer);
      delete buffer;
   }
}
