KDb

KDbEscapedString.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2010 JarosÅ‚aw Staniek <[email protected]>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this program; see the file COPYING. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "KDbEscapedString.h"
21 #include "kdb_debug.h"
22 
23 #include <QDataStream>
24 
25 KDbEscapedString &KDbEscapedString::prepend(const KDbEscapedString &s)
26 {
27  if (s.isValid()) {
28  if (m_valid)
30  }
31  else {
33  m_valid = false;
34  }
35  return *this;
36 }
37 
38 KDbEscapedString &KDbEscapedString::append(const KDbEscapedString &s)
39 {
40  if (s.isValid()) {
41  if (m_valid)
43  }
44  else {
46  m_valid = false;
47  }
48  return *this;
49 }
50 
51 KDbEscapedString &KDbEscapedString::insert(int i, const KDbEscapedString &s)
52 {
53  if (s.isValid()) {
54  if (m_valid)
55  QByteArray::insert(i, s);
56  }
57  else {
59  m_valid = false;
60  }
61  return *this;
62 }
63 
64 KDbEscapedString &KDbEscapedString::replace(int index, int len, const KDbEscapedString &s)
65 {
66  if (s.isValid()) {
67  if (m_valid)
68  QByteArray::replace(index, len, s);
69  }
70  else {
72  m_valid = false;
73  }
74  return *this;
75 }
76 
77 KDbEscapedString &KDbEscapedString::replace(char before, const KDbEscapedString &after)
78 {
79  if (after.isValid()) {
80  if (m_valid)
81  QByteArray::replace(before, after);
82  }
83  else {
85  m_valid = false;
86  }
87  return *this;
88 }
89 
90 KDbEscapedString &KDbEscapedString::replace(const KDbEscapedString &before, const QByteArray &after)
91 {
92  if (before.isValid()) {
93  if (m_valid)
94  QByteArray::replace(before, after);
95  }
96  else {
98  m_valid = false;
99  }
100  return *this;
101 }
102 
103 KDbEscapedString &KDbEscapedString::replace(const QByteArray &before, const KDbEscapedString &after)
104 {
105  if (after.isValid()) {
106  if (m_valid)
107  QByteArray::replace(before, after);
108  }
109  else {
111  m_valid = false;
112  }
113  return *this;
114 }
115 
116 KDbEscapedString &KDbEscapedString::replace(const KDbEscapedString &before, const KDbEscapedString &after)
117 {
118  if (before.isValid() && after.isValid()) {
119  if (m_valid)
120  QByteArray::replace(before, after);
121  }
122  else {
124  m_valid = false;
125  }
126  return *this;
127 }
128 
129 QList<KDbEscapedString> KDbEscapedString::split(char sep) const
130 {
132  foreach(const QByteArray& ba, QByteArray::split(sep))
133  result.append(KDbEscapedString(ba));
134  return result;
135 }
136 
137 #ifndef QT_NO_DATASTREAM
138 KDB_EXPORT QDataStream& operator<<(QDataStream &stream, const KDbEscapedString &string)
139 {
140  stream << string.isValid();
141  if (string.isValid())
142  stream << string.toByteArray();
143  return stream;
144 }
145 
146 KDB_EXPORT QDataStream& operator>>(QDataStream &stream, KDbEscapedString &string)
147 {
148  bool valid;
149  stream >> valid;
150  if (valid) {
151  QByteArray ba;
152  stream >> ba;
153  string = ba;
154  }
155  else {
156  string = KDbEscapedString::invalid();
157  }
158  return stream;
159 }
160 #endif
161 
162 short KDbEscapedString::toShort(bool *ok, int base) const
163 {
164  if (!checkValid(ok))
165  return 0;
166  return QByteArray::toShort(ok, base);
167 }
168 
169 ushort KDbEscapedString::toUShort(bool *ok, int base) const
170 {
171  if (!checkValid(ok))
172  return 0;
173  return QByteArray::toUShort(ok, base);
174 }
175 
176 int KDbEscapedString::toInt(bool *ok, int base) const
177 {
178  if (!checkValid(ok))
179  return 0;
180  return QByteArray::toInt(ok, base);
181 }
182 
183 uint KDbEscapedString::toUInt(bool *ok, int base) const
184 {
185  if (!checkValid(ok))
186  return 0;
187  return QByteArray::toUInt(ok, base);
188 }
189 
190 long KDbEscapedString::toLong(bool *ok, int base) const
191 {
192  if (!checkValid(ok))
193  return 0;
194  return QByteArray::toLong(ok, base);
195 }
196 
197 ulong KDbEscapedString::toULong(bool *ok, int base) const
198 {
199  if (!checkValid(ok))
200  return 0;
201  return QByteArray::toULong(ok, base);
202 }
203 
204 qlonglong KDbEscapedString::toLongLong(bool *ok, int base) const
205 {
206  if (!checkValid(ok))
207  return 0;
208  return QByteArray::toLongLong(ok, base);
209 }
210 
211 qulonglong KDbEscapedString::toULongLong(bool *ok, int base) const
212 {
213  if (!checkValid(ok))
214  return 0;
215  return QByteArray::toULongLong(ok, base);
216 }
217 
218 float KDbEscapedString::toFloat(bool *ok) const
219 {
220  if (!checkValid(ok))
221  return 0;
222  return QByteArray::toFloat(ok);
223 }
224 
225 double KDbEscapedString::toDouble(bool *ok) const
226 {
227  if (!checkValid(ok))
228  return 0;
229  return QByteArray::toDouble(ok);
230 }
231 
232 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2) const
233 {
234  if (!m_valid || !a1.isValid() || !a2.isValid())
235  return KDbEscapedString::invalid();
236  return KDbEscapedString(toString().arg(a1.toString(), a2.toString()));
237 }
238 
239 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3) const
240 {
241  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid())
242  return KDbEscapedString::invalid();
243  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString()));
244 }
245 
246 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3,
247  const KDbEscapedString &a4) const
248 {
249  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid())
250  return KDbEscapedString::invalid();
251  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString()));
252 }
253 
254 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3,
255  const KDbEscapedString &a4, const KDbEscapedString &a5) const
256 {
257  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid())
258  return KDbEscapedString::invalid();
259  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(),
260  a5.toString()));
261 }
262 
263 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3,
264  const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6) const
265 {
266  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid()
267  || !a6.isValid())
268  return KDbEscapedString::invalid();
269  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(),
270  a5.toString(), a6.toString()));
271 }
272 
273 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3,
274  const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6,
275  const KDbEscapedString &a7) const
276 {
277  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid()
278  || !a6.isValid() || !a7.isValid())
279  return KDbEscapedString::invalid();
280  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(),
281  a5.toString(), a6.toString(), a7.toString()));
282 }
283 
284 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3,
285  const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6,
286  const KDbEscapedString &a7, const KDbEscapedString &a8) const
287 {
288  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid()
289  || !a6.isValid() || !a7.isValid() || !a8.isValid())
290  return KDbEscapedString::invalid();
291  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(),
292  a5.toString(), a6.toString(), a7.toString(), a8.toString()));
293 }
294 
295 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a1, const KDbEscapedString &a2, const KDbEscapedString &a3,
296  const KDbEscapedString &a4, const KDbEscapedString &a5, const KDbEscapedString &a6,
297  const KDbEscapedString &a7, const KDbEscapedString &a8, const KDbEscapedString &a9) const
298 {
299  if (!m_valid || !a1.isValid() || !a2.isValid() || !a3.isValid() || !a4.isValid() || !a5.isValid()
300  || !a6.isValid() || !a7.isValid() || !a8.isValid() || !a9.isValid())
301  return KDbEscapedString::invalid();
302  return KDbEscapedString(toString().arg(a1.toString(), a2.toString(), a3.toString(), a4.toString(),
303  a5.toString(), a6.toString(), a7.toString(), a8.toString(), a9.toString()));
304 }
305 
306 KDbEscapedString KDbEscapedString::arg(const KDbEscapedString &a, int fieldWidth, const QChar & fillChar) const
307 {
308  if (!m_valid || !a.isValid())
309  return KDbEscapedString::invalid();
310  return KDbEscapedString(toString().arg(a.toString(), fieldWidth, fillChar));
311 }
312 
313 KDbEscapedString KDbEscapedString::arg(const QString &a, int fieldWidth, const QChar & fillChar) const
314 {
315  if (!m_valid)
316  return KDbEscapedString::invalid();
317  return KDbEscapedString(toString().arg(a, fieldWidth, fillChar));
318 }
319 
320 KDbEscapedString KDbEscapedString::arg(const QByteArray &a, int fieldWidth, const QChar & fillChar) const
321 {
322  if (!m_valid)
323  return KDbEscapedString::invalid();
324  return KDbEscapedString(toString().arg(QLatin1String(a), fieldWidth, fillChar));
325 }
326 
327 KDbEscapedString KDbEscapedString::arg(int a, int fieldWidth, int base, const QChar & fillChar) const
328 {
329  if (!m_valid)
330  return KDbEscapedString::invalid();
331  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
332 }
333 
334 KDbEscapedString KDbEscapedString::arg(uint a, int fieldWidth, int base, const QChar & fillChar) const
335 {
336  if (!m_valid)
337  return KDbEscapedString::invalid();
338  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
339 }
340 
341 KDbEscapedString KDbEscapedString::arg(long a, int fieldWidth, int base, const QChar & fillChar) const
342 {
343  if (!m_valid)
344  return KDbEscapedString::invalid();
345  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
346 }
347 
348 KDbEscapedString KDbEscapedString::arg(ulong a, int fieldWidth, int base, const QChar & fillChar) const
349 {
350  if (!m_valid)
351  return KDbEscapedString::invalid();
352  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
353 }
354 
355 KDbEscapedString KDbEscapedString::arg(qlonglong a, int fieldWidth, int base, const QChar & fillChar) const
356 {
357  if (!m_valid)
358  return KDbEscapedString::invalid();
359  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
360 }
361 
362 KDbEscapedString KDbEscapedString::arg(qulonglong a, int fieldWidth, int base, const QChar & fillChar) const
363 {
364  if (!m_valid)
365  return KDbEscapedString::invalid();
366  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
367 }
368 
369 KDbEscapedString KDbEscapedString::arg(short a, int fieldWidth, int base, const QChar & fillChar) const
370 {
371  if (!m_valid)
372  return KDbEscapedString::invalid();
373  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
374 }
375 
376 KDbEscapedString KDbEscapedString::arg(ushort a, int fieldWidth, int base, const QChar & fillChar) const
377 {
378  if (!m_valid)
379  return KDbEscapedString::invalid();
380  return KDbEscapedString(toString().arg(a, fieldWidth, base, fillChar));
381 }
382 
383 KDbEscapedString KDbEscapedString::arg(QChar a, int fieldWidth, const QChar & fillChar) const
384 {
385  if (!m_valid)
386  return KDbEscapedString::invalid();
387  return KDbEscapedString(toString().arg(a, fieldWidth, fillChar));
388 }
389 
390 KDbEscapedString KDbEscapedString::arg(char a, int fieldWidth, const QChar & fillChar) const
391 {
392  if (!m_valid)
393  return KDbEscapedString::invalid();
394  return KDbEscapedString(toString().arg(a, fieldWidth, fillChar));
395 }
396 
397 KDbEscapedString KDbEscapedString::arg(double a, int fieldWidth, char format, int precision, const QChar & fillChar) const
398 {
399  if (!m_valid)
400  return KDbEscapedString::invalid();
401  return KDbEscapedString(toString().arg(a, fieldWidth, format, precision, fillChar));
402 }
403 
404 KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbEscapedString& string)
405 {
406  if (string.isValid())
407  dbg.nospace() << "KDbEscapedString:" << string.toByteArray();
408  else
409  dbg.nospace() << "KDbEscapedString(INVALID)";
410  return dbg.space();
411 }
void append(const T &value)
ushort toUShort(bool *ok, int base) const const
double toDouble(bool *ok) const const
QByteArray & append(char ch)
QDebug & nospace()
Specialized string for escaping.
QDebug & space()
QList< QByteArray > split(char sep) const const
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
ulong toULong(bool *ok, int base) const const
void clear()
QByteArray & prepend(char ch)
uint toUInt(bool *ok, int base) const const
float toFloat(bool *ok) const const
qlonglong toLongLong(bool *ok, int base) const const
short toShort(bool *ok, int base) const const
bool isValid() const
QByteArray & replace(int pos, int len, const char *after)
long toLong(bool *ok, int base) const const
int toInt(bool *ok, int base) const const
bool isValid(QStringView ifopt)
qulonglong toULongLong(bool *ok, int base) const const
static KDbEscapedString invalid()
QDataStream & operator>>(QDataStream &in, KDateTime &dateTime)
QByteArray & insert(int i, char ch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.