Statistics
| Branch: | Revision:

root / src / sim / cconfiguration.cc @ e26d3d25

History | View | Annotate | Download (9.49 KB)

1
//=========================================================================
2
//  CCONFIGURATION.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga
8
//
9
//=========================================================================
10

    
11
/*--------------------------------------------------------------*
12
  Copyright (C) 2003-2008 Andras Varga
13
  Copyright (C) 2006-2008 OpenSim Ltd.
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18

    
19

    
20
#include <errno.h>
21
#include "cconfiguration.h"
22
#include "cconfigoption.h"
23
#include "unitconversion.h"
24
#include "stringutil.h"
25
#include "fileutil.h"
26
#include "stringtokenizer.h"
27
#include "fnamelisttokenizer.h"
28
#include "exception.h"
29

    
30
USING_NAMESPACE
31

    
32

    
33
bool cConfiguration::parseBool(const char *s, const char *defaultValue, bool fallbackValue)
34
{
35
    if (!s)
36
        s = defaultValue;
37
    if (!s)
38
        return fallbackValue;
39
    if (strcmp(s,"yes")==0 || strcmp(s,"true")==0)
40
        return true;
41
    else if (strcmp(s,"no")==0 || strcmp(s,"false")==0)
42
        return false;
43
    else
44
        throw opp_runtime_error("`%s' is not a valid boolean value, use true/false", s);
45
}
46

    
47
long cConfiguration::parseLong(const char *s, const char *defaultValue, long fallbackValue)
48
{
49
    if (!s)
50
        s = defaultValue;
51
    if (!s)
52
        return fallbackValue;
53
    return opp_atol(s);
54
}
55

    
56
double cConfiguration::parseDouble(const char *s, const char *unit, const char *defaultValue, double fallbackValue)
57
{
58
    if (!s)
59
        s = defaultValue;
60
    if (!s)
61
        return fallbackValue;
62
    return UnitConversion::parseQuantity(s, unit);
63
}
64

    
65
std::string cConfiguration::parseString(const char *s, const char *defaultValue, const char *fallbackValue)
66
{
67
    if (!s)
68
        s = defaultValue;
69
    if (!s)
70
        return fallbackValue;
71
    if (*s == '"')
72
        return opp_parsequotedstr(s);
73
    else
74
        return s;
75
}
76

    
77
std::string cConfiguration::parseFilename(const char *s, const char *baseDir, const char *defaultValue)
78
{
79
    if (!s)
80
        s = defaultValue;
81
    if (!s || !s[0])
82
        return "";
83
    return tidyFilename(concatDirAndFile(baseDir, s).c_str());
84
}
85

    
86
std::vector<std::string> cConfiguration::parseFilenames(const char *s, const char *baseDir, const char *defaultValue)
87
{
88
    if (!s)
89
        s = defaultValue;
90
    if (!s)
91
        s = "";
92
    std::vector<std::string> result;
93
    FilenamesListTokenizer tokenizer(s);
94
    const char *fname;
95
    while ((fname = tokenizer.nextToken())!=NULL)
96
    {
97
        if (fname[0]=='@' && fname[1]=='@')
98
            result.push_back("@@" + tidyFilename(concatDirAndFile(baseDir, fname+2).c_str()));
99
        else if (fname[0]=='@')
100
            result.push_back("@" + tidyFilename(concatDirAndFile(baseDir, fname+1).c_str()));
101
        else
102
            result.push_back(tidyFilename(concatDirAndFile(baseDir, fname).c_str()));
103
    }
104
    return result;
105
}
106

    
107
std::string cConfiguration::adjustPath(const char *s, const char *baseDir, const char *defaultValue)
108
{
109
    if (!s)
110
        s = defaultValue;
111
    if (!s)
112
        s = "";
113
    std::string result;
114
    StringTokenizer tokenizer(s, PATH_SEPARATOR);
115
    const char *dirName;
116
    while ((dirName = tokenizer.nextToken())!=NULL)
117
    {
118
        if (result.size()!=0)
119
            result += ";";
120
        result += tidyFilename(concatDirAndFile(baseDir, dirName).c_str());
121
    }
122
    return result;
123
}
124

    
125
//---
126

    
127
#define TRY(CODE)   try { CODE; } catch (std::exception& e) {throw cRuntimeError("Error getting option %s= from the configuration: %s", option->getName(), e.what());}
128

    
129
inline const char *fallback(const char *s, const char *defaultValue, const char *fallbackValue)
130
{
131
    return s!=NULL ? s : defaultValue!=NULL ? defaultValue : fallbackValue;
132
}
133

    
134
static void assertType(cConfigOption *option, bool isPerObject, cConfigOption::Type requiredType)
135
{
136
    if (option->isPerObject() != isPerObject)
137
    {
138
        if (option->isPerObject())
139
            throw cRuntimeError("Option %s= is read from the configuration in the wrong way: it is per-object configuration", option->getName());
140
        else
141
            throw cRuntimeError("Option %s= is read from the configuration in the wrong way: it is global (not per-object) configuration", option->getName());
142
    }
143
    if (option->getType() != requiredType)
144
        throw cRuntimeError("Option %s= is read from the configuration with the wrong type (type=%s, actual=%s)",
145
                            option->getName(), cConfigOption::getTypeName(requiredType), cConfigOption::getTypeName(option->getType()));
146
}
147

    
148
const char *cConfiguration::getAsCustom(cConfigOption *option, const char *fallbackValue)
149
{
150
    assertType(option, false, cConfigOption::CFG_CUSTOM);
151
    TRY(return fallback(getConfigValue(option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
152
}
153

    
154
bool cConfiguration::getAsBool(cConfigOption *option, bool fallbackValue)
155
{
156
    assertType(option, false, cConfigOption::CFG_BOOL);
157
    TRY(return parseBool(getConfigValue(option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
158
}
159

    
160
long cConfiguration::getAsInt(cConfigOption *option, long fallbackValue)
161
{
162
    assertType(option, false, cConfigOption::CFG_INT);
163
    TRY(return parseLong(getConfigValue(option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
164
}
165

    
166
double cConfiguration::getAsDouble(cConfigOption *option, double fallbackValue)
167
{
168
    assertType(option, false, cConfigOption::CFG_DOUBLE);
169
    TRY(return parseDouble(getConfigValue(option->getName()), option->getUnit(), substituteVariables(option->getDefaultValue()), fallbackValue));
170
}
171

    
172
std::string cConfiguration::getAsString(cConfigOption *option, const char *fallbackValue)
173
{
174
    assertType(option, false, cConfigOption::CFG_STRING);
175
    TRY(return parseString(getConfigValue(option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
176
}
177

    
178
std::string cConfiguration::getAsFilename(cConfigOption *option)
179
{
180
    assertType(option, false, cConfigOption::CFG_FILENAME);
181
    const KeyValue& keyvalue = getConfigEntry(option->getName());
182
    TRY(return parseFilename(keyvalue.getValue(), keyvalue.getBaseDirectory(), substituteVariables(option->getDefaultValue())));
183
}
184

    
185
std::vector<std::string> cConfiguration::getAsFilenames(cConfigOption *option)
186
{
187
    assertType(option, false, cConfigOption::CFG_FILENAMES);
188
    const KeyValue& keyvalue = getConfigEntry(option->getName());
189
    TRY(return parseFilenames(keyvalue.getValue(), keyvalue.getBaseDirectory(), substituteVariables(option->getDefaultValue())));
190
}
191

    
192
std::string cConfiguration::getAsPath(cConfigOption *option)
193
{
194
    assertType(option, false, cConfigOption::CFG_PATH);
195
    const KeyValue& keyvalue = getConfigEntry(option->getName());
196
    TRY(return adjustPath(keyvalue.getValue(), keyvalue.getBaseDirectory(), substituteVariables(option->getDefaultValue())));
197
}
198

    
199
//----
200

    
201
const char *cConfiguration::getAsCustom(const char *objectFullPath, cConfigOption *option, const char *fallbackValue)
202
{
203
    assertType(option, true, cConfigOption::CFG_CUSTOM);
204
    TRY(return fallback(getPerObjectConfigValue(objectFullPath, option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
205
}
206

    
207
bool cConfiguration::getAsBool(const char *objectFullPath, cConfigOption *option, bool fallbackValue)
208
{
209
    assertType(option, true, cConfigOption::CFG_BOOL);
210
    TRY(return parseBool(getPerObjectConfigValue(objectFullPath, option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
211
}
212

    
213
long cConfiguration::getAsInt(const char *objectFullPath, cConfigOption *option, long fallbackValue)
214
{
215
    assertType(option, true, cConfigOption::CFG_INT);
216
    TRY(return parseLong(getPerObjectConfigValue(objectFullPath, option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
217
}
218

    
219
double cConfiguration::getAsDouble(const char *objectFullPath, cConfigOption *option, double fallbackValue)
220
{
221
    assertType(option, true, cConfigOption::CFG_DOUBLE);
222
    TRY(return parseDouble(getPerObjectConfigValue(objectFullPath, option->getName()), option->getUnit(), substituteVariables(option->getDefaultValue()), fallbackValue));
223
}
224

    
225
std::string cConfiguration::getAsString(const char *objectFullPath, cConfigOption *option, const char *fallbackValue)
226
{
227
    assertType(option, true, cConfigOption::CFG_STRING);
228
    TRY(return parseString(getPerObjectConfigValue(objectFullPath, option->getName()), substituteVariables(option->getDefaultValue()), fallbackValue));
229
}
230

    
231
std::string cConfiguration::getAsFilename(const char *objectFullPath, cConfigOption *option)
232
{
233
    assertType(option, true, cConfigOption::CFG_FILENAME);
234
    const KeyValue& keyvalue = getConfigEntry(option->getName());
235
    TRY(return parseFilename(keyvalue.getValue(), keyvalue.getBaseDirectory(), substituteVariables(option->getDefaultValue())));
236
}
237

    
238
std::vector<std::string> cConfiguration::getAsFilenames(const char *objectFullPath, cConfigOption *option)
239
{
240
    assertType(option, true, cConfigOption::CFG_FILENAMES);
241
    const KeyValue& keyvalue = getConfigEntry(option->getName());
242
    TRY(return parseFilenames(keyvalue.getValue(), keyvalue.getBaseDirectory(), substituteVariables(option->getDefaultValue())));
243
}
244

    
245
std::string cConfiguration::getAsPath(const char *objectFullPath, cConfigOption *option)
246
{
247
    assertType(option, true, cConfigOption::CFG_FILENAMES);
248
    const KeyValue& keyvalue = getConfigEntry(option->getName());
249
    TRY(return adjustPath(keyvalue.getValue(), keyvalue.getBaseDirectory(), substituteVariables(option->getDefaultValue())));
250
}
251