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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
21 #ifndef __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__
22 #define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__
24 #include "stg/common.h"
32 class BASE_PROPERTY_PARSER
35 virtual ~BASE_PROPERTY_PARSER() {}
36 virtual bool Parse(const char ** attr, const std::string & attrName, const std::string & fromEncoding) = 0;
40 class PROPERTY_PARSER : public BASE_PROPERTY_PARSER
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); }
55 bool PROPERTY_PARSER<std::string>::Parse(const char ** attr, const std::string & attrName, const std::string & toEncoding)
57 if (!encoding.empty() && !toEncoding.empty())
60 if (!func(attr, tmp, attrName))
62 value = IconvString(tmp, encoding, toEncoding);
66 return func(attr, value, attrName);
69 typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
71 bool CheckValue(const char ** attr, const std::string & attrName);
75 bool GetValue(const char ** attr, T & value, const std::string & attrName)
77 if (CheckValue(attr, attrName))
78 if (str2x(attr[1], value) < 0)
85 bool GetValue<std::string>(const char ** attr, std::string & value, const std::string & attrName)
87 if (!CheckValue(attr, attrName))
95 bool GetValue<double>(const char ** attr, double & value, const std::string & attrName)
97 if (CheckValue(attr, attrName))
98 if (strtodouble2(attr[1], value))
103 bool GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName);
105 bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName);
107 template <typename T>
109 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>)
111 parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
114 template <typename T>
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>)
118 parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func, toEncoding)));
121 bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & fromEncoding, const std::string & attrName = "value");