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__
27 #include "stg/common.h"
29 class BASE_PROPERTY_PARSER
32 virtual ~BASE_PROPERTY_PARSER() {}
33 virtual bool Parse(const char ** attr) = 0;
37 class PROPERTY_PARSER : public BASE_PROPERTY_PARSER
40 typedef bool (* FUNC)(const char **, T &);
41 PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {}
42 virtual bool Parse(const char ** attr) { return func(attr, value); }
48 typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
50 bool CheckValue(const char ** attr);
54 bool GetValue(const char ** attr, T & value)
57 if (str2x(attr[1], value) < 0)
64 bool GetValue<std::string>(const char ** attr, std::string & value)
66 if (!CheckValue(attr))
74 bool GetValue<double>(const char ** attr, double & value)
77 if (strtodouble2(attr[1], value))
82 bool GetEncodedValue(const char ** attr, std::string & value);
84 bool GetIPValue(const char ** attr, uint32_t& value);
87 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
91 void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
93 parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
96 bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr);