args.h 9.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* ALTA --- Analysis of Bidirectional Reflectance Distribution Functions

   Copyright (C) 2013, 2014 Inria

   This file is part of ALTA.

   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0.  If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/.  */

11 12 13
#pragma once

#include <string>
14
#include <sstream>
15 16 17 18
#include <map>
#include <cstdlib>
#include <iostream>

Laurent Belcour's avatar
Laurent Belcour committed
19 20
#include "common.h"

21 22
/*! \brief A useful class for storing the high-level arguments of a program
 *  or a function.
23 24
 *  \ingroup core
 *  \internal
25 26 27 28 29
 *
 *  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.
 */
30 31 32 33 34
class arguments
{
	public: // functions

		// Constructor and destructor
Laurent Belcour's avatar
Laurent Belcour committed
35 36
		arguments()
		{
37
		}
38
		arguments(int argc, char** const argv)
39 40 41 42
        {
            for(int i=0; i<argc; ++i)
            {
                std::string temp(argv[i]) ;
43 44
                std::string key;
                std::string data;
45

46 47 48 49
                if(temp.compare(0, 2, "--") == 0)
                {
//#define DEBUG_ARGS
                    key = temp.substr(2, temp.size()-2) ;
50
#ifdef DEBUG_ARGS
51
                    std::cout << "<<DEBUG>> (" << i << ")" << key << " -> [ ";
52
#endif
53 54 55 56
                    if(++i < argc)
                    {

                        std::string next(argv[i]) ;
57 58 59 60
                        if(next[0] == '-')
                        {
                            --i;
                        }
61
                        else if(next[0] == '[' && next[next.size()-1] != ']')
62 63
                        {
                            data.append(next);
64

65 66 67 68 69
                            ++i;
                            while(i < argc && next[next.size()-1] != ']')
                            {
                                next = argv[i] ;
                                data.append(" ");
70
#ifdef DEBUG_ARGS
71
                                std::cout << " ";
72
#endif
73
                                data.append(next);
74
#ifdef DEBUG_ARGS
75
                                std::cout << "(" << i << ")" << next;
76
#endif
77 78 79 80 81 82 83

                                ++i;
                            }
                            --i;
                        }
                        else
                        {
84
#ifdef DEBUG_ARGS
85
                            std::cout << next;
86
#endif
87 88 89 90 91 92
                            data.append(next);
                        }
                    }
#ifdef DEBUG_ARGS
                    std::cout << "]" << std::endl;
#endif
93
                    _map.insert(std::pair<std::string, std::string>(key, data)) ;
94
                }
95

96 97
            }
        }
98 99 100
		~arguments()
		{
		}
101

102
		//! \brief is the elements in the command line ?
103 104 105 106 107 108 109 110 111 112
		bool is_defined(const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return true ;
			}
			else
			{
				return false ;
			}
113
        }
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

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

131
		//! \brief access the element stored value
132 133 134 135 136 137 138 139
		std::string operator[](const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return _map.find(key)->second ;
			}
			else
			{
140
                //std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
141 142
				return std::string() ;
			}
143 144
        }
        //! \brief update the value \a val stored under key \a key
145
        void update(const std::string& key, const std::string& val)
146 147
        {
            _map[key] = val;
148
        }
149 150 151 152 153 154 155 156 157 158 159 160
		//! \brief acces to the string value associated with the parameter
		//! \a key.
		//!
		//! The \a default_value argument will be returned if the \a key
		//! has no associated value.
		std::string get_string(const std::string& key, std::string default_value = "") const
		{
			if(_map.count(key) > 0)
				return _map.find(key)->second.c_str() ;
			else
				return default_value ;
		}
161 162 163 164 165
		//! \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.
166
		float get_float(const std::string& key, float default_value = 0.0f) const
167
		{
168
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
169
				return (float)atof(_map.find(key)->second.c_str()) ;
170
			else
171
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
172
		}
173 174 175 176 177
		//! \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.
178
		int get_int(const std::string& key, int default_value = 0) const
179
		{
180
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
181
				return atoi(_map.find(key)->second.c_str()) ;
182
			else
183
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
184
		} 
185 186 187 188 189
		//! \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.
190
		vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
Laurent Belcour's avatar
Laurent Belcour committed
191
		{
192
			vec res(size);
193 194
			for(int i=0; i<size; ++i)
				res[i] = default_value;
195

Laurent Belcour's avatar
Laurent Belcour committed
196 197
			if(_map.count(key) > 0)
			{
Laurent Belcour's avatar
Laurent Belcour committed
198
				std::string s = _map.find(key)->second;
199
				if(s[0] == '[') // Is an array of type [a, b, c]
Laurent Belcour's avatar
Laurent Belcour committed
200 201
				{
					int i = 0;
202
					size_t pos = 1;
203
					while(pos != std::string::npos && i<size)
Laurent Belcour's avatar
Laurent Belcour committed
204
					{
205
						size_t ppos = s.find(',', pos);
Laurent Belcour's avatar
Laurent Belcour committed
206 207 208

						if(ppos != std::string::npos)
						{
209
							res[i] = atof(s.substr(pos, ppos-pos).c_str());
Laurent Belcour's avatar
Laurent Belcour committed
210 211 212
							pos = ppos+1;
							++i;
						}
213 214
						else
						{
215 216
							std::string temp = s.substr(pos, std::string::npos);
							res[i] = atof(temp.substr(0, temp.size()-1).c_str());
217 218 219
							pos = ppos;
							++i;
						}
Laurent Belcour's avatar
Laurent Belcour committed
220 221 222 223 224 225
					}
					return res;
				}
			}

			float val = get_float(key, default_value);
226 227 228 229
			for(int i=0; i<size; ++i)
			{
				res[i] = val;
			}
Laurent Belcour's avatar
Laurent Belcour committed
230 231
			return res;
		}
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		//! \brief access a vector of element of type \a T of variable length.
		//! the string associated with the key \a key should have the form
		//! "[a, b, c]" where a, b, c are of type \a T.
		template<typename T>
		std::vector<T> get_vec(const std::string& key) const
		{
			std::vector<T> res;

			if(_map.count(key) > 0)
			{
				std::string str = _map.find(key)->second;
				if(str[0] == '[' && str[str.size()-1] == ']') // Is an array of type [a, b, c]
				{
					std::stringstream stream(str.substr(1, str.size()-2));
					char s[256];
					while(stream.good())
					{
						// Extract the value v from the stream s
						stream.get(s, 256, ',');
						stream.ignore(1);
						std::stringstream sstream(s);
						T v;
						sstream >> v;

						// Push the value at the end of the vector
						res.push_back(v);
					}
				}
			}
			return res;
		}

265 266 267 268 269 270
        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;
271

272 273 274 275 276 277 278 279 280
                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)
                        {
281 282
                            std::string temp = s.substr(pos, ppos-pos);
                            res.push_back(temp);
283 284 285 286 287
                            pos = ppos+1;
                        }
                        else
                        {
                            std::string temp = s.substr(pos, std::string::npos);
288 289
                            temp = temp.substr(0, temp.size()-1);
                            res.push_back(temp);
290 291 292 293 294 295 296 297 298
                            pos = ppos;
                        }
                    }
                }
            }

            return res;
        }

299 300 301 302 303 304 305 306 307
		  //! \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++)
			  {
308 309 310 311 312 313 314
				  if(!it->first.empty())
				  {
					  cmd.append(" --");
					  cmd.append(it->first);
					  cmd.append(" ");
					  cmd.append(it->second);
				  }
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
			  }

			  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();
344 345
			  char** argv;
			  argv = new char*[argc];
346 347 348 349 350 351
			  for(int i=0; i<argc; ++i)
			  {
				  argv[i] = &cmd_vec[i][0];
			  }

			  arguments current_args(argc, argv);
352
			  delete[] argv;
353 354 355
			  return current_args;
		  }

356 357 358 359 360 361 362 363 364 365
          friend std::ostream& operator<<(std::ostream& out, const arguments& args)
          {
              std::map<std::string, std::string>::const_iterator it;
              for(it=args._map.begin(); it!=args._map.end(); ++it)
              {
                  out<< "[" << it->first << "] -> " << it->second << std::endl;
              }
              return out;
          }

366

367 368 369 370 371
	private: // data

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

} ;