/***************************************************************************
 *   Copyright (C) 2004 by Armando Navarro Vázquez                         *
 *   armando@rmntec                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <sstream>
#include "xyzparser.h"
#include "molecule.h"
#include "tokenizer.h"


XYZParser::XYZParser(const char* file)
    : QuantumParser(file)
{}

XYZParser::XYZParser(std::istream* stream) : QuantumParser(stream)
{}


XYZParser::~XYZParser()
{}


bool XYZParser::ParseFile(std::streampos pos)
{
  m_file->clear();
  m_file->seekg(pos,std::ios::beg);
  return GetGeometry();

}

bool XYZParser::GetGeometry()
{
  std::string line;
  while(std::getline(*m_file,line))
  {
    std::cout << line << std::endl;
    StringTokenizer token(line," \t\r");
    if ( CheckLine(line)  ) GetMolecule(line);
  }
  std::vector<CMolecule>::iterator mt;
  for(mt=m_molecule->begin();mt!=m_molecule->end();++mt)
  {
    mt->SetConexions();
  }
  return true;
}

void XYZParser::GetMolecule(std::string& line)
{
  static bool bfirst=true;
  if ( bfirst)
  {
  CMolecule molecule;     
  m_molecule->push_back(molecule);
  }
  bfirst=false;
  int counter=0;
  do
  {
    StringTokenizer token(line," \t\r");
    if ( token.size() != 4 )
    {
     bfirst=true;
 
      return;
    }
    CAtom atom;
    atom.name=token.at(0);
    std::stringstream stream;
    stream << ++counter;
//    atom.label=stream.str();
    atom.c.x=atof(token.at(1).c_str());
    atom.c.y=atof(token.at(2).c_str());
    atom.c.z=atof(token.at(3).c_str());
    m_molecule->back().m_atom.push_back(atom);
  } while(std::getline(*m_file,line));
}

bool XYZParser::CheckLine(std::string& line)
{
  StringTokenizer token(line," \t\r");
  if ( token.size() == 4 )
  {
    for(size_t i=1;i<4;++i)
    {
       if ( !strtools::isnum(token.at(i)) ) return false;
    }
    return true;
  }
  else return false;
}



