Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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