args.h 2.4 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 12 13 14
class arguments
{
	public: // functions

		// Constructor and destructor
Laurent Belcour's avatar
Laurent Belcour committed
15 16 17
		arguments()
		{
		} ;
18 19 20 21 22 23 24 25 26 27 28 29
		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) ;
30 31
					int j = i+1;
					while(j < argc) 
32
					{
33
						std::string next(argv[j]) ;
34 35
						if(next.compare(0, 2, "--") != 0)
						{
36
							data.append(next) ;
37
						}
38 39 40 41 42
						else
						{
							break ;
						}
						++j;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
					}
				}
				_map.insert(std::pair<std::string, std::string>(key, data)) ;
			}
		} ;
		~arguments() { } ;

		// Access elements
		bool is_defined(const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return true ;
			}
			else
			{
				return false ;
			}
		} ;
		std::string operator[](const std::string& key) const
		{
			if(_map.count(key) > 0)
			{
				return _map.find(key)->second ;
			}
			else
			{
				std::cerr << "Underfined request to key : \"" << key << "\"" << std::endl ;
				return std::string() ;
			}
		} ;
74
		float get_float(const std::string& key, float default_value = 0.0f) const
75
		{
76
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
77
				return atof(_map.find(key)->second.c_str()) ;
78
			else
79
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
80
		}
81
		int get_int(const std::string& key, int default_value = 0) const
82
		{
83
			if(_map.count(key) > 0)
Laurent Belcour's avatar
Laurent Belcour committed
84
				return atoi(_map.find(key)->second.c_str()) ;
85
			else
86
				return default_value ;
Laurent Belcour's avatar
Laurent Belcour committed
87
		} 
88
		vec get_vec(const std::string& key, int size, float default_value = 0.0f) const
Laurent Belcour's avatar
Laurent Belcour committed
89
		{
90
			vec res(size);
Laurent Belcour's avatar
Laurent Belcour committed
91 92
			if(_map.count(key) > 0)
			{
Laurent Belcour's avatar
Laurent Belcour committed
93
				std::string s = _map.find(key)->second;
Laurent Belcour's avatar
Laurent Belcour committed
94 95 96
				if(s[0] == '\[') // Is an array of type [a, b, c]
				{
					int i = 0;
97
					size_t pos = 1;
98
					while(pos != std::string::npos && i<size)
Laurent Belcour's avatar
Laurent Belcour committed
99
					{
100
						size_t ppos = s.find(',', pos);
Laurent Belcour's avatar
Laurent Belcour committed
101 102 103 104 105 106 107

						if(ppos != std::string::npos)
						{
							res[i] = atof(s.substr(pos, ppos).c_str());
							pos = ppos+1;
							++i;
						}
108 109 110 111 112 113
						else
						{
							res[i] = atof(s.substr(pos, ppos-1).c_str());
							pos = ppos;
							++i;
						}
Laurent Belcour's avatar
Laurent Belcour committed
114 115 116 117 118 119
					}
					return res;
				}
			}

			float val = get_float(key, default_value);
120 121 122 123
			for(int i=0; i<size; ++i)
			{
				res[i] = val;
			}
Laurent Belcour's avatar
Laurent Belcour committed
124 125
			return res;
		}
126 127 128 129 130 131

	private: // data

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

} ;