All pastes #247171 Raw Edit

Mine

public cpp v1 · immutable
#247171 ·published 2006-11-13 02:56 UTC
rendered paste body
//*******************************************************************// Project:		Artificial Life// Title:		Config Parser// Type:		Utility//// Author:		Alex Walker// Created: 	Nov. 8, 2006// Modified:	Nov. 11, 2006//// Config Parser is a templated class allowing parsing of files that// adhere to a simple format.  It utilitizes a map to store the// results, so it expects a pair of "key value."  Either a space or// a new line will suffice as a delimiter between elements.//// Example file:////		WIDTH 640//		HEIGHT 480////*******************************************************************#ifndef _AL_CONFIGPARSER_H_#define _AL_CONFIGPARSER_H_#include <map>using std::map;using std::make_pair;#include <vector>using std::vector;#include <string>using std::string;#include <fstream>using std::ifstream;using std::ofstream;template <typename T>class ConfigParser{   private:	    string filename;		map<string, T> * parsed;		   public:	    ConfigParser<T>(const string & = "");		ConfigParser<T>(const ConfigParser<T> &);		ConfigParser<T> & operator=(const ConfigParser<T> &);        ~ConfigParser<T>();		bool parse(const string & = "");		T get(const string & );		T getStr(const string & );		bool set(const string & , const T & );		bool exists(const string & );		bool write();		bool isParsed(){ return parsed->size() > 0; }		vector<T> * asVector();};template <typename T>ConfigParser<T>::ConfigParser(const string & fn) : filename(fn){	parsed = new map<string, T>();	if( filename != "" )		parse();}template <typename T>ConfigParser<T>::ConfigParser(const ConfigParser<T> & rhs){	*this = rhs;}template <typename T>ConfigParser<T> & ConfigParser<T>::operator=(const ConfigParser<T> & rhs){	if( this != &rhs )	{		delete parsed;		parsed = new map<string, T>( rhs.parsed->begin(), parsed->end() );		filename = rhs.filename;	}	return *this;}template <typename T>ConfigParser<T>::~ConfigParser(){	filename.clear();	delete parsed;	}template <typename T>bool ConfigParser<T>::parse(const string & fn){	if( fn != "" ) filename = fn;	ifstream fin( filename.c_str() );	if( fin.is_open() )	{		// erase previous data		parsed->clear();		while( !fin.eof() )		{			string attribute = "";			T value;						if( fin >> attribute && attribute != "" )			{				if( fin >> value )				{					// dereference and assign attribute to value					(*parsed)[attribute] = value;					attribute.clear();				}			}		}		fin.close();		return true;	}	return false;}template <typename T>T ConfigParser<T>::get(const string & attribute){	typename map<string, T>::iterator i = parsed->find(attribute);	// check success	if( i != parsed->end() )	{		return (*parsed)[attribute];	}	return 0;}template <typename T>T ConfigParser<T>::getStr(const string & attribute){	typename map<string, T>::iterator i = parsed->find(attribute);	// check success	if( i != parsed->end() )	{		return (*parsed)[attribute];	}	return "";}template <typename T>vector<T> * ConfigParser<T>::asVector(){	vector<T> * v = new vector<T>(parsed->size());	typename map<string, T>::iterator i = parsed->begin();	for(; i != parsed->end(); i++)	{		v->push_back( i->second );	}	return v;}template <typename T>bool ConfigParser<T>::set(const string & attribute, const T & value){	// update value, return iterator to location and bool	pair< typename map<string, T>::iterator, bool > p	= parsed->insert( make_pair(attribute, value) );	// p.second is a bool for success/fail	return p.second;}template <typename T>bool ConfigParser<T>::exists(const string & attribute){	// find attribute, return iterator to location and bool	pair< typename map<string, T>::iterator, bool > 		p = parsed->find( attribute );	// p.second is a bool for success/fail	return p.second;}template <typename T>bool ConfigParser<T>::write(){	if( isParsed() )	{		ofstream fout( filename.c_str(), std::ios::trunc );		if( fout.is_open() )		{			typename map<string, T>::iterator i = parsed->begin();			for(; i != parsed->end(); i++)			{				fout << i->first << ' ' << i->second << '\n';			}			fout.close();			return true;		}	}		return false;}#endif