args.h 9.6 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 43 44 45 46
    {
      for(int i=0; i<argc; ++i)
      {
        std::string temp(argv[i]) ;
        std::string key;
        std::string data;

        if(temp.compare(0, 2, "--") == 0)
47
        {
48 49 50 51 52
            key = temp.substr(2, temp.size()-2) ;
  #ifdef DEBUG_ARGS
            std::cout << "<<DEBUG>> (" << i << ")" << key << " -> [ ";
  #endif
            if(++i < argc)
53
            {
54 55
                std::string next(argv[i]) ;
                if(next[0] == '-')
56
                {
57 58 59 60 61
                    --i;
                }
                else if(next[0] == '[' && next[next.size()-1] != ']')
                {
                    data.append(next);
62

63 64 65 66 67 68 69 70 71 72 73 74 75
                    ++i;
                    while(i < argc && next[next.size()-1] != ']')
                    {
                        next = argv[i] ;
                        data.append(" ");
  #ifdef DEBUG_ARGS
                        std::cout << " ";
  #endif
                        data.append(next);
  #ifdef DEBUG_ARGS
                        std::cout << "(" << i << ")" << next;
  #endif
                        ++i;
76
                    }
77
                    --i;
78
                }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                else
                {
  #ifdef DEBUG_ARGS
                    std::cout << next;
  #endif
                    data.append(next);
                }
            }//end of if 
  #ifdef DEBUG_ARGS
            std::cout << "]" << std::endl;
  #endif
            _map.insert(std::pair<std::string, std::string>(key, data)) ;
        }//end of if test with "--"
      }//end of for-loop
    }
		
    ~arguments()
96 97
		{
		}
98

99
		//! \brief is the elements in the command line ?
100 101 102 103 104 105 106 107 108 109
		bool is_defined(const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return true ;
			}
			else
			{
				return false ;
			}
110
    }
111

112 113 114 115 116 117 118
    //! \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)
119
        {
120 121 122 123 124
            return _map.find(key)->second[0] == '[' ;
        }
        else
        {
            return false ;
125
        }
126
    }
127

128
		//! \brief access the element stored value
129 130
		std::string operator[](const std::string& key) const
		{
131 132 133 134 135 136
  		if(_map.count(key) > 0)
  		{
  			return _map.find(key)->second ;
  		}
  		else
  		{
137
                //std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
138 139 140 141 142 143 144 145 146 147 148
  			return std::string() ;
  		}
    }
    
    //! \brief update the value \a val stored under key \a key
    void update(const std::string& key, const std::string& val)
    {
        _map[key] = val;
    }
		
    //! \brief acces to the string value associated with the parameter
149 150 151 152 153 154 155 156 157 158 159
		//! \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 ;
		}
160

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 178
		//! \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.
179
		int get_int(const std::string& key, int default_value = 0) const
180
		{
181
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
182
				return atoi(_map.find(key)->second.c_str()) ;
183
			else
184
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
185
		} 
186 187
	
  	//! \brief acces to a vector of float of size \a size associated with
188 189 190 191
		//! the parameter \a key.
		//!
		//! The \a default_value argument will be returned if the \a key
		//! has no associated value.
192
		vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
Laurent Belcour's avatar
Laurent Belcour committed
193
		{
194
			vec res(size);
195 196
			for(int i=0; i<size; ++i)
				res[i] = default_value;
197

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

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

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

235 236 237 238 239 240
		//! \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
		{
241
		  std::vector<T> res;
242

243 244 245 246
      //TODO: RP: it is not very efficient to call count + find 
      // because find is called twice !!! 
      // My advice: find should be called once, check if the key exist
      // and if yes use the iterator returned by find
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			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;
		}

271
    //RP: why is this method coded and specialized ?
272 273 274 275 276 277
    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;
278

279 280 281 282 283 284
          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);
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
                  if(ppos != std::string::npos)
                  {
                      std::string temp = s.substr(pos, ppos-pos);
                      res.push_back(temp);
                      pos = ppos+1;
                  }
                  else
                  {
                      std::string temp = s.substr(pos, std::string::npos);
                      temp = temp.substr(0, temp.size()-1);
                      res.push_back(temp);
                      pos = ppos;
                  }
              }
          }
301
        }
302 303
      return res;
    }
304

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

			  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();
350 351
			  char** argv;
			  argv = new char*[argc];
352 353 354 355 356 357
			  for(int i=0; i<argc; ++i)
			  {
				  argv[i] = &cmd_vec[i][0];
			  }

			  arguments current_args(argc, argv);
358
			  delete[] argv;
359 360 361
			  return current_args;
		  }

362 363 364 365 366
      //! \brief Friend function to display the content of an argument object
      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)
367
          {
368
              out<< "[" << it->first << "] -> " << it->second << std::endl;
369
          }
370 371
          return out;
      }
372

373

374 375 376 377 378
	private: // data

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

} ;