File: configuration.cpp

package info (click to toggle)
fsm-lite 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 192 kB
  • sloc: cpp: 563; makefile: 37
file content (136 lines) | stat: -rw-r--r-- 4,894 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "configuration.h"
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib> // std::exit()
#include <cassert>
#include <getopt.h>
using namespace std;

configuration::configuration(int argc, char **argv)
    : prog(argv[0]), listfile(""), tmpfile(""), inputformat(input_reader::input_fasta),
      minlength(9), maxlength(100), minfreq(1), minsupport(2), maxsupport(~0u),
      verbose(false), debug(false), good(false)
{
    assert (argc > 0);
    good = parse(argc, argv);
}

int atoi_min(char const *value, int min, char const *parameter, char const *name)
{
    istringstream iss(value);
    int i;
    char c;
    if (!(iss >> i) || iss.get(c))
    {
        cerr << "error: argument of " << parameter << " must be of type <int>, and greater than or equal to " << min << endl
             << "Check " << name << " --help' for more information." << endl;
        exit(1);
    }

    if (i < min)
    {
        cerr << "error: argument of " << parameter << " must be greater than or equal to " << min << endl
             << "Check `" << name << " --help' for more information." << endl;
        exit(1);
    }
    return i;
}

void configuration::print_short_usage() const
{
    cerr << "See " << prog << " --help for more information." << endl;
    exit(1);
}

void configuration::print_usage() const
{
    cerr << "usage: " << prog << " -l <file> -t <file> [options]" << endl
         << "  -l,--list <file>     Text file that lists all input files as whitespace-separated pairs "<< endl
         << "                            <data-name> <data-filename>" << endl
         << "                       where <data-name> is unique identifier (without whitespace)" << endl
         << "                       and <data-filename> is full path to each input file." << endl
         << "                       Default data file format is FASTA (uncompressed)." << endl
         << "  -t,--tmp <file>      Store temporary index data" << endl
         << "options:" << endl
         << "  -m,--min <int>       Minimum length to report (default 9)" << endl
         << "  -M,--max <int>       Maximum length to report (default 100)" << endl
         << "  -f,--freq <int>      Minimum frequency per input file to report (default 1)" << endl
         << "  -s,--minsupp <int>   Minimum number of input files with support to report (default 2)" << endl
         << "  -S,--maxsupp <int>   Maximum number of input files with support to report (default inf)" << endl
         << "  -v,--verbose         Verbose output" << endl;
    exit(1);
}

bool configuration::parse(int argc, char **argv)
{    
    static struct option long_options[] =
        {
            {"list",      required_argument, 0, 'l'},
            {"tmp",       required_argument, 0, 't'},
            {"min",       required_argument, 0, 'm'},
            {"max",       required_argument, 0, 'M'},
            {"freq",      required_argument, 0, 'f'},
            {"minsupp",   required_argument, 0, 's'},
            {"maxsupp",   required_argument, 0, 'S'},
            {"verbose",   no_argument,       0, 'v'},
            {"debug",     no_argument,       0, 'D'},
            {"help",      no_argument,       0, 'h'},
            {0, 0, 0, 0}
        };
    int option_index = 0;
    int c;
    while ((c = getopt_long(argc, argv, "l:t:m:M:f:s:S:vDh",
                            long_options, &option_index)) != -1) 
    {
        switch(c) 
        {
        case 'l':
            listfile = string(optarg); break;
        case 't':
            tmpfile = string(optarg); break;
        case 'm':
            minlength = atoi_min(optarg, 1, "-m,--min", argv[0]); break;
        case 'M':
            maxlength = atoi_min(optarg, 1, "-M,--max", argv[0]); break;
        case 'f':
            minfreq = atoi_min(optarg, 1, "-f,--freq", argv[0]); break;
        case 's':
            minsupport = atoi_min(optarg, 1, "-s,--minsupp", argv[0]); break;
        case 'S':
            maxsupport = atoi_min(optarg, 1, "-S,--maxsupp", argv[0]); break;
        case 'v':
            verbose = true; break;
        case 'D':
            debug = true; break;
        case 'h':
            print_usage(); return 1;
        default:
            cerr << "error: invalid parameter!" << endl;
            return false;
        }
    }
    if (listfile == "")
    {
        cerr << "error: -l,--list is a required parameter" << endl;
        return false;
    }
    if (tmpfile == "")
    {
        cerr << "error: -t,--tmp is a required parameter" << endl;
        return false;
    }

    if (minlength > maxlength)
    {
        cerr << "error: -m,--min must be smaller than or equal to -M,--max" << endl;
        return false;
    }
    if (minsupport > maxsupport)
    {
        cerr << "error: -s,--minsupp must be smaller than or equal to -S,--maxsupp" << endl;
        return false;
    }

    return true;
}