Commit ebba11c9 authored by berenger-bramas's avatar berenger-bramas
Browse files

Add functions to manage parameters from users input.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/scalfmm/scalfmm/trunk@104 2616d619-271b-44dc-8df4-d4a8f33a7222
parent e7ffa655
#ifndef FCONVERT_HPP
#define FCONVERT_HPP
#include <sstream>
#include <iostream>
#include <string.h>
#include "../Containers/FTreeCoordinate.hpp"
#include "../Utils/F3DPosition.hpp"
......@@ -36,21 +33,6 @@ public :
return outPosition;
}
/**
* This function gives a parameter in a standart type
* @parameter inArg parameter position has to be strictly less than argc/userParemetersCount
* @return argv[inArg] in the template VariableType form
* @warning VariableType need to work with istream >> operator
* <code> const int argInt = userParemetersAt<int>(1,-1); </code>
*/
template <class VariableType>
static const VariableType charToOther(const char* const str, const VariableType& defaultValue = VariableType()){
std::istringstream iss(str,std::istringstream::in);
VariableType value;
iss >> value;
if( /*iss.tellg()*/ iss.eof() ) return value;
return defaultValue;
}
};
#endif
......
#ifndef FPARAMETERS_H
#define FPARAMETERS_H
#include <sstream>
#include <iostream>
#include <string.h>
namespace FParameters{
/**
* This function gives a parameter in a standart type
* @parameter inArg parameter position has to be strictly less than argc/userParemetersCount
* @return argv[inArg] in the template VariableType form
* @warning VariableType need to work with istream >> operator
* <code> const int argInt = userParemetersAt<int>(1,-1); </code>
*/
template <class VariableType>
static const VariableType StrToOther(const char* const str, const VariableType& defaultValue = VariableType()){
std::istringstream iss(str,std::istringstream::in);
VariableType value;
iss >> value;
if( /*iss.tellg()*/ iss.eof() ) return value;
return defaultValue;
}
/** To put a char into lower format
*
*/
char toLower(const char c){
return ('A' <= c && c <= 'Z' ? (c - 'A') + 'a' : c);
}
/** To know if two char are equals
*
*/
bool areCharsEquals(const char c1, const char c2, const bool caseSensible = false){
return (caseSensible && c1 == c2) || (!caseSensible && toLower(c1) == toLower(c2));
}
/** To know if two str are equals
*
*/
bool areStrEquals(const char* const inStr1, const char* const inStr2, const bool caseSensible = false){
int idxStr = 0;
while(inStr1[idxStr] != '\0' && inStr2[idxStr] != '\0'){
if(!areCharsEquals(inStr1[idxStr] ,inStr2[idxStr],caseSensible)){
return false;
}
++idxStr;
}
return inStr1[idxStr] == inStr2[idxStr];
}
/** To find a parameters from user format char parameters
*
*/
int findParameter(const char* const inName, const int argc, const char* const * const argv, const bool caseSensible = false){
for(int idxArg = 0; idxArg < argc ; ++idxArg){
if(areStrEquals(inName, argv[idxArg], caseSensible)){
return idxArg;
}
}
return -1;
}
}
#endif // FPARAMETERS_H
#include "FUTester.hpp"
#include "../Src/Utils/FParameters.hpp"
// compile by g++ utestMorton.cpp -o utestMorton.exe
/**
* This file is a unit test for the FTreeCoordinate class
*/
/** this class test the list container */
class TestParameters : public FUTester<TestParameters> {
void Lower(){
assert(FParameters::toLower('A') == 'a');
assert(FParameters::toLower('Z') == 'z');
assert(FParameters::toLower('a') == 'a');
assert(FParameters::toLower('z') == 'z');
assert(FParameters::toLower('m') == 'm');
assert(FParameters::toLower(';') == ';');
}
void CharsEquals(){
assert(FParameters::areCharsEquals('a','A',false));
assert(FParameters::areCharsEquals('A','A',false));
assert(FParameters::areCharsEquals('A','a',false));
assert(FParameters::areCharsEquals('a','a',false));
assert(!FParameters::areCharsEquals('a','l',false));
assert(!FParameters::areCharsEquals('a','M',false));
assert(!FParameters::areCharsEquals('1',';',false));
assert(!FParameters::areCharsEquals('a','A',true));
assert(FParameters::areCharsEquals('A','A',true));
assert(!FParameters::areCharsEquals('A','a',true));
assert(FParameters::areCharsEquals('a','a',true));
assert(FParameters::areCharsEquals(';',';',true));
}
void StrsEquals(){
assert(FParameters::areStrEquals("","",false));
assert(FParameters::areStrEquals("aa","Aa",false));
assert(!FParameters::areStrEquals("az","azz",false));
assert(FParameters::areStrEquals("aza","azA",false));
assert(FParameters::areStrEquals("a","a",true));
assert(FParameters::areStrEquals("az","az",true));
assert(!FParameters::areStrEquals("aza","azA",true));
assert(!FParameters::areStrEquals("az","azA",true));
}
void FindParameter(){
const int argc = 4;
const char* argv[4] = {
"toto",
"titi",
"TaTa",
"tu.tU"
};
assert(FParameters::findParameter("tOto",argc,argv,false) == 0);
assert(FParameters::findParameter("titi",argc,argv,false) == 1);
assert(FParameters::findParameter("tAtA",argc,argv,false) == 2);
assert(FParameters::findParameter("tu.tu",argc,argv,false) == 3);
for(int idx = 0 ; idx < argc ; ++idx){
assert(FParameters::findParameter(argv[idx],argc,argv,true) == idx);
}
assert(FParameters::findParameter("tu.tu",argc,argv,true) == -1);
assert(FParameters::findParameter("TOTO",argc,argv,true) == -1);
}
void StrToOther(){
assert(FParameters::StrToOther<int>("0") == 0);
assert(FParameters::StrToOther<int>("a",1) == 1);
}
// set test
void SetTests(){
AddTest(&TestParameters::Lower,"Test Lower");
AddTest(&TestParameters::CharsEquals,"Test Char equality");
AddTest(&TestParameters::StrsEquals,"Test Str equality");
AddTest(&TestParameters::FindParameter,"Test Str equality");
AddTest(&TestParameters::StrToOther,"Test Convert");
}
};
// You must do this
TestClass(TestParameters)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment