|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r58248 - in trunk: boost/program_options libs/program_options/src libs/program_options/test
From: s.ochsenknecht_at_[hidden]
Date: 2009-12-09 08:45:02
Author: s_ochsenknecht
Date: 2009-12-09 08:45:01 EST (Wed, 09 Dec 2009)
New Revision: 58248
URL: http://svn.boost.org/trac/boost/changeset/58248
Log:
Allow passing file name to parse_config_file(), Fixes #3264
Added:
trunk/libs/program_options/test/config_test.cfg (contents, props changed)
Text files modified:
trunk/boost/program_options/errors.hpp | 7 +++++++
trunk/boost/program_options/parsers.hpp | 15 +++++++++++++++
trunk/libs/program_options/src/parsers.cpp | 31 +++++++++++++++++++++++++++++++
trunk/libs/program_options/test/parsers_test.cpp | 11 ++++++++++-
4 files changed, 63 insertions(+), 1 deletions(-)
Modified: trunk/boost/program_options/errors.hpp
==============================================================================
--- trunk/boost/program_options/errors.hpp (original)
+++ trunk/boost/program_options/errors.hpp 2009-12-09 08:45:01 EST (Wed, 09 Dec 2009)
@@ -206,6 +206,13 @@
{}
};
+ /** Class thrown if config file can not be read */
+ class BOOST_PROGRAM_OPTIONS_DECL reading_file : public error {
+ public:
+ reading_file(const char* filename)
+ : error(std::string("can not read file ").append(filename))
+ {}
+ };
}}
Modified: trunk/boost/program_options/parsers.hpp
==============================================================================
--- trunk/boost/program_options/parsers.hpp (original)
+++ trunk/boost/program_options/parsers.hpp 2009-12-09 08:45:01 EST (Wed, 09 Dec 2009)
@@ -147,6 +147,8 @@
= ext_parser());
/** Parse a config file.
+
+ Read from given stream.
*/
template<class charT>
#if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
@@ -156,6 +158,19 @@
parse_config_file(std::basic_istream<charT>&, const options_description&,
bool allow_unregistered = false);
+ /** Parse a config file.
+
+ Read from file with the given name. The character type is
+ passed to the file stream.
+ */
+ template<class charT>
+#if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
+ BOOST_PROGRAM_OPTIONS_DECL
+#endif
+ basic_parsed_options<charT>
+ parse_config_file(const char* filename, const options_description&,
+ bool allow_unregistered = false);
+
/** Controls if the 'collect_unregistered' function should
include positional options, or not. */
enum collect_unrecognized_mode
Modified: trunk/libs/program_options/src/parsers.cpp
==============================================================================
--- trunk/libs/program_options/src/parsers.cpp (original)
+++ trunk/libs/program_options/src/parsers.cpp 2009-12-09 08:45:01 EST (Wed, 09 Dec 2009)
@@ -20,6 +20,7 @@
#include <boost/throw_exception.hpp>
#include <cctype>
+#include <fstream>
#if !defined(__GNUC__) || __GNUC__ < 3
#include <iostream>
@@ -134,6 +135,36 @@
const options_description& desc,
bool allow_unregistered);
#endif
+
+ template<class charT>
+ basic_parsed_options<charT>
+ parse_config_file(const char* filename,
+ const options_description& desc,
+ bool allow_unregistered)
+ {
+ // Parser return char strings
+ std::basic_ifstream< charT > strm(filename);
+ if (!strm)
+ {
+ boost::throw_exception(reading_file(filename));
+ }
+ return parse_config_file(strm, desc, allow_unregistered);
+ }
+
+ template
+ BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<char>
+ parse_config_file(const char* filename,
+ const options_description& desc,
+ bool allow_unregistered);
+
+#ifndef BOOST_NO_STD_WSTRING
+ template
+ BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<wchar_t>
+ parse_config_file(const char* filename,
+ const options_description& desc,
+ bool allow_unregistered);
+#endif
+
// This versio, which accepts any options without validation, is disabled,
// in the hope that nobody will need it and we cant drop it altogether.
Added: trunk/libs/program_options/test/config_test.cfg
==============================================================================
--- (empty file)
+++ trunk/libs/program_options/test/config_test.cfg 2009-12-09 08:45:01 EST (Wed, 09 Dec 2009)
@@ -0,0 +1,9 @@
+gv1 = 0#asd
+empty_value =
+plug3 = 7
+b = true
+
+[m1]
+v1 = 1
+
+v2 = 2
Modified: trunk/libs/program_options/test/parsers_test.cpp
==============================================================================
--- trunk/libs/program_options/test/parsers_test.cpp (original)
+++ trunk/libs/program_options/test/parsers_test.cpp 2009-12-09 08:45:01 EST (Wed, 09 Dec 2009)
@@ -229,7 +229,16 @@
check_value(a1[3], "b", "true");
check_value(a1[4], "m1.v1", "1");
check_value(a1[5], "m1.v2", "2");
-
+
+ // same test, but now options come from file
+ vector<option> a2 = parse_config_file<char>("config_test.cfg", desc).options;
+ BOOST_REQUIRE(a2.size() == 6);
+ check_value(a2[0], "gv1", "0");
+ check_value(a2[1], "empty_value", "");
+ check_value(a2[2], "plug3", "7");
+ check_value(a2[3], "b", "true");
+ check_value(a2[4], "m1.v1", "1");
+ check_value(a2[5], "m1.v2", "2");
}
void test_environment()
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk