summaryrefslogtreecommitdiff
path: root/src/plugins/baremetal/debugservers/uvsc/xmlprojectwriter.cpp
blob: 64ba2ee20a516d8473dadb55884b649174782ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2020 Denis Shienkov <denis.shienkov@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "xmlproject.h"
#include "xmlprojectwriter.h"
#include "xmlproperty.h"
#include "xmlpropertygroup.h"

#include <ostream>

namespace BareMetal::Gen::Xml {

// ProjectWriter

ProjectWriter::ProjectWriter(std::ostream *device)
    : m_device(device)
{
    m_writer.reset(new QXmlStreamWriter(&m_buffer));
    m_writer->setAutoFormatting(true);
    m_writer->setAutoFormattingIndent(2);
}

bool ProjectWriter::write(const Project *project)
{
    m_buffer.clear();
    m_writer->writeStartDocument();
    project->accept(this);
    m_writer->writeEndDocument();
    if (m_writer->hasError())
        return false;
    m_device->write(&*std::cbegin(m_buffer), m_buffer.size());
    return m_device->good();
}

void ProjectWriter::visitPropertyStart(const Property *property)
{
    const QString value = property->value().toString();
    const QString name = QString::fromUtf8(property->name());
    m_writer->writeTextElement(name, value);
}

void ProjectWriter::visitPropertyEnd(const Property *property)
{
    Q_UNUSED(property)
}

void ProjectWriter::visitPropertyGroupStart(const PropertyGroup *propertyGroup)
{
    const QString name = QString::fromUtf8(propertyGroup->name());
    m_writer->writeStartElement(name);
}

void ProjectWriter::visitPropertyGroupEnd(const PropertyGroup *propertyGroup)
{
    Q_UNUSED(propertyGroup)
    m_writer->writeEndElement();
}

QXmlStreamWriter *ProjectWriter::writer() const
{
    return m_writer.get();
}

// ProjectOptionsWriter

ProjectOptionsWriter::ProjectOptionsWriter(std::ostream *device)
    : m_device(device)
{
    m_writer.reset(new QXmlStreamWriter(&m_buffer));
    m_writer->setAutoFormatting(true);
    m_writer->setAutoFormattingIndent(2);
}

bool ProjectOptionsWriter::write(const ProjectOptions *projectOptions)
{
    m_buffer.clear();
    m_writer->writeStartDocument();
    projectOptions->accept(this);
    m_writer->writeEndDocument();
    if (m_writer->hasError())
        return false;
    m_device->write(&*std::cbegin(m_buffer), m_buffer.size());
    return m_device->good();
}

void ProjectOptionsWriter::visitPropertyStart(const Property *property)
{
    const QString value = property->value().toString();
    const QString name = QString::fromUtf8(property->name());
    m_writer->writeTextElement(name, value);
}

void ProjectOptionsWriter::visitPropertyEnd(const Property *property)
{
    Q_UNUSED(property)
}

void ProjectOptionsWriter::visitPropertyGroupStart(const PropertyGroup *propertyGroup)
{
    const QString name = QString::fromUtf8(propertyGroup->name());
    m_writer->writeStartElement(name);
}

void ProjectOptionsWriter::visitPropertyGroupEnd(const PropertyGroup *propertyGroup)
{
    Q_UNUSED(propertyGroup)
    m_writer->writeEndElement();
}

QXmlStreamWriter *ProjectOptionsWriter::writer() const
{
    return m_writer.get();
}

} // BareMetal::Gen::Xml