From b854af7dc6bb17046ca4deb212ec2f1ab88815a9 Mon Sep 17 00:00:00 2001 From: Daniel Brunner <0xFEEDC0DE64@gmail.com> Date: Mon, 17 Sep 2018 20:25:02 +0200 Subject: [PATCH] Added utils to core lib --- DbCoreLib.pro | 10 ++++++-- randomdevice.cpp | 36 +++++++++++++++++++++++++++ randomdevice.h | 24 ++++++++++++++++++ utils/jsonutils.cpp | 28 +++++++++++++++++++++ utils/jsonutils.h | 59 +++++++++++++++++++++++++++++++++++++++++++++ utils/timeutils.cpp | 23 ++++++++++++++++++ utils/timeutils.h | 10 ++++++++ 7 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 randomdevice.cpp create mode 100644 randomdevice.h create mode 100644 utils/jsonutils.cpp create mode 100644 utils/jsonutils.h create mode 100644 utils/timeutils.cpp create mode 100644 utils/timeutils.h diff --git a/DbCoreLib.pro b/DbCoreLib.pro index 5129a26..e0817db 100644 --- a/DbCoreLib.pro +++ b/DbCoreLib.pro @@ -7,10 +7,16 @@ TARGET = dbcore DEFINES += DBCORELIB_LIBRARY -SOURCES += +SOURCES += \ + randomdevice.cpp \ + utils/timeutils.cpp \ + utils/jsonutils.cpp HEADERS += dbcorelib_global.h \ - fixedsizematrix.h + fixedsizematrix.h \ + randomdevice.h \ + utils/timeutils.h \ + utils/jsonutils.h FORMS += diff --git a/randomdevice.cpp b/randomdevice.cpp new file mode 100644 index 0000000..30c7222 --- /dev/null +++ b/randomdevice.cpp @@ -0,0 +1,36 @@ +#include "randomdevice.h" + +#include + +RandomDevice::RandomDevice(qint64 size, QObject *parent) : + QIODevice(parent), + m_pos(0), + m_size(size) +{ +} + +qint64 RandomDevice::pos() const +{ + return m_pos; +} + +qint64 RandomDevice::size() const +{ + return m_size; +} + +qint64 RandomDevice::readData(char *data, qint64 maxlen) +{ + std::generate_n(data, maxlen, std::rand); + m_pos += maxlen; + return maxlen; +} + +qint64 RandomDevice::writeData(const char *data, qint64 len) +{ + Q_UNUSED(data) + Q_UNUSED(len) + + qCritical() << "cannot write to this device!"; + return 0; +} diff --git a/randomdevice.h b/randomdevice.h new file mode 100644 index 0000000..8d7cf13 --- /dev/null +++ b/randomdevice.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +class RandomDevice : public QIODevice +{ + Q_OBJECT + +public: + explicit RandomDevice(qint64 size, QObject *parent = Q_NULLPTR); + + // QIODevice interface + qint64 pos() const Q_DECL_OVERRIDE; + qint64 size() const Q_DECL_OVERRIDE; + +protected: + // QIODevice interface + qint64 readData(char *data, qint64 maxlen) Q_DECL_OVERRIDE; + qint64 writeData(const char *data, qint64 len) Q_DECL_OVERRIDE; + +private: + qint64 m_pos; + qint64 m_size; +}; diff --git a/utils/jsonutils.cpp b/utils/jsonutils.cpp new file mode 100644 index 0000000..f87816a --- /dev/null +++ b/utils/jsonutils.cpp @@ -0,0 +1,28 @@ +#include "jsonutils.h" + +#include +#include + +template<> +QJsonDocument getJson(const QJsonDocument &document) +{ + return document; +} + +template<> +QJsonObject getJson(const QJsonDocument &document) +{ + if(!document.isObject()) + throw std::runtime_error("JSON document does not contain an object!"); + + return document.object(); +} + +template<> +QJsonArray getJson(const QJsonDocument &document) +{ + if(!document.isArray()) + throw std::runtime_error("JSON document does not contain an object!"); + + return document.array(); +} diff --git a/utils/jsonutils.h b/utils/jsonutils.h new file mode 100644 index 0000000..04c6524 --- /dev/null +++ b/utils/jsonutils.h @@ -0,0 +1,59 @@ +#pragma once + +#include "dbcorelib_global.h" + +#include +#include +#include + +#include + +template +T getJson(const QJsonDocument &document); + +template<> +QJsonDocument DBCORELIB_EXPORT getJson(const QJsonDocument &document); + +template<> +QJsonObject DBCORELIB_EXPORT getJson(const QJsonDocument &document); + +template<> +QJsonArray DBCORELIB_EXPORT getJson(const QJsonDocument &document); + +template +T getJson(const QByteArray &byteArray); + +template +T getJson(QIODevice &device); + +template +T getJson(const QString &filename); + + + +template +T getJson(const QByteArray &byteArray) +{ + QJsonParseError error; + auto document = QJsonDocument::fromJson(byteArray, &error); + if(error.error != QJsonParseError::NoError) + throw std::runtime_error(QString("Could not parse json: %0").arg(error.errorString()).toStdString()); + + return getJson(document); +} + +template +T getJson(QIODevice &device) +{ + return getJson(device.readAll()); +} + +template +T getJson(const QString &filename) +{ + QFile file(filename); + if(!file.open(QIODevice::ReadOnly|QIODevice::Text)) + throw std::runtime_error(QString("Could not open json file %0: %1").arg(filename, file.errorString()).toStdString()); + + return getJson(file); +} diff --git a/utils/timeutils.cpp b/utils/timeutils.cpp new file mode 100644 index 0000000..e20eb62 --- /dev/null +++ b/utils/timeutils.cpp @@ -0,0 +1,23 @@ +#include "timeutils.h" + +int timeToSeconds(const QTime &time) +{ + return QTime(0, 0).secsTo(time); +} + +QTime timeBetween(const QTime &l, const QTime &r) +{ + Q_ASSERT(l <= r); + return QTime(0, 0).addSecs(l.secsTo(r)); +} + +QTime timeAdd(const QTime &l, const QTime &r) +{ + Q_ASSERT(timeToSeconds(l) + timeToSeconds(r) < 86400); + return l.addSecs(QTime(0, 0).secsTo(r)); +} + +QTime timeNormalise(const QTime &time) +{ + return QTime(time.hour(), time.minute()); +} diff --git a/utils/timeutils.h b/utils/timeutils.h new file mode 100644 index 0000000..254e011 --- /dev/null +++ b/utils/timeutils.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include "dbcorelib_global.h" + +int DBCORELIB_EXPORT timeToSeconds(const QTime &time); +QTime DBCORELIB_EXPORT timeBetween(const QTime &l, const QTime &r); +QTime DBCORELIB_EXPORT timeAdd(const QTime &l, const QTime &r); +QTime DBCORELIB_EXPORT timeNormalise(const QTime &time);