kconf_update
kconfigutils.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright 2010 Canonical Ltd 00003 Author: Aurélien Gâteau <aurelien.gateau@canonical.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License (LGPL) as published by the Free Software Foundation; 00008 either version 2 of the License, or (at your option) any later 00009 version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 #include "kconfigutils.h" 00022 00023 // KDE 00024 #include <kconfig.h> 00025 #include <kconfiggroup.h> 00026 00027 namespace KConfigUtils 00028 { 00029 00030 bool hasGroup(KConfig *config, const QStringList &lst) 00031 { 00032 KConfigGroup group = openGroup(config, lst); 00033 return group.exists(); 00034 } 00035 00036 KConfigGroup openGroup(KConfig *config, const QStringList &_lst) 00037 { 00038 if (_lst.isEmpty()) { 00039 return KConfigGroup(config, QString()); 00040 } 00041 00042 QStringList lst = _lst; 00043 00044 KConfigGroup cg; 00045 for (cg = KConfigGroup(config, lst.takeFirst()); !lst.isEmpty(); cg = KConfigGroup(&cg, lst.takeFirst())) {} 00046 return cg; 00047 } 00048 00049 QStringList parseGroupString(const QString &_str, bool *ok, QString *error) 00050 { 00051 QString str = unescapeString(_str.trimmed(), ok, error); 00052 if (!ok) { 00053 return QStringList(); 00054 } 00055 00056 *ok = true; 00057 if (str[0] != '[') { 00058 // Simplified notation, no '[' 00059 return QStringList() << str; 00060 } 00061 00062 if (!str.endsWith(']')) { 00063 *ok = false; 00064 *error = QString("Missing closing ']' in %1").arg(_str); 00065 return QStringList(); 00066 } 00067 // trim outer brackets 00068 str.chop(1); 00069 str.remove(0, 1); 00070 00071 return str.split("]["); 00072 } 00073 00074 QString unescapeString(const QString &src, bool *ok, QString *error) 00075 { 00076 QString dst; 00077 int length = src.length(); 00078 for (int pos = 0; pos < length; ++pos) { 00079 QChar ch = src.at(pos); 00080 if (ch != '\\') { 00081 dst += ch; 00082 } else { 00083 ++pos; 00084 if (pos == length) { 00085 *ok = false; 00086 *error = QString("Unfinished escape sequence in %1").arg(src); 00087 return QString(); 00088 } 00089 ch = src.at(pos); 00090 if (ch == 's') { 00091 dst += ' '; 00092 } else if (ch == 't') { 00093 dst += '\t'; 00094 } else if (ch == 'n') { 00095 dst += '\n'; 00096 } else if (ch == 'r') { 00097 dst += '\r'; 00098 } else if (ch == '\\') { 00099 dst += '\\'; 00100 } else if (ch == 'x') { 00101 if (pos + 2 < length) { 00102 char value = src.mid(pos + 1, 2).toInt(ok, 16); 00103 if (*ok) { 00104 dst += QChar::fromAscii(value); 00105 pos += 2; 00106 } else { 00107 *error = QString("Invalid hex escape sequence at column %1 in %2").arg(pos).arg(src); 00108 return QString(); 00109 } 00110 } else { 00111 *ok = false; 00112 *error = QString("Unfinished hex escape sequence at column %1 in %2").arg(pos).arg(src); 00113 return QString(); 00114 } 00115 } else { 00116 *ok = false; 00117 *error = QString("Invalid escape sequence at column %1 in %2").arg(pos).arg(src); 00118 return QString(); 00119 } 00120 } 00121 } 00122 00123 *ok = true; 00124 return dst; 00125 } 00126 00127 } // namespace
KDE 4.6 API Reference