-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline_parser.cpp
186 lines (159 loc) · 5.86 KB
/
cmdline_parser.cpp
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
/*
* cmdline_parser.cpp
*
* Created on: 22 Feb 2012
* Author: simon
*/
#include "cmdline_parser.h"
//-------------------------------------------------------------------------//
// Constructor, initialise the variables_map object with default
// constructor, options_descriptor with the name "Options"
//-------------------------------------------------------------------------//
cmdline_parser::cmdline_parser(void) : vm(), od("Options")
{
// Shorter alias for the boost::program_options namespace
namespace po = boost::program_options;
// Add two cmdline options
// --help or -?
// --input-file or -i
od.add_options()
("help,?", "produce help message")
((GROUPING +",g").c_str(),"enable grouping")
((PACKING +",p").c_str(), "enable packing")
((ENCODING +",e").c_str(), "Perform a decode")
((DECODING +",d").c_str(), "Perform an encode")
((INPUTFILE+",i").c_str(), po::value<std::string>(),
"input file ")
((OUTPUTFILE+",o").c_str(), po::value<std::string>(),
"output file name")
((CKEY+",c").c_str(), po::value<int>(),
"Ceaser Key")
((VKEY+",v").c_str(), po::value<std::string>(), "Vignere Key")
((XKEY+",x").c_str(), po::value<int32_t>(), "XOR Key");
};
//-------------------------------------------------------------------------//
// Process the cmdline
//-------------------------------------------------------------------------//
bool cmdline_parser::process_cmdline(int argc, char * argv[])
{
// Shorter alias for the boost::program_options namespace
namespace po = boost::program_options;
// Clear the variable map
vm.clear();
// Parse the cmdline arguments defined by argc and argv,
// looking for the options defined in the od variable,
// and store them in the vm variable.
po::store(po::parse_command_line(argc, argv, od), vm);
po::notify(vm);
//ensure only one encrypt/decrypt type is specified
int encCount = vm.count(XKEY)+vm.count(CKEY)+vm.count(VKEY);
bool success = true;
if(vm.count(OUTPUTFILE) == 0)
{
success = false;
errors.push_back(
OUTPUTFILE + " not specified.");
}
if(vm.count(INPUTFILE) == 0)
{
success = false;
errors.push_back(
INPUTFILE + " not specified.");
}
if(encCount!=1){
success = false;
errors.push_back ("Too many/Few Encryption methods.");
}
return success;
}
//-----------------------------------------------------------------------//
// Return the input filename
//-------------------------------------------------------------------------//
std::string cmdline_parser::get_input_filename(void) const
{
// Return whatever value is stored as a string
return vm[INPUTFILE].as<std::string>();
}
//-----------------------------------------------------------------------//
// Return Encoding?
//-------------------------------------------------------------------------//
bool cmdline_parser::get_encoding(void) const
{
// Return Whether encoding has been selected
return vm.count(ENCODING) > 0;
}
//-----------------------------------------------------------------------//
// Return Decoding?
//-------------------------------------------------------------------------//
bool cmdline_parser::get_decoding(void) const
{
// Return Whether encoding has been selected
return vm.count(DECODING) > 0;
}
//-----------------------------------------------------------------------//
// Return Cypher Type?
//-------------------------------------------------------------------------//
char cmdline_parser::get_cypher(void) const {
return (vm.count(CKEY)==1)? 'c': (vm.count(VKEY)==1)? 'v' : 'x';
}
int cmdline_parser::get_ckey(void) const{
return vm[CKEY].as<int>();
}
std::string cmdline_parser::get_vkey(void) const{
return vm[VKEY].as<std::string>();
}
int32_t cmdline_parser::get_xkey(void) const{
return vm[XKEY].as<int32_t>();
}
bool cmdline_parser::should_group(void) const
{
return vm.count(GROUPING) >0;
};
bool cmdline_parser::should_pack(void) const
{
return vm.count(PACKING) >0;
};
//-----------------------------------------------------------------------//
// Return output filename
//-------------------------------------------------------------------------//
std::string cmdline_parser::get_output_filename(void) const
{
// Return whatever value is stored as a string
return vm[OUTPUTFILE].as<std::string>();
}
//-----------------------------------------------------------------------//
// Should we print cmdline help?
//-------------------------------------------------------------------------//
bool cmdline_parser::should_print_help(void) const
{
// Are there instances of the help option stored in the variable map
return vm.count("help") > 0;
}
//-------------------------------------------------------------------------//
// Print out the options_descriptor to the supplied output stream
//-------------------------------------------------------------------------//
void cmdline_parser::print_help(std::ostream & out) const
{
out << od;
}
//-------------------------------------------------------------------------//
// Print out the options_descriptor to the supplied output stream
//-------------------------------------------------------------------------//
void cmdline_parser::print_errors(std::ostream & out) const
{
std::cerr << "Error processing command line arguments:" << std::endl;
std::copy(errors.begin(), errors.end(),
std::ostream_iterator<std::string>(out, "\n"));
}
// Definition of static strings in the class
const std::string cmdline_parser::INPUTFILE = "input-file";
const std::string cmdline_parser::OUTPUTFILE = "Output-File";
//Definition of static strings in class
//My static string variables.
const std::string cmdline_parser::XKEY = "XOR-Key";
const std::string cmdline_parser::VKEY = "Vignere-Key";
const std::string cmdline_parser::CKEY = "Ceaser-Key";
const std::string cmdline_parser::GROUPING = "Grouping";
const std::string cmdline_parser::PACKING = "Packing";
const std::string cmdline_parser::ENCODING="Encoding";
const std::string cmdline_parser::DECODING = "Decoding";