args.h 7.75 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
                    if(++i < argc)
                    {

                        std::string next(argv[i]) ;
46 47 48 49 50 51
                        if(next[0] == '-')
                        {
                            --i;
                            continue;
                        }

52 53 54
                        if(next[0] == '[' && next[next.size()-1] != ']')
                        {
                            data.append(next);
55

56 57 58 59 60
                            ++i;
                            while(i < argc && next[next.size()-1] != ']')
                            {
                                next = argv[i] ;
                                data.append(" ");
61
#ifdef DEBUG_ARGS
62
                                std::cout << " ";
63
#endif
64
                                data.append(next);
65
#ifdef DEBUG_ARGS
66
                                std::cout << "(" << i << ")" << next;
67
#endif
68 69 70 71 72 73 74

                                ++i;
                            }
                            --i;
                        }
                        else
                        {
75
#ifdef DEBUG_ARGS
76
                            std::cout << next;
77
#endif
78 79 80 81 82 83 84 85 86 87
                            data.append(next);
                        }
                    }
#ifdef DEBUG_ARGS
                    std::cout << "]" << std::endl;
#endif
                }
                _map.insert(std::pair<std::string, std::string>(key, data)) ;
            }
        }
88 89 90
		~arguments()
		{
		}
91

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

        //! \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 ;
            }
        }

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

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

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

			float val = get_float(key, default_value);
199 200 201 202
			for(int i=0; i<size; ++i)
			{
				res[i] = val;
			}
Laurent Belcour's avatar
Laurent Belcour committed
203 204
			return res;
		}
205

206 207 208 209 210 211
        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;
212

213 214 215 216 217 218 219 220 221
                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)
                        {
222 223
                            std::string temp = s.substr(pos, ppos-pos);
                            res.push_back(temp);
224 225 226 227 228
                            pos = ppos+1;
                        }
                        else
                        {
                            std::string temp = s.substr(pos, std::string::npos);
229 230
                            temp = temp.substr(0, temp.size()-1);
                            res.push_back(temp);
231 232 233 234 235 236 237 238 239
                            pos = ppos;
                        }
                    }
                }
            }

            return res;
        }

240 241 242 243 244 245 246 247 248
		  //! \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++)
			  {
249 250 251 252 253 254 255
				  if(!it->first.empty())
				  {
					  cmd.append(" --");
					  cmd.append(it->first);
					  cmd.append(" ");
					  cmd.append(it->second);
				  }
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			  }

			  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();
285 286
			  char** argv;
			  argv = new char*[argc];
287 288 289 290 291 292
			  for(int i=0; i<argc; ++i)
			  {
				  argv[i] = &cmd_vec[i][0];
			  }

			  arguments current_args(argc, argv);
293
			  delete argv;
294 295 296 297
			  return current_args;
		  }


298 299 300 301 302
	private: // data

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

} ;