Skip to content

Commit 645adb4

Browse files
author
Sascha Ochsenknecht
committed
Allow passing file name to parse_config_file(), Fixes #3264
[SVN r58248]
1 parent 80b3a04 commit 645adb4

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

include/boost/program_options/errors.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ namespace boost { namespace program_options {
206206
{}
207207
};
208208

209+
/** Class thrown if config file can not be read */
210+
class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error {
211+
public:
212+
reading_file(const char* filename)
213+
: error(std::string("can not read file ").append(filename))
214+
{}
215+
};
209216
}}
210217

211218

include/boost/program_options/parsers.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ namespace boost { namespace program_options {
147147
= ext_parser());
148148

149149
/** Parse a config file.
150+
151+
Read from given stream.
150152
*/
151153
template<class charT>
152154
#if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
@@ -156,6 +158,19 @@ namespace boost { namespace program_options {
156158
parse_config_file(std::basic_istream<charT>&, const options_description&,
157159
bool allow_unregistered = false);
158160

161+
/** Parse a config file.
162+
163+
Read from file with the given name. The character type is
164+
passed to the file stream.
165+
*/
166+
template<class charT>
167+
#if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
168+
BOOST_PROGRAM_OPTIONS_DECL
169+
#endif
170+
basic_parsed_options<charT>
171+
parse_config_file(const char* filename, const options_description&,
172+
bool allow_unregistered = false);
173+
159174
/** Controls if the 'collect_unregistered' function should
160175
include positional options, or not. */
161176
enum collect_unrecognized_mode

src/parsers.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <boost/throw_exception.hpp>
2121

2222
#include <cctype>
23+
#include <fstream>
2324

2425
#if !defined(__GNUC__) || __GNUC__ < 3
2526
#include <iostream>
@@ -134,6 +135,36 @@ namespace boost { namespace program_options {
134135
const options_description& desc,
135136
bool allow_unregistered);
136137
#endif
138+
139+
template<class charT>
140+
basic_parsed_options<charT>
141+
parse_config_file(const char* filename,
142+
const options_description& desc,
143+
bool allow_unregistered)
144+
{
145+
// Parser return char strings
146+
std::basic_ifstream< charT > strm(filename);
147+
if (!strm)
148+
{
149+
boost::throw_exception(reading_file(filename));
150+
}
151+
return parse_config_file(strm, desc, allow_unregistered);
152+
}
153+
154+
template
155+
BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<char>
156+
parse_config_file(const char* filename,
157+
const options_description& desc,
158+
bool allow_unregistered);
159+
160+
#ifndef BOOST_NO_STD_WSTRING
161+
template
162+
BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<wchar_t>
163+
parse_config_file(const char* filename,
164+
const options_description& desc,
165+
bool allow_unregistered);
166+
#endif
167+
137168

138169
// This versio, which accepts any options without validation, is disabled,
139170
// in the hope that nobody will need it and we cant drop it altogether.

test/config_test.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
gv1 = 0#asd
2+
empty_value =
3+
plug3 = 7
4+
b = true
5+
6+
[m1]
7+
v1 = 1
8+
9+
v2 = 2

test/parsers_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,16 @@ void test_config_file()
229229
check_value(a1[3], "b", "true");
230230
check_value(a1[4], "m1.v1", "1");
231231
check_value(a1[5], "m1.v2", "2");
232-
232+
233+
// same test, but now options come from file
234+
vector<option> a2 = parse_config_file<char>("config_test.cfg", desc).options;
235+
BOOST_REQUIRE(a2.size() == 6);
236+
check_value(a2[0], "gv1", "0");
237+
check_value(a2[1], "empty_value", "");
238+
check_value(a2[2], "plug3", "7");
239+
check_value(a2[3], "b", "true");
240+
check_value(a2[4], "m1.v1", "1");
241+
check_value(a2[5], "m1.v2", "2");
233242
}
234243

235244
void test_environment()

0 commit comments

Comments
 (0)