]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/property.h
Merge branch 'fix-gts-auth-errors'
[stg.git] / stglibs / srvconf.lib / parsers / property.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 "stg/common.h"
25
26 #include <map>
27 #include <string>
28
29 namespace STG
30 {
31
32 class BASE_PROPERTY_PARSER
33 {
34     public:
35         virtual ~BASE_PROPERTY_PARSER() {}
36         virtual bool Parse(const char ** attr) = 0;
37 };
38
39 template <typename T>
40 class PROPERTY_PARSER : public BASE_PROPERTY_PARSER
41 {
42     public:
43         typedef bool (* FUNC)(const char **, T &);
44         PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {}
45         virtual bool Parse(const char ** attr) { return func(attr, value); }
46     private:
47         T & value;
48         FUNC func;
49 };
50
51 typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
52
53 bool CheckValue(const char ** attr);
54
55 template <typename T>
56 inline
57 bool GetValue(const char ** attr, T & value)
58 {
59 if (CheckValue(attr))
60     if (str2x(attr[1], value) < 0)
61         return false;
62 return true;
63 }
64
65 template <>
66 inline
67 bool GetValue<std::string>(const char ** attr, std::string & value)
68 {
69 if (!CheckValue(attr))
70     return false;
71 value = attr[1];
72 return true;
73 }
74
75 template <>
76 inline
77 bool GetValue<double>(const char ** attr, double & value)
78 {
79 if (CheckValue(attr))
80     if (strtodouble2(attr[1], value))
81         return false;
82 return true;
83 }
84
85 bool GetEncodedValue(const char ** attr, std::string & value);
86
87 bool GetIPValue(const char ** attr, uint32_t& value);
88
89 template <typename T>
90 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
91
92 template <typename T>
93 inline
94 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
95 {
96     parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
97 }
98
99 bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr);
100
101 } // namespace STG
102
103 #endif