arguements.h
/** Define your CLI arguments **/
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>
#define ARGS_CAPACITY 8
enum ARG_TYPES {
bool_ = 0,
int_ = 1,
float_ = 2,
double_ = 3,
string_ = 4
};
typedef struct VALUE {
int type;
bool bool_;
int int_;
float float_;
double double_;
std::string string_;
} value;
class Arguments{
public:
Arguments(int argc, char ** argv);
bool useHarrisDetector;
int maxCorners, maxTrackbar, minDistance, blockSize;
double qualityLevel,k;
std::string file, subimage, text;
std::map<std::string,VALUE> arguments_container;
void getArguments();
static std::string predefinedArgs[ARGS_CAPACITY];
static int predefinedArgTypes[ARGS_CAPACITY];
private:
int argc; char ** argv;
std::vector<std::string> args;
void Arguments::parseArguments();
};
cpp
#include "arguments.hpp"
/*
What is expected from arguments:
0 - bool
1 - int
2 - float
3 - double
4 (anything else) - string
*/
std::string Arguments::predefinedArgs[ARGS_CAPACITY] =
{ "file", "maxCorners", "maxTrackbar", "qualityLevel", "minDistance", "blockSize", "useHarrisDetector", "k" };
int Arguments::predefinedArgTypes[ARGS_CAPACITY] =
{ 4, 1, 1, 3, 1, 1, 0, 3};
// Set default values here:
Arguments::Arguments(int argc, char ** argv):
// Initiate default arguments
file("../../data/lena.jpg"),
maxCorners(100),
maxTrackbar(100),
qualityLevel(0.01),
minDistance(10),
blockSize(3),
useHarrisDetector(false),
k(0.04)
{
std::cout << Arguments::predefinedArgs << std::endl;
this->parseArguments();
this->getArguments();
};
void Arguments::parseArguments()
{
int i = -1;
std::string s;
bool match_procceed;
for (std::vector<std::string>::iterator it = this->args.begin(); it != this->args.end(); it++)
{
match_procceed = false;
i++;
if (i == 0)
continue;
std::cout << "args[" << i << "] = " << this->args[i] << std::endl;
// int o = (int) this->args[i].std::string::compare(0,14,"randomPosition");
int r = (int) this->args[i].std::string::compare(0,11,"randomPosition");
size_t len;
// const std::string[12] pArgs = predefinedArgs;
const std::string* PA_ptr = predefinedArgs;
const std::string* pPA = predefinedArgs;
// Search needle:
for (int c=0; c<ARGS_CAPACITY;c++)
{
pPA = (PA_ptr+c);
len = pPA->length(); // length of "needle"
//int lenWord = this->args[i].length();
int o = this->args[i].std::string::compare(
0,len,*pPA);
if ( this->args[i].std::string::compare(
0,len,*pPA) == 0)
{
s = this->args[i].substr (len+1,this->args[i].length());
VALUE value;
switch(this->predefinedArgTypes[c]){
case bool_:
value.bool_ = (bool) std::stoi(s);
value.type=bool_;
break;
case int_:
value.int_ = (int) std::stoi(s);
value.type=int_;
break;
case float_:
value.float_ = (float) std::stoi(s);
value.type=float_;
break;
case double_:
value.double_ = (double) std::stoi(s);
value.type=double_;
break;
default:
value.string_ = s;
value.type=string_;
} // end of switch
this->arguments_container.insert(
std::make_pair(
this->args[i].substr (0,len), value
));
match_procceed = true;
break;
}
} // end of inner for - searching needle
if ( match_procceed )
continue;
} // outer loop - goung through input argv data
};
void Arguments::getArguments(){
// Load defaults
int i =-1;
for (std::map<std::string,VALUE>::iterator it = this->arguments_container.begin(); it != this->arguments_container.end(); it++)
{
int len = (it->first).length();
switch(it->second.type){
case bool_:
if ( it->first.std::string::compare(
0,len,"useHarrisDetector") == 0)
{ this->useHarrisDetector = it->second.bool_;break;}
break;
case int_:
if ( it->first.std::string::compare(
0,len,"maxCorners") == 0)
{ this->maxCorners = it->second.int_;break;}
else if ( it->first.std::string::compare(
0,len,"maxTrackbar") == 0)
{ this->maxTrackbar = it->second.int_;break;}
else if ( it->first.std::string::compare(
0,len,"minDistance") == 0)
{ this->minDistance = it->second.int_;break;}
else if ( it->first.std::string::compare(
0,len,"qualityLevel") == 0)
{ this->qualityLevel = it->second.int_;break;}
else if ( it->first.std::string::compare(
0,len,"blockSize") == 0)
{ this->blockSize = it->second.int_;break;}
break;
case double_:
if ( it->first.std::string::compare(
0,len,"qualityLevel") == 0)
{ this->qualityLevel = it->second.double_;break;}
else if ( it->first.std::string::compare(
0,len,"k") == 0)
{ this->k = it->second.double_;break;}
break;
case string_:
if ( it->first.std::string::compare(
0,len,"file") == 0)
{ this->file = it->second.string_;break;}
break;
}
} // for
}
wrapper.hj
// These 7 are used to get argv to vector
#ifndef WRAPPER_H
#define WRAPPER_H
#include "arguments.hpp"
class Wrapper {
public:
Wrapper(int , char ** );
Arguments arguments;
};
#endif
cpp
#include "wrapper.hpp"
Wrapper::Wrapper(int argc, char ** argv):
/*
argc(argc),
argv(argv),
args(argv, argv+argc)*/
arguments(argc, argv)
{
};