Subversion Repositories gelsvn

Rev

Rev 373 | Rev 410 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 jab 1
#ifndef ARG_EXTRACTER
2
#define ARG_EXTRACTER
3
#include <vector>
4
#include <algorithm>
5
#include <cstdlib>
6
#include <list>
7
#include <string>
349 awk 8
#include <cassert>
60 jab 9
 
10
namespace Util
11
{
12
		template<class T>
13
		inline T string_convert(const std::string& x)
14
		{
349 awk 15
            assert(0);
60 jab 16
				T t;
17
				return t;
18
		}
19
		template<> 
20
		inline int string_convert(const std::string& x){ 
21
				return std::atoi(x.c_str());}
22
		template<> 
23
		inline float string_convert(const std::string& x){ 
373 jrf 24
				return static_cast<float>(std::atof(x.c_str()));}
60 jab 25
		template<> 
26
		inline double string_convert(const std::string& x){ 
27
				return std::atof(x.c_str());}
28
		template<> 
29
		inline std::string string_convert(const std::string& x){ 
30
				return x;}
31
 
32
 
33
		struct UpCase {void operator()(char& x) {x=toupper(x);}};
34
 
35
		inline void up_case_string(std::string& s)
36
		{
37
				std::for_each(s.begin(), s.end(), UpCase());
38
		}
39
 
40
 
41
 
42
 
43
		class ArgExtracter
44
		{	
45
				std::list<std::string> avec;
46
				typedef std::list<std::string>::iterator LSI;
47
 
48
				bool extract(const std::string& argname, LSI& iter)
49
						{
50
								for(iter = avec.begin();iter != avec.end(); ++iter)
51
								{
52
										if((*iter)==argname)
53
										{
54
												iter = avec.erase(iter);
55
												return true;
56
										}
57
								}
58
								return false;
59
						}
60
 
61
		public:
62
 
63
				ArgExtracter(int argc, char **argv)
64
						{
65
								for(int i=0;i<argc; ++i)
66
										avec.push_back(std::string(argv[i]));
67
						}
68
 
69
				bool extract(const std::string& argname)
70
						{
71
								LSI iter;
72
								return extract(argname, iter);
73
						}
74
 
75
				template<class T>
76
				bool extract(const std::string& argname, T& val)
77
						{
78
								LSI iter;
79
								if(extract(argname, iter))
80
								{
81
										val = string_convert<T>(iter->c_str());
82
										avec.erase(iter);
83
										return true;
84
								}
85
								return false;
86
						}
87
 
88
				int no_remaining_args() const
89
						{
90
								return avec.size();
91
						}
92
 
93
				const std::string& get_last_arg() const
94
						{
382 jab 95
							if(no_remaining_args() > 0)
60 jab 96
								return avec.back();
97
						}
98
 
99
				void get_all_args(std::vector<std::string>& args)
100
						{
101
								args = std::vector<std::string>(avec.begin(), avec.end());
102
						}
103
		};
104
 
105
}
106
 
107
 
108
#endif