args.h 3.33 KB
Newer Older
1 2 3 4 5 6 7
#pragma once

#include <string>
#include <map>
#include <cstdlib>
#include <iostream>

Laurent Belcour's avatar
Laurent Belcour committed
8 9
#include "common.h"

10 11 12 13 14 15 16
/*! \brief A useful class for storing the high-level arguments of a program
 *  or a function.
 *
 *  The set of parameters are parsed from the command line using the 
 *  constructor. They are stored as std::string in a std::map.
 *  \todo add functionalities to provide new parameters values.
 */
17 18 19 20 21
class arguments
{
	public: // functions

		// Constructor and destructor
Laurent Belcour's avatar
Laurent Belcour committed
22 23 24
		arguments()
		{
		} ;
25 26 27 28 29 30 31 32 33 34 35 36
		arguments(int argc, char** const argv)
		{
			std::string key ;
			std::string data ;
			for(int i=0; i<argc; ++i)
			{
				std::string temp(argv[i]) ;
				std::string data ;

				if(temp.compare(0, 2, "--") == 0)
				{
					key = temp.substr(2, temp.size()-2) ;
37 38
					int j = i+1;
					while(j < argc) 
39
					{
40
						std::string next(argv[j]) ;
41 42
						if(next.compare(0, 2, "--") != 0)
						{
43
							data.append(next) ;
44
						}
45 46 47 48 49
						else
						{
							break ;
						}
						++j;
50 51 52 53 54 55 56
					}
				}
				_map.insert(std::pair<std::string, std::string>(key, data)) ;
			}
		} ;
		~arguments() { } ;

57
		//! \brief is the elements in the command line ?
58 59 60 61 62 63 64 65 66 67 68
		bool is_defined(const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return true ;
			}
			else
			{
				return false ;
			}
		} ;
69
		//! \brief access the element stored value
70 71 72 73 74 75 76 77 78 79 80 81
		std::string operator[](const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return _map.find(key)->second ;
			}
			else
			{
				std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
				return std::string() ;
			}
		} ;
82 83 84 85 86
		//! \brief acces to the float value associated with the parameter
		//! \a key.
		//!
		//! The \a default_value argument will be returned if the \a key
		//! has no associated value.
87
		float get_float(const std::string& key, float default_value = 0.0f) const
88
		{
89
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
90
				return atof(_map.find(key)->second.c_str()) ;
91
			else
92
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
93
		}
94 95 96 97 98
		//! \brief acces to the integer value associated with the parameter
		//! \a key.
		//!
		//! The \a default_value argument will be returned if the \a key
		//! has no associated value.
99
		int get_int(const std::string& key, int default_value = 0) const
100
		{
101
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
102
				return atoi(_map.find(key)->second.c_str()) ;
103
			else
104
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
105
		} 
106 107 108 109 110
		//! \brief acces to a vector of float of size \a size associated with
		//! the parameter \a key.
		//!
		//! The \a default_value argument will be returned if the \a key
		//! has no associated value.
111
		vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
Laurent Belcour's avatar
Laurent Belcour committed
112
		{
113
			vec res(size);
Laurent Belcour's avatar
Laurent Belcour committed
114 115
			if(_map.count(key) > 0)
			{
Laurent Belcour's avatar
Laurent Belcour committed
116
				std::string s = _map.find(key)->second;
Laurent Belcour's avatar
Laurent Belcour committed
117 118 119
				if(s[0] == '\[') // Is an array of type [a, b, c]
				{
					int i = 0;
120
					size_t pos = 1;
121
					while(pos != std::string::npos && i<size)
Laurent Belcour's avatar
Laurent Belcour committed
122
					{
123
						size_t ppos = s.find(',', pos);
Laurent Belcour's avatar
Laurent Belcour committed
124 125 126 127 128 129 130

						if(ppos != std::string::npos)
						{
							res[i] = atof(s.substr(pos, ppos).c_str());
							pos = ppos+1;
							++i;
						}
131 132 133 134 135 136
						else
						{
							res[i] = atof(s.substr(pos, ppos-1).c_str());
							pos = ppos;
							++i;
						}
Laurent Belcour's avatar
Laurent Belcour committed
137 138 139 140 141 142
					}
					return res;
				}
			}

			float val = get_float(key, default_value);
143 144 145 146
			for(int i=0; i<size; ++i)
			{
				res[i] = val;
			}
Laurent Belcour's avatar
Laurent Belcour committed
147 148
			return res;
		}
149 150 151 152 153 154

	private: // data

		std::map<std::string, std::string> _map ;

} ;