File indexing completed on 2024-05-12 15:34:15

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2010 Canonical Ltd
0004     SPDX-FileContributor: Aurélien Gâteau <aurelien.gateau@canonical.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 #include "kconfigutils.h"
0009 
0010 // KDE
0011 #include <kconfig.h>
0012 #include <kconfiggroup.h>
0013 
0014 namespace KConfigUtils
0015 {
0016 bool hasGroup(KConfig *config, const QStringList &lst)
0017 {
0018     KConfigGroup group = openGroup(config, lst);
0019     return group.exists();
0020 }
0021 
0022 KConfigGroup openGroup(KConfig *config, const QStringList &lst)
0023 {
0024     KConfigGroup cg = config->group("");
0025 
0026     for (const auto &i : lst) {
0027         cg = cg.group(i);
0028     }
0029     return cg;
0030 }
0031 
0032 QStringList parseGroupString(const QString &_str, bool *ok, QString *error)
0033 {
0034     QString str = unescapeString(_str.trimmed(), ok, error);
0035     if (!*ok) {
0036         return QStringList();
0037     }
0038 
0039     *ok = true;
0040     if (!str.startsWith(QLatin1Char{'['})) {
0041         // Simplified notation, no '['
0042         return QStringList{str};
0043     }
0044 
0045     if (!str.endsWith(QLatin1Char{']'})) {
0046         *ok = false;
0047         *error = QStringLiteral("Missing closing ']' in %1").arg(_str);
0048         return QStringList();
0049     }
0050     // trim outer brackets
0051     str.chop(1);
0052     str.remove(0, 1);
0053 
0054     return str.split(QLatin1String{"]["});
0055 }
0056 
0057 QString unescapeString(const QString &src, bool *ok, QString *error)
0058 {
0059     QString dst;
0060     const int length = src.length();
0061     for (int pos = 0; pos < length; ++pos) {
0062         QChar ch = src.at(pos);
0063         if (ch != QLatin1Char{'\\'}) {
0064             dst += ch;
0065         } else {
0066             ++pos;
0067             if (pos == length) {
0068                 *ok = false;
0069                 *error = QStringLiteral("Unfinished escape sequence in %1").arg(src);
0070                 return QString();
0071             }
0072 
0073             ch = src.at(pos);
0074             switch (ch.unicode()) {
0075             case L's':
0076                 dst += QLatin1Char{' '};
0077                 break;
0078             case L't':
0079                 dst += QLatin1Char{'\t'};
0080                 break;
0081             case L'n':
0082                 dst += QLatin1Char{'\n'};
0083                 break;
0084             case L'r':
0085                 dst += QLatin1Char{'\r'};
0086                 break;
0087             case L'\\':
0088                 dst += QLatin1Char{'\\'};
0089                 break;
0090             case L'x': {
0091                 if (pos + 2 < length) {
0092 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0093                     char value = QStringView(src).mid(pos + 1, 2).toInt(ok, 16);
0094 #else
0095                     char value = src.midRef(pos + 1, 2).toInt(ok, 16);
0096 #endif
0097                     if (*ok) {
0098                         dst += QLatin1Char{value};
0099                         pos += 2;
0100                     } else {
0101                         *error = QStringLiteral("Invalid hex escape sequence at column %1 in %2").arg(pos).arg(src);
0102                         return QString();
0103                     }
0104                 } else {
0105                     *ok = false;
0106                     *error = QStringLiteral("Unfinished hex escape sequence at column %1 in %2").arg(pos).arg(src);
0107                     return QString();
0108                 }
0109 
0110                 break;
0111             }
0112             default: {
0113                 *ok = false;
0114                 *error = QStringLiteral("Invalid escape sequence at column %1 in %2").arg(pos).arg(src);
0115                 return QString();
0116             }
0117             }
0118         }
0119     }
0120 
0121     *ok = true;
0122     return dst;
0123 }
0124 
0125 } // namespace