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 <iostream>
#include <stdexcept>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stdint.h>
#include <stdint.h>
#include <string.h>
#include <cstring>
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace std;
@ -57,13 +55,16 @@ option::option (char _letter,
void option::execute (void) {
void
option::execute (void) {
throw runtime_error(
"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);
throw runtime_error(
"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 ()
<< (opt.is_required () ? " \t" : "]\t") << opt.longopt ()
<< "\t" << opt.description ();
@ -177,7 +179,6 @@ bytesoption::execute (const std::string& data) {
size_t defaultvalue = *m_data;
try {
bytesmodifier modifier = m_modifier;
off_t cursor = data.size () - 1;
@ -231,7 +232,7 @@ bytesoption::execute (const std::string& data) {
--cursor;
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);
*m_data *= multiplier;
} catch (...) {
@ -245,6 +246,7 @@ bytesoption::execute (const std::string& data) {
m_found = true;
}
/*
* Internal helper options. Print help and usage.
* A callback to the processor which triggers output of the help message.
@ -264,22 +266,23 @@ class helpoption : public option {
helpoption (processor * _processor):
option (HELP_CHARACTER, HELP_NAME, HELP_DESCRIPTION, false),
m_processor (_processor)
{ ; }
{ ; }
virtual void execute (void);
virtual void execute (const std::string& data)
{ option::execute (data); }
{ option::execute (data); }
};
const char helpoption::HELP_CHARACTER = 'h';
const char *helpoption::HELP_NAME = "help";
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 ();
exit (EXIT_SUCCESS);
}
@ -294,17 +297,17 @@ processor::processor ()
processor::~processor () {
for(auto i = m_options.begin(); i != m_options.end(); i++)
delete *i;
for (auto i: m_options)
delete i;
}
void processor::print_usage (void) {
void
processor::print_usage (void) {
cout << "Usage: " << m_command << " [options]" << endl;
for(list<option *>::const_iterator i = m_options.begin ();
i != m_options.end (); i++)
cout << '\t' << **i << endl;
for (const auto i: m_options)
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.
*/
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 < argc);
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
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.
*/
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 < argc);
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)
assert (arg[0] == '-');
assert (arg[1] == '-');
assert (strlen(arg) >= 4);
assert (strlen (arg) >= 4);
// Skip past the dashes to the argument name
arg += 2;
// If there's an equals it's has a value to extract
const char * data = strchr (arg, '=');
if( data) {
if (data) {
arg = strndup (arg, data - arg); // Copy just the arg name
data++; // Skip the '='
}
option * o = m_longopt[arg];
option *o = m_longopt[arg];
if (!o)
throw runtime_error ("Cannot match option");
if(data)
if (data)
o->execute (data);
else
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,
// etc) between parses
for(auto i = m_options.begin (); i != m_options.end (); i++)
(*i)->reset ();
for (auto i: m_options)
i->reset ();
const unsigned int FIRST_ARGUMENT = 1;
try {
@ -463,12 +468,13 @@ processor::parse_args (int argc, const char ** argv) {
exit (EXIT_FAILURE);
}
for (auto i = m_options.begin (); i != m_options.end (); ++i)
(*i)->finish ();
for (auto i: m_options)
i->finish ();
}
void processor::add_option (option * opt) {
void
processor::add_option (option *opt) {
if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ())
throw logic_error ("Short option already exists");
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_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
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 ())
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
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 ());
// 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
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 ())
throw logic_error ("Cannot remove an option which is not present");
option * opt = (*l_candidate).second;
// 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 ());
// 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
*/
void parse_stream (std::istream & is) {
char * buffer = new char[MAX_CHUNK_LENGTH + 1];
try {
} catch (...) {
delete [] buffer;
throw;
}
unique_ptr<char[]> buffer (new char [MAX_CHUNK_LENGTH + 1]);
}

View File

@ -222,13 +222,13 @@ namespace util {
/* Description of types available for parsing
*/
enum bytestype {
BYTES_SINGLE,
BYTES_KILO,
BYTES_MEGA,
BYTES_GIGA,
BYTES_TERA,
BYTES_PETA,
BYTES_EXA,
BYTES_SINGLE = 0,
BYTES_KILO = 1,
BYTES_MEGA = 2,
BYTES_GIGA = 3,
BYTES_TERA = 4,
BYTES_PETA = 5,
BYTES_EXA = 6,
// Currently does not support yota or zeta as there can be
// 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
void test_null_opt(void) {
void
test_null_opt (void) {
unique_ptr<processor> p(new processor());
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
void test_present_opt(void) {
void
test_present_opt (void) {
unique_ptr<processor> p(new processor());
bool is_present;
p->add_option(new presentoption('p', "present", "option is present", &is_present));
// Short option form
const char * argv1[] = { "./foo", "-p" };
const char *argv1[] = { "./foo", "-p" };
is_present = false;
p->parse_args(elems(argv1), argv1);
CHECK (is_present);
// Long option form
const char * argv2[] = { "./foo", "--present" };
const char *argv2[] = { "./foo", "--present" };
is_present = false;
p->parse_args(elems(argv2), argv2);
CHECK (is_present);
// Check that value is reset from true if not present
const char * argv3[] = { "./foo" };
const char *argv3[] = { "./foo" };
is_present = true;
p->parse_args(elems(argv3), argv3);
CHECK (!is_present);
@ -60,50 +62,50 @@ void test_present_opt(void) {
// Check all forms of boolean inputs
void test_bool_opt(void) {
void
test_bool_opt (void) {
unique_ptr<processor> p(new processor());
bool value = false;
p->add_option(new valueoption<bool>('b', "bool",
"testing boolean actions", &value));
p->add_option (new valueoption<bool>('b', "bool", "testing boolean actions", &value));
// List all legal forms of positive or negative boolean values
const char * argv[] = { "./foo", "-b", NULL };
const char * positive[] = { "1", "true", "yes" };
const char * negative[] = { "0", "false", "no" };
const char *argv[] = { "./foo", "-b", NULL };
const char *positive[] = { "1", "true", "yes" };
const char *negative[] = { "0", "false", "no" };
// 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];
p->parse_args(elems(argv), argv);
p->parse_args (elems (argv), argv);
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];
p->parse_args(elems(argv), argv);
p->parse_args (elems (argv), argv);
CHECK (value == false);
}
// 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];
CHECK_THROWS (
std::domain_error,
p->parse_args (elems(argv), argv)
p->parse_args (elems (argv), argv)
);
}
}
template<typename T>
void test_numeric_opt(void) {
unique_ptr<processor> p(new processor());
void
test_numeric_opt (void) {
unique_ptr<processor> p(new processor ());
T value;
p->add_option(new valueoption<T>('t', "type",
"testing type option", &value));
p->add_option (new valueoption<T> ('t', "type", "testing type option", &value));
T values[] = {
// TODO: Enable minimum value testing. Currently disabled as
@ -111,46 +113,47 @@ void test_numeric_opt(void) {
// option.
//numeric_limits<T>::min(),
numeric_limits<T>::max(),
numeric_limits<T>::max (),
0
};
const char * argv_short[] = { "./foo", "-t", 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;
string str_short, str_long;
out_short << values[i];
str_short = out_short.str();
argv_short[2] = str_short.c_str();
str_short = out_short.str ();
argv_short[2] = str_short.c_str ();
value = 2;
p->parse_args(elems(argv_short), argv_short);
p->parse_args (elems (argv_short), argv_short);
CHECK (value == values[i]);
out_long << "--type=" << values[i];
str_long = out_long.str();
argv_long[1] = str_long.c_str();
str_long = out_long.str ();
argv_long[1] = str_long.c_str ();
value = 2;
p->parse_args(elems(argv_long), argv_long);
p->parse_args (elems (argv_long), argv_long);
CHECK (value == values[i]);
}
}
void test_bytes_opt(void) {
unique_ptr<processor> p(new processor());
void
test_bytes_opt(void) {
unique_ptr<processor> p(new processor ());
struct {
const char * param;
const char *param;
bytesoption::bytestype type;
bytesoption::bytesmodifier mod;
size_t size;
size_t size;
} commands[] = {
{ "1", bytesoption::BYTES_MEGA,
{ "1", bytesoption::BYTES_MEGA,
bytesoption::BYTES_BASE2,
1UL * 1024 * 1024 },
@ -180,58 +183,60 @@ void test_bytes_opt(void) {
};
const char * argv[] = {
const char *argv[] = {
"./foo",
"-b",
NULL
};
for(unsigned int i = 0; i < elems(commands); ++i) {
for (unsigned int i = 0; i < elems (commands); ++i) {
size_t size = 0;
option * opt = new bytesoption('b', "bytes",
"testing sizeof bytes", &size, commands[i].type,
option * opt = new bytesoption ('b', "bytes",
"testing sizeof bytes", &size, commands[i].type,
commands[i].mod);
p->add_option(opt);
argv[elems(argv) - 1] = commands[i].param;
p->parse_args(elems(argv), argv);
argv[elems (argv) - 1] = commands[i].param;
p->parse_args (elems (argv), argv);
CHECK_EQ (commands[i].size, size);
delete p->remove_option(opt);
delete p->remove_option (opt);
}
}
void test_insert_remove_opt(void) {
unique_ptr<processor> p(new processor());
nulloption opt('n', "null-option", "null testing action");
void
test_insert_remove_opt (void) {
unique_ptr<processor> p(new processor ());
nulloption opt ('n', "null-option", "null testing action");
p->add_option(&opt);
CHECK_EQ (p->remove_option('n'), (option*)&opt);
p->add_option (&opt);
CHECK_EQ (p->remove_option ('n'), (option*)&opt);
p->add_option(&opt);
CHECK_EQ (p->remove_option("null-option"), (option*)&opt);
p->add_option (&opt);
CHECK_EQ (p->remove_option ("null-option"), (option*)&opt);
p->add_option(&opt);
CHECK_THROWS (std::logic_error, p->add_option(&opt));
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) {
unique_ptr<processor> p(new processor());
void
test_required (void) {
unique_ptr<processor> p (new processor ());
p->add_option (new nulloption ('n',
"null",
"null testing",
true));
"null",
"null testing",
true));
static const char *argv[] = {
"./cpptest",
"-n",
"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));
}