Whitespace and minor c++11 fixups

This commit is contained in:
Danny Robson 2013-02-26 18:56:25 +11:00
parent 0b30b56c58
commit d482f1fde4
3 changed files with 123 additions and 118 deletions

View File

@ -22,17 +22,15 @@
#include "config.h" #include "config.h"
#include <iostream>
#include <stdexcept>
#include <cassert> #include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <cmath> #include <cstring>
#include <stdint.h> #include <iostream>
#include <memory>
#include <stdint.h> #include <stdexcept>
#include <string.h>
using namespace std; using namespace std;
@ -57,13 +55,16 @@ option::option (char _letter,
void option::execute (void) { void
option::execute (void) {
throw runtime_error( throw runtime_error(
"Cannot provide no value for the option '" + m_longopt + "'" "Cannot provide no value for the option '" + m_longopt + "'"
); );
} }
void option::execute (const string& data) {
void
option::execute (const string& data) {
assert(data.size() > 0); assert(data.size() > 0);
throw runtime_error( throw runtime_error(
"Cannot provide a value for the option '" + m_longopt + "'" "Cannot provide a value for the option '" + m_longopt + "'"
@ -71,7 +72,8 @@ void option::execute (const string& data) {
} }
ostream& operator<< (ostream & os, const option& opt) { ostream&
operator<< (ostream & os, const option& opt) {
os << (opt.is_required () ? " -" : "[-" ) << opt.shortopt () os << (opt.is_required () ? " -" : "[-" ) << opt.shortopt ()
<< (opt.is_required () ? " \t" : "]\t") << opt.longopt () << (opt.is_required () ? " \t" : "]\t") << opt.longopt ()
<< "\t" << opt.description (); << "\t" << opt.description ();
@ -177,7 +179,6 @@ bytesoption::execute (const std::string& data) {
size_t defaultvalue = *m_data; size_t defaultvalue = *m_data;
try { try {
bytesmodifier modifier = m_modifier; bytesmodifier modifier = m_modifier;
off_t cursor = data.size () - 1; off_t cursor = data.size () - 1;
@ -231,7 +232,7 @@ bytesoption::execute (const std::string& data) {
--cursor; --cursor;
assert (cursor >= 0); assert (cursor >= 0);
multiplier = pow ((double)modifier_factor, (int)specified); multiplier = std::pow (modifier_factor, (int)specified);
get_arg (data.substr(0, cursor + 1), m_data); get_arg (data.substr(0, cursor + 1), m_data);
*m_data *= multiplier; *m_data *= multiplier;
} catch (...) { } catch (...) {
@ -245,6 +246,7 @@ bytesoption::execute (const std::string& data) {
m_found = true; m_found = true;
} }
/* /*
* Internal helper options. Print help and usage. * Internal helper options. Print help and usage.
* A callback to the processor which triggers output of the help message. * A callback to the processor which triggers output of the help message.
@ -264,22 +266,23 @@ class helpoption : public option {
helpoption (processor * _processor): helpoption (processor * _processor):
option (HELP_CHARACTER, HELP_NAME, HELP_DESCRIPTION, false), option (HELP_CHARACTER, HELP_NAME, HELP_DESCRIPTION, false),
m_processor (_processor) m_processor (_processor)
{ ; } { ; }
virtual void execute (void); virtual void execute (void);
virtual void execute (const std::string& data) virtual void execute (const std::string& data)
{ option::execute (data); } { option::execute (data); }
}; };
const char helpoption::HELP_CHARACTER = 'h'; const char helpoption::HELP_CHARACTER = 'h';
const char *helpoption::HELP_NAME = "help"; const char *helpoption::HELP_NAME = "help";
const char *helpoption::HELP_DESCRIPTION = const char *helpoption::HELP_DESCRIPTION =
"display help and usage information"; "display help and usage information";
void helpoption::execute (void) { void
helpoption::execute (void) {
m_processor->print_usage (); m_processor->print_usage ();
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }
@ -294,17 +297,17 @@ processor::processor ()
processor::~processor () { processor::~processor () {
for(auto i = m_options.begin(); i != m_options.end(); i++) for (auto i: m_options)
delete *i; delete i;
} }
void processor::print_usage (void) { void
processor::print_usage (void) {
cout << "Usage: " << m_command << " [options]" << endl; cout << "Usage: " << m_command << " [options]" << endl;
for(list<option *>::const_iterator i = m_options.begin (); for (const auto i: m_options)
i != m_options.end (); i++) cout << '\t' << *i << endl;
cout << '\t' << **i << endl;
} }
@ -321,12 +324,13 @@ void processor::print_usage (void) {
* *
* @return the number of tokens consumed for this option; must be at least 1. * @return the number of tokens consumed for this option; must be at least 1.
*/ */
unsigned int processor::parse_short (int pos, int argc, const char ** argv) { unsigned int
processor::parse_short (int pos, int argc, const char **argv) {
assert (pos > 0); assert (pos > 0);
assert (pos < argc); assert (pos < argc);
assert (argv[pos] != NULL); assert (argv[pos] != NULL);
const char * arg = argv[pos]; const char *arg = argv[pos];
// Must begin with a dash, then have at least one non-dash character // Must begin with a dash, then have at least one non-dash character
assert (arg[0] == '-'); assert (arg[0] == '-');
@ -375,33 +379,34 @@ unsigned int processor::parse_short (int pos, int argc, const char ** argv) {
* *
* @return the number of tokens consumed for this option; must be 1. * @return the number of tokens consumed for this option; must be 1.
*/ */
unsigned int processor::parse_long (int pos, int argc, const char ** argv) { unsigned int
processor::parse_long (int pos, int argc, const char ** argv) {
assert (pos > 0); assert (pos > 0);
assert (pos < argc); assert (pos < argc);
assert (argv[pos] != NULL); assert (argv[pos] != NULL);
const char * arg = argv[pos]; const char *arg = argv[pos];
// We must have at least two hyphens, and two letters (else a short opt) // We must have at least two hyphens, and two letters (else a short opt)
assert (arg[0] == '-'); assert (arg[0] == '-');
assert (arg[1] == '-'); assert (arg[1] == '-');
assert (strlen(arg) >= 4); assert (strlen (arg) >= 4);
// Skip past the dashes to the argument name // Skip past the dashes to the argument name
arg += 2; arg += 2;
// If there's an equals it's has a value to extract // If there's an equals it's has a value to extract
const char * data = strchr (arg, '='); const char * data = strchr (arg, '=');
if( data) { if (data) {
arg = strndup (arg, data - arg); // Copy just the arg name arg = strndup (arg, data - arg); // Copy just the arg name
data++; // Skip the '=' data++; // Skip the '='
} }
option * o = m_longopt[arg]; option *o = m_longopt[arg];
if (!o) if (!o)
throw runtime_error ("Cannot match option"); throw runtime_error ("Cannot match option");
if(data) if (data)
o->execute (data); o->execute (data);
else else
o->execute (); o->execute ();
@ -432,8 +437,8 @@ processor::parse_args (int argc, const char ** argv) {
// Must make sure each option has a chance to reset state (clear flags, // Must make sure each option has a chance to reset state (clear flags,
// etc) between parses // etc) between parses
for(auto i = m_options.begin (); i != m_options.end (); i++) for (auto i: m_options)
(*i)->reset (); i->reset ();
const unsigned int FIRST_ARGUMENT = 1; const unsigned int FIRST_ARGUMENT = 1;
try { try {
@ -463,12 +468,13 @@ processor::parse_args (int argc, const char ** argv) {
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
for (auto i = m_options.begin (); i != m_options.end (); ++i) for (auto i: m_options)
(*i)->finish (); i->finish ();
} }
void processor::add_option (option * opt) { void
processor::add_option (option *opt) {
if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ()) if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ())
throw logic_error ("Short option already exists"); throw logic_error ("Short option already exists");
if (m_longopt.find (opt->longopt ()) != m_longopt.end ()) if (m_longopt.find (opt->longopt ()) != m_longopt.end ())
@ -477,20 +483,20 @@ void processor::add_option (option * opt) {
m_shortopt[opt->shortopt ()] = opt; m_shortopt[opt->shortopt ()] = opt;
m_longopt [opt->longopt ()] = opt; m_longopt [opt->longopt ()] = opt;
m_options.push_back( opt); m_options.push_back (opt);
} }
option* processor::remove_option (char letter) { option*
processor::remove_option (char letter) {
// Locate the option by short name // Locate the option by short name
std::map<char, option*>::iterator s_candidate = m_shortopt.find (letter); const auto s_candidate = m_shortopt.find (letter);
if (s_candidate == m_shortopt.end ()) if (s_candidate == m_shortopt.end ())
throw logic_error ("Cannot remove an option which is not present"); throw logic_error ("Cannot remove an option which is not present");
option * opt = (*s_candidate).second; option *opt = (*s_candidate).second;
// Locate the long option entry // Locate the long option entry
std::map<string, option*>::iterator l_candidate = m_longopt.find (opt->longopt ()); const auto l_candidate = m_longopt.find (opt->longopt ());
assert (l_candidate != m_longopt.end ()); assert (l_candidate != m_longopt.end ());
// Remove all references and return // Remove all references and return
@ -502,16 +508,16 @@ option* processor::remove_option (char letter) {
} }
option* processor::remove_option (const char * name) { option*
processor::remove_option (const char *name) {
// Locate the option by long name // Locate the option by long name
std::map<string, option*>::iterator l_candidate = m_longopt.find (name); const auto l_candidate = m_longopt.find (name);
if (l_candidate == m_longopt.end ()) if (l_candidate == m_longopt.end ())
throw logic_error ("Cannot remove an option which is not present"); throw logic_error ("Cannot remove an option which is not present");
option * opt = (*l_candidate).second; option * opt = (*l_candidate).second;
// Locate the short option entry // Locate the short option entry
std::map<char, option*>::iterator s_candidate = m_shortopt.find (opt->shortopt ()); const auto s_candidate = m_shortopt.find (opt->shortopt ());
assert (s_candidate != m_shortopt.end ()); assert (s_candidate != m_shortopt.end ());
// Remove all references and return the option object // Remove all references and return the option object
@ -526,12 +532,6 @@ option* processor::remove_option (const char * name) {
/* Parse args from a stream, one arg per line /* Parse args from a stream, one arg per line
*/ */
void parse_stream (std::istream & is) { void parse_stream (std::istream & is) {
char * buffer = new char[MAX_CHUNK_LENGTH + 1]; unique_ptr<char[]> buffer (new char [MAX_CHUNK_LENGTH + 1]);
try {
} catch (...) {
delete [] buffer;
throw;
}
} }

View File

@ -222,13 +222,13 @@ namespace util {
/* Description of types available for parsing /* Description of types available for parsing
*/ */
enum bytestype { enum bytestype {
BYTES_SINGLE, BYTES_SINGLE = 0,
BYTES_KILO, BYTES_KILO = 1,
BYTES_MEGA, BYTES_MEGA = 2,
BYTES_GIGA, BYTES_GIGA = 3,
BYTES_TERA, BYTES_TERA = 4,
BYTES_PETA, BYTES_PETA = 5,
BYTES_EXA, BYTES_EXA = 6,
// Currently does not support yota or zeta as there can be // Currently does not support yota or zeta as there can be
// trouble converting them without loss into 64bit quantities. // trouble converting them without loss into 64bit quantities.

View File

@ -18,7 +18,8 @@ using namespace util;
// Check that null options don't throw anything // Check that null options don't throw anything
void test_null_opt(void) { void
test_null_opt (void) {
unique_ptr<processor> p(new processor()); unique_ptr<processor> p(new processor());
p->add_option(new nulloption('n', "null", "testing null option")); p->add_option(new nulloption('n', "null", "testing null option"));
@ -34,25 +35,26 @@ void test_null_opt(void) {
// Check if presence options can be used successfully // Check if presence options can be used successfully
void test_present_opt(void) { void
test_present_opt (void) {
unique_ptr<processor> p(new processor()); unique_ptr<processor> p(new processor());
bool is_present; bool is_present;
p->add_option(new presentoption('p', "present", "option is present", &is_present)); p->add_option(new presentoption('p', "present", "option is present", &is_present));
// Short option form // Short option form
const char * argv1[] = { "./foo", "-p" }; const char *argv1[] = { "./foo", "-p" };
is_present = false; is_present = false;
p->parse_args(elems(argv1), argv1); p->parse_args(elems(argv1), argv1);
CHECK (is_present); CHECK (is_present);
// Long option form // Long option form
const char * argv2[] = { "./foo", "--present" }; const char *argv2[] = { "./foo", "--present" };
is_present = false; is_present = false;
p->parse_args(elems(argv2), argv2); p->parse_args(elems(argv2), argv2);
CHECK (is_present); CHECK (is_present);
// Check that value is reset from true if not present // Check that value is reset from true if not present
const char * argv3[] = { "./foo" }; const char *argv3[] = { "./foo" };
is_present = true; is_present = true;
p->parse_args(elems(argv3), argv3); p->parse_args(elems(argv3), argv3);
CHECK (!is_present); CHECK (!is_present);
@ -60,50 +62,50 @@ void test_present_opt(void) {
// Check all forms of boolean inputs // Check all forms of boolean inputs
void test_bool_opt(void) { void
test_bool_opt (void) {
unique_ptr<processor> p(new processor()); unique_ptr<processor> p(new processor());
bool value = false; bool value = false;
p->add_option(new valueoption<bool>('b', "bool", p->add_option (new valueoption<bool>('b', "bool", "testing boolean actions", &value));
"testing boolean actions", &value));
// List all legal forms of positive or negative boolean values // List all legal forms of positive or negative boolean values
const char * argv[] = { "./foo", "-b", NULL }; const char *argv[] = { "./foo", "-b", NULL };
const char * positive[] = { "1", "true", "yes" }; const char *positive[] = { "1", "true", "yes" };
const char * negative[] = { "0", "false", "no" }; const char *negative[] = { "0", "false", "no" };
// For each boolean value, ensure that it returns as expected // For each boolean value, ensure that it returns as expected
for(off_t i = 0; i < elems(positive); ++i) { for (off_t i = 0; i < elems (positive); ++i) {
argv[2] = positive[i]; argv[2] = positive[i];
p->parse_args(elems(argv), argv); p->parse_args (elems (argv), argv);
CHECK (value == true); CHECK (value == true);
} }
for(off_t i = 0; i < elems(negative); ++i) { for (off_t i = 0; i < elems (negative); ++i) {
argv[2] = negative[i]; argv[2] = negative[i];
p->parse_args(elems(argv), argv); p->parse_args (elems (argv), argv);
CHECK (value == false); CHECK (value == false);
} }
// Check that invalid forms of boolean all throw exceptions // Check that invalid forms of boolean all throw exceptions
const char * invalid[] = { "foo", "y", "null" }; const char* invalid[] = { "foo", "y", "null" };
for(off_t i = 0; i < elems(invalid); ++i) { for (off_t i = 0; i < elems (invalid); ++i) {
argv[2] = invalid[i]; argv[2] = invalid[i];
CHECK_THROWS ( CHECK_THROWS (
std::domain_error, std::domain_error,
p->parse_args (elems(argv), argv) p->parse_args (elems (argv), argv)
); );
} }
} }
template<typename T> template<typename T>
void test_numeric_opt(void) { void
unique_ptr<processor> p(new processor()); test_numeric_opt (void) {
unique_ptr<processor> p(new processor ());
T value; T value;
p->add_option(new valueoption<T>('t', "type", p->add_option (new valueoption<T> ('t', "type", "testing type option", &value));
"testing type option", &value));
T values[] = { T values[] = {
// TODO: Enable minimum value testing. Currently disabled as // TODO: Enable minimum value testing. Currently disabled as
@ -111,46 +113,47 @@ void test_numeric_opt(void) {
// option. // option.
//numeric_limits<T>::min(), //numeric_limits<T>::min(),
numeric_limits<T>::max(), numeric_limits<T>::max (),
0 0
}; };
const char * argv_short[] = { "./foo", "-t", NULL }; const char * argv_short[] = { "./foo", "-t", NULL };
const char * argv_long[] = { "./foo", NULL }; const char * argv_long[] = { "./foo", NULL };
for(off_t i = 0; i < elems(values); ++i) { for(off_t i = 0; i < elems (values); ++i) {
ostringstream out_short, out_long; ostringstream out_short, out_long;
string str_short, str_long; string str_short, str_long;
out_short << values[i]; out_short << values[i];
str_short = out_short.str(); str_short = out_short.str ();
argv_short[2] = str_short.c_str(); argv_short[2] = str_short.c_str ();
value = 2; value = 2;
p->parse_args(elems(argv_short), argv_short); p->parse_args (elems (argv_short), argv_short);
CHECK (value == values[i]); CHECK (value == values[i]);
out_long << "--type=" << values[i]; out_long << "--type=" << values[i];
str_long = out_long.str(); str_long = out_long.str ();
argv_long[1] = str_long.c_str(); argv_long[1] = str_long.c_str ();
value = 2; value = 2;
p->parse_args(elems(argv_long), argv_long); p->parse_args (elems (argv_long), argv_long);
CHECK (value == values[i]); CHECK (value == values[i]);
} }
} }
void test_bytes_opt(void) { void
unique_ptr<processor> p(new processor()); test_bytes_opt(void) {
unique_ptr<processor> p(new processor ());
struct { struct {
const char * param; const char *param;
bytesoption::bytestype type; bytesoption::bytestype type;
bytesoption::bytesmodifier mod; bytesoption::bytesmodifier mod;
size_t size; size_t size;
} commands[] = { } commands[] = {
{ "1", bytesoption::BYTES_MEGA, { "1", bytesoption::BYTES_MEGA,
bytesoption::BYTES_BASE2, bytesoption::BYTES_BASE2,
1UL * 1024 * 1024 }, 1UL * 1024 * 1024 },
@ -180,58 +183,60 @@ void test_bytes_opt(void) {
}; };
const char * argv[] = { const char *argv[] = {
"./foo", "./foo",
"-b", "-b",
NULL NULL
}; };
for(unsigned int i = 0; i < elems(commands); ++i) { for (unsigned int i = 0; i < elems (commands); ++i) {
size_t size = 0; size_t size = 0;
option * opt = new bytesoption('b', "bytes", option * opt = new bytesoption ('b', "bytes",
"testing sizeof bytes", &size, commands[i].type, "testing sizeof bytes", &size, commands[i].type,
commands[i].mod); commands[i].mod);
p->add_option(opt); p->add_option(opt);
argv[elems(argv) - 1] = commands[i].param; argv[elems (argv) - 1] = commands[i].param;
p->parse_args(elems(argv), argv); p->parse_args (elems (argv), argv);
CHECK_EQ (commands[i].size, size); CHECK_EQ (commands[i].size, size);
delete p->remove_option(opt); delete p->remove_option (opt);
} }
} }
void test_insert_remove_opt(void) { void
unique_ptr<processor> p(new processor()); test_insert_remove_opt (void) {
nulloption opt('n', "null-option", "null testing action"); unique_ptr<processor> p(new processor ());
nulloption opt ('n', "null-option", "null testing action");
p->add_option(&opt); p->add_option (&opt);
CHECK_EQ (p->remove_option('n'), (option*)&opt); CHECK_EQ (p->remove_option ('n'), (option*)&opt);
p->add_option(&opt); p->add_option (&opt);
CHECK_EQ (p->remove_option("null-option"), (option*)&opt); CHECK_EQ (p->remove_option ("null-option"), (option*)&opt);
p->add_option(&opt); p->add_option (&opt);
CHECK_THROWS (std::logic_error, p->add_option(&opt)); CHECK_THROWS (std::logic_error, p->add_option (&opt));
p->remove_option(&opt); p->remove_option (&opt);
} }
void test_required (void) { void
unique_ptr<processor> p(new processor()); test_required (void) {
unique_ptr<processor> p (new processor ());
p->add_option (new nulloption ('n', p->add_option (new nulloption ('n',
"null", "null",
"null testing", "null testing",
true)); true));
static const char *argv[] = { static const char *argv[] = {
"./cpptest", "./cpptest",
"-n", "-n",
"value" "value"
}; };
CHECK_NOTHROW (p->parse_args (elems(argv), argv)); CHECK_NOTHROW (p->parse_args (elems (argv), argv));
CHECK_THROWS (std::runtime_error, p->parse_args (1, argv)); CHECK_THROWS (std::runtime_error, p->parse_args (1, argv));
} }