[go: up one dir, main page]

Menu

[r1]: / gizmod3 / gizmod / Gizmod.cpp  Maximize  Restore  History

Download this file

195 lines (163 with data), 5.2 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
*********************************************************************
*************************************************************************
***
*** \file Gizmod.cpp
*** \brief Gizmod class body
***
*****************************************
*****************************************
**/
/*
Copyright (c) 2007, Tim Burrell
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Gizmod.hpp"
#include "../libH/Debug.hpp"
#include "../libH/Exception.hpp"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
using namespace std;
using namespace boost;
using namespace boost::program_options;
using namespace H;
////////////////////////////////////////////////////////////////////////////
// Type Defs / defines
///////////////////////////////////////
/**
* \def CONFIG_FILE
* The default path of the config file
*/
#define CONFIG_FILE PACKAGE_NAME ".conf"
/**
* \def SCRIPT_FILE
* The default path of the config file
*/
#define SCRIPT_FILE PACKAGE_NAME ".py"
////////////////////////////////////////////////////////////////////////////
// Construction
///////////////////////////////////////
/**
* \brief Default Constructor
*/
Gizmod::Gizmod() {
cout << getProps();
mConfigScript = SCRIPT_FILE;
}
/**
* \brief Default Destructor
*/
Gizmod::~Gizmod() {
cdbg << "Gizmod Shutting Down..." << endl << endl;
}
////////////////////////////////////////////////////////////////////////////
// Class Body
///////////////////////////////////////
/**
* \brief Enter the main run loop
*/
void Gizmod::enterLoop() {
}
/**
* \brief Get the program's propers
*/
string Gizmod::getProps() {
return "\nGizmod v" + string(PACKAGE_VERSION) + " -- (c) 2007, Tim Burrell\n";
}
/**
* \brief Setup Gizmod
*
* Initialize Gizmod
*/
void Gizmod::initGizmod() {
}
/**
* \brief generic Init stuff
* \param argc number of command line arguments
* \param argv command line arguments
* \return true if the program should continue, false otherwise
*
* load the config file, process command line options, etc
*/
bool Gizmod::initialize(int argc, char ** argv) {
// generic options
options_description GenericOptions("Generic Options");
GenericOptions.add_options()
("debug,g", "Enable debug mode")
("help,h", "Display informative help message")
("verbosity,V", value<int>(), "Set debug vebosity level (0-5) [Default = 0]")
("version,v", "Print version information")
;
// config file options that can be loaded via command line as well
options_description ConfigurationOptions("Configuration Options");
ConfigurationOptions.add_options()
("config,c", value<string>(), "Set config file script")
;
// hiGizmodn options
options_description HiddenOptions("Hidden Options");
HiddenOptions.add_options();
// create command line options group
options_description CommandLineOptions;
CommandLineOptions.add(GenericOptions).add(ConfigurationOptions).add(HiddenOptions);
// create config file options group
options_description ConfigFileOptions;
ConfigFileOptions.add(ConfigurationOptions).add(HiddenOptions);
// create a visible options group for help display
options_description VisibleOptions("");
VisibleOptions.add(GenericOptions).add(ConfigurationOptions);
// create the variables map
variables_map VarMap;
// try parsing the command line
try {
store(parse_command_line(argc, argv, CommandLineOptions), VarMap);
} catch (exception e) {
cout << VisibleOptions;
throw H::Exception("Invalid Command Line Argument(s)");
}
// try parsing the config file
try {
ifstream ifs(CONFIG_FILE);
store(parse_config_file(ifs, ConfigFileOptions), VarMap);
} catch (exception e) {
cout << VisibleOptions;
throw H::Exception("Invalid Configuration File");
}
// build the option map
notify(VarMap);
// check for options
if (VarMap.count("help")) {
cout << VisibleOptions << endl;
return false;
}
if (VarMap.count("version")) {
return false;
}
if (VarMap.count("debug")) {
Debug::setDebugEnabled(true);
cdbg << "Debug Mode Enabled" << endl;
}
if (VarMap.count("verbosity")) {
Debug::setDebugVerbosity(VarMap["verbosity"].as<int>());
cdbg << "Debug Verbosity set to [" << VarMap["verbosity"].as<int>() << "]" << endl;
}
if (VarMap.count("config")) {
mConfigScript = VarMap["config"].as<string>();
cdbg << "Config Script set to [" << VarMap["config"].as<string>() << "]" << endl;
}
cout << endl;
return true;
}