]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/property.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / libs / srvconf / 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 #pragma once
22
23 #include "stg/common.h"
24
25 #include <map>
26 #include <string>
27
28 namespace STG
29 {
30
31 struct BasePropertyParser
32 {
33     virtual ~BasePropertyParser() = default;
34     virtual bool Parse(const char** attr, const std::string& attrName, const std::string& fromEncoding) = 0;
35 };
36
37 template <typename T>
38 class PropertyParser : public BasePropertyParser
39 {
40     public:
41         using Func = bool (*)(const char **, T&, const std::string&);
42         PropertyParser(T& v, Func f) : value(v), func(f) {}
43         PropertyParser(T& v, Func f, const std::string& e) : value(v), func(f), encoding(e) {}
44         bool Parse(const char** attr, const std::string& attrName, const std::string& /*fromEncoding*/) override { return func(attr, value, attrName); }
45     private:
46         T & value;
47         Func func;
48         std::string encoding;
49 };
50
51 template <>
52 inline
53 bool PropertyParser<std::string>::Parse(const char** attr, const std::string& attrName, const std::string& toEncoding)
54 {
55     if (!encoding.empty() && !toEncoding.empty())
56     {
57         std::string tmp;
58         if (!func(attr, tmp, attrName))
59             return false;
60         value = IconvString(tmp, encoding, toEncoding);
61         return true;
62     }
63
64     return func(attr, value, attrName);
65 }
66
67 using PropertyParsers = std::map<std::string, BasePropertyParser *>;
68
69 bool checkValue(const char** attr, const std::string& attrName);
70
71 template <typename T>
72 inline
73 bool getValue(const char** attr, T& value, const std::string& attrName)
74 {
75     if (checkValue(attr, attrName))
76         if (str2x(attr[1], value) < 0)
77             return false;
78     return true;
79 }
80
81 template <>
82 inline
83 bool getValue<std::string>(const char** attr, std::string& value, const std::string& attrName)
84 {
85     if (!checkValue(attr, attrName))
86         return false;
87     value = attr[1];
88     return true;
89 }
90
91 template <>
92 inline
93 bool getValue<double>(const char** attr, double& value, const std::string& attrName)
94 {
95     if (checkValue(attr, attrName))
96         if (strtodouble2(attr[1], value))
97             return false;
98     return true;
99 }
100
101 bool getEncodedValue(const char** attr, std::string& value, const std::string& attrName);
102
103 bool getIPValue(const char** attr, uint32_t& value, const std::string& attrName);
104
105 template <typename T>
106 inline
107 void addParser(PropertyParsers& parsers, const std::string& name, T& value, const typename PropertyParser<T>::Func& func = getValue<T>)
108 {
109     parsers.insert(std::make_pair(ToLower(name), new PropertyParser<T>(value, func)));
110 }
111
112 template <typename T>
113 inline
114 void addParser(PropertyParsers& parsers, const std::string& name, T& value, const std::string& toEncoding, const typename PropertyParser<T>::Func& func = getValue<T>)
115 {
116     parsers.insert(std::make_pair(ToLower(name), new PropertyParser<T>(value, func, toEncoding)));
117 }
118
119 bool tryParse(PropertyParsers& parsers, const std::string& name, const char** attr, const std::string& fromEncoding, const std::string& attrName = "value");
120
121 } // namespace STG