KOSMIndoorMap

mapcssparser.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "mapcssparser.h"
8#include "mapcssparser_p.h"
9#include "logging.h"
10
11#include "mapcssloader.h"
12#include "mapcssparser_impl.h"
13#include "mapcssdeclaration_p.h"
14#include "mapcssscanner.h"
15#include "mapcssstyle.h"
16
17#include <QDebug>
18#include <QFile>
19#include <QFileInfo>
20#include <QScopeGuard>
21
22#include <cstring>
23
24template <char Q>
25[[nodiscard]] static char* unquoteString(const char *str)
26{
27 const auto size = strlen(str) - 2;
28 if (size <= 0) {
29 return nullptr;
30 }
31 auto out = (char*)malloc(size + 1);
32 memset(out, 0, size + 1);
33 auto outIt = out;
34 for (auto it = str + 1; it < str + size + 1; ++it, ++outIt) {
35 if (*it == '\\') {
36 ++it;
37 switch (*it) {
38 case '\\':
39 case Q:
40 *outIt = *it; break;
41 case 'n':
42 *outIt = '\n'; break;
43 case 't':
44 *outIt = '\t'; break;
45 default:
46 *outIt++ = '\\';
47 *outIt = *it;
48 }
49 } else {
50 *outIt = *it;
51 }
52 }
53 return out;
54}
55
56[[nodiscard]] char* unquoteSingleQuotedString(const char *str)
57{
58 return unquoteString<'\''>(str);
59}
60
61[[nodiscard]] char* unquoteDoubleQuotedString(const char *str)
62{
63 return unquoteString<'"'>(str);
64}
65
66using namespace KOSMIndoorMap;
67
68MapCSSParser::MapCSSParser()
69 : d(new MapCSSParserPrivate)
70{
71}
72
73MapCSSParser::~MapCSSParser() = default;
74
76{
77 return d->m_error != MapCSSParser::NoError;
78}
79
80MapCSSParser::Error MapCSSParser::error() const
81{
82 return d->m_error;
83}
84
86{
87 return d->m_currentUrl;
88}
89
90QString MapCSSParser::errorMessage() const
91{
92 if (!d->m_error) {
93 return {};
94 }
95
96 return d->m_errorMsg + QLatin1String(": ") + url().toString() + QLatin1Char(':') + QString::number(d->m_line) + QLatin1Char(':') + QString::number(d->m_column);
97}
98
99MapCSSStyle MapCSSParser::parse(const QString &fileName)
100{
101 return parse(MapCSSLoader::resolve(fileName));
102}
103
104MapCSSStyle MapCSSParser::parse(const QUrl &url)
105{
106 MapCSSStyle style;
107 d->parse(&style, url, {});
108 if (d->m_error) {
109 return MapCSSStyle();
110 }
111
112 return style;
113}
114
115void MapCSSParserPrivate::parse(MapCSSStyle *style, const QUrl &url, ClassSelectorKey importClass)
116{
117 const auto fileName = MapCSSLoader::toLocalFile(url);
118 QFile f(fileName);
119 if (!f.open(QFile::ReadOnly)) {
120 qCWarning(Log) << f.fileName() << f.errorString();
121 if (!QFile::exists(fileName)) {
122 m_error = MapCSSParser::FileNotFoundError;
123 m_currentUrl = url;
124 } else {
125 m_error = MapCSSParser::FileIOError;
126 }
127 m_errorMsg = f.errorString();
128 return;
129 }
130 m_currentUrl = url;
131 m_currentStyle = style;
132 m_importClass = importClass;
133
134 yyscan_t scanner;
135 if (yylex_init_extra(this, &scanner)) {
136 return;
137 }
138 const auto lexerCleanup = qScopeGuard([&scanner]{ yylex_destroy(scanner); });
139
140 const auto b = f.readAll();
141 YY_BUFFER_STATE state;
142 state = yy_scan_string(b.constData(), scanner);
143 m_error = MapCSSParser::SyntaxError;
144 if (yyparse(this, scanner)) {
145 return;
146 }
147
148 yy_delete_buffer(state, scanner);
149
150 m_error = MapCSSParser::NoError;
151 m_currentStyle = nullptr;
152 m_importClass = {};
153}
static QString toLocalFile(const QUrl &url)
Translate local or remote URL to locally loadable (cache) file.
static QUrl resolve(const QString &style, const QUrl &baseUrl={})
Resolve style to an absolute URL to load.
QUrl url() const
URL of the parsed MapCSS style sheet.
bool hasError() const
Returns true if an error occured during parsing and the returned style is invalid.
A parsed MapCSS style sheet.
Definition mapcssstyle.h:33
OSM-based multi-floor indoor maps for buildings.
bool exists() const const
QString number(double n, char format, int precision)
QString errorString() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 12 2024 12:03:15 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.