KOSMIndoorMap

abstractreader.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "abstractreader.h"
7#include "datatypes.h"
8#include "datasetmergebuffer.h"
9
10#include <QDebug>
11#include <QIODevice>
12
13#include <cassert>
14#include <cstring>
15
16using namespace OSM;
17
18namespace OSM {
19/** Readonly QIODevice adapter for memory-mapped data. */
20class MemoryMapIoDevice : public QIODevice
21{
23public:
24 explicit MemoryMapIoDevice(const uint8_t *data, std::size_t size, QObject *parent = nullptr)
26 , m_data(data)
27 , m_size(size)
28 {}
29 ~MemoryMapIoDevice() = default;
30
31 bool open(QIODevice::OpenMode mode) override
32 {
33 if (mode != QIODevice::ReadOnly) {
34 return false;
35 }
36 return QIODevice::open(mode);
37 }
38
39 bool isSequential() const override
40 {
41 return false;
42 }
43
44 qint64 size() const override
45 {
46 return m_size;
47 }
48
49protected:
50 qint64 readData(char *data, qint64 maxlen) override
51 {
52 const auto readSize = std::min<qint64>(m_size - pos(), maxlen);
53 if (readSize < 0) {
54 return 0;
55 }
56 std::memcpy(data, reinterpret_cast<const char*>(m_data + pos()), readSize);
57 return readSize;
58 }
59
60 qint64 writeData([[maybe_unused]] const char *data, [[maybe_unused]] qint64 len) override
61 {
62 return 0;
63 }
64
65private:
66 const uint8_t *m_data = nullptr;
67 const std::size_t m_size = {};
68};
69}
70
71
72AbstractReader::AbstractReader(OSM::DataSet *dataSet)
73 : m_dataSet(dataSet)
74{
75 assert(dataSet);
76}
77
78AbstractReader::~AbstractReader() = default;
79
81{
82 m_mergeBuffer = buffer;
83}
84
85void AbstractReader::read(const uint8_t *data, std::size_t len)
86{
87 readFromData(data, len);
88 if (!m_error.isEmpty()) {
89 qWarning() << m_error;
90 }
91}
92
94{
95 readFromIODevice(io);
96 if (!m_error.isEmpty()) {
97 qWarning() << m_error;
98 }
99}
100
101void AbstractReader::readFromData(const uint8_t *data, std::size_t len)
102{
103 assert(data);
104 MemoryMapIoDevice io(data, len);
105 io.open(QIODevice::ReadOnly);
106 readFromIODevice(&io);
107}
108
109void AbstractReader::readFromIODevice(QIODevice *io)
110{
111 assert(io);
112 QByteArray data = io->readAll();
113 readFromData(reinterpret_cast<const uint8_t*>(data.constData()), data.size());
114}
115
116bool AbstractReader::hasError() const
117{
118 return !m_error.isEmpty();
119}
120
122{
123 return m_error;
124}
125
127{
128 m_mergeBuffer ? m_mergeBuffer->nodes.push_back(std::move(node)) : m_dataSet->addNode(std::move(node));
129}
130
131void AbstractReader::addWay(OSM::Way &&way)
132{
133 m_mergeBuffer ? m_mergeBuffer->ways.push_back(std::move(way)) : m_dataSet->addWay(std::move(way));
134}
135
136void AbstractReader::addRelation(OSM::Relation &&relation)
137{
138 m_mergeBuffer ? m_mergeBuffer->relations.push_back(std::move(relation)) : m_dataSet->addRelation(std::move(relation));
139}
140
141#include "abstractreader.moc"
void addNode(OSM::Node &&node)
Add read elements to the merge buffer if set, or the dataset otherwise.
void read(const uint8_t *data, std::size_t len)
Read the given data.
void setMergeBuffer(OSM::DataSetMergeBuffer *buffer)
Sets a merge buffer.
virtual void readFromData(const uint8_t *data, std::size_t len)
Implement for actual parsing.
QString errorString() const
Error message in case parsing failed for some reason.
Holds OSM elements produced by a parser prior to merging into OSM::DataSet.
A set of nodes, ways and relations.
Definition datatypes.h:346
An OSM node.
Definition datatypes.h:204
An OSM relation.
Definition datatypes.h:316
An OSM way.
Definition datatypes.h:232
Low-level types and functions to work with raw OSM data as efficiently as possible.
const char * constData() const const
qsizetype size() const const
virtual bool open(QIODeviceBase::OpenMode mode)
virtual qint64 pos() const const
QByteArray readAll()
Q_OBJECTQ_OBJECT
QObject * parent() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.