summaryrefslogtreecommitdiff
path: root/ext/Log4Qt/src/helpers/optionconverter.cpp
blob: e148a2df7874475fcb8fd7f7107f24cd98f17038 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/******************************************************************************
 *
 * package:     Log4Qt
 * file:        optionconverter.cpp
 * created:     September 2007
 * author:      Martin Heinrich
 *
 * 
 * changes      Feb 2009, Martin Heinrich
 *              - Fixed a problem were OptionConverter::toBoolean would not
 *                return the default value, if the conversion fails.
 *
 *
 * Copyright 2007 - 2009 Martin Heinrich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 ******************************************************************************/


/******************************************************************************
 * Dependencies
 ******************************************************************************/


#include "helpers/optionconverter.h"

#include <QtCore/QDebug>
#include "helpers/logerror.h"
#include "helpers/properties.h"
#include "logger.h"
#include "consoleappender.h"



namespace Log4Qt
{
	
	
	/**************************************************************************
	 * Declarations
	 **************************************************************************/
	
	
	
	/**************************************************************************
	 * C helper functions
	 **************************************************************************/
	
	
    LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt::OptionConverter)

    
    
	/**************************************************************************
	 * Class implementation: OptionConverter
	 **************************************************************************/
	
	
    QString OptionConverter::findAndSubst(const Properties &rProperties,
                                          const QString &rKey)
    {
        QString value = rProperties.property(rKey);
        if (value.isNull())
            return value;
        
        const QString begin_subst = QLatin1String("${");
        const QString end_subst = QLatin1String("}");
        const int begin_length = begin_subst.length();
        const int end_length = end_subst.length();
        
        // Don't return a null string, the null string indicates that the 
        // property key does not exist.
        QString result = QLatin1String("");
        
        int i = 0;
        int begin;
        int end;
        while (i < value.length())
        {
            begin = value.indexOf(begin_subst, i);
            if (begin == -1)
            {
                result += value.mid(i);
                i = value.length();
            }
            else
            {
                result += value.mid(i, begin - i);
                end = value.indexOf(end_subst, i + begin_length);
                if (end == -1)
                {
                    LogError e = LOG4QT_ERROR(QT_TR_NOOP("Missing closing bracket for opening bracket at %1. Invalid subsitution in value %2."), 
                                              CONFIGURATOR_INVALID_SUBSTITUTION_ERROR,
                                              "Log4Qt::OptionConverter");
                    e << begin << value;
                    logger()->error(e);
                    return result;
                }
                else
                {
                    result += findAndSubst(rProperties, value.mid(begin + begin_length, end - begin - end_length - 1));
                    i = end + end_length;
                }
            }
        }
        return result;
    }
    
    
    QString OptionConverter::classNameJavaToCpp(const QString &rClassName)
    {
        const QLatin1String java_class_delimiter(".");
        const QLatin1String cpp_class_delimiter("::");
        
        QString result = rClassName;
        return result.replace(java_class_delimiter, cpp_class_delimiter);
    }
    
    
    bool OptionConverter::toBoolean(const QString &rOption, 
                                    bool *p_ok)
    {
        const QLatin1String str_true("true");
        const QLatin1String str_enabled("enabled");
        const QLatin1String str_one("1");
        const QLatin1String str_false("false");
        const QLatin1String str_disabled("disabled");
        const QLatin1String str_zero("0");
        
        if (p_ok)
            *p_ok = true;
        QString s = rOption.trimmed().toLower();
        if (s == str_true || s == str_enabled || s == str_one)
            return true;
        if (s == str_false || s == str_disabled || s == str_zero)
            return false;
        
        if (p_ok)
            *p_ok = false;
        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a boolean"), 
                                  CONFIGURATOR_INVALID_OPTION_ERROR,
                                  "Log4Qt::OptionConverter");
        e << rOption;
        logger()->error(e);
        return false;
    }

    
    bool OptionConverter::toBoolean(const QString &rOption, 
                                    bool default_value)
    {
        bool ok;
        bool result = toBoolean(rOption, &ok);
        if (ok)
            return result;
        else
            return default_value;
    }
    
    qint64 OptionConverter::toFileSize(const QString &rOption, 
                                       bool *p_ok)
    {
        // - Search for unit
        // - Convert characters befor unit to int
        // - Error, if
        //   - the conversion failed
        //   - the value < 0
        //   - there is text after the unit characters
        
        if (p_ok)
            *p_ok = false;
        QString s = rOption.trimmed().toLower();
        qint64 f = 1;
        int i;
        i = s.indexOf(QLatin1String("kb"));
        if (i >= 0)
            f = 1024;
        else 
        {
            i = s.indexOf(QLatin1String("mb"));
            if (i >= 0)
                f = 1024 * 1024;
            else 
            {
                i = s.indexOf(QLatin1String("gb"));
                if (i >= 0)
                    f = 1024 * 1024 * 1024;
            }
        }
        if (i < 0)
            i = s.length();
        bool ok;
        qint64 value = s.left(i).toLongLong(&ok);
        if (!ok || value < 0 || s.length() > i + 2)
        {
            LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a file size"), 
                                      CONFIGURATOR_INVALID_OPTION_ERROR,
                                      "Log4Qt::OptionConverter");
            e << rOption;
            logger()->error(e);
            return 0;
        }
        if (p_ok)
            *p_ok = true;
        return value * f;
    }

    
    int OptionConverter::toInt(const QString &rOption, 
                               bool *p_ok)
    {
        int value = rOption.trimmed().toInt(p_ok);
        if (*p_ok)
            return value;
        
        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for an integer"), 
                                  CONFIGURATOR_INVALID_OPTION_ERROR,
                                  "Log4Qt::OptionConverter");
        e << rOption;
        logger()->error(e);
        return 0;
    }
    
    
    Level OptionConverter::toLevel(const QString &rOption, 
                                   bool *p_ok)
    {
        bool ok;
        Level level = Level::fromString(rOption.toUpper().trimmed(), &ok);
        if (p_ok)
            *p_ok = ok;
        if (ok)
            return level;

        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a level"), 
                                  CONFIGURATOR_INVALID_OPTION_ERROR,
                                  "Log4Qt::OptionConverter");
        e << rOption;
        logger()->error(e);
        return level;
    }

    
    Level OptionConverter::toLevel(const QString &rOption, 
                                   const Level &rDefaultValue)
    {
        bool ok;
        Level result = toLevel(rOption, &ok);
        if (ok)
            return result;
        else
            return rDefaultValue;
    }

    
    int OptionConverter::toTarget(const QString &rOption, 
                                  bool *p_ok)
    {
        const QLatin1String java_stdout("system.out"); 
        const QLatin1String cpp_stdout("stdout_target"); 
        const QLatin1String java_stderr("system.err"); 
        const QLatin1String cpp_stderr("stderr_target");
        
        if (p_ok)
            *p_ok = true;
        QString s = rOption.trimmed().toLower();
        if (s == java_stdout || s == cpp_stdout)
            return ConsoleAppender::STDOUT_TARGET;
        if (s == java_stderr || s == cpp_stderr)
            return ConsoleAppender::STDERR_TARGET;

        if (p_ok)
            *p_ok = false;
        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a target"), 
                                  CONFIGURATOR_INVALID_OPTION_ERROR,
                                  "Log4Qt::OptionConverter");
        e << rOption;
        logger()->error(e);
        return ConsoleAppender::STDOUT_TARGET;
    }
    
    
    
	/**************************************************************************
	 * Implementation: Operators, Helper
	 **************************************************************************/


	
} // namespace Log4Qt