Repository navigation
Expand file tree
/
Copy pathAppBase.cpp
More file actions
151 lines (118 loc) · 2.54 KB
/
Copy pathAppBase.cpp
File metadata and controls
151 lines (118 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* \file AppBase.cpp
* \author Jan Milik <milikjan@fit.cvut.cz>
* \date 2012-10-03
*
* \brief Implementation file for the AppBase class.
*/
#include "AppBase.h"
#include "Logger.h"
#include <sstream>
namespace cppapp {
/**
*
*/
string AppBase::getDefaultConfigFile()
{
ostringstream s;
s << getenv("HOME") <<
"/." <<
pathBasename(options_.getExecutable());
return s.str();
}
void AppBase::printUsage(std::ostream& out)
{
options_.printUsage(out);
out << std::endl;
}
void AppBase::readConfig()
{
if (!readConfig_)
return;
string configFileName = config_->get(CPPAPP_CONFIG_FILE_CFG_KEY,
getDefaultConfigFile())->asString();
Ref<FileInput> configInput = new FileInput(configFileName);
if (configInput->exists()) {
ConfigParser parser(config_);
parser.parse(configInput);
configInput->close();
} else {
LOG_WARNING("Configuration file " << configFileName << " does not exist.");
}
// Override the configuration read from the file by the command line
// options.
options_.setConfigKeys(config_);
}
/**
*
*/
void AppBase::setUp()
{
options().add('c',
"-",
"CONFIG_FILE",
CPPAPP_CONFIG_FILE_CFG_KEY,
"Use the specified config file instead of the default.");
options().add('o',
"-",
"OUTPUT_FILE",
CPPAPP_OUTPUT_FILE_CFG_KEY,
"Write output to file OUTPUT_FILE.");
options().add('h', "", "Print this help.");
}
/**
*
*/
int AppBase::onRun()
{
if ((bool)options().get('h')) {
printUsage(std::cout);
exit(EXIT_SUCCESS);
}
VAR(out, config_->get(CPPAPP_OUTPUT_FILE_CFG_KEY));
if (out.isNotNull()) {
output_ = new FileOutput(out->asString());
}
//Option out = options().get('o');
//if ((bool)out) {
// output_ = new FileOutput((string)out);
//}
return EXIT_SUCCESS;
}
/**
* Constructor.
*/
AppBase::AppBase() :
readConfig_(true),
config_(Config::globalConfig()),
output_(new StandardOutput())
{
}
/**
* Constructor.
*/
AppBase::AppBase(bool readConfig) :
readConfig_(readConfig),
config_(Config::globalConfig()),
output_(new StandardOutput())
{
}
int AppBase::run(int argc, char* argv[])
{
// Call setUp hook.
setUp();
Logger::defaultConfig();
// Parse command line options
options_.parse(argc, argv);
if (!options_.isValid()) {
printUsage(std::cerr);
return EXIT_FAILURE;
}
if (options_.get(CPPAPP_CONFIG_FILE_CFG_KEY).isSet)
options_.get(CPPAPP_CONFIG_FILE_CFG_KEY).setConfigKey(config_);
// Parse configuration file
readConfig();
// Run the actual application.
return onRun();
}
} // namespace cppapp