Subversion Repositories gelsvn

Rev

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