close Warning: Can't use blame annotator:
No changeset 1891 in the repository

source: source/ariba/communication/modules/transport/protlib/configuration.h@ 4987

Last change on this file since 4987 was 4987, checked in by Christoph Mayer, 15 years ago

isrunning function für timer

File size: 5.7 KB
RevLine 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file configuration.h
3/// Handling of simple (key, value) configuration files
4/// ----------------------------------------------------------
5/// $Id: configuration.h 2549 2007-04-02 22:17:37Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/configuration.h $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29#ifndef NATFW__CONFIG_H
30#define NATFW__CONFIG_H
31
32#include <fstream>
33#include <map>
34#include <list>
35#include <exception>
36
37#include "address.h"
38
39namespace natfw {
40 using protlib::hostaddress;
41
42
43/**
44 * An exception to be thrown if an invalid configuration is read.
45 */
46class config_error : public std::exception {
47 public:
48 config_error(const std::string &msg="Unspecified configuration error",
49 int line=-1) throw () : msg(msg), line(line) { }
50 virtual ~config_error() throw () { }
51
52 std::string get_msg() const throw () { return msg; }
53 int get_line() const throw () { return line; }
54
55 private:
56 std::string msg;
57 int line;
58};
59
60inline std::ostream &operator<<(std::ostream &os, const config_error &err) {
61 if ( err.get_line() > 0 )
62 return os << err.get_msg() << " at line " << err.get_line();
63 else
64 return os << err.get_msg();
65}
66
67
68/**
69 * The specification of a configuration entry.
70 *
71 * This specifies the name of the key, the value type, and optionally an
72 * initial default value.
73 */
74class config_entry {
75 public:
76 enum type_t {
77 T_BOOL, T_INT, T_FLOAT, T_STR, T_IPv4, T_IPv6,
78 T_IPv4_LIST, T_IPv6_LIST, T_END
79 };
80
81 config_entry(std::string key, type_t type, bool required=true)
82 : key(key), type(type), required(required) { }
83
84 config_entry(std::string key, bool value)
85 : key(key), type(T_BOOL), bool_value(value) { }
86
87 config_entry(std::string key, int value)
88 : key(key), type(T_INT), int_value(value) { }
89
90 config_entry(std::string key, float value)
91 : key(key), type(T_FLOAT), float_value(value) { }
92
93 config_entry(std::string key, std::string value)
94 : key(key), type(T_STR), str_value(value) { }
95
96 config_entry() : type(T_END) { }
97
98 private:
99 std::string key;
100 type_t type;
101 bool required;
102 bool defined;
103
104 bool bool_value;
105 int int_value;
106 float float_value;
107 std::string str_value;
108 hostaddress ipv4_value;
109 hostaddress ipv6_value;
110 std::list<hostaddress> address_list; // for both IPv4 and IPv6
111
112 friend class configuration;
113};
114
115
116/**
117 * A class for handling simple configuration files.
118 *
119 * The configuration consists of (key, value) pairs, where both key and value
120 * are strings.
121 *
122 * A configuration file is line-oriented and has the following format:
123 * [space] key [space] = [space] value [space] EOL
124 *
125 * Value can be a boolean value, an integer, a float, an IP address (either
126 * IPv4 or IPv6), or a string. String values have to be quoted using double
127 * quotes. If a double quote should appear in the string, you have to quote it
128 * using a backslash. A backslash in turn has to be quoted using another
129 * backslash.
130 *
131 * Lines starting with '#' and empty lines are ignored.
132 */
133class configuration {
134 public:
135 configuration(config_entry defaults[]);
136
137 void load(const std::string &filename) throw (config_error);
138 void load(std::istream &in) throw (config_error);
139 void dump(std::ostream &out) throw (config_error);
140
141 bool is_defined(const std::string &key) const throw ();
142
143 std::string get_string(const std::string &key) const throw ();
144 bool get_bool(const std::string &key) const throw ();
145 int get_int(const std::string &key) const throw ();
146 float get_float(const std::string &key) const throw ();
147 hostaddress get_ipv4_address(const std::string &key) const throw ();
148 hostaddress get_ipv6_address(const std::string &key) const throw ();
149
150 std::list<hostaddress> get_ipv4_address_list(
151 const std::string &key) const throw ();
152 std::list<hostaddress> get_ipv6_address_list(
153 const std::string &key) const throw ();
154
155 private:
156 typedef std::map<std::string, config_entry>::const_iterator c_iter;
157 std::map<std::string, config_entry> values;
158
159 void strip_leading_space(std::istream &in) const;
160 void skip_rest_of_line(std::istream &in) const;
161 void parse_and_assign(const std::string &key, std::istream &in);
162
163 bool parse_bool(std::istream &in) const;
164 int parse_int(std::istream &in) const;
165 float parse_float(std::istream &in) const;
166 std::string parse_string(std::istream &in) const;
167 hostaddress parse_ipv4_address(std::istream &in) const;
168 hostaddress parse_ipv6_address(std::istream &in) const;
169 std::list<hostaddress> parse_ipv4_address_list(std::istream &in) const;
170 std::list<hostaddress> parse_ipv6_address_list(std::istream &in) const;
171 void write_string(std::ostream &out, const std::string &str) const;
172 void dump_address_list(std::ostream &out,
173 const std::list<hostaddress> &addresses) const;
174};
175
176
177} // namespace natfw
178
179#endif // NATFW__CONFIG_H
Note: See TracBrowser for help on using the repository browser.