args.h 3.38 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
/*! \brief A useful class for storing the high-level arguments of a program
 *  or a function.
12 13
 *  \ingroup core
 *  \internal
14 15 16 17 18
 *
 *  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.
 */
19 20 21 22 23
class arguments
{
	public: // functions

		// Constructor and destructor
Laurent Belcour's avatar
Laurent Belcour committed
24 25 26
		arguments()
		{
		} ;
27 28 29 30 31 32 33 34 35 36 37 38
		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) ;
39 40
					int j = i+1;
					while(j < argc) 
41
					{
42
						std::string next(argv[j]) ;
43 44
						if(next.compare(0, 2, "--") != 0)
						{
45
							data.append(next) ;
46
						}
47 48 49 50 51
						else
						{
							break ;
						}
						++j;
52 53 54 55 56 57 58
					}
				}
				_map.insert(std::pair<std::string, std::string>(key, data)) ;
			}
		} ;
		~arguments() { } ;

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

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

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

	private: // data

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

} ;