util
kdevstringhandler.cpp
00001 /* This file is part of KDevelop 00002 Copyright 2009 Andreas Pakulat <apaku@gmx.de> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 00019 This file mostly code takes from Qt's QSettings class, the copyright 00020 header from that file follows: 00021 00022 **************************************************************************** 00023 ** 00024 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 00025 ** Contact: Nokia Corporation (qt-info@nokia.com) 00026 ** 00027 ** This file is part of the QtCore module of the Qt Toolkit. 00028 ** 00029 ** $QT_BEGIN_LICENSE:LGPL$ 00030 ** Commercial Usage 00031 ** Licensees holding valid Qt Commercial licenses may use this file in 00032 ** accordance with the Qt Commercial License Agreement provided with the 00033 ** Software or, alternatively, in accordance with the terms contained in 00034 ** a written agreement between you and Nokia. 00035 ** 00036 ** GNU Lesser General Public License Usage 00037 ** Alternatively, this file may be used under the terms of the GNU Lesser 00038 ** General Public License version 2.1 as published by the Free Software 00039 ** Foundation and appearing in the file LICENSE.LGPL included in the 00040 ** packaging of this file. Please review the following information to 00041 ** ensure the GNU Lesser General Public License version 2.1 requirements 00042 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 00043 ** 00044 ** In addition, as a special exception, Nokia gives you certain 00045 ** additional rights. These rights are described in the Nokia Qt LGPL 00046 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 00047 ** package. 00048 ** 00049 ** GNU General Public License Usage 00050 ** Alternatively, this file may be used under the terms of the GNU 00051 ** General Public License version 3.0 as published by the Free Software 00052 ** Foundation and appearing in the file LICENSE.GPL included in the 00053 ** packaging of this file. Please review the following information to 00054 ** ensure the GNU General Public License version 3.0 requirements will be 00055 ** met: http://www.gnu.org/copyleft/gpl.html. 00056 ** 00057 ** If you are unsure which license is appropriate for your use, please 00058 ** contact the sales department at http://www.qtsoftware.com/contact. 00059 ** $QT_END_LICENSE$ 00060 ** 00061 **************************************************************************** 00062 */ 00063 00064 00065 #include "kdevstringhandler.h" 00066 00067 #include <QStringList> 00068 #include <QString> 00069 #include <QChar> 00070 #include <QDataStream> 00071 #include <QVariant> 00072 00073 namespace KDevelop 00074 { 00075 QString joinWithEscaping( const QStringList& input, const QChar& joinchar, const QChar& escapechar ) 00076 { 00077 QStringList tmp = input; 00078 return tmp.replaceInStrings( joinchar, QString( joinchar ) + QString( escapechar ) ).join( joinchar ); 00079 } 00080 00081 QStringList splitWithEscaping( const QString& input, const QChar& splitchar, const QChar& escapechar ) 00082 { 00083 enum State { Normal, SeenEscape } state; 00084 00085 state = Normal; 00086 00087 QStringList result; 00088 QString currentstring; 00089 for( int i = 0; i < input.size(); i++ ) { 00090 switch( state ) { 00091 case Normal: 00092 if( input[i] == escapechar ) { 00093 state = SeenEscape; 00094 } else if( input[i] == splitchar ) { 00095 result << currentstring; 00096 currentstring = ""; 00097 } else { 00098 currentstring += input[i]; 00099 } 00100 break; 00101 case SeenEscape: 00102 currentstring += input[i]; 00103 state = Normal; 00104 break; 00105 } 00106 } 00107 if( !currentstring.isEmpty() ) { 00108 result << currentstring; 00109 } 00110 return result; 00111 } 00112 00113 QVariant stringToQVariant(const QString& s) 00114 { 00115 // Taken from qsettings.cpp, stringToVariant() 00116 if (s.startsWith(QLatin1Char('@'))) { 00117 if (s.endsWith(QLatin1Char(')'))) { 00118 if (s.startsWith(QLatin1String("@Variant("))) { 00119 QByteArray a(s.toLatin1().mid(9)); 00120 QDataStream stream(&a, QIODevice::ReadOnly); 00121 stream.setVersion(QDataStream::Qt_4_4); 00122 QVariant result; 00123 stream >> result; 00124 return result; 00125 } 00126 } 00127 } 00128 return QVariant(); 00129 00130 } 00131 00132 QString qvariantToString(const QVariant& variant) 00133 { 00134 // Taken from qsettings.cpp, variantToString() 00135 QByteArray a; 00136 { 00137 QDataStream s(&a, QIODevice::WriteOnly); 00138 s.setVersion(QDataStream::Qt_4_4); 00139 s << variant; 00140 } 00141 00142 QString result = QLatin1String("@Variant("); 00143 result += QString::fromLatin1(a.constData(), a.size()); 00144 result += QLatin1Char(')'); 00145 return result; 00146 00147 } 00148 } 00149
KDE 4.4 API Reference