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>
24 #include "parser_state.h"
26 #include "stg/common.h"
40 void ReadConfigFile(const std::string & filePath, void (C::* callback)(const std::string&, const std::string&), C * obj)
42 std::ifstream stream(filePath.c_str());
45 while (std::getline(stream, line))
49 std::string::size_type pos = line.find_first_of('#');
50 if (pos != std::string::npos)
51 line = line.substr(0, pos);
54 pos = line.find_first_of('=');
55 if (pos == std::string::npos)
57 std::ostringstream error;
58 error << "Bad file format, missing '=' in '" << filePath << ":" << num << "'.";
59 throw std::runtime_error(error.str().c_str());
61 (obj->*callback)(Trim(line.substr(0, pos)), Trim(line.substr(pos + 1, line.length() - pos - 1)));
65 } // namespace anonymous
68 using SGCONF::OPTION_BLOCK;
69 using SGCONF::OPTION_BLOCKS;
71 using SGCONF::PARSER_STATE;
73 OPTION::OPTION(const std::string & shortName,
74 const std::string & longName,
76 const std::string & description)
77 : m_shortName(shortName),
80 m_description(description)
84 OPTION::OPTION(const std::string & longName,
86 const std::string & description)
87 : m_longName(longName),
89 m_description(description)
93 OPTION::OPTION(const OPTION & rhs)
94 : m_shortName(rhs.m_shortName),
95 m_longName(rhs.m_longName),
96 m_action(rhs.m_action->Clone()),
97 m_description(rhs.m_description)
106 OPTION & OPTION::operator=(const OPTION & rhs)
108 m_shortName = rhs.m_shortName;
109 m_longName = rhs.m_longName;
110 m_action = rhs.m_action->Clone();
111 m_description = rhs.m_description;
115 void OPTION::Help(size_t level) const
118 throw ERROR("Option is not defined.");
119 std::string indent(level, '\t');
121 if (!m_shortName.empty())
122 std::cout << "-" << m_shortName << ", ";
123 std::cout << "--" << m_longName << " " << m_action->ParamDescription()
124 << "\t" << m_description << m_action->DefaultDescription() << "\n";
125 m_action->Suboptions().Help(level);
128 bool OPTION::Check(const char * arg) const
137 return m_longName == arg + 1;
139 return m_shortName == arg;
142 PARSER_STATE OPTION::Parse(int argc, char ** argv)
145 throw ERROR("Option is not defined.");
148 return m_action->Parse(argc, argv);
150 catch (const ACTION::ERROR & ex)
152 if (m_longName.empty())
153 throw ERROR("-" + m_shortName + ": " + ex.what());
155 throw m_shortName.empty() ? ERROR("--" + m_longName + ": " + ex.what())
156 : ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what());
160 void OPTION::ParseValue(const std::string & value)
163 throw ERROR("Option is not defined.");
166 return m_action->ParseValue(value);
168 catch (const ACTION::ERROR & ex)
170 throw ERROR(m_longName + ": " + ex.what());
174 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
175 const std::string & longName,
177 const std::string & description)
179 m_options.push_back(OPTION(shortName, longName, action, description));
183 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
185 const std::string & description)
187 m_options.push_back(OPTION(longName, action, description));
191 void OPTION_BLOCK::Help(size_t level) const
193 if (m_options.empty())
195 if (!m_description.empty())
196 std::cout << m_description << ":\n";
197 std::for_each(m_options.begin(),
199 std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
202 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
204 PARSER_STATE state(false, argc, argv);
205 while (state.argc > 0 && !state.stop)
207 std::vector<OPTION>::iterator it = std::find_if(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Check), *state.argv));
208 if (it != m_options.end())
209 state = it->Parse(--state.argc, ++state.argv);
217 void OPTION_BLOCK::ParseFile(const std::string & filePath)
219 if (access(filePath.c_str(), R_OK))
220 throw ERROR("File '" + filePath + "' does not exists.");
221 ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this);
224 void OPTION_BLOCK::OptionCallback(const std::string & key, const std::string & value)
226 for (std::vector<OPTION>::iterator it = m_options.begin(); it != m_options.end(); ++it)
227 if (it->Name() == key)
228 it->ParseValue(value);
231 void OPTION_BLOCKS::Help(size_t level) const
233 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
234 while (it != m_blocks.end())
242 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
244 PARSER_STATE state(false, argc, argv);
245 std::list<OPTION_BLOCK>::iterator it(m_blocks.begin());
246 while (!state.stop && it != m_blocks.end())
248 state = it->Parse(state.argc, state.argv);