Debian Codegraph @

data/0.12-dev/impl/Arguments.h

raw
1 /*
2 * Arguments 2.0 Beta - A Command Line Processing Library
3 * Copyright (C) 2000, 2001 Jared Davis
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef INCLUDED_OPENDELI_ARGUMENTS_H
21 #define INCLUDED_OPENDELI_ARGUMENTS_H
22
23 #if (defined(_WIN32) && defined(_DEBUG))
24 // In visual studio, compiling this library gives you a billion and a half
25 // error messages that "debug information has been truncated to 255 chars"
26 // because the visual studio debugger can't handle symbols longer than 255
27 // characters. This disables this warning so we comile cleanly.
28 #pragma warning( disable: 4786 )
29 #endif
30
31 #include <vector>
32 #include <string>
33 #include <strstream>
34
35 class Arguments
36 {
37 private:
38
39 // Vector of the arguments that take spaces.
40 static std::vector<std::string> s_spaceArgs;
41
42 // Vector of argument-value pairs.
43 std::vector< std::pair<std::string, std::pair<std::string, bool> > > d_arguments;
44
45
46 // explodes a delimited string into its tokens.
47 static void ExplodeString(const std::string& str, std::vector<std::string>& tokens, char delimiter)
48 throw(std::bad_alloc);
49
50 // finds the index of the argument arg.
51 int findArgument(const std::string& arg) const throw();
52
53 //
54 // convert(source, target)
55 // Converts a variable of type A into a variable of type B by passing
56 // it through a strstream.
57 //
58 template<class A, class B>
59 static bool convert(const A& source, B& target) throw()
60 {
61 std::strstream ss;
62 ss.clear();
63 ss << source;
64 ss >> target;
65 return (!ss.bad() && !ss.fail());
66 }
67
68
69 public:
70
71 static void setArgumentsWithSpaces(const std::string& args)
72 throw(std::bad_alloc);
73
74 Arguments(int argc, const char** argv)
75 throw(std::bad_alloc);
76
77 unsigned int size() const throw();
78
79 bool has(const std::string& arg) const throw(std::bad_alloc);
80
81 template<class A, class B> bool get
82 (const std::string& arg, A& value, const B& default_value) const
83 throw(std::bad_alloc)
84 {
85 int index = findArgument(arg);
86
87 if (index == -1) {
88 value = default_value;
89 return false;
90 }
91
92 else
93 {
94 bool ret = convert(d_arguments[index].second.first, value);
95 if (!ret)
96 value = default_value;
97 return ret;
98 }
99 }
100
101
102 #ifndef NDEBUG
103 void debug() const;
104 #endif
105
106 };
107
108 #endif // INCLUDED_OPENDELI_ARGUMENTS_H