/*
** SellaNMS
** $Id: confignode.h,v 1.2 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_COMMON_CONFIGNODE_H
#define __DGS_COMMON_CONFIGNODE_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include <pthread.h>
#include <stl/slist>
#include <stl/string>
#include <stl/vector>
#include <stl/utility>

class ConfigNode;

/* FIXME: Concept Checks - References to forward definitions must be pointers. See below: */
/* typedef slist<pair<string, ConfigNode*> >::iterator node_iter_t; */

typedef slist<pair<string, ConfigNode> >::iterator node_iter_t;

class ConfigNode {
	public:
		ConfigNode();
		ConfigNode(ConfigNode const&);
		~ConfigNode();
		void clear();
		bool empty();
		void print(unsigned int depth = 0, bool flag = false); /* Prints the contents of the container. */
		ConfigNode const& operator=(ConfigNode const &src);
		RESULT addNode(const string key, const ConfigNode* node);
		ConfigNode* getNode(const string &request);
		vector<ConfigNode>* getNodes(const string &request);
		string* getValue(const string &request, unsigned long index = 0);
		vector<string>* getValues(const string &request);

		/* Helper Functions */
		bool isNode(const string &request);
		bool assignNode(const string &request, ConfigNode &dst, const ConfigNode &def);
		bool assignNode(const string &request, ConfigNode &dst, const ConfigNode *def = NULL);
		bool assignNode(const string &request, ConfigNode *&dst, const ConfigNode &def); /* Allocates Memory */
		bool assignNode(const string &request, ConfigNode *&dst, const ConfigNode *def = NULL); /* Allocates Memory */
		bool assignValue(const string &request, unsigned long &dst, const unsigned long &def = 0);
		bool assignValue(const string &request, unsigned long &dst, const unsigned long &def, const unsigned long &min);
		bool assignValue(const string &request, unsigned long &dst, const unsigned long &def, const unsigned long &min, const unsigned long &max);
		bool assignValue(const string &request, unsigned long &dst, const unsigned long &def, const unsigned long &min, const unsigned long &max, const long &mult); /* All variables are affected by the mult. */
		bool assignValue(const string &request, long &dst, const long &def = 0);
		bool assignValue(const string &request, string &dst, const string& def = "");
		bool assignValue(const string &request, string *&dst, const string& def); /* Allocates memory */
		bool assignValue(const string &request, string *&dst, const string* def = NULL); /* Allocates memory */
		bool assignValues(const string &request, vector<string> &dst);
		bool assignValues(const string &request, vector<string> &dst, const vector<string> &def);
		bool assignValues(const string &request, vector<string> *&dst, const vector<string> &def); /* Allocates memory */
		bool assignValues(const string &request, vector<string> *&dst, const vector<string> *def = NULL); /* Allocates memory */

	private:
		ConfigNode* findRequestedNode(const string& request); /* Returns pointer to private data. Do not deallocate! */

		slist<pair<string, ConfigNode> > nodes;

		pthread_mutex_t configMutex;
};

#endif

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