/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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.
*/

#ident "$Project: polymake $$Id: argv_options.cc,v 1.4 2004/06/22 16:22:41 gawrilow Exp $"

#include <argv_options.h>
#include <cstring>
#include <cctype>

namespace pm {

bool extract_options(int& argc, const char** argv, int n_fixed_args, int n_options, argv_option* options) {
   if (argc<=n_fixed_args) return false;

   int i=n_fixed_args+1, inew=i;
   for (; i<argc; ++i) {
      if (argv[i][0] != '-' || isdigit(argv[i][1])) {
	 if (inew<i) argv[inew]=argv[i];
	 ++inew;  continue;
      }
      bool recognized=false;
      for (int o=0; o<n_options; ++o) {
	 if (!strcmp(argv[i], options[o].name)) {
	    if (options[o].takes_value) {
	       if (i==argc-1) return false;
	       options[o].value=argv[++i];
	    } else {
	       options[o].value="yes";
	    }
	    recognized=true; break;
	 }
      }
      if (!recognized) return false;
   }
   argc=inew;
   return true;
}

} // end namespace pm
