args.h 3.49 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
		arguments()
		{
26
        }
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
					}
				}
				_map.insert(std::pair<std::string, std::string>(key, data)) ;
			}
56 57 58 59
        }
        ~arguments()
        {
        }
60

61
		//! \brief is the elements in the command line ?
62 63 64 65 66 67 68 69 70 71
		bool is_defined(const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return true ;
			}
			else
			{
				return false ;
			}
72
        }
73
		//! \brief access the element stored value
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
			{
82
                //std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
83 84
				return std::string() ;
			}
85
        }
86 87 88 89 90
		//! \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.
91
		float get_float(const std::string& key, float default_value = 0.0f) const
92
		{
93
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
94
				return atof(_map.find(key)->second.c_str()) ;
95
			else
96
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
97
		}
98 99 100 101 102
		//! \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.
103
		int get_int(const std::string& key, int default_value = 0) const
104
		{
105
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
106
				return atoi(_map.find(key)->second.c_str()) ;
107
			else
108
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
109
		} 
110 111 112 113 114
		//! \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.
115
		vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
Laurent Belcour's avatar
Laurent Belcour committed
116
		{
117
			vec res(size);
118 119 120
            for(int i=0; i<size; ++i)
                res[i] = default_value;

Laurent Belcour's avatar
Laurent Belcour committed
121 122
			if(_map.count(key) > 0)
			{
Laurent Belcour's avatar
Laurent Belcour committed
123
				std::string s = _map.find(key)->second;
124
				if(s[0] == '[') // Is an array of type [a, b, c]
Laurent Belcour's avatar
Laurent Belcour committed
125 126
				{
					int i = 0;
127
					size_t pos = 1;
128
					while(pos != std::string::npos && i<size)
Laurent Belcour's avatar
Laurent Belcour committed
129
					{
130
						size_t ppos = s.find(',', pos);
Laurent Belcour's avatar
Laurent Belcour committed
131 132 133 134 135 136 137

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

			float val = get_float(key, default_value);
150 151 152 153
			for(int i=0; i<size; ++i)
			{
				res[i] = val;
			}
Laurent Belcour's avatar
Laurent Belcour committed
154 155
			return res;
		}
156 157 158 159 160 161

	private: // data

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

} ;