/* Copyright (c) 1997-2009
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   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, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   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.
*/


#include <read_int_args.h>
#include <sstream>

namespace polymake { namespace topaz {

/** Reads integers, specified individually or as a sequence,
 *  e.g. {1 7...9 20} = {1 7 8 9 20}. (perl lovers might use just two
 *  points to indicate a sequence.) 
 */

Set<int> read_int_args_as_set (const int argc, const char* argv[], const int start_index)
{
   Set<int> V;
   for (int i=start_index; i<argc; ++i) {
      std::istringstream istr(argv[i]);
      int v1;
      istr >> v1;
      if (!istr.eof()) {                 // not a single vertex
	 for (int j=0; j<2; ++j)          // remove the first two '.'
	    if (istr.get()!='.')
	       throw std::runtime_error("invalid integer input");  
	 if (istr.peek()=='.')            // remove third '.' if necessary
	    istr.get();
	 int v2;
	 istr >> v2;
	 V+=range(v1,v2);
      } else {                           // single vertex
	 V+=v1;
      }
   }
   return V;
}
   
std::list<int> read_int_args_as_list (const int argc, const char* argv[], const int start_index)
{
   std::list<int> V;
   for (int i=start_index; i<argc; ++i) {
      std::istringstream istr(argv[i]);
      int v1;
      istr >> v1;
      if (!istr.eof()) {                 // not a single vertex
	 for (int j=0; j<2; ++j)          // remove the first two '.'
	    if (istr.get()!='.')
	       throw std::runtime_error("invalid integer input");  
	 if (istr.peek()=='.')            // remove third '.' if necessary
	    istr.get();
	 int v2;
	 istr >> v2;
	 for (; v1<=v2; ++v1)
	    V.push_back(v1);
      } else {                           // single vertex
	 V.push_back(v1);
      }
   }
   return V;
}

} }


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
