chooseOutputModule
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 05 May 2015 22:19:24 +0200
changeset 1665f51abd5fb8
parent 15 09adb33465e4
child 17 6d5ee720386c
chooseOutputModule
c++/parameter-lister/CLI.cpp
c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp
c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h
c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp
c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h
     1.1 --- a/c++/parameter-lister/CLI.cpp	Mon May 04 00:34:27 2015 +0200
     1.2 +++ b/c++/parameter-lister/CLI.cpp	Tue May 05 22:19:24 2015 +0200
     1.3 @@ -1,11 +1,11 @@
     1.4  #include <cstdlib>
     1.5  #include <iostream>
     1.6  #include <vector>
     1.7 +#include <map>
     1.8  #include <algorithm>
     1.9  #include <boost/optional.hpp>
    1.10  #include <memory>
    1.11  
    1.12 -#include "info/globalcode/parameterLister/terminalCodes/TerminalCodes.h"
    1.13  #include "info/globalcode/parameterLister/OutputModule.h"
    1.14  #include "info/globalcode/parameterLister/TerminalOutputModule.h"
    1.15  
    1.16 @@ -16,6 +16,9 @@
    1.17  namespace globalcode {
    1.18  namespace parameterLister {
    1.19  
    1.20 +const string ENV_BASE = "PARAMETER_LISTER";
    1.21 +const string ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT";
    1.22 +
    1.23  boost::optional<string> getenv(const string name) {
    1.24  	const char * value = ::getenv(name.c_str());
    1.25  	if (value == 0) {
    1.26 @@ -29,12 +32,21 @@
    1.27  }
    1.28  }
    1.29  
    1.30 +shared_ptr<OutputModule> chooseOutputModule() {
    1.31 +	boost::optional<string> moduleName = getenv(ENV_OUTPUT_MODULE_NAME);
    1.32 +	if (moduleName.is_initialized()) {
    1.33 +		// TODO: use a map instead of sequence of IFs
    1.34 +		if (moduleName.get() == "terminal") {
    1.35 +			return shared_ptr<OutputModule>(new TerminalOutputModule());
    1.36 +		}
    1.37 +	}
    1.38 +
    1.39 +	return shared_ptr<OutputModule>(new OutputModule());
    1.40 +}
    1.41 +
    1.42  int main(int argc, char* argv[]) {
    1.43 -	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    1.44 -	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    1.45  
    1.46 -	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
    1.47 -
    1.48 +	/** Load arguments */
    1.49  	string command = argv[0];
    1.50  	vector<string> args;
    1.51  
    1.52 @@ -42,28 +54,8 @@
    1.53  		args.push_back(argv[i]);
    1.54  	}
    1.55  
    1.56 -	/** Load environment variable */
    1.57 -	if (args.size() > 0) {
    1.58 -		string envName = args[0];
    1.59 -		boost::optional<string> outputModule = getenv(envName);
    1.60 +	/** Do formatting */
    1.61 +	shared_ptr<OutputModule> outputModule = chooseOutputModule();
    1.62  
    1.63 -		if (outputModule.is_initialized()) {
    1.64 -			cout << "ENV: " << envName << " = " << outputModule.get() << endl;
    1.65 -		} else {
    1.66 -			cout << "ENV: " << envName << " is missing" << endl;
    1.67 -		}
    1.68 -	}
    1.69 -
    1.70 -	/** Do formatting */
    1.71 -	{
    1.72 -		shared_ptr<OutputModule> om(new OutputModule());
    1.73 -		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
    1.74 -		shared_ptr<OutputModule> o(new TerminalOutputModule());
    1.75 -
    1.76 -		om->process(cout, command, args);
    1.77 -		tom->process(cout, command, args);
    1.78 -		o->process(cout, command, args);
    1.79 -	}
    1.80 -
    1.81 -	return 0;
    1.82 +	return outputModule->process(cout, command, args);
    1.83  }
     2.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp	Mon May 04 00:34:27 2015 +0200
     2.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp	Tue May 05 22:19:24 2015 +0200
     2.3 @@ -7,7 +7,7 @@
     2.4  namespace globalcode {
     2.5  namespace parameterLister {
     2.6  
     2.7 -void OutputModule::process(std::ostream &output, std::string &command, std::vector<std::string> &args) {
     2.8 +int OutputModule::process(std::ostream &output, std::string &command, std::vector<std::string> &args) {
     2.9  	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    2.10  	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    2.11  
    2.12 @@ -20,6 +20,8 @@
    2.13  	});
    2.14  
    2.15  	output << "</outputModule>" << endl;
    2.16 +	
    2.17 +	return 0;
    2.18  }
    2.19  
    2.20  
     3.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h	Mon May 04 00:34:27 2015 +0200
     3.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h	Tue May 05 22:19:24 2015 +0200
     3.3 @@ -10,7 +10,14 @@
     3.4  
     3.5  class OutputModule {
     3.6  public:
     3.7 -	virtual void process(std::ostream &output, std::string &command, std::vector<std::string> &args);
     3.8 +	/**
     3.9 +	 * 
    3.10 +     * @param output
    3.11 +     * @param command
    3.12 +     * @param args
    3.13 +     * @return exit code
    3.14 +     */
    3.15 +	virtual int process(std::ostream &output, std::string &command, std::vector<std::string> &args);
    3.16  private:
    3.17  
    3.18  };
     4.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp	Mon May 04 00:34:27 2015 +0200
     4.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp	Tue May 05 22:19:24 2015 +0200
     4.3 @@ -1,19 +1,26 @@
     4.4  #include "TerminalOutputModule.h"
     4.5 +#include "terminalCodes/TerminalCodes.h"
     4.6  
     4.7  namespace info {
     4.8  namespace globalcode {
     4.9  namespace parameterLister {
    4.10  
    4.11 -void TerminalOutputModule::process(std::ostream &output, std::string &command, std::vector<std::string> &args) {
    4.12 +int TerminalOutputModule::process(std::ostream &output, std::string &command, std::vector<std::string> &args) {
    4.13  	using namespace std;
    4.14 -	output << "<terminalOutputModule>" << endl;
    4.15 +	
    4.16 +	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    4.17 +	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    4.18 +	
    4.19 +	output << fgGreen << "<terminalOutputModule>" << fgReset << endl;
    4.20  	
    4.21  	for (int i = 0; i < args.size(); i++) {
    4.22  		string s = args[i];
    4.23  		output << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl;
    4.24  	}
    4.25  	
    4.26 -	output << "</terminalOutputModule>" << endl;
    4.27 +	output << fgGreen << "</terminalOutputModule>" << fgReset << endl;
    4.28 +	
    4.29 +	return 0;
    4.30  }
    4.31  
    4.32  }
     5.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h	Mon May 04 00:34:27 2015 +0200
     5.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h	Tue May 05 22:19:24 2015 +0200
     5.3 @@ -11,7 +11,7 @@
     5.4  
     5.5  class TerminalOutputModule : public OutputModule {
     5.6  public:
     5.7 -	virtual void process(std::ostream &output, std::string &command, std::vector<std::string> &args);
     5.8 +	virtual int process(std::ostream &output, std::string &command, std::vector<std::string> &args);
     5.9  private:
    5.10  
    5.11  };