Added utils to core lib
This commit is contained in:
@@ -7,10 +7,16 @@ TARGET = dbcore
|
|||||||
|
|
||||||
DEFINES += DBCORELIB_LIBRARY
|
DEFINES += DBCORELIB_LIBRARY
|
||||||
|
|
||||||
SOURCES +=
|
SOURCES += \
|
||||||
|
randomdevice.cpp \
|
||||||
|
utils/timeutils.cpp \
|
||||||
|
utils/jsonutils.cpp
|
||||||
|
|
||||||
HEADERS += dbcorelib_global.h \
|
HEADERS += dbcorelib_global.h \
|
||||||
fixedsizematrix.h
|
fixedsizematrix.h \
|
||||||
|
randomdevice.h \
|
||||||
|
utils/timeutils.h \
|
||||||
|
utils/jsonutils.h
|
||||||
|
|
||||||
FORMS +=
|
FORMS +=
|
||||||
|
|
||||||
|
36
randomdevice.cpp
Normal file
36
randomdevice.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include "randomdevice.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
24
randomdevice.h
Normal file
24
randomdevice.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QIODevice>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
28
utils/jsonutils.cpp
Normal file
28
utils/jsonutils.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "jsonutils.h"
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
|
||||||
|
template<>
|
||||||
|
QJsonDocument getJson<QJsonDocument>(const QJsonDocument &document)
|
||||||
|
{
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
QJsonObject getJson<QJsonObject>(const QJsonDocument &document)
|
||||||
|
{
|
||||||
|
if(!document.isObject())
|
||||||
|
throw std::runtime_error("JSON document does not contain an object!");
|
||||||
|
|
||||||
|
return document.object();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
QJsonArray getJson<QJsonArray>(const QJsonDocument &document)
|
||||||
|
{
|
||||||
|
if(!document.isArray())
|
||||||
|
throw std::runtime_error("JSON document does not contain an object!");
|
||||||
|
|
||||||
|
return document.array();
|
||||||
|
}
|
59
utils/jsonutils.h
Normal file
59
utils/jsonutils.h
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "dbcorelib_global.h"
|
||||||
|
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonParseError>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getJson(const QJsonDocument &document);
|
||||||
|
|
||||||
|
template<>
|
||||||
|
QJsonDocument DBCORELIB_EXPORT getJson<QJsonDocument>(const QJsonDocument &document);
|
||||||
|
|
||||||
|
template<>
|
||||||
|
QJsonObject DBCORELIB_EXPORT getJson<QJsonObject>(const QJsonDocument &document);
|
||||||
|
|
||||||
|
template<>
|
||||||
|
QJsonArray DBCORELIB_EXPORT getJson<QJsonArray>(const QJsonDocument &document);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getJson(const QByteArray &byteArray);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getJson(QIODevice &device);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getJson(const QString &filename);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
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<T>(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getJson(QIODevice &device)
|
||||||
|
{
|
||||||
|
return getJson<T>(device.readAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
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<T>(file);
|
||||||
|
}
|
23
utils/timeutils.cpp
Normal file
23
utils/timeutils.cpp
Normal file
@@ -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());
|
||||||
|
}
|
10
utils/timeutils.h
Normal file
10
utils/timeutils.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QTime>
|
||||||
|
|
||||||
|
#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);
|
Reference in New Issue
Block a user