FParameters.hpp 2.19 KB
Newer Older
1
2
3
4
5
6
7
#ifndef FPARAMETERS_H
#define FPARAMETERS_H

#include <sstream>
#include <iostream>
#include <string.h>

8
9
10
11
/** This file proposes some methods
  * to work with user input parameters.
  */

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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