/*
 *
 *
 *    arguments.cc
 *
 *    C++ source code for class my::arguments member functions.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1999-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser 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
 *
 *    =========================================================================
 *
 *
 */


#include <my++/arguments.h>

my::arguments::arguments (int argc, char **argv, char *valid, char *valid_wa) {
	ArgumentValue newarg;
	std::string lastarg;

	for (int i = 1; i < argc; ++i) {
		if (strlen(argv[i]) < 2)
			continue;
		if (argv [i][0] == '-') {
			for (size_t j = 1; j < strlen(argv[i]); j++) {
				if (!isSet (argv[i][j])) {
					newarg.name = argv[i][j];
					lastarg = newarg.name;
					arglist.push_back(newarg);
				} else {
					lastarg.erase();
				}
			}
		} else {
			if (!lastarg.empty()) {
				arglist.back().value = argv[i];
				lastarg.erase();
			}
		}
	}
}


bool my::arguments::get (const std::string &name, std::string &value) const {
	ArgumentList::const_iterator ai;

	for (ai = arglist.begin(); ai != arglist.end(); ++ai) {
		if (ai->name == name) {
			value = ai->value;
			return true;
		}
	}

	return false;
}

const std::string my::arguments::get (const std::string &name) const {
	ArgumentList::const_iterator ai;

	for (ai = arglist.begin(); ai != arglist.end(); ++ai) {
		if (ai->name == name)
			return std::string(ai->name);
	}

	return std::string();
}

bool my::arguments::is_set (const std::string &name) const {
	ArgumentList::const_iterator ai;

	for (ai = arglist.begin(); ai != arglist.end(); ++ai)
		if (ai->name == name)
			return true;
	return false;
}


// deprecated
bool my::arguments::getValue (char name, std::string &value) const {
	ArgumentList::const_iterator ai;

	for (ai = arglist.begin(); ai != arglist.end(); ++ai) {
		if (ai->name[0] == name) {
			value = ai->value;
			return true;
		}
	}

	return false;
}


// deprecated
const std::string my::arguments::getValue (char name) const {
	std::string value;
	getValue (name, value);
	return value;
}


// deprecated
bool my::arguments::isSet (char name) const {
	ArgumentList::const_iterator ai;

	for (ai = arglist.begin(); ai != arglist.end(); ++ai) {
		if (ai->name[0] == name)
			return true;
	}

	return false;
}


// deprecated
bool my::arguments::allNames (std::list<char> &namelist) const {
	ArgumentList::const_iterator ai;
	for (ai = arglist.begin(); ai != arglist.end(); ++ai)
		namelist.push_back(ai->name[0]);
	return true;
}
