1/* This file is part of the KDE libraries
2 Copyright 2010 Canonical Ltd
3 Author: Aurélien Gâteau <aurelien.gateau@canonical.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License (LGPL) as published by the Free Software Foundation;
8 either version 2 of the License, or (at your option) any later
9 version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21#include "kconfigutils.h"
22
23// KDE
24#include <kconfig.h>
25#include <kconfiggroup.h>
26
27namespace KConfigUtils
28{
29
30bool hasGroup(KConfig *config, const QStringList &lst)
31{
32 KConfigGroup group = openGroup(config, lst);
33 return group.exists();
34}
35
36KConfigGroup openGroup(KConfig *config, const QStringList &_lst)
37{
38 if (_lst.isEmpty()) {
39 return KConfigGroup(config, QString());
40 }
41
42 QStringList lst = _lst;
43
44 KConfigGroup cg;
45 for (cg = KConfigGroup(config, lst.takeFirst()); !lst.isEmpty(); cg = KConfigGroup(&cg, lst.takeFirst())) {}
46 return cg;
47}
48
49QStringList parseGroupString(const QString &_str, bool *ok, QString *error)
50{
51 QString str = unescapeString(_str.trimmed(), ok, error);
52 if (!ok) {
53 return QStringList();
54 }
55
56 *ok = true;
57 if (str[0] != '[') {
58 // Simplified notation, no '['
59 return QStringList() << str;
60 }
61
62 if (!str.endsWith(']')) {
63 *ok = false;
64 *error = QString("Missing closing ']' in %1").arg(_str);
65 return QStringList();
66 }
67 // trim outer brackets
68 str.chop(1);
69 str.remove(0, 1);
70
71 return str.split("][");
72}
73
74QString unescapeString(const QString &src, bool *ok, QString *error)
75{
76 QString dst;
77 int length = src.length();
78 for (int pos = 0; pos < length; ++pos) {
79 QChar ch = src.at(pos);
80 if (ch != '\\') {
81 dst += ch;
82 } else {
83 ++pos;
84 if (pos == length) {
85 *ok = false;
86 *error = QString("Unfinished escape sequence in %1").arg(src);
87 return QString();
88 }
89 ch = src.at(pos);
90 if (ch == 's') {
91 dst += ' ';
92 } else if (ch == 't') {
93 dst += '\t';
94 } else if (ch == 'n') {
95 dst += '\n';
96 } else if (ch == 'r') {
97 dst += '\r';
98 } else if (ch == '\\') {
99 dst += '\\';
100 } else if (ch == 'x') {
101 if (pos + 2 < length) {
102 char value = src.mid(pos + 1, 2).toInt(ok, 16);
103 if (*ok) {
104 dst += QChar::fromLatin1(value);
105 pos += 2;
106 } else {
107 *error = QString("Invalid hex escape sequence at column %1 in %2").arg(pos).arg(src);
108 return QString();
109 }
110 } else {
111 *ok = false;
112 *error = QString("Unfinished hex escape sequence at column %1 in %2").arg(pos).arg(src);
113 return QString();
114 }
115 } else {
116 *ok = false;
117 *error = QString("Invalid escape sequence at column %1 in %2").arg(pos).arg(src);
118 return QString();
119 }
120 }
121 }
122
123 *ok = true;
124 return dst;
125}
126
127} // namespace
128