args.h 7.58 KB
Newer Older
1 2 3
#pragma once

#include <string>
4
#include <sstream>
5 6 7 8
#include <map>
#include <cstdlib>
#include <iostream>

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

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

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

35 36 37 38
                if(temp.compare(0, 2, "--") == 0)
                {
//#define DEBUG_ARGS
                    key = temp.substr(2, temp.size()-2) ;
39
#ifdef DEBUG_ARGS
40
                    std::cout << "<<DEBUG>> (" << i << ")" << key << " -> [ ";
41
#endif
42 43 44 45 46 47 48
                    if(++i < argc)
                    {

                        std::string next(argv[i]) ;
                        if(next[0] == '[' && next[next.size()-1] != ']')
                        {
                            data.append(next);
49

50 51 52 53 54
                            ++i;
                            while(i < argc && next[next.size()-1] != ']')
                            {
                                next = argv[i] ;
                                data.append(" ");
55
#ifdef DEBUG_ARGS
56
                                std::cout << " ";
57
#endif
58
                                data.append(next);
59
#ifdef DEBUG_ARGS
60
                                std::cout << "(" << i << ")" << next;
61
#endif
62 63 64 65 66 67 68

                                ++i;
                            }
                            --i;
                        }
                        else
                        {
69
#ifdef DEBUG_ARGS
70
                            std::cout << next;
71
#endif
72 73 74 75 76 77 78 79 80 81
                            data.append(next);
                        }
                    }
#ifdef DEBUG_ARGS
                    std::cout << "]" << std::endl;
#endif
                }
                _map.insert(std::pair<std::string, std::string>(key, data)) ;
            }
        }
82 83 84
		~arguments()
		{
		}
85

86
		//! \brief is the elements in the command line ?
87 88 89 90 91 92 93 94 95 96
		bool is_defined(const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return true ;
			}
			else
			{
				return false ;
			}
97
        }
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

        //! \brief is the data at the given key in a vector format?
        //! No matter the type of the data, this function will test is the
        //! mapped string is of type "[ .... ]".
        //! It returns false if there is no associated entry.
        bool is_vec(const std::string& key) const
        {
            if(_map.count(key) > 0)
            {
                return _map.find(key)->second[0] == '[' ;
            }
            else
            {
                return false ;
            }
        }

115
		//! \brief access the element stored value
116 117 118 119 120 121 122 123
		std::string operator[](const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return _map.find(key)->second ;
			}
			else
			{
124
                //std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
125 126
				return std::string() ;
			}
127
        }
128 129 130 131 132
		//! \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.
133
		float get_float(const std::string& key, float default_value = 0.0f) const
134
		{
135
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
136
				return atof(_map.find(key)->second.c_str()) ;
137
			else
138
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
139
		}
140 141 142 143 144
		//! \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.
145
		int get_int(const std::string& key, int default_value = 0) const
146
		{
147
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
148
				return atoi(_map.find(key)->second.c_str()) ;
149
			else
150
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
151
		} 
152 153 154 155 156
		//! \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.
157
		vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
Laurent Belcour's avatar
Laurent Belcour committed
158
		{
159
			vec res(size);
160 161 162
            for(int i=0; i<size; ++i)
                res[i] = default_value;

Laurent Belcour's avatar
Laurent Belcour committed
163 164
			if(_map.count(key) > 0)
			{
Laurent Belcour's avatar
Laurent Belcour committed
165
				std::string s = _map.find(key)->second;
166
				if(s[0] == '[') // Is an array of type [a, b, c]
Laurent Belcour's avatar
Laurent Belcour committed
167 168
				{
					int i = 0;
169
					size_t pos = 1;
170
					while(pos != std::string::npos && i<size)
Laurent Belcour's avatar
Laurent Belcour committed
171
					{
172
						size_t ppos = s.find(',', pos);
Laurent Belcour's avatar
Laurent Belcour committed
173 174 175

						if(ppos != std::string::npos)
						{
176
                            res[i] = atof(s.substr(pos, ppos-pos).c_str());
Laurent Belcour's avatar
Laurent Belcour committed
177 178 179
							pos = ppos+1;
							++i;
						}
180 181
						else
						{
182 183
                            std::string temp = s.substr(pos, std::string::npos);
                            res[i] = atof(temp.substr(0, temp.size()-1).c_str());
184 185 186
							pos = ppos;
							++i;
						}
Laurent Belcour's avatar
Laurent Belcour committed
187 188 189 190 191 192
					}
					return res;
				}
			}

			float val = get_float(key, default_value);
193 194 195 196
			for(int i=0; i<size; ++i)
			{
				res[i] = val;
			}
Laurent Belcour's avatar
Laurent Belcour committed
197 198
			return res;
		}
199

200 201 202 203 204 205
        std::vector<std::string> get_vec(const std::string& key) const
        {
            std::vector<std::string> res;
            if(_map.count(key) > 0)
            {
                std::string s = _map.find(key)->second;
206

207 208 209 210 211 212 213 214 215
                if(s[0] == '[') // Is an array of type [a, b, c]
                {
                    size_t pos = 1;
                    while(pos != std::string::npos)
                    {
                        size_t ppos = s.find(',', pos);

                        if(ppos != std::string::npos)
                        {
216 217
                            std::string temp = s.substr(pos, ppos-pos);
                            res.push_back(temp);
218 219 220 221 222
                            pos = ppos+1;
                        }
                        else
                        {
                            std::string temp = s.substr(pos, std::string::npos);
223 224
                            temp = temp.substr(0, temp.size()-1);
                            res.push_back(temp);
225 226 227 228 229 230 231 232 233
                            pos = ppos;
                        }
                    }
                }
            }

            return res;
        }

234 235 236 237 238 239 240 241 242
		  //! \brief get the reconstructed command line arguments (without
		  //! the executable name
		  std::string get_cmd() const
		  {
			  std::string cmd;

			  std::map<std::string, std::string>::const_iterator it = _map.begin();
			  for(;it != _map.end(); it++)
			  {
243 244 245 246 247 248 249
				  if(!it->first.empty())
				  {
					  cmd.append(" --");
					  cmd.append(it->first);
					  cmd.append(" ");
					  cmd.append(it->second);
				  }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
			  }

			  return cmd;
		  }

		  //! \brief Create an argument object from a string describing a command
		  //! line.
		  static arguments create_arguments(const std::string& n)
		  {
			  std::vector<std::string> cmd_vec;
			  std::stringstream stream(n);
#ifdef DEBUG_ARGS
			  std::cout << "<<DEBUG>> create argument vector: [";
#endif
			  while(stream.good())
			  {
				  std::string temp;
				  stream >> temp;
#ifdef DEBUG_ARGS
				  std::cout << temp << ", ";
#endif

				  cmd_vec.push_back(temp);
			  }
#ifdef DEBUG_ARGS
			  std::cout << "]" << std::endl;
#endif

			  int argc = cmd_vec.size();
279 280
			  char** argv;
			  argv = new char*[argc];
281 282 283 284 285 286
			  for(int i=0; i<argc; ++i)
			  {
				  argv[i] = &cmd_vec[i][0];
			  }

			  arguments current_args(argc, argv);
287
			  delete argv;
288 289 290 291
			  return current_args;
		  }


292 293 294 295 296
	private: // data

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

} ;