KConfig

kconfigutils.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2010 Canonical Ltd
4  SPDX-FileContributor: Aurélien Gâteau <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 #include "kconfigutils.h"
9 
10 // KDE
11 #include <kconfig.h>
12 #include <kconfiggroup.h>
13 
14 namespace KConfigUtils
15 {
16 
17 bool hasGroup(KConfig *config, const QStringList &lst)
18 {
19  KConfigGroup group = openGroup(config, lst);
20  return group.exists();
21 }
22 
23 KConfigGroup openGroup(KConfig *config, const QStringList &lst)
24 {
25  KConfigGroup cg = config->group("");
26 
27  for (const auto &i : lst) {
28  cg = cg.group(i);
29  }
30  return cg;
31 }
32 
33 QStringList parseGroupString(const QString &_str, bool *ok, QString *error)
34 {
35  QString str = unescapeString(_str.trimmed(), ok, error);
36  if (!*ok) {
37  return QStringList();
38  }
39 
40  *ok = true;
41  if (str[0] != '[') {
42  // Simplified notation, no '['
43  return QStringList() << str;
44  }
45 
46  if (!str.endsWith(']')) {
47  *ok = false;
48  *error = QStringLiteral("Missing closing ']' in %1").arg(_str);
49  return QStringList();
50  }
51  // trim outer brackets
52  str.chop(1);
53  str.remove(0, 1);
54 
55  return str.split(QStringLiteral("]["));
56 }
57 
58 QString unescapeString(const QString &src, bool *ok, QString *error)
59 {
60  QString dst;
61  int length = src.length();
62  for (int pos = 0; pos < length; ++pos) {
63  QChar ch = src.at(pos);
64  if (ch != '\\') {
65  dst += ch;
66  } else {
67  ++pos;
68  if (pos == length) {
69  *ok = false;
70  *error = QStringLiteral("Unfinished escape sequence in %1").arg(src);
71  return QString();
72  }
73  ch = src.at(pos);
74  if (ch == 's') {
75  dst += ' ';
76  } else if (ch == 't') {
77  dst += '\t';
78  } else if (ch == 'n') {
79  dst += '\n';
80  } else if (ch == 'r') {
81  dst += '\r';
82  } else if (ch == '\\') {
83  dst += '\\';
84  } else if (ch == 'x') {
85  if (pos + 2 < length) {
86  char value = src.midRef(pos + 1, 2).toInt(ok, 16);
87  if (*ok) {
88  dst += QChar::fromLatin1(value);
89  pos += 2;
90  } else {
91  *error = QStringLiteral("Invalid hex escape sequence at column %1 in %2").arg(pos).arg(src);
92  return QString();
93  }
94  } else {
95  *ok = false;
96  *error = QStringLiteral("Unfinished hex escape sequence at column %1 in %2").arg(pos).arg(src);
97  return QString();
98  }
99  } else {
100  *ok = false;
101  *error = QStringLiteral("Invalid escape sequence at column %1 in %2").arg(pos).arg(src);
102  return QString();
103  }
104  }
105  }
106 
107  *ok = true;
108  return dst;
109 }
110 
111 } // namespace
int toInt(bool *ok, int base) const const
KConfigGroup group(const QByteArray &group)
Returns an object for the named subgroup.
Definition: kconfigbase.cpp:31
QString & remove(int position, int n)
void chop(int n)
bool exists() const
Check whether the containing KConfig object acutally contains a group with this name.
QChar fromLatin1(char c)
QString trimmed() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringRef midRef(int position, int n) const const
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:38
The central class of the KDE configuration data system.
Definition: kconfig.h:56
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
const QChar at(int position) const const
int length() const const
KDB_EXPORT QString unescapeString(const QString &string, char quote, int *errorPosition=nullptr)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 22:47:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.