/*-------------------------------------------------------------------------------------*/
/*  NOMAD - Nonsmooth Optimization by Mesh Adaptive Direct search - version 3.0.0      */
/*                                                                                     */
/*  Copyright (C) 2001-2008  Mark Abramson        - the Boeing Company, Seattle        */
/*                           Charles Audet        - Ecole Polytechnique, Montreal      */
/*                           Gilles Couture       - Ecole Polytechnique, Montreal      */
/*                           John Dennis          - Rice University, Houston           */
/*                           Sebastien Le Digabel - Ecole Polytechnique, Montreal      */
/*                                                                                     */
/*  funded in part by AFOSR and Exxon Mobil                                            */
/*                                                                                     */
/*  Author: Sebastien Le Digabel                                                       */
/*                                                                                     */
/*  Contact information:                                                               */
/*    Ecole Polytechnique de Montreal - GERAD                                          */
/*    C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada                  */
/*    e-mail: Sebastien.Le.Digabel@gerad.ca                                            */
/*    phone : 1-514-340-6053 #6673                                                     */
/*    fax   : 1-514-340-5665                                                           */
/*                                                                                     */
/*  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 3 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, see <http://www.gnu.org/licenses/>.                               */
/*                                                                                     */
/*  You can find information on the NOMAD software at www.gerad.ca/nomad               */
/*-------------------------------------------------------------------------------------*/
#include "Parameter_Entry.hpp"
using namespace std;

/*-----------------------------------*/
/*  . constructor (from a string)    */
/*  . ignores all entries after '#'  */
/*-----------------------------------*/
Parameter_Entry::Parameter_Entry ( const string & entry ) :
  _ok                   (false) ,
  _unique               (true ) ,
  _has_been_interpreted (false) ,
  _next                 (NULL )   {

  int           i , idst;
  string        s;
  istringstream in(entry);
  in >> _name;
  if ( _name[0] == '#' )
    _name.clear();
  else {

    NOMAD::toupper ( _name );

    while (true) {
      in >> s;

      // comment:
      if ( in.fail() || s[0]=='#' )
	break;

      // DISPLAY_STATS or STATS_FILE:
      if ( _name == "DISPLAY_STATS" || _name == "STATS_FILE" ) {

	string keyword , ss = s;
	bool   interpreted  = false;
	NOMAD::toupper(ss);
	
	display_stats_type dst = _DS_OBJ_;
	while ( dst < _DS_UNDEFINED_ ) {

	  keyword = get_display_stats_keyword (dst);

	  i = static_cast<int> ( ss.rfind ( keyword , ss.size()-1 ) );

	  if ( i >= 0 ) {
	    string s1 = s.substr ( 0 , i );
	    string s2 = s.substr ( i+keyword.size() );
	    if ( !s1.empty() )
	      _values.push_back ( s1 );
	    _values.push_back ( keyword );
	    if ( !s2.empty() )
	      _values.push_back ( s2 );
	    _values.push_back ( string() );
	    interpreted = true;
	    break;
	  }

	  idst = dst;
	  ++idst;
	  dst = static_cast<display_stats_type> (idst);
	}

	if ( !interpreted ) {
	  _values.push_back (s);
	  _values.push_back (string());
	}

	continue;
      }

      // string with quotes:
      if ( s[0] == '\"' || s[0] == '\'' ) {
	
	char quote = s[0];
	
	s.erase(s.begin());
	
	if ( s[s.size()-1] == quote ) {
	  s.resize(s.size()-1);
	  _values.push_back(s);
	  continue;
	}
	
	string ss;
	getline ( in , ss , quote );
	
	if ( in.fail() || !in.good()) {
	  _ok = false;
	  return;
	}

	_values.push_back ( s + ss );

	continue;
      }
      
      // vector:
      if ( s.size() > 1 && ( s[0] == '[' || s[0] == '(' ) ) {
	_values.push_back ( s[0]=='[' ? "[" : "(" );
	s.erase(s.begin());
      }
      int  sm1 = static_cast<int>(s.size()) - 1;
      char c   = s[sm1];
      if ( s.size() > 1 && ( c == ']' || c == ')' ) ) {
	s.resize(sm1);
	_values.push_back (s);
	_values.push_back ( c==']' ? "]" : ")" );
	continue;
      }

      // other values:
      _values.push_back (s);
      
    }
    
    if ( !_values.empty() )
      _ok = true;
  }
}

/*------------------------------*/
/*             display          */
/*------------------------------*/
void Parameter_Entry::display ( ostream & out ) const {
  if ( _ok ) {
    out << _name << ": ";
    list<string>::const_iterator end = _values.end();
    for ( list<string>::const_iterator it = _values.begin() ; it != end ; ++it )
      out << "[" << *it << "] ";
    // out << "has_been_interpreted=" << _has_been_interpreted;
    // out << "unique=" << _unique;
    // if ( _next )
    //   out << "--> " << *_next;
  }
}
