Subversion Repositories gelsvn

Rev

Rev 349 | Rev 382 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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