Subversion Repositories gelsvn

Rev

Rev 448 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 448 Rev 595
-
 
1
/**
-
 
2
 * @file ArgExtracter.h
-
 
3
 * @brief Simple functionality for getting command line parameters.
-
 
4
 */
-
 
5
/* ----------------------------------------------------------------------- *
-
 
6
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
-
 
7
 * Copyright (C) the authors and DTU Informatics
-
 
8
 * For license and list of authors, see ../../doc/intro.pdf
-
 
9
 * ----------------------------------------------------------------------- */
-
 
10
 
1
#ifndef __UTIL_ARG_EXTRACTER__
11
#ifndef __UTIL_ARG_EXTRACTER__
2
#define __UTIL_ARG_EXTRACTER__
12
#define __UTIL_ARG_EXTRACTER__
3
#include <vector>
13
#include <vector>
4
#include <algorithm>
14
#include <algorithm>
5
#include <cstdlib>
15
#include <cstdlib>
6
#include <list>
16
#include <list>
7
#include <string>
17
#include <string>
8
#include <cassert>
18
#include <cassert>
9
 
19
 
10
namespace Util
20
namespace Util
11
{
21
{
12
		template<class T>
22
    template<class T>
13
		inline T string_convert(const std::string& x)
23
    inline T string_convert(const std::string& x)
14
		{
24
    {
15
            assert(0);
25
        assert(0);
16
				T t;
26
        T t;
17
				return t;
27
        return t;
18
		}
28
    }
19
		template<> 
29
    template<> 
20
		inline int string_convert(const std::string& x){ 
30
    inline int string_convert(const std::string& x){ 
21
				return std::atoi(x.c_str());}
31
        return std::atoi(x.c_str());}
22
		template<> 
32
    template<> 
23
		inline float string_convert(const std::string& x){ 
33
    inline float string_convert(const std::string& x){ 
24
				return static_cast<float>(std::atof(x.c_str()));}
34
        return static_cast<float>(std::atof(x.c_str()));}
25
		template<> 
35
    template<> 
26
		inline double string_convert(const std::string& x){ 
36
    inline double string_convert(const std::string& x){ 
27
				return std::atof(x.c_str());}
37
        return std::atof(x.c_str());}
28
		template<> 
38
    template<> 
29
		inline std::string string_convert(const std::string& x){ 
39
    inline std::string string_convert(const std::string& x){ 
30
				return x;}
40
        return x;}
31
 
41
 
32
 
42
 
33
		struct UpCase {void operator()(char& x) {x=toupper(x);}};
43
    struct UpCase {void operator()(char& x) {x=toupper(x);}};
34
 
44
 
35
		inline void up_case_string(std::string& s)
45
    inline void up_case_string(std::string& s)
36
		{
46
    {
37
				std::for_each(s.begin(), s.end(), UpCase());
47
        std::for_each(s.begin(), s.end(), UpCase());
38
		}
48
    }
39
 
-
 
40
 
49
 
41
 
50
 
42
 
51
 
-
 
52
    /// Extract arguments from command line parameters.
43
		class ArgExtracter
53
    class ArgExtracter
44
		{	
54
    {	
45
				std::list<std::string> avec;
55
        std::list<std::string> avec;
46
				typedef std::list<std::string>::iterator LSI;
56
        typedef std::list<std::string>::iterator LSI;
47
 
57
 
48
				bool extract(const std::string& argname, LSI& iter)
58
        bool extract(const std::string& argname, LSI& iter)
49
						{
59
        {
50
								for(iter = avec.begin();iter != avec.end(); ++iter)
60
            for(iter = avec.begin();iter != avec.end(); ++iter)
51
								{
61
            {
52
										if((*iter)==argname)
62
                if((*iter)==argname)
53
										{
63
                {
54
												iter = avec.erase(iter);
64
                    iter = avec.erase(iter);
55
												return true;
65
                    return true;
56
										}
66
                }
57
								}
67
            }
58
								return false;
68
            return false;
59
						}
69
        }
60
 
70
 
61
		public:
71
    public:
62
 
72
 
63
				ArgExtracter(int argc, char **argv)
73
        ArgExtracter(int argc, char **argv)
64
						{
74
        {
65
								for(int i=0;i<argc; ++i)
75
            for(int i=0;i<argc; ++i)
66
										avec.push_back(std::string(argv[i]));
76
                avec.push_back(std::string(argv[i]));
67
						}
77
        }
68
 
78
 
69
				bool extract(const std::string& argname)
79
        bool extract(const std::string& argname)
70
						{
80
        {
71
								LSI iter;
81
            LSI iter;
72
								return extract(argname, iter);
82
            return extract(argname, iter);
73
						}
83
        }
74
 
84
 
75
				template<class T>
85
        template<class T>
76
				bool extract(const std::string& argname, T& val)
86
        bool extract(const std::string& argname, T& val)
77
						{
87
        {
78
								LSI iter;
88
            LSI iter;
79
								if(extract(argname, iter))
89
            if(extract(argname, iter))
80
								{
90
            {
81
										val = string_convert<T>(iter->c_str());
91
                val = string_convert<T>(iter->c_str());
82
										avec.erase(iter);
92
                avec.erase(iter);
83
										return true;
93
                return true;
84
								}
94
            }
85
								return false;
95
            return false;
86
						}
96
        }
87
 
97
 
88
				int no_remaining_args() const
98
        size_t no_remaining_args() const
89
						{
99
        {
90
								return avec.size();
100
            return avec.size();
91
						}
101
        }
92
 
102
 
93
				const std::string& get_last_arg() const
103
        const std::string& get_last_arg() const
94
						{
104
        {
95
              static const std::string emptystring("");
105
            static const std::string emptystring("");
96
							if(no_remaining_args() > 0)
106
            if(no_remaining_args() > 0)
97
								return avec.back();
107
                return avec.back();
98
              return emptystring;
108
            return emptystring;
99
						}
109
        }
100
 
110
 
101
				void get_all_args(std::vector<std::string>& args)
111
        void get_all_args(std::vector<std::string>& args)
102
						{
112
        {
103
								args = std::vector<std::string>(avec.begin(), avec.end());
113
            args = std::vector<std::string>(avec.begin(), avec.end());
104
						}
114
        }
105
		};
115
    };
106
 
116
 
107
}
117
}
108
 
118
 
109
 
119
 
110
#endif
120
#endif
111
 
121