00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef NATFW__CONFIG_H
00030 #define NATFW__CONFIG_H
00031
00032 #include <fstream>
00033 #include <map>
00034 #include <list>
00035 #include <exception>
00036
00037 #include "address.h"
00038
00039 namespace natfw {
00040 using protlib::hostaddress;
00041
00042
00046 class config_error : public std::exception {
00047 public:
00048 config_error(const std::string &msg="Unspecified configuration error",
00049 int line=-1) throw () : msg(msg), line(line) { }
00050 virtual ~config_error() throw () { }
00051
00052 std::string get_msg() const throw () { return msg; }
00053 int get_line() const throw () { return line; }
00054
00055 private:
00056 std::string msg;
00057 int line;
00058 };
00059
00060 inline std::ostream &operator<<(std::ostream &os, const config_error &err) {
00061 if ( err.get_line() > 0 )
00062 return os << err.get_msg() << " at line " << err.get_line();
00063 else
00064 return os << err.get_msg();
00065 }
00066
00067
00074 class config_entry {
00075 public:
00076 enum type_t {
00077 T_BOOL, T_INT, T_FLOAT, T_STR, T_IPv4, T_IPv6,
00078 T_IPv4_LIST, T_IPv6_LIST, T_END
00079 };
00080
00081 config_entry(std::string key, type_t type, bool required=true)
00082 : key(key), type(type), required(required) { }
00083
00084 config_entry(std::string key, bool value)
00085 : key(key), type(T_BOOL), bool_value(value) { }
00086
00087 config_entry(std::string key, int value)
00088 : key(key), type(T_INT), int_value(value) { }
00089
00090 config_entry(std::string key, float value)
00091 : key(key), type(T_FLOAT), float_value(value) { }
00092
00093 config_entry(std::string key, std::string value)
00094 : key(key), type(T_STR), str_value(value) { }
00095
00096 config_entry() : type(T_END) { }
00097
00098 private:
00099 std::string key;
00100 type_t type;
00101 bool required;
00102 bool defined;
00103
00104 bool bool_value;
00105 int int_value;
00106 float float_value;
00107 std::string str_value;
00108 hostaddress ipv4_value;
00109 hostaddress ipv6_value;
00110 std::list<hostaddress> address_list;
00111
00112 friend class configuration;
00113 };
00114
00115
00133 class configuration {
00134 public:
00135 configuration(config_entry defaults[]);
00136
00137 void load(const std::string &filename) throw (config_error);
00138 void load(std::istream &in) throw (config_error);
00139 void dump(std::ostream &out) throw (config_error);
00140
00141 bool is_defined(const std::string &key) const throw ();
00142
00143 std::string get_string(const std::string &key) const throw ();
00144 bool get_bool(const std::string &key) const throw ();
00145 int get_int(const std::string &key) const throw ();
00146 float get_float(const std::string &key) const throw ();
00147 hostaddress get_ipv4_address(const std::string &key) const throw ();
00148 hostaddress get_ipv6_address(const std::string &key) const throw ();
00149
00150 std::list<hostaddress> get_ipv4_address_list(
00151 const std::string &key) const throw ();
00152 std::list<hostaddress> get_ipv6_address_list(
00153 const std::string &key) const throw ();
00154
00155 private:
00156 typedef std::map<std::string, config_entry>::const_iterator c_iter;
00157 std::map<std::string, config_entry> values;
00158
00159 void strip_leading_space(std::istream &in) const;
00160 void skip_rest_of_line(std::istream &in) const;
00161 void parse_and_assign(const std::string &key, std::istream &in);
00162
00163 bool parse_bool(std::istream &in) const;
00164 int parse_int(std::istream &in) const;
00165 float parse_float(std::istream &in) const;
00166 std::string parse_string(std::istream &in) const;
00167 hostaddress parse_ipv4_address(std::istream &in) const;
00168 hostaddress parse_ipv6_address(std::istream &in) const;
00169 std::list<hostaddress> parse_ipv4_address_list(std::istream &in) const;
00170 std::list<hostaddress> parse_ipv6_address_list(std::istream &in) const;
00171 void write_string(std::ostream &out, const std::string &str) const;
00172 void dump_address_list(std::ostream &out,
00173 const std::list<hostaddress> &addresses) const;
00174 };
00175
00176
00177 }
00178
00179 #endif // NATFW__CONFIG_H