/*
** SellaNMS
** $Id: suffix.cxx,v 1.7 2005/12/01 05:57:59 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static const char rcsid[] = "$Id: suffix.cxx,v 1.7 2005/12/01 05:57:59 sella Exp $";

#include "suffix.h"
#include <stdio.h>
#include <stl/vector>

RESULT Suffix::setName(string &name) {
	this->name = name;

	return SUCCESS;
}

string Suffix::getName() {
	return this->name;
}

ACTION Suffix::evaluate(variable_binding_t &input) {
	return 0;
}

RESULT Suffix::compile(ConfigNode *config) {
	RESULT result = SUCCESS;

	ConfigNode 		dependency_config;
	
	string			value;
	vector<string>	values;

	Dependency		dependency;

	if (config->assignValue("function", value)) {
		if (value.compare("ignore") == 0) {
			this->function = FUNCTION_NOP;
		} else if (value.compare("add") == 0) {
			this->function = FUNCTION_ADD;
		} else if (value.compare("subtract") == 0) {
			this->function = FUNCTION_SUB;
		} else if (value.compare("multiply") == 0) {
			this->function = FUNCTION_MUL;
		} else if (value.compare("divide") == 0) {
			this->function = FUNCTION_DIV;
		} else {
			LMSG("Policy: unknown suffix-modifier function %s.", value.c_str());

			result = FAILURE;
		}
	} else {
		this->function = FUNCTION_NOP;
	}

	config->assignValue("argument", this->argument, 0);

	if (config->assignValues("dependency", values)) {
		for (vector<string>::iterator v = values.begin(); v != values.end(); ++v) {
			if (config->assignNode("dependency " + *v, dependency_config)) {
				dependency.setName(*v);

				if (dependency.compile(&dependency_config) == SUCCESS) {
					this->dependencies.insert(this->dependencies.end(), dependency);
				} else {
					LMSG("Policy: failed to compile dependency %s.", v->c_str());

					result = FAILURE;
				}
			} else {
				LMSG("Policy: missing configuration for dependency %s.", v->c_str());

				result = FAILURE;
			}
		}
	} else {
		this->dependencies.clear();
	}	
	
	return result;
}

RESULT Suffix::clear() {
	this->function = 0;
	this->argument = 0;
	this->dependencies.clear();

	return SUCCESS;
}

RESULT Suffix::print(unsigned long depth) {
	string	tab = "";
	for (unsigned long i = 0; i < depth; i++) {
		tab.append("   ");
	}

	LMSG("%ssuffix-modifier %s {", tab.c_str(), this->name.c_str());
	LMSG("%s   function 0x%x;", tab.c_str(), this->function);
	LMSG("%s   argument %d;", tab.c_str(), this->argument);

	if (!this->dependencies.empty()) {
		LMSG("");
		for (slist<Dependency>::iterator d = this->dependencies.begin(); d != this->dependencies.end(); ++d) {
			d->print(depth + 1);
		}
	}
	LMSG("%s}", tab.c_str());

	return SUCCESS;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3 sts=3
*/
