summaryrefslogtreecommitdiff
path: root/misc/config.cpp
blob: 1fa0ccbd9cab03447c14211ae881c5f2d0a38327 (plain)
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
/* 
 * File:   config.cpp
 * Author: savop
 * 
 * Created on 15. August 2009, 13:03
 */

#include <stdio.h>
#include <vdr/tools.h>
#include <getopt.h>
#include "config.h"
#include "../common.h"

cUPnPConfig::cUPnPConfig(){
    this->mParsedArgs = NULL;
    this->mInterface = NULL;
    this->mAddress = NULL;
    this->mAutoSetup = 0;
    this->mEnable = 0;
    this->mPort = 0;
}

cUPnPConfig::~cUPnPConfig(){}

cUPnPConfig* cUPnPConfig::get(){
    if(cUPnPConfig::mInstance == NULL)
        cUPnPConfig::mInstance = new cUPnPConfig();

    if(cUPnPConfig::mInstance)
        return cUPnPConfig::mInstance;
    else return NULL;
}

cUPnPConfig* cUPnPConfig::mInstance = NULL;
int cUPnPConfig::verbosity = 0;

bool cUPnPConfig::processArgs(int argc, char* argv[]){
  // Implement command line argument processing here if applicable.
    static struct option long_options[] = {
        {"int",     required_argument, NULL, 'i'},
        {"address", required_argument, NULL, 'a'},
        {"port",    required_argument, NULL, 'p'},
        {"autodetect", no_argument,    NULL, 'd'},
        {"verbose",    no_argument,    NULL, 'v'},
        {0, 0, 0, 0}
    };

    int c = 0;

    // Check if anything went wrong by setting 'success' to false
    // As there are multiple tests you may get a faulty behavior
    // if the current value is not considered in latter tests.
    // Asume that: Success = true;
    // success && false = false; --> new value of success is false
    // success && true = true; --> still true.
    // So, in the case of true and only true, success contains the
    // desired value.
    bool success = true;
    bool ifaceExcistent = false;
    bool addExcistent = false;
    static int verbose = 0;

    while((c = getopt_long(argc, argv, "i:a:p:dv",long_options, NULL)) != -1){
        switch(c){
            case 'i': 
                if(addExcistent) { ERROR("Address given but must be absent!"); return false; }
                success = this->parseSetup(SETUP_SERVER_INT, optarg) && success;
                success = this->parseSetup(SETUP_SERVER_ADDRESS, NULL) && success;
                success = this->parseSetup(SETUP_SERVER_AUTO, "0") && success;
                ifaceExcistent = true;
                break;
            case 'a':
                if(ifaceExcistent) { ERROR("Interface given but must be absent!"); return false; }
                success = this->parseSetup(SETUP_SERVER_ADDRESS, optarg) && success;
                success = this->parseSetup(SETUP_SERVER_INT, NULL) && success;
                success = this->parseSetup(SETUP_SERVER_AUTO, "0") && success;
                addExcistent = true;
                break;
            case 'p':
                success = this->parseSetup(SETUP_SERVER_PORT, optarg) && success;
                success = this->parseSetup(SETUP_SERVER_AUTO, "0") && success;
                break;
            case 'd':
                success = this->parseSetup(SETUP_SERVER_AUTO, optarg) && success;
                break;
            case 'v':
                cUPnPConfig::verbosity++;
                verbose++;
                WARNING("Verbosity level: %i ", verbose);
                break;
            default:
                return false;
        }
    }

    return success;
}

bool cUPnPConfig::parseSetup(const char *Name, const char *Value)
{
    const char* ptr;
    if(*this->mParsedArgs && (ptr = strstr(this->mParsedArgs,Name))!=NULL){
        MESSAGE(VERBOSE_SDK, "Skipping %s=%s, was overridden in command line.",Name, Value);
        return true;
    }

    MESSAGE(VERBOSE_SDK, "VARIABLE %s has value %s", Name, Value);
    // Parse your own setup parameters and store their values.
    if      (!strcasecmp(Name, SETUP_SERVER_ENABLED))    this->mEnable = atoi(Value);
    else if (!strcasecmp(Name, SETUP_SERVER_AUTO))       this->mAutoSetup = atoi(Value);
    else if (!strcasecmp(Name, SETUP_SERVER_INT))        this->mInterface = strdup0(Value); // (Value) ? strn0cpy(this->mInterface, Value, strlen(this->mInterface)) : NULL;
    else if (!strcasecmp(Name, SETUP_SERVER_ADDRESS))    this->mAddress = strdup0(Value); //(Value) ? strn0cpy(this->mAddress, Value, strlen(this->mAddress)) : NULL;
    else if (!strcasecmp(Name, SETUP_SERVER_PORT))       this->mPort = atoi(Value);
    else return false;

    this->mParsedArgs = cString::sprintf("%s%s",*this->mParsedArgs,Name);

    return true;
}