]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/include/stg/property_parsers.h
Better handling of errors from server. Refactoring.
[stg.git] / stglibs / srvconf.lib / include / stg / property_parsers.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #ifndef __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__
22 #define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__
23
24 #include <map>
25 #include <string>
26
27 #include "stg/common.h"
28
29 class BASE_PROPERTY_PARSER
30 {
31     public:
32         virtual ~BASE_PROPERTY_PARSER() {}
33         virtual bool Parse(const char ** attr) = 0;
34 };
35
36 template <typename T>
37 class PROPERTY_PARSER : public BASE_PROPERTY_PARSER
38 {
39     public:
40         typedef bool (* FUNC)(const char **, T &);
41         PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {}
42         virtual bool Parse(const char ** attr) { return func(attr, value); }
43     private:
44         T & value;
45         FUNC func;
46 };
47
48 typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
49
50 bool CheckValue(const char ** attr);
51
52 template <typename T>
53 bool GetValue(const char ** attr, T & value)
54 {
55 if (CheckValue(attr))
56     if (str2x(attr[1], value) < 0)
57         return false;
58 return true;
59 }
60
61 template <>
62 bool GetValue<std::string>(const char ** attr, std::string & value)
63 {
64 if (!CheckValue(attr))
65     return false;
66 value = attr[1];
67 return true;
68 }
69
70 template <>
71 bool GetValue<double>(const char ** attr, double & value)
72 {
73 if (CheckValue(attr))
74     if (strtodouble2(attr[1], value))
75         return false;
76 return true;
77 }
78
79 bool GetEncodedValue(const char ** attr, std::string & value);
80
81 bool GetIPValue(const char ** attr, uint32_t& value);
82
83 template <typename T>
84 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
85
86 template <typename T>
87 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
88 {
89     parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
90 }
91
92 bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr);
93
94 #endif