1 //
2 // Copyright (C) 2005-2013 the pekwm development team
3 //
4 // This program is licensed under the GNU GPL.
5 // See the LICENSE file for more information.
6 //
7 
8 #ifndef _PEKWM_CFGPARSERKEY_HH_
9 #define _PEKWM_CFGPARSERKEY_HH_
10 
11 #include "Compat.hh"
12 
13 #include <string>
14 #include <cstdlib>
15 
16 #ifdef PEKWM_HAVE_LIMITS
17 #include <limits>
18 #endif // PEKWM_HAVE_LIMITS
19 
20 //! @brief CfgParserKey base class.
21 class CfgParserKey {
22 public:
23 	//! @brief CfgParserKey constructor.
CfgParserKey(const char * name)24 	CfgParserKey(const char *name) : _name(name) { }
25 	//! @brief CfgParserKey destructor.
~CfgParserKey(void)26 	virtual ~CfgParserKey(void) { }
27 
28 	//! @brief Returns Key name.
getName(void) const29 	const char *getName(void) const { return _name; }
30 
31 	//! @brief Parses value and sets Key value.
parseValue(const std::string &)32 	virtual void parseValue(const std::string &) { }
33 
34 protected:
35 	const char *_name; //!< Key name.
36 };
37 
38 /**
39  * CfgParserKey numeric type with minimum, maximum and default
40  * value. The type must be available in numeric_limits.
41  */
42 template<typename T>
43 class CfgParserKeyNumeric : public CfgParserKey {
44 public:
45 	/**
46 	 * CfgParserKeyNumeric constructor, sets default values
47 	 *
48 	 * @param name Name of the key.
49 	 * @param set Variable to store parsed value in.
50 	 * @param default_val Default value for key, defaults to 0.
51 	 * @param value_min Minimum value for key, defaults to limits<T>::min().
52 	 * @param value_min Maximum value for key, defaults to limits<T>::max().
53 	 */
CfgParserKeyNumeric(const char * name,T & set,const T default_val=0,const T value_min=std::numeric_limits<T>::min (),const T value_max=std::numeric_limits<T>::max ())54 	CfgParserKeyNumeric(const char *name, T &set, const T default_val = 0,
55 			    const T value_min = std::numeric_limits<T>::min(),
56 			    const T value_max = std::numeric_limits<T>::max())
57 		: CfgParserKey(name),
58 		  _set(set), _default(default_val),
59 		  _value_min(value_min), _value_max(value_max)
60 	{
61 	}
62 
63 	/**
64 	 * CfgParserKeyNumeric destructor.
65 	 */
~CfgParserKeyNumeric(void)66 	virtual ~CfgParserKeyNumeric(void) { }
67 
68 	/**
69 	 * Parses and store integer value.
70 	 *
71 	 * @param value Reference to string representing integer value.
72 	 */
73 	virtual void
parseValue(const std::string & value_str)74 	parseValue(const std::string &value_str)
75 	{
76 		double value;
77 		char *endptr;
78 
79 		// Get long value.
80 		value = strtod(value_str.c_str(), &endptr);
81 
82 		// Check for validity, 0 is returned on failiure with endptr set to the
83 		// beginning of the string, else we are (semi) ok.
84 		if ((value == 0) && (endptr == value_str.c_str())) {
85 			_set = _default;
86 
87 		} else {
88 			T value_for_type = static_cast<T>(value);
89 
90 			if (value_for_type < _value_min) {
91 				_set = _value_min;
92 				throw std::string("value to low, min value "
93 						  + std::to_string(_value_min));
94 			} if (value_for_type > _value_max)  {
95 				_set = _value_max;
96 				throw std::string("value to high, max value "
97 						  + std::to_string(_value_max));
98 			}
99 
100 			_set = value_for_type;
101 		}
102 	}
103 
104 private:
105 	T &_set; /**< Reference to store parsed value in. */
106 	const T _default; /**< Default value. */
107 	const T _value_min; /**< Minimum value. */
108 	const T _value_max; /**< Maximum value. */
109 };
110 
111 //! @brief CfgParser Key boolean value parser.
112 class CfgParserKeyBool : public CfgParserKey {
113 public:
114 	CfgParserKeyBool(const char *name,
115 			 bool &set, const bool default_val = false);
116 	virtual ~CfgParserKeyBool(void);
117 
118 	virtual void parseValue(const std::string &value);
119 
120 private:
121 	bool &_set; //! Reference to stored parsed value in.
122 	const bool _default; //! Default value.
123 };
124 
125 //! @brief CfgParser Key string value parser.
126 class CfgParserKeyString : public CfgParserKey {
127 public:
128 	CfgParserKeyString(const char *name,
129 			   std::string &set, const std::string default_val = "",
130 			   const std::string::size_type length_min = 0);
131 	virtual ~CfgParserKeyString(void);
132 
133 	virtual void parseValue(const std::string &value);
134 
135 private:
136 	std::string &_set; //!< Reference to store parsed value in.
137 	const std::string::size_type _length_min; //!< Minimum length of string.
138 };
139 
140 //! @brief CfgParser Key path parser.
141 class CfgParserKeyPath : public CfgParserKey {
142 public:
143 	//! @brief CfgParserKeyPath constructor.
144 	CfgParserKeyPath(const char *name,
145 			 std::string &set, const std::string default_val = "");
146 	virtual ~CfgParserKeyPath(void);
147 
148 	virtual void parseValue(const std::string &value);
149 
150 private:
151 	std::string &_set; //!< Reference to store parsed value in.
152 	std::string _default; //!< Default value.
153 };
154 
155 #endif // _PEKWM_CFGPARSERKEY_HH_
156