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) = 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 virtual bool Parse(const char ** attr, const std::string & attrName) { return func(attr, value, attrName); }
51 typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
53 bool CheckValue(const char ** attr, const std::string & attrName);
57 bool GetValue(const char ** attr, T & value, const std::string & attrName)
59 if (CheckValue(attr, attrName))
60 if (str2x(attr[1], value) < 0)
67 bool GetValue<std::string>(const char ** attr, std::string & value, const std::string & attrName)
69 if (!CheckValue(attr, attrName))
77 bool GetValue<double>(const char ** attr, double & value, const std::string & attrName)
79 if (CheckValue(attr, attrName))
80 if (strtodouble2(attr[1], value))
85 bool GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName);
87 bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName);
90 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
94 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
96 parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
99 bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & attrName = "value");