Whitespace and minor c++11 fixups
This commit is contained in:
parent
0b30b56c58
commit
d482f1fde4
84
options.cpp
84
options.cpp
@ -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.
|
||||
@ -279,7 +281,8 @@ const char *helpoption::HELP_DESCRIPTION =
|
||||
"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,7 +324,8 @@ 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);
|
||||
|
||||
@ -375,7 +379,8 @@ 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);
|
||||
|
||||
@ -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 ())
|
||||
@ -481,16 +487,16 @@ void processor::add_option (option * 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;
|
||||
|
||||
// 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]);
|
||||
}
|
||||
|
||||
|
14
options.hpp
14
options.hpp
@ -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.
|
||||
|
@ -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,7 +35,8 @@ 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));
|
||||
@ -60,12 +62,12 @@ 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 };
|
||||
@ -99,11 +101,11 @@ void test_bool_opt(void) {
|
||||
|
||||
|
||||
template<typename T>
|
||||
void test_numeric_opt(void) {
|
||||
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
|
||||
@ -141,7 +143,8 @@ void test_numeric_opt(void) {
|
||||
}
|
||||
|
||||
|
||||
void test_bytes_opt(void) {
|
||||
void
|
||||
test_bytes_opt(void) {
|
||||
unique_ptr<processor> p(new processor ());
|
||||
|
||||
struct {
|
||||
@ -202,7 +205,8 @@ void test_bytes_opt(void) {
|
||||
}
|
||||
|
||||
|
||||
void test_insert_remove_opt(void) {
|
||||
void
|
||||
test_insert_remove_opt (void) {
|
||||
unique_ptr<processor> p(new processor ());
|
||||
nulloption opt ('n', "null-option", "null testing action");
|
||||
|
||||
@ -219,7 +223,8 @@ void test_insert_remove_opt(void) {
|
||||
}
|
||||
|
||||
|
||||
void test_required (void) {
|
||||
void
|
||||
test_required (void) {
|
||||
unique_ptr<processor> p (new processor ());
|
||||
p->add_option (new nulloption ('n',
|
||||
"null",
|
||||
|
Loading…
Reference in New Issue
Block a user