Bring aboard classes to handle each kind of configuration interface
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 31 Aug 2011 02:53:30 +0000 (23:53 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 31 Aug 2011 02:53:30 +0000 (23:53 -0300)
src/config/configurationcommandline.cpp [new file with mode: 0644]
src/config/configurationcommandline.h [new file with mode: 0644]
src/config/configurationfile.cpp [new file with mode: 0644]
src/config/configurationfile.h [new file with mode: 0644]
src/config/configurationinterface.cpp [new file with mode: 0644]
src/config/configurationinterface.h [new file with mode: 0644]

diff --git a/src/config/configurationcommandline.cpp b/src/config/configurationcommandline.cpp
new file mode 100644 (file)
index 0000000..92baf22
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/configurationcommandline.h"
+
+#include <boost/assert.hpp>
+
+#include <logfunc.hpp>
+
+#include "config/configurationoptions.h"
+
+using namespace std;
+using boost::program_options::command_line_parser;
+using boost::program_options::options_description;
+using boost::program_options::parsed_options;
+using boost::program_options::positional_options_description;
+using boost::program_options::variables_map;
+using I2n::Logger::GlobalLogger;
+
+//-----------------------------------------------------------------------------
+// ConfigurationCommandLine
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Parameterized constructor.
+ *
+ * @param _argc The number of arguments in command line.
+ * @param _argv A vector containing the command line elements.
+ */
+ConfigurationCommandLine::ConfigurationCommandLine(
+        const int _argc,
+        char **_argv
+) :
+    argc( _argc ),
+    argv( _argv ) // TODO perform a deep copy, not swallow
+{
+    BOOST_ASSERT( argc > 0 );
+    BOOST_ASSERT( argv != NULL );
+}
+
+/**
+ * @brief Copy constructor.
+ *
+ * @param other A object to be copied.
+ */
+ConfigurationCommandLine::ConfigurationCommandLine(
+        const ConfigurationCommandLine &other
+) :
+    argc( other.argc ),
+    argv( other.argv ) // TODO perform a deep copy, not swallow
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+ConfigurationCommandLine::~ConfigurationCommandLine()
+{
+}
+
+ConfigurationCommandLine & ConfigurationCommandLine::operator =(
+        const ConfigurationCommandLine &other
+)
+{
+    argc = other.argc;
+    argv = other.argv; // TODO perform a deep copy, not swallow
+
+    return *this;
+}
+
+/**
+ * @brief Reads the command line, storing the parsed tokens in the
+ * @c variables_map.
+ *
+ * @param vm The variables_map object to store configuration tokens.
+ *
+ * @return @c true on success and @c false otherwise.
+ */
+bool ConfigurationCommandLine::process( variables_map *vm )
+{
+    BOOST_ASSERT( vm != NULL );
+
+    try
+    {
+        ConfigurationOptions options;
+
+        options_description generic = options.get_generic_options();
+        options_description config = options.get_configuration_options();
+
+        options_description cmdline_options;
+        cmdline_options.add( generic ).add( config );
+
+        positional_options_description p;
+        (void) p.add( options.HostNameCmdStr.c_str(), -1 );
+
+        parsed_options parsed_opt = command_line_parser( argc, argv ).
+                options( cmdline_options ).
+                positional( p ).
+                allow_unregistered().
+                run();
+        store( parsed_opt, *vm );
+        notify( *vm );
+    }
+    catch ( const std::exception &ex )
+    {
+        GlobalLogger.error() << ex.what() << endl;
+        return false;
+    }
+
+    return true;
+}
+
+/**
+ * @brief Store the parsed tokens in @a configuration
+ *
+ * @param
+ * @param
+ *
+ * @return
+ */
+bool ConfigurationCommandLine::parse(
+        const variables_map &vm,
+        Configuration *configuration
+)
+{
+    BOOST_ASSERT( configuration != NULL );
+
+    ConfigurationOptions options;
+
+    // do not proceed if it is one of the halt options (i.e. version, help)
+    bool halt = options.halt_on_generic_options( vm );
+    if ( halt )
+    {
+        return false;
+    }
+
+    bool generic_parsed = options.parse_generic_options( vm, configuration );
+    bool configuration_parsed = options.parse_configuration_options( vm, configuration );
+
+    return ( generic_parsed && configuration_parsed );
+}
diff --git a/src/config/configurationcommandline.h b/src/config/configurationcommandline.h
new file mode 100644 (file)
index 0000000..e40804e
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef CONFIGURATIONCOMMANDLINE_H
+#define CONFIGURATIONCOMMANDLINE_H
+
+#include <boost/program_options.hpp>
+
+#include "config/configurationinterface.h"
+
+//-----------------------------------------------------------------------------
+// ConfigurationCommandLine
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Configuration interface for command line.
+ */
+class ConfigurationCommandLine : public ConfigurationInterface
+{
+public:
+    ConfigurationCommandLine( const int _argc, char **_argv );
+    ConfigurationCommandLine( const ConfigurationCommandLine &other );
+    virtual ~ConfigurationCommandLine();
+
+    ConfigurationCommandLine& operator=( const ConfigurationCommandLine &other );
+
+    bool process( boost::program_options::variables_map *vm );
+
+    bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    );
+
+private:
+    int argc;
+    char **argv;
+
+};
+
+#endif // CONFIGURATIONCOMMANDLINE_H
diff --git a/src/config/configurationfile.cpp b/src/config/configurationfile.cpp
new file mode 100644 (file)
index 0000000..ab7689a
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/configurationfile.h"
+
+#include <fstream>
+#include <iostream>
+
+#include <boost/assert.hpp>
+
+#include <logfunc.hpp>
+
+#include "config/configurationoptions.h"
+
+using namespace std;
+using boost::program_options::options_description;
+using boost::program_options::parsed_options;
+using boost::program_options::parse_config_file;
+using boost::program_options::variables_map;
+using I2n::Logger::GlobalLogger;
+
+//-----------------------------------------------------------------------------
+// ConfigurationFile
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Parameterized constructor.
+ *
+ * @param file_name The name and path of the file containing the configuration.
+ */
+ConfigurationFile::ConfigurationFile( string file_name ) :
+    FileName( file_name )
+{
+    BOOST_ASSERT( !file_name.empty() );
+}
+
+/**
+ * @brief Destructor.
+ */
+ConfigurationFile::~ConfigurationFile()
+{
+}
+
+/**
+ * @brief Reads the configuration file, storing the parsed tokens in the
+ * @c variables_map.
+ *
+ * @param vm The variables_map object to store configuration tokens.
+ *
+ * @return @c true on success and @c false otherwise.
+ */
+bool ConfigurationFile::process( variables_map *vm )
+{
+    BOOST_ASSERT( vm != NULL );
+
+    ifstream ifs( FileName.c_str() );
+    if ( !ifs )
+    {
+        GlobalLogger.error() << "Error: could not open " << FileName
+            << " file." << endl;
+        return false;
+    }
+
+    try
+    {
+        ConfigurationOptions options;
+
+        options_description config = options.get_configuration_options();
+        options_description config_file_options;
+        config_file_options.add( config );
+
+        parsed_options parsed_opt = parse_config_file( ifs, config_file_options );
+        store( parsed_opt, *vm );
+        notify( *vm );
+    }
+    catch ( const std::exception &ex )
+    {
+        GlobalLogger.error() << ex.what() << endl;
+        return false;
+    }
+
+    return true;
+}
+
+/**
+ * @brief Store the parsed tokens in @a configuration
+ *
+ * @param
+ * @param
+ *
+ * @return
+ */
+bool ConfigurationFile::parse(
+        const variables_map &vm,
+        Configuration *configuration
+)
+{
+    BOOST_ASSERT( configuration != NULL );
+
+    ConfigurationOptions options;
+
+    bool configuration_parsed = options.parse_configuration_options( vm, configuration );
+
+    return configuration_parsed;
+}
+
diff --git a/src/config/configurationfile.h b/src/config/configurationfile.h
new file mode 100644 (file)
index 0000000..2190a27
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef CONFIGURATIONFILE_H
+#define CONFIGURATIONFILE_H
+
+#include <string>
+
+#include <boost/program_options.hpp>
+
+#include "config/configurationinterface.h"
+
+//-----------------------------------------------------------------------------
+// ConfigurationFile
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Configuration interface for files.
+ */
+class ConfigurationFile : public ConfigurationInterface
+{
+public:
+    ConfigurationFile( std::string file_name );
+    virtual ~ConfigurationFile();
+
+    bool process( boost::program_options::variables_map *vm );
+
+    bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    );
+
+private:
+    std::string FileName;
+
+};
+
+#endif // CONFIGURATIONFILE_H
diff --git a/src/config/configurationinterface.cpp b/src/config/configurationinterface.cpp
new file mode 100644 (file)
index 0000000..0ade8dd
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/configurationinterface.h"
+
+//-----------------------------------------------------------------------------
+// ConfigurationInterface
+//-----------------------------------------------------------------------------
+
+ConfigurationInterface::ConfigurationInterface()
+{
+}
+
+ConfigurationInterface::~ConfigurationInterface()
+{
+}
diff --git a/src/config/configurationinterface.h b/src/config/configurationinterface.h
new file mode 100644 (file)
index 0000000..e097761
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef CONFIGURATIONINTERFACE_H
+#define CONFIGURATIONINTERFACE_H
+
+#include <boost/program_options.hpp>
+
+#include "config/configuration.h"
+
+//-----------------------------------------------------------------------------
+// ConfigurationInterface
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Abstract class for configuration interfaces.
+ */
+class ConfigurationInterface
+{
+public:
+    ConfigurationInterface();
+    virtual ~ConfigurationInterface();
+
+    virtual bool process( boost::program_options::variables_map *vm ) = 0;
+
+    virtual bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    ) = 0;
+
+};
+
+#endif // CONFIGURATIONINTERFACE_H