You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

168 lines
3.6 KiB

// License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html
#include <cstring>
#include <iostream>
#include "auxiliary.h"
#include "general_functions.h"
using namespace std;
processArguments::processArguments(
int theArgc, const char* const theArgv[]):
argc(theArgc), argv(theArgv)
{
checkIfArgcWithinRange();
checkIfArg2Is0Or1OnlyAndSetMintOrMelt();
checkIfCorrectNumberOfArgumentsForMintOrMelt();
checkIf3rdOnwardArgsAreNotPositveInt();
setRemainingDataMemberValues();
printProcessedArguments();
}
void processArguments::checkIfArgcWithinRange()
{
if (argc < 5 or argc > 6)
{
cerr
<< "For Mint, no. of arguments must be 6 and for Melt no."
<< " of arguments must be 5 but recived " << argc
<< " argument(s).\n";
exit(1);
}
}
void processArguments::checkIfArg2Is0Or1OnlyAndSetMintOrMelt()
{
string secondArgumentIncorrect =
string("Second argument can be only 0 for Mint and 1 for Melt")
+ ". But it is '" + argv[1] + "'.\n";
if (is_whole_number(argv[1]))
{
mintOrMelt = stoi(argv[1]);
if (mintOrMelt > 1 or mintOrMelt < 0)
{
cerr << secondArgumentIncorrect;
exit(1);
}
}
else
{
cerr << secondArgumentIncorrect;
exit(1);
}
}
void processArguments::checkIfCorrectNumberOfArgumentsForMintOrMelt()
{
if (mintOrMelt == mint and argc != 6)
{
cerr
<< "For Mint, no. of arguments can be excatly 6 only. But "
<< "recived " << argc << ".\n";
exit(1);
}
else if (mintOrMelt == melt and argc != 5)
{
cerr
<< "For Melt, no. of arguments can be excatly 5 only. But "
<< "recived " << argc << ".\n";
exit(1);
}
}
void processArguments::checkIf3rdOnwardArgsAreNotPositveInt()
{
checkIfArgumentIsNotPositiveInt(argv[2], "3rd");
checkIfArgumentIsNotPositiveInt(argv[3], "4th");
checkIfArgumentIsNotPositiveInt(argv[4], "5th");
if (mintOrMelt == mint)
{
checkIfArgumentIsNotPositiveInt(argv[5], "6th");
}
}
void processArguments::checkIfArgumentIsNotPositiveInt (
const char* argument, string itsIndex)
{
if (! is_natural_number(argument))
{
cerr
<< "All arguments from 3rd onward must be positive "
<< "integer but " << itsIndex << " i.e. '" << argument
<< "' is not.\n";
exit(1);
}
}
void processArguments::setRemainingDataMemberValues()
{
seed = stoi(argv[2]);
length= stoi(argv[3]);
size= stoi(argv[4]);
if (mintOrMelt == mint)
{
mod= stoi(argv[5]);
}
}
string processArguments::codewordType() const
{
if (mintOrMelt == mint)
{
return "mint";
}
else if (mintOrMelt == melt)
{
return "melt";
}
else
{
cerr_and_exit(
2,
"Unexpected Internal",
__FILE__,
__func__,
__LINE__);
}
}
short processArguments::codewordInt() const
{
return mintOrMelt;
}
int processArguments::randomGeneratorSeed() const
{
return seed;
}
int processArguments::codewordLength() const
{
return length;
}
int processArguments::codebookSize() const
{
return size;
}
int processArguments::modulus() const
{
return mod;
}
void processArguments::printProcessedArguments() const
{
cout
<< "Mode: " << codewordType() << " "
<< "Seed: " << seed << " "
<< "Codeword Length: " << length << " "
<< "Codebook size: " << size;
if (mintOrMelt == mint)
{
cout << " Modulus: " << mod;
}
cout << endl;
}