]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/property.h
Port to CMake, get rid of os_int.h.
[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 #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, const std::string & attrName, const std::string & fromEncoding) = 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 &, const std::string &);
44         PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {}
45         PROPERTY_PARSER(T & v, FUNC f, const std::string & e) : value(v), func(f), encoding(e) {}
46         virtual bool Parse(const char ** attr, const std::string & attrName, const std::string & /*fromEncoding*/) { return func(attr, value, attrName); }
47     private:
48         T & value;
49         FUNC func;
50         std::string encoding;
51 };
52
53 template <>
54 inline
55 bool PROPERTY_PARSER<std::string>::Parse(const char ** attr, const std::string & attrName, const std::string & toEncoding)
56 {
57 if (!encoding.empty() && !toEncoding.empty())
58     {
59     std::string tmp;
60     if (!func(attr, tmp, attrName))
61         return false;
62     value = IconvString(tmp, encoding, toEncoding);
63     return true;
64     }
65 else
66     return func(attr, value, attrName);
67 }
68
69 typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
70
71 bool CheckValue(const char ** attr, const std::string & attrName);
72
73 template <typename T>
74 inline
75 bool GetValue(const char ** attr, T & value, const std::string & attrName)
76 {
77 if (CheckValue(attr, attrName))
78     if (str2x(attr[1], value) < 0)
79         return false;
80 return true;
81 }
82
83 template <>
84 inline
85 bool GetValue<std::string>(const char ** attr, std::string & value, const std::string & attrName)
86 {
87 if (!CheckValue(attr, attrName))
88     return false;
89 value = attr[1];
90 return true;
91 }
92
93 template <>
94 inline
95 bool GetValue<double>(const char ** attr, double & value, const std::string & attrName)
96 {
97 if (CheckValue(attr, attrName))
98     if (strtodouble2(attr[1], value))
99         return false;
100 return true;
101 }
102
103 bool GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName);
104
105 bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName);
106
107 template <typename T>
108 inline
109 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>)
110 {
111     parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
112 }
113
114 template <typename T>
115 inline
116 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const std::string & toEncoding, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>)
117 {
118     parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func, toEncoding)));
119 }
120
121 bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & fromEncoding, const std::string & attrName = "value");
122
123 } // namespace STG
124
125 #endif