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),
42 virtual ~ParserError() throw() {}
48 size_t skipSpaces(const std::string& value, size_t start)
50 while (start < value.length() && std::isspace(value[start]))
55 size_t checkChar(const std::string& value, size_t start, char ch)
57 if (start >= value.length())
58 throw ParserError(start, "Unexpected end of string. Expected '" + std::string(1, ch) + "'.");
59 if (value[start] != ch)
60 throw ParserError(start, "Expected '" + std::string(1, ch) + "', got '" + std::string(1, value[start]) + "'.");
64 std::pair<size_t, std::string> readString(const std::string& value, size_t start)
67 while (start < value.length() && !std::isspace(value[start]) &&
68 value[start] != ',' && value[start] != '(' && value[start] != ')')
69 dest.push_back(value[start++]);
71 if (start == value.length())
72 throw ParserError(start, "Unexpected end of string. Expected string.");
74 throw ParserError(start, "Unexpected whitespace. Expected string.");
76 return std::make_pair(start, dest);
79 Config::Pairs toPairs(const std::vector<std::string>& values)
82 return Config::Pairs();
83 std::string value(values[0]);
86 while (start < value.size()) {
88 start = skipSpaces(value, start);
91 start = checkChar(value, start, ',');
92 start = skipSpaces(value, start);
94 size_t pairStart = start;
95 start = checkChar(value, start, '(');
96 const std::pair<size_t, std::string> key = readString(value, start);
98 pair.first = key.second;
99 start = skipSpaces(value, start);
100 start = checkChar(value, start, ',');
101 start = skipSpaces(value, start);
102 const std::pair<size_t, std::string> val = readString(value, start);
104 pair.second = val.second;
105 start = skipSpaces(value, start);
106 start = checkChar(value, start, ')');
107 if (res.find(pair.first) != res.end())
108 throw ParserError(pairStart, "Duplicate field.");
114 bool toBool(const std::vector<std::string>& values)
118 std::string value(values[0]);
119 return strncasecmp(value.c_str(), "yes", 3) == 0;
122 std::string toString(const std::vector<std::string>& values)
129 template <typename T>
130 T toInt(const std::vector<std::string>& values)
135 if (str2x(values[0], res) == 0)
140 Config::Pairs parseVector(const std::string& paramName, const MODULE_SETTINGS& params)
142 for (size_t i = 0; i < params.moduleParams.size(); ++i)
143 if (params.moduleParams[i].param == paramName)
144 return toPairs(params.moduleParams[i].value);
145 return Config::Pairs();
148 bool parseBool(const std::string& paramName, const MODULE_SETTINGS& params)
150 for (size_t i = 0; i < params.moduleParams.size(); ++i)
151 if (params.moduleParams[i].param == paramName)
152 return toBool(params.moduleParams[i].value);
156 std::string parseString(const std::string& paramName, const MODULE_SETTINGS& params)
158 for (size_t i = 0; i < params.moduleParams.size(); ++i)
159 if (params.moduleParams[i].param == paramName)
160 return toString(params.moduleParams[i].value);
164 template <typename T>
165 T parseInt(const std::string& paramName, const MODULE_SETTINGS& params)
167 for (size_t i = 0; i < params.moduleParams.size(); ++i)
168 if (params.moduleParams[i].param == paramName)
169 return toInt<T>(params.moduleParams[i].value);
173 std::string parseAddress(const std::string& address)
175 size_t pos = address.find_first_of(':');
176 if (pos == std::string::npos)
177 throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
178 return address.substr(pos + 1);
181 Config::Type parseConnectionType(const std::string& address)
183 size_t pos = address.find_first_of(':');
184 if (pos == std::string::npos)
185 throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
186 std::string type = ToLower(address.substr(0, pos));
189 else if (type == "tcp")
191 throw ParserError(0, "Invalid connection type. Should be either 'unix' or 'tcp', got '" + type + "'");
194 } // namespace anonymous
196 Config::Config(const MODULE_SETTINGS& settings)
197 : match(parseVector("match", settings)),
198 modify(parseVector("modify", settings)),
199 reply(parseVector("reply", settings)),
200 verbose(parseBool("verbose", settings)),
201 address(parseString("bind_address", settings)),
202 bindAddress(parseAddress(address)),
203 connectionType(parseConnectionType(address)),
204 portStr(parseString("port", settings)),
205 port(parseInt<uint16_t>("port", settings)),
206 key(parseString("key", settings))