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>
23 #include "stg/common.h"
31 struct BasePropertyParser
33 virtual ~BasePropertyParser() = default;
34 virtual bool Parse(const char** attr, const std::string& attrName, const std::string& fromEncoding) = 0;
38 class PropertyParser : public BasePropertyParser
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); }
53 bool PropertyParser<std::string>::Parse(const char** attr, const std::string& attrName, const std::string& toEncoding)
55 if (!encoding.empty() && !toEncoding.empty())
58 if (!func(attr, tmp, attrName))
60 value = IconvString(tmp, encoding, toEncoding);
64 return func(attr, value, attrName);
67 using PropertyParsers = std::map<std::string, BasePropertyParser *>;
69 bool checkValue(const char** attr, const std::string& attrName);
73 bool getValue(const char** attr, T& value, const std::string& attrName)
75 if (checkValue(attr, attrName))
76 if (str2x(attr[1], value) < 0)
83 bool getValue<std::string>(const char** attr, std::string& value, const std::string& attrName)
85 if (!checkValue(attr, attrName))
93 bool getValue<double>(const char** attr, double& value, const std::string& attrName)
95 if (checkValue(attr, attrName))
96 if (strtodouble2(attr[1], value))
101 bool getEncodedValue(const char** attr, std::string& value, const std::string& attrName);
103 bool getIPValue(const char** attr, uint32_t& value, const std::string& attrName);
105 template <typename T>
107 void addParser(PropertyParsers& parsers, const std::string& name, T& value, const typename PropertyParser<T>::Func& func = getValue<T>)
109 parsers.insert(std::make_pair(ToLower(name), new PropertyParser<T>(value, func)));
112 template <typename T>
114 void addParser(PropertyParsers& parsers, const std::string& name, T& value, const std::string& toEncoding, const typename PropertyParser<T>::Func& func = getValue<T>)
116 parsers.insert(std::make_pair(ToLower(name), new PropertyParser<T>(value, func, toEncoding)));
119 bool tryParse(PropertyParsers& parsers, const std::string& name, const char** attr, const std::string& fromEncoding, const std::string& attrName = "value");