File indexing completed on 2024-12-08 12:45:03
0001 /* This file is part of the KDE project 0002 Copyright (C) 2010 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KDbEscapedString.h" 0021 #include "kdb_debug.h" 0022 0023 #include <QDataStream> 0024 0025 KDbEscapedString &KDbEscapedString::prepend(const KDbEscapedString &s) 0026 { 0027 if (s.isValid()) { 0028 if (m_valid) 0029 QByteArray::prepend(s); 0030 } 0031 else { 0032 QByteArray::clear(); 0033 m_valid = false; 0034 } 0035 return *this; 0036 } 0037 0038 KDbEscapedString &KDbEscapedString::append(const KDbEscapedString &s) 0039 { 0040 if (s.isValid()) { 0041 if (m_valid) 0042 QByteArray::append(s); 0043 } 0044 else { 0045 QByteArray::clear(); 0046 m_valid = false; 0047 } 0048 return *this; 0049 } 0050 0051 KDbEscapedString &KDbEscapedString::insert(int i, const KDbEscapedString &s) 0052 { 0053 if (s.isValid()) { 0054 if (m_valid) 0055 QByteArray::insert(i, s); 0056 } 0057 else { 0058 QByteArray::clear(); 0059 m_valid = false; 0060 } 0061 return *this; 0062 } 0063 0064 KDbEscapedString &KDbEscapedString::replace(int index, int len, const KDbEscapedString &s) 0065 { 0066 if (s.isValid()) { 0067 if (m_valid) 0068 QByteArray::replace(index, len, s); 0069 } 0070 else { 0071 QByteArray::clear(); 0072 m_valid = false; 0073 } 0074 return *this; 0075 } 0076 0077 KDbEscapedString &KDbEscapedString::replace(char before, const KDbEscapedString &after) 0078 { 0079 if (after.isValid()) { 0080 if (m_valid) 0081 QByteArray::replace(before, after); 0082 } 0083 else { 0084 QByteArray::clear(); 0085 m_valid = false; 0086 } 0087 return *this; 0088 } 0089 0090 KDbEscapedString &KDbEscapedString::replace(const KDbEscapedString &before, const QByteArray &after) 0091 { 0092 if (before.isValid()) { 0093 if (m_valid) 0094 QByteArray::replace(before, after); 0095 } 0096 else { 0097 QByteArray::clear(); 0098 m_valid = false; 0099 } 0100 return *this; 0101 } 0102 0103 KDbEscapedString &KDbEscapedString::replace(const QByteArray &before, const KDbEscapedString &after) 0104 { 0105 if (after.isValid()) { 0106 if (m_valid) 0107 QByteArray::replace(before, after); 0108 } 0109 else { 0110 QByteArray::clear(); 0111 m_valid = false; 0112 } 0113 return *this; 0114 } 0115 0116 KDbEscapedString &KDbEscapedString::replace(const KDbEscapedString &before, const KDbEscapedString &after) 0117 { 0118 if (before.isValid() && after.isValid()) { 0119 if (m_valid) 0120 QByteArray::replace(before, after); 0121 } 0122 else { 0123 QByteArray::clear(); 0124 m_valid = false; 0125 } 0126 return *this; 0127 } 0128 0129 QList<KDbEscapedString> KDbEscapedString::split(char sep) const 0130 { 0131 QList<KDbEscapedString> result; 0132 foreach(const QByteArray& ba, QByteArray::split(sep)) 0133 result.append(KDbEscapedString(ba)); 0134 return result; 0135 } 0136 0137 #ifndef QT_NO_DATASTREAM 0138 KDB_EXPORT QDataStream& operator<<(QDataStream &stream, const KDbEscapedString &string) 0139 { 0140 stream << string.isValid(); 0141 if (string.isValid()) 0142 stream << string.toByteArray(); 0143 return stream; 0144 } 0145 0146 KDB_EXPORT QDataStream& operator>>(QDataStream &stream, KDbEscapedString &string) 0147 { 0148 bool valid; 0149 stream >> valid; 0150 if (valid) { 0151 QByteArray ba; 0152 stream >> ba; 0153 string = ba; 0154 } 0155 else { 0156 string = KDbEscapedString::invalid(); 0157 } 0158 return stream; 0159 } 0160 #endif 0161 0162 short KDbEscapedString::toShort(bool *ok, int base) const 0163 { 0164 if (!checkValid(ok)) 0165 return 0; 0166 return QByteArray::toShort(ok, base); 0167 } 0168 0169 ushort KDbEscapedString::toUShort(bool *ok, int base) const 0170 { 0171 if (!checkValid(ok)) 0172 return 0; 0173 return QByteArray::toUShort(ok, base); 0174 } 0175 0176 int KDbEscapedString::toInt(bool *ok, int base) const 0177 { 0178 if (!checkValid(ok)) 0179 return 0; 0180 return QByteArray::toInt(ok, base); 0181 } 0182 0183 uint KDbEscapedString::toUInt(bool *ok, int base) const 0184 { 0185 if (!checkValid(ok)) 0186 return 0; 0187 return QByteArray::toUInt(ok, base); 0188 } 0189 0190 long KDbEscapedString::toLong(bool *ok, int base) const 0191 { 0192 if (!checkValid(ok)) 0193 return 0; 0194 return QByteArray::toLong(ok, base); 0195 } 0196 0197 ulong KDbEscapedString::toULong(bool *ok, int base) const 0198 { 0199 if (!checkValid(ok)) 0200 return 0; 0201 return QByteArray::toULong(ok, base); 0202 } 0203 0204 qlonglong KDbEscapedString::toLongLong(bool *ok, int base) const 0205 { 0206 if (!checkValid(ok)) 0207 return 0; 0208 return QByteArray::toLongLong(ok, base); 0209 } 0210 0211 qulonglong KDbEscapedString::toULongLong(bool *ok, int base) const 0212 { 0213 if (!checkValid(ok)) 0214 return 0; 0215 return QByteArray::toULongLong(ok, base); 0216 } 0217 0218 float KDbEscapedString::toFloat(bool *ok) const 0219 { 0220 if (!checkValid(ok)) 0221 return 0; 0222 return QByteArray::toFloat(ok); 0223 } 0224 0225 double KDbEscapedString::toDouble(bool *ok) const 0226 { 0227 if (!checkValid(ok)) 0228 return 0; 0229 return QByteArray::toDouble(ok); 0230 } 0231 0232 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2) const 0233 { 0234 if (!m_valid || !a1.isValid() || !a2.isValid()) 0235 return KDbEscapedString::invalid(); 0236 return KDbEscapedString(toString().arg(a1.toString(), a2.toString())); 0237 } 0238 0239 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3) const 0240 { 0241 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid()) 0242 return KDbEscapedString::invalid(); 0243 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString())); 0244 } 0245 0246 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3, 0247 const KDbEscapedString &a4) const 0248 { 0249 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid()) 0250 return KDbEscapedString::invalid(); 0251 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString())); 0252 } 0253 0254 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3, 0255 const KDbEscapedString &a4, const KDbEscapedString &a5) const 0256 { 0257 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid()) 0258 return KDbEscapedString::invalid(); 0259 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(), 0260 a5.toString())); 0261 } 0262 0263 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3, 0264 const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6) const 0265 { 0266 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid() 0267 || !a6.isValid()) 0268 return KDbEscapedString::invalid(); 0269 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(), 0270 a5.toString(), a6.toString())); 0271 } 0272 0273 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3, 0274 const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6, 0275 const KDbEscapedString &a7) const 0276 { 0277 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid() 0278 || !a6.isValid() || !a7.isValid()) 0279 return KDbEscapedString::invalid(); 0280 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(), 0281 a5.toString(), a6.toString(), a7.toString())); 0282 } 0283 0284 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3, 0285 const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6, 0286 const KDbEscapedString &a7, const KDbEscapedString &a8) const 0287 { 0288 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid() 0289 || !a6.isValid() || !a7.isValid() || !a8.isValid()) 0290 return KDbEscapedString::invalid(); 0291 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(), 0292 a5.toString(), a6.toString(), a7.toString(), a8.toString())); 0293 } 0294 0295 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3, 0296 const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6, 0297 const KDbEscapedString &a7, const KDbEscapedString &a8, const KDbEscapedString &a9) const 0298 { 0299 if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid() 0300 || !a6.isValid() || !a7.isValid() || !a8.isValid() || !a9.isValid()) 0301 return KDbEscapedString::invalid(); 0302 return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(), 0303 a5.toString(), a6.toString(), a7.toString(), a8.toString(), a9.toString())); 0304 } 0305 0306 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a, int fieldWidth, const QChar & fillChar) const 0307 { 0308 if (!m_valid || !a.isValid()) 0309 return KDbEscapedString::invalid(); 0310 return KDbEscapedString(toString().arg(a.toString(), fieldWidth, fillChar)); 0311 } 0312 0313 KDbEscapedString KDbEscapedString::arg(const QString &a, int fieldWidth, const QChar & fillChar) const 0314 { 0315 if (!m_valid) 0316 return KDbEscapedString::invalid(); 0317 return KDbEscapedString(toString().arg(a, fieldWidth, fillChar)); 0318 } 0319 0320 KDbEscapedString KDbEscapedString::arg(const QByteArray &a, int fieldWidth, const QChar & fillChar) const 0321 { 0322 if (!m_valid) 0323 return KDbEscapedString::invalid(); 0324 return KDbEscapedString(toString().arg(QLatin1String(a), fieldWidth, fillChar)); 0325 } 0326 0327 KDbEscapedString KDbEscapedString::arg(int a, int fieldWidth, int base, const QChar & fillChar) const 0328 { 0329 if (!m_valid) 0330 return KDbEscapedString::invalid(); 0331 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0332 } 0333 0334 KDbEscapedString KDbEscapedString::arg(uint a, int fieldWidth, int base, const QChar & fillChar) const 0335 { 0336 if (!m_valid) 0337 return KDbEscapedString::invalid(); 0338 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0339 } 0340 0341 KDbEscapedString KDbEscapedString::arg(long a, int fieldWidth, int base, const QChar & fillChar) const 0342 { 0343 if (!m_valid) 0344 return KDbEscapedString::invalid(); 0345 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0346 } 0347 0348 KDbEscapedString KDbEscapedString::arg(ulong a, int fieldWidth, int base, const QChar & fillChar) const 0349 { 0350 if (!m_valid) 0351 return KDbEscapedString::invalid(); 0352 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0353 } 0354 0355 KDbEscapedString KDbEscapedString::arg(qlonglong a, int fieldWidth, int base, const QChar & fillChar) const 0356 { 0357 if (!m_valid) 0358 return KDbEscapedString::invalid(); 0359 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0360 } 0361 0362 KDbEscapedString KDbEscapedString::arg(qulonglong a, int fieldWidth, int base, const QChar & fillChar) const 0363 { 0364 if (!m_valid) 0365 return KDbEscapedString::invalid(); 0366 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0367 } 0368 0369 KDbEscapedString KDbEscapedString::arg(short a, int fieldWidth, int base, const QChar & fillChar) const 0370 { 0371 if (!m_valid) 0372 return KDbEscapedString::invalid(); 0373 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0374 } 0375 0376 KDbEscapedString KDbEscapedString::arg(ushort a, int fieldWidth, int base, const QChar & fillChar) const 0377 { 0378 if (!m_valid) 0379 return KDbEscapedString::invalid(); 0380 return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar)); 0381 } 0382 0383 KDbEscapedString KDbEscapedString::arg(QChar a, int fieldWidth, const QChar & fillChar) const 0384 { 0385 if (!m_valid) 0386 return KDbEscapedString::invalid(); 0387 return KDbEscapedString(toString().arg(a, fieldWidth, fillChar)); 0388 } 0389 0390 KDbEscapedString KDbEscapedString::arg(char a, int fieldWidth, const QChar & fillChar) const 0391 { 0392 if (!m_valid) 0393 return KDbEscapedString::invalid(); 0394 return KDbEscapedString(toString().arg(a, fieldWidth, fillChar)); 0395 } 0396 0397 KDbEscapedString KDbEscapedString::arg(double a, int fieldWidth, char format, int precision, const QChar & fillChar) const 0398 { 0399 if (!m_valid) 0400 return KDbEscapedString::invalid(); 0401 return KDbEscapedString(toString().arg(a, fieldWidth, format, precision, fillChar)); 0402 } 0403 0404 KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbEscapedString& string) 0405 { 0406 if (string.isValid()) 0407 dbg.nospace() << "KDbEscapedString:" << string.toByteArray(); 0408 else 0409 dbg.nospace() << "KDbEscapedString(INVALID)"; 0410 return dbg.space(); 0411 }