]> git.stg.codes - stg.git/blob - sgconf/options.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / sgconf / options.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "options.h"
22
23 #include "action.h"
24 #include "parser_state.h"
25
26 #include "stg/common.h"
27
28 #include <fstream>
29 #include <sstream>
30 #include <iostream>
31 #include <functional>
32 #include <algorithm>
33
34 #include <unistd.h>
35
36 namespace
37 {
38
39 template <class C>
40 void ReadConfigFile(const std::string & filePath, void (C::* callback)(const std::string&, const std::string&), C * obj)
41 {
42 std::ifstream stream(filePath.c_str());
43 std::string line;
44 size_t num = 0;
45 while (std::getline(stream, line))
46     {
47     ++num;
48     line = Trim(line);
49     std::string::size_type pos = line.find_first_of('#');
50     if (pos != std::string::npos)
51         line = line.substr(0, pos);
52     if (line.empty())
53         continue;
54     pos = line.find_first_of('=');
55     if (pos == std::string::npos)
56         {
57         std::ostringstream error;
58         error << "Bad file format, missing '=' in '" << filePath << ":" << num << "'.";
59         throw std::runtime_error(error.str().c_str());
60         }
61     (obj->*callback)(Trim(line.substr(0, pos)), Trim(line.substr(pos + 1, line.length() - pos - 1)));
62     }
63 }
64
65 } // namespace anonymous
66
67 using SGCONF::OPTION;
68 using SGCONF::OPTION_BLOCK;
69 using SGCONF::OPTION_BLOCKS;
70 using SGCONF::ACTION;
71 using SGCONF::PARSER_STATE;
72
73 OPTION::OPTION(const std::string & shortName,
74                const std::string & longName,
75                ACTION * action,
76                const std::string & description)
77     : m_shortName(shortName),
78       m_longName(longName),
79       m_action(action),
80       m_description(description)
81 {
82 }
83
84 OPTION::OPTION(const std::string & longName,
85                ACTION * action,
86                const std::string & description)
87     : m_longName(longName),
88       m_action(action),
89       m_description(description)
90 {
91 }
92
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)
98 {
99 }
100
101 OPTION::~OPTION()
102 {
103 delete m_action;
104 }
105
106 OPTION & OPTION::operator=(const OPTION & rhs)
107 {
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;
112 return *this;
113 }
114
115 void OPTION::Help(size_t level) const
116 {
117 if (!m_action)
118     throw ERROR("Option is not defined.");
119 std::string indent(level, '\t');
120 std::cout << indent;
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);
126 }
127
128 bool OPTION::Check(const char * arg) const
129 {
130 if (arg == NULL)
131     return false;
132
133 if (*arg++ != '-')
134     return false;
135
136 if (*arg == '-')
137 {
138     return m_longName == arg + 1;
139 }
140
141 return m_shortName == arg;
142 }
143
144 PARSER_STATE OPTION::Parse(int argc, char ** argv, void * data)
145 {
146 if (!m_action)
147     throw ERROR("Option is not defined.");
148 try
149     {
150     return m_action->Parse(argc, argv, data);
151     }
152 catch (const ACTION::ERROR & ex)
153     {
154     if (m_longName.empty())
155         throw ERROR("-" + m_shortName + ": " + ex.what());
156     else
157         throw m_shortName.empty() ? ERROR("--" + m_longName + ": " + ex.what())
158                                   : ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what());
159     }
160 }
161
162 void OPTION::ParseValue(const std::string & value)
163 {
164 if (!m_action)
165     throw ERROR("Option is not defined.");
166 try
167     {
168     return m_action->ParseValue(value);
169     }
170 catch (const ACTION::ERROR & ex)
171     {
172     throw ERROR(m_longName + ": " + ex.what());
173     }
174 }
175
176 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
177                                  const std::string & longName,
178                                  ACTION * action,
179                                  const std::string & description)
180 {
181 m_options.push_back(OPTION(shortName, longName, action, description));
182 return *this;
183 }
184
185 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
186                                  ACTION * action,
187                                  const std::string & description)
188 {
189 m_options.push_back(OPTION(longName, action, description));
190 return *this;
191 }
192
193 void OPTION_BLOCK::Help(size_t level) const
194 {
195 if (m_options.empty())
196     return;
197 if (!m_description.empty())
198     std::cout << m_description << ":\n";
199 std::for_each(m_options.begin(),
200               m_options.end(),
201               std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
202 }
203
204 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv, void * data)
205 {
206 PARSER_STATE state(false, argc, argv);
207 if (state.argc == 0)
208     return state;
209 while (state.argc > 0 && !state.stop)
210     {
211     std::vector<OPTION>::iterator it = std::find_if(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Check), *state.argv));
212     if (it != m_options.end())
213         state = it->Parse(--state.argc, ++state.argv, data);
214     else
215         break;
216     ++it;
217     }
218 return state;
219 }
220
221 void OPTION_BLOCK::ParseFile(const std::string & filePath)
222 {
223 if (access(filePath.c_str(), R_OK))
224     throw ERROR("File '" + filePath + "' does not exists.");
225 ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this);
226 }
227
228 void OPTION_BLOCK::OptionCallback(const std::string & key, const std::string & value)
229 {
230 for (std::vector<OPTION>::iterator it = m_options.begin(); it != m_options.end(); ++it)
231     if (it->Name() == key)
232         it->ParseValue(value);
233 }
234
235 void OPTION_BLOCKS::Help(size_t level) const
236 {
237 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
238 while (it != m_blocks.end())
239     {
240     it->Help(level);
241     std::cout << "\n";
242     ++it;
243     }
244 }
245
246 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
247 {
248 PARSER_STATE state(false, argc, argv);
249 std::list<OPTION_BLOCK>::iterator it(m_blocks.begin());
250 while (state.argc > 0 && !state.stop && it != m_blocks.end())
251     {
252     state = it->Parse(state.argc, state.argv);
253     ++it;
254     }
255 return state;
256 }