File indexing completed on 2024-11-24 04:44:30

0001 /*
0002     This file is part of oxaccess.
0003 
0004     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "oxutils.h"
0010 
0011 #include <QStringList>
0012 
0013 using namespace OXA;
0014 
0015 QString OXUtils::writeBoolean(bool value)
0016 {
0017     return value ? QStringLiteral("true") : QStringLiteral("false");
0018 }
0019 
0020 QString OXUtils::writeNumber(qlonglong value)
0021 {
0022     return QString::number(value);
0023 }
0024 
0025 QString OXUtils::writeString(const QString &value)
0026 {
0027     QStringList lines = value.split(QLatin1Char('\n'));
0028 
0029     for (int i = 0; i < lines.count(); ++i) {
0030         lines[i].replace(QLatin1Char('\\'), QStringLiteral("\\\\"));
0031         lines[i].replace(QLatin1Char('"'), QStringLiteral("\\\""));
0032     }
0033 
0034     return lines.join(QLatin1Char('\n'));
0035 }
0036 
0037 QString OXUtils::writeName(const QString &value)
0038 {
0039     // TODO: assert on invalid names
0040     return value;
0041 }
0042 
0043 QString OXUtils::writeDateTime(const QDateTime &value)
0044 {
0045     QString result;
0046 
0047     // workaround, as QDateTime does not support negative time_t values
0048     QDateTime Time_t_S(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::UTC);
0049 
0050     if (value < Time_t_S) {
0051         result = QString::number(Time_t_S.secsTo(value));
0052     } else {
0053         result = QString::number(value.toUTC().toSecsSinceEpoch());
0054     }
0055 
0056     return QString(result + QLatin1StringView("000"));
0057 }
0058 
0059 QString OXUtils::writeDate(QDate value)
0060 {
0061     return writeDateTime(QDateTime(value, QTime(0, 0, 0), Qt::UTC));
0062 }
0063 
0064 bool OXUtils::readBoolean(const QString &text)
0065 {
0066     if (text == QLatin1StringView("true")) {
0067         return true;
0068     } else if (text == QLatin1StringView("false")) {
0069         return false;
0070     } else {
0071         Q_ASSERT(false);
0072         return false;
0073     }
0074 }
0075 
0076 qlonglong OXUtils::readNumber(const QString &text)
0077 {
0078     return text.toLongLong();
0079 }
0080 
0081 QString OXUtils::readString(const QString &text)
0082 {
0083     QString value(text);
0084     value.replace(QLatin1StringView("\\\""), QLatin1StringView("\""));
0085     value.replace(QLatin1StringView("\\\\"), QLatin1StringView("\\"));
0086 
0087     return value;
0088 }
0089 
0090 QString OXUtils::readName(const QString &text)
0091 {
0092     return text;
0093 }
0094 
0095 QDateTime OXUtils::readDateTime(const QString &text)
0096 {
0097     // remove the trailing '000', they exceed the integer dimension
0098     const int ticks = text.mid(0, text.length() - 3).toLongLong();
0099 
0100     // workaround, as QDateTime does not support negative time_t values
0101     QDateTime value;
0102     if (ticks < 0) {
0103         value = QDateTime::fromSecsSinceEpoch(0);
0104         value = value.addSecs(ticks);
0105     } else {
0106         value = QDateTime::fromSecsSinceEpoch(ticks);
0107     }
0108 
0109     return value;
0110 }
0111 
0112 QDate OXUtils::readDate(const QString &text)
0113 {
0114     return readDateTime(text).date();
0115 }