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"
28 #include <strings.h> // strncasecmp
35 struct ParserError : public std::runtime_error
37 ParserError(size_t pos, const std::string& message)
38 : runtime_error("Parsing error at position " + x2str(pos) + ". " + message),
47 size_t skipSpaces(const std::string& value, size_t start)
49 while (start < value.length() && std::isspace(value[start]))
54 size_t checkChar(const std:string& value, size_t start, char ch)
56 if (start >= value.length())
57 throw ParserError(start, "Unexpected end of string. Expected '" + std::string(ch) + "'.");
58 if (value[start] != ch)
59 throw ParserError(start, "Expected '" + std::string(ch) + "', got '" + std::string(value[start]) + "'.");
63 std::pair<size_t, std::string> readString(const std::string& value, size_t start)
66 while (start < value.length() && !std::isspace(value[start]))
67 dest.push_back(value[start++]);
69 if (start == value.length())
70 throw ParserError(start, "Unexpected end of string. Expected string.");
72 throw ParserError(start, "Unexpected whitespace. Expected string.");
77 Config::Pairs toPairs(const std::vector<std::string>& values)
80 return Config::Pairs();
81 std::string value(values[0]);
84 while (start < value.size()) {
86 start = skipSpaces(value, start);
87 size_t pairStart = start;
88 start = checkChar(value, start, '(');
89 std::pair<size_t, std::string> key = readString(value, start);
91 pair.first = key.second;
92 start = skipSpaces(value, start);
93 start = checkChar(value, start, ',')
94 start = skipSpaces(value, start);
95 std::pair<size_t, std::string> value = readString(value, start);
97 pair.second = value.second;
98 start = skipSpaces(value, start);
99 start = checkChar(value, start, ')');
100 if (res.find(pair.first) != res.end())
101 throw ParserError(pairStart, "Duplicate field.");
107 bool toBool(const std::vector<std::string>& values)
111 std::string value(values[0]);
112 return strncasecmp(value.c_str(), "yes", 3) == 0;
115 std::string toString(const std::vector<std::string>& values)
122 template <typename T>
123 T toInt(const std::vector<std::string>& values)
128 if (srt2x(values[0], res) == 0)
133 Config::Pairs parseVector(const std::string& paramName, const MODULE_SETTINGS& params)
135 for (size_t i = 0; i < params.moduleParams.size(); ++i)
136 if (params.moduleParams[i].first == paramName)
137 return toPairs(params.moduleParams[i].second);
138 return Config::Pairs();
141 bool parseBool(const std::string& paramName, const MODULE_SETTINGS& params)
143 for (size_t i = 0; i < params.moduleParams.size(); ++i)
144 if (params.moduleParams[i].first == paramName)
145 return toBool(params.moduleParams[i].second);
149 std::string parseString(const std::string& paramName, const MODULE_SETTINGS& params)
151 for (size_t i = 0; i < params.moduleParams.size(); ++i)
152 if (params.moduleParams[i].first == paramName)
153 return toString(params.moduleParams[i].second);
157 template <typename T>
158 T parseInt(const std::string& paramName, const MODULE_SETTINGS& params)
160 for (size_t i = 0; i < params.moduleParams.size(); ++i)
161 if (params.moduleParams[i].first == paramName)
162 return toInt<T>(params.moduleParams[i].second);
166 } // namespace anonymous
168 Config::Config(const MODULE_SETTINGS& settings)
169 : match(parseVector("match", settings)),
170 modify(parseVector("modify", settings)),
171 reply(parseVector("reply", settings)),
172 verbose(parseBool("verbose", settings)),
173 bindAddress(parseString("bind_address", settings)),
174 port(parseInt<uint16_t>("port", settings)),
175 key(parseString("key", settings))